From 547459314bd5a942c7dfc8914ddae93a8bac9151 Mon Sep 17 00:00:00 2001 From: Andy Allan Date: Thu, 24 Dec 2020 13:31:04 +0000 Subject: [PATCH] Refactor diary entry forms to use a custom form builder --- app/assets/stylesheets/common.scss | 2 - app/views/diary_entries/_form.html.erb | 48 ++++++++--------------- app/views/diary_entries/edit.html.erb | 4 +- app/views/diary_entries/new.html.erb | 4 +- app/views/shared/_html_help.erb | 0 app/views/shared/_markdown_help.html.erb | 19 +++++++++ app/views/shared/_richtext_field.html.erb | 18 +++++++++ config/locales/en.yml | 27 +++++++++---- lib/richtext_form_builder.rb | 15 +++++++ 9 files changed, 90 insertions(+), 47 deletions(-) create mode 100644 app/views/shared/_html_help.erb create mode 100644 app/views/shared/_markdown_help.html.erb create mode 100644 app/views/shared/_richtext_field.html.erb create mode 100644 lib/richtext_form_builder.rb diff --git a/app/assets/stylesheets/common.scss b/app/assets/stylesheets/common.scss index 7ae057740..d8369e3ce 100644 --- a/app/assets/stylesheets/common.scss +++ b/app/assets/stylesheets/common.scss @@ -1281,8 +1281,6 @@ tr.turn:hover { .diary_entries { #map { - position: relative; - width: 90%; height: 400px; border: 1px solid $grey; display: none; diff --git a/app/views/diary_entries/_form.html.erb b/app/views/diary_entries/_form.html.erb index 9585b09eb..ed7164e88 100644 --- a/app/views/diary_entries/_form.html.erb +++ b/app/views/diary_entries/_form.html.erb @@ -1,35 +1,19 @@ -
-
-
- - <%= f.text_field :title, :class => "richtext_title" %> -
-
- - <%= richtext_area :diary_entry, :body, :cols => 80, :rows => 20, :format => @diary_entry.body_format %> +<%= f.text_field :title %> +<%= f.richtext_field :body, :cols => 80, :rows => 20, :format => @diary_entry.body_format %> +<%= f.collection_select :language_code, Language.order(:english_name), :code, :name %> + +
+ <%= t ".location" -%> + + <%= tag.div "", :id => "map", :data => { :lat => @lat, :lon => @lon, :zoom => @zoom } %> + +
+ <%= f.text_field :latitude, :wrapper_class => "col-sm-4", :id => "latitude" %> + <%= f.text_field :longitude, :wrapper_class => "col-sm-4", :id => "longitude" %> + -
- - <%= f.collection_select :language_code, Language.order(:english_name), :code, :name %>
-
-
- - <%= tag.div "", :id => "map", :data => { :lat => @lat, :lon => @lon, :zoom => @zoom } %> -
-
- - <%= f.text_field :latitude, :size => 20, :id => "latitude" %> -
-
- - <%= f.text_field :longitude, :size => 20, :id => "longitude" %> -
- -
-
+
- <%= f.submit %> -
+<%= f.primary %> diff --git a/app/views/diary_entries/edit.html.erb b/app/views/diary_entries/edit.html.erb index 5ea619328..9d03e774a 100644 --- a/app/views/diary_entries/edit.html.erb +++ b/app/views/diary_entries/edit.html.erb @@ -6,8 +6,6 @@

<%= @title %>

<% end %> -<%= error_messages_for "diary_entry" %> - -<%= form_for @diary_entry, :url => diary_entry_path(current_user, @diary_entry), :html => { :method => :put } do |f| %> +<%= form_for @diary_entry, :builder => RichtextFormBuilder, :url => diary_entry_path(current_user, @diary_entry), :html => { :method => :put } do |f| %> <%= render :partial => "form", :locals => { :f => f } %> <% end %> diff --git a/app/views/diary_entries/new.html.erb b/app/views/diary_entries/new.html.erb index dfe69f22a..9d8ad6170 100644 --- a/app/views/diary_entries/new.html.erb +++ b/app/views/diary_entries/new.html.erb @@ -6,8 +6,6 @@

<%= @title %>

<% end %> -<%= error_messages_for "diary_entry" %> - -<%= form_for @diary_entry do |f| %> +<%= form_for @diary_entry, :builder => RichtextFormBuilder do |f| %> <%= render :partial => "form", :locals => { :f => f } %> <% end %> diff --git a/app/views/shared/_html_help.erb b/app/views/shared/_html_help.erb new file mode 100644 index 000000000..e69de29bb diff --git a/app/views/shared/_markdown_help.html.erb b/app/views/shared/_markdown_help.html.erb new file mode 100644 index 000000000..5bd8c4955 --- /dev/null +++ b/app/views/shared/_markdown_help.html.erb @@ -0,0 +1,19 @@ +
<%= t ".title_html" %>
+
+
<%= t ".headings" %>
+
# <%= t ".heading" %>
+ ## <%= t ".subheading" %>
+
<%= t ".unordered" %>
+
* <%= t ".first" %>
+ * <%= t ".second" %>
+ +
<%= t ".ordered" %>
+
1. <%= t ".first" %>
+ 2. <%= t ".second" %>
+ +
<%= t ".link" %>
+
[<%= t ".text" %>](<%= t ".url" %>)
+ +
<%= t ".image" %>
+
![<%= t ".alt" %>](<%= t ".url" %>)
+
diff --git a/app/views/shared/_richtext_field.html.erb b/app/views/shared/_richtext_field.html.erb new file mode 100644 index 000000000..eef982342 --- /dev/null +++ b/app/views/shared/_richtext_field.html.erb @@ -0,0 +1,18 @@ +
+ +
+
+ <%= builder.text_area(attribute, options.merge(:wrapper => false, "data-preview-url" => preview_url(:type => type))) %> +
+
+
+
+
+ <%= render :partial => "shared/#{type}_help" %> + <%= submit_tag t(".edit"), :id => "#{id}_doedit", :class => "richtext_doedit btn btn-primary", :disabled => true %> + <%= submit_tag t(".preview"), :id => "#{id}_dopreview", :class => "richtext_dopreview btn btn-primary" %> +
+
+
+
+
diff --git a/config/locales/en.yml b/config/locales/en.yml index 21ea7e106..ebbe71b2c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -399,13 +399,8 @@ en: new: title: New Diary Entry form: - subject: "Subject:" - body: "Body:" - language: "Language:" - location: "Location:" - latitude: "Latitude:" - longitude: "Longitude:" - use_map_link: "use map" + location: Location + use_map_link: Use Map index: title: "Users' diaries" title_friends: "Friends' diaries" @@ -1578,6 +1573,24 @@ en: as_unread: "Message marked as unread" destroy: destroyed: "Message deleted" + shared: + markdown_help: + title_html: Parsed with kramdown + headings: Headings + heading: Heading + subheading: Subheading + unordered: Unordered list + ordered: Ordered list + first: First item + second: Second item + link: Link + text: Text + image: Image + alt: Alt text + url: URL + richtext_field: + edit: Edit + preview: Preview site: about: next: Next diff --git a/lib/richtext_form_builder.rb b/lib/richtext_form_builder.rb new file mode 100644 index 000000000..4198f3822 --- /dev/null +++ b/lib/richtext_form_builder.rb @@ -0,0 +1,15 @@ +class RichtextFormBuilder < BootstrapForm::FormBuilder + def richtext_field(attribute, options = {}) + id = "#{@object_name}_#{attribute}" + type = options.delete(:format) || "markdown" + + @template.render(:partial => "shared/richtext_field", + :locals => { :object => @object, + :attribute => attribute, + :object_name => @object_name, + :id => id, + :type => type, + :options => options, + :builder => self }) + end +end -- 2.43.2