From 78bf2993e4c2f895b5debcdd6101a1febbcb69ca Mon Sep 17 00:00:00 2001 From: Andy Allan Date: Wed, 13 Jan 2021 14:05:39 +0000 Subject: [PATCH] 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. --- app/views/diary_entries/edit.html.erb | 2 +- app/views/diary_entries/new.html.erb | 2 +- app/views/shared/_richtext_field.html.erb | 25 +++++++-------- config/initializers/bootstrap_form.rb | 2 ++ lib/bootstrap_form/inputs/richtext_field.rb | 35 +++++++++++++++++++++ lib/richtext_form_builder.rb | 15 --------- 6 files changed, 50 insertions(+), 31 deletions(-) create mode 100644 config/initializers/bootstrap_form.rb create mode 100644 lib/bootstrap_form/inputs/richtext_field.rb delete mode 100644 lib/richtext_form_builder.rb 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 -- 2.43.2