From: Andy Allan Date: Wed, 13 Jan 2021 14:05:39 +0000 (+0000) Subject: Refactor richtext fields to use a custom bootstrap_form input. X-Git-Tag: live~2257^2 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/78bf2993e4c2f895b5debcdd6101a1febbcb69ca?ds=sidebyside Refactor richtext fields to use a custom bootstrap_form input. This allows us to use form_group_builder and get all the label and help text handling in line with other bootstrap_form inputs. --- diff --git a/app/views/diary_entries/edit.html.erb b/app/views/diary_entries/edit.html.erb index 9d03e774a..c8df07f4c 100644 --- a/app/views/diary_entries/edit.html.erb +++ b/app/views/diary_entries/edit.html.erb @@ -6,6 +6,6 @@

<%= @title %>

<% end %> -<%= form_for @diary_entry, :builder => RichtextFormBuilder, :url => diary_entry_path(current_user, @diary_entry), :html => { :method => :put } do |f| %> +<%= bootstrap_form_for @diary_entry, :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 9d8ad6170..0a10cfd03 100644 --- a/app/views/diary_entries/new.html.erb +++ b/app/views/diary_entries/new.html.erb @@ -6,6 +6,6 @@

<%= @title %>

<% end %> -<%= form_for @diary_entry, :builder => RichtextFormBuilder do |f| %> +<%= bootstrap_form_for @diary_entry do |f| %> <%= render :partial => "form", :locals => { :f => f } %> <% end %> diff --git a/app/views/shared/_richtext_field.html.erb b/app/views/shared/_richtext_field.html.erb index 168e9efe4..cfe6f9827 100644 --- a/app/views/shared/_richtext_field.html.erb +++ b/app/views/shared/_richtext_field.html.erb @@ -1,17 +1,14 @@ -
- -
-
- <%= 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" %> -
+
+
+ <%= 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/initializers/bootstrap_form.rb b/config/initializers/bootstrap_form.rb new file mode 100644 index 000000000..a61932c53 --- /dev/null +++ b/config/initializers/bootstrap_form.rb @@ -0,0 +1,2 @@ +# Include our custom RichtextField input method for `f.richtext_field` in forms +BootstrapForm::FormBuilder.include BootstrapForm::Inputs::RichtextField diff --git a/lib/bootstrap_form/inputs/richtext_field.rb b/lib/bootstrap_form/inputs/richtext_field.rb new file mode 100644 index 000000000..a2256566f --- /dev/null +++ b/lib/bootstrap_form/inputs/richtext_field.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +# A custom richtext_field form group. By using form_group_builder we get to use +# the built-in methods for generating labels and help text. +module BootstrapForm + module Inputs + module RichtextField + extend ActiveSupport::Concern + include Base + + # It's not clear to me why this needs to be duplicated from the upstream BootstrapForm::FormBuilder class + delegate :content_tag, :capture, :concat, :tag, :to => :@template + + included do + def richtext_field_with_bootstrap(name, options = {}) + id = "#{@object_name}_#{name}" + type = options.delete(:format) || "markdown" + + form_group_builder(name, options) do + @template.render(:partial => "shared/richtext_field", + :locals => { :object => @object, + :attribute => name, + :object_name => @object_name, + :id => id, + :type => type, + :options => options, + :builder => self }) + end + end + + alias_method :richtext_field, :richtext_field_with_bootstrap + end + end + end +end diff --git a/lib/richtext_form_builder.rb b/lib/richtext_form_builder.rb deleted file mode 100644 index 4198f3822..000000000 --- a/lib/richtext_form_builder.rb +++ /dev/null @@ -1,15 +0,0 @@ -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