]> git.openstreetmap.org Git - rails.git/commitdiff
Refactor richtext fields to use a custom bootstrap_form input.
authorAndy Allan <git@gravitystorm.co.uk>
Wed, 13 Jan 2021 14:05:39 +0000 (14:05 +0000)
committerAndy Allan <git@gravitystorm.co.uk>
Wed, 13 Jan 2021 14:05:39 +0000 (14:05 +0000)
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
app/views/diary_entries/new.html.erb
app/views/shared/_richtext_field.html.erb
config/initializers/bootstrap_form.rb [new file with mode: 0644]
lib/bootstrap_form/inputs/richtext_field.rb [new file with mode: 0644]
lib/richtext_form_builder.rb [deleted file]

index 9d03e774ab27fdf2dcee2f07c4ba4392daf772de..c8df07f4c4ee42ddd79ce690a1eeaecbd8ba20f7 100644 (file)
@@ -6,6 +6,6 @@
   <h1><%= @title %></h1>
 <% 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 %>
index 9d8ad61708b5104ff8f42c8251babd3a382bf850..0a10cfd03c0a24f90445a4a88ef4a72003a2fbad 100644 (file)
@@ -6,6 +6,6 @@
   <h1><%= @title %></h1>
 <% end %>
 
-<%= form_for @diary_entry, :builder => RichtextFormBuilder do |f| %>
+<%= bootstrap_form_for @diary_entry do |f| %>
   <%= render :partial => "form", :locals => { :f => f } %>
 <% end %>
index 168e9efe43cedc242a7900cae7759584385ad72c..cfe6f982788b3249f32bf027e540bad3ea0e4b23 100644 (file)
@@ -1,17 +1,14 @@
-<div class="form-group">
-  <label><%= object.class.human_attribute_name(attribute) %></label>
-  <div id="<%= id %>_container" class="form-row richtext_container">
-    <div id="<%= id %>_content" class="col-sm-8 mb-3 mb-sm-0 richtext_content">
-      <%= builder.text_area(attribute, options.merge(:wrapper => false, "data-preview-url" => preview_url(:type => type))) %>
-      <div id="<%= id %>_preview" class="richtext_preview richtext text-break"></div>
-    </div>
-    <div id="<%= id %>_help" class="col-sm-4 richtext_help">
-      <div class="card bg-light h-100">
-        <div class="card-body">
-          <%= 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" %>
-        </div>
+<div id="<%= id %>_container" class="form-row richtext_container">
+  <div id="<%= id %>_content" class="col-sm-8 mb-3 mb-sm-0 richtext_content">
+    <%= builder.text_area(attribute, options.merge(:wrapper => false, "data-preview-url" => preview_url(:type => type))) %>
+    <div id="<%= id %>_preview" class="richtext_preview richtext text-break"></div>
+  </div>
+  <div id="<%= id %>_help" class="col-sm-4 richtext_help">
+    <div class="card bg-light h-100">
+      <div class="card-body">
+        <%= 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" %>
       </div>
     </div>
   </div>
diff --git a/config/initializers/bootstrap_form.rb b/config/initializers/bootstrap_form.rb
new file mode 100644 (file)
index 0000000..a61932c
--- /dev/null
@@ -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 (file)
index 0000000..a225656
--- /dev/null
@@ -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 (file)
index 4198f38..0000000
+++ /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