Module: Tolaria::FormBuildable

Included in:
Admin::FormBuilder, Ransack::Helpers::FormBuilder
Defined in:
lib/tolaria/form_buildable.rb

Instance Method Summary collapse

Instance Method Details

#attachment_field(method, options = {}) ⇒ Object

Returns a file upload field with a more pleasant interface than browser file inputs. Changes messaging if the method already exists. Options are forwarded to the hidden file_field.



63
64
65
66
67
68
69
# File 'lib/tolaria/form_buildable.rb', line 63

def attachment_field(method, options = {})
  render(partial:"admin/shared/forms/attachment_field", locals: {
    f: self,
    method: method,
    options: options,
  })
end

#has_many(association, association_objects = nil, allow_create: true, &block) ⇒ Object

Opens an ERB block for UI to manage a has_many + accepts_nested_attributes_for association in the current form. The block will look similar to this:

<%= f.has_many :footnotes do |ff| %> <%= ff.has_many_header allow_destroy:true %> <% # Your nested model fields for footnote here %> <% end %>

You must use f.has_many_header inside the block to create headers.

If you need to pass an ordered collection or otherwise not allow automatically determining the association objects from the association symbol you can pass the collection manually as association_objects.

If allow_create is false then the button to append a new model instance will be disabled. The default is true.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/tolaria/form_buildable.rb', line 156

def has_many(association, association_objects = nil, allow_create:true, &block)

  new_object = self.object.send(association).klass.new

  view_template = self.fields_for(association, new_object, child_index:new_object.object_id) do |builder|
    yield(builder)
  end

  existing_fields = self.fields_for(association, association_objects) do |builder|
    yield(builder)
  end

  render(partial:"admin/shared/forms/has_many", locals: {
    association: association,
    button_label: association.to_s.humanize.singularize.titleize,
    new_object: new_object,
    existing_fields: existing_fields,
    allow_create: allow_create,
    f: self,
    data: {
      template: view_template.tr("\n"," "),
      id: new_object.object_id,
    }
  })

end

#has_many_header(allow_destroy: false) ⇒ Object

Creates a header suitable for use inside has_many for separating form elements. If allow_destroy is true, controls will be exposed that allow removing nested instances of the model. The default is false to match Rails’.



186
187
188
189
190
191
# File 'lib/tolaria/form_buildable.rb', line 186

def has_many_header(allow_destroy:false)
  render(partial:"admin/shared/forms/has_many_header", locals: {
    allow_destroy: allow_destroy,
    f: self,
  })
end

#hint(hint_text, options = {}) ⇒ Object

Returns a p.hint used to explain a nearby form field containing the given hint_text.



6
7
8
9
# File 'lib/tolaria/form_buildable.rb', line 6

def hint(hint_text, options = {})
  css_class = "hint #{options.delete(:class)}"
  (:p, (:span, hint_text.chomp), class:css_class, **options)
end

#image_association_select(method, collection, value_method, text_method, preview_url_method, options = {}) ⇒ Object

Creates a searchable_select that also shows a dynamic image preview of the selected record. Useful for previewing images or avatars chosen by name. preview_url_method should be a method name to call on the associated model instance that returns a fully-qualified URL to the image preview.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tolaria/form_buildable.rb', line 35

def image_association_select(method, collection, value_method, text_method, preview_url_method, options = {})
  render(partial:"admin/shared/forms/image_association_select", locals: {
    f: self,
    method: method,
    collection: collection,
    value_method: value_method,
    text_method: text_method,
    preview_url_method: preview_url_method,
    options: options,
    html_options: options,
  })
end

#image_field(method, options = {}) ⇒ Object

Returns an image upload field with a more pleasant interface than browser file inputs. Changes messaging if the method already exists.

Special Options

  • :preview_url If the file already exists, provide a URL to a 42×42px version of the image, and it will be displayed to the user in a preview box to better communicate which file they are replacing.

Other options are forwarded to the hidden file_field.



81
82
83
84
85
86
87
88
# File 'lib/tolaria/form_buildable.rb', line 81

def image_field(method, options = {})
  render(partial:"admin/shared/forms/image_field", locals: {
    f: self,
    method: method,
    options: options,
    preview_url: options[:preview_url]
  })
end

#markdown_composer(method, options = {}) ⇒ Object

Renders a Markdown composer element for editing method, with fullscreen previewing and some text assistance tools. Requires that you set Tolaria.config.markdown_renderer. Options are forwarded to text_area.



52
53
54
55
56
57
58
# File 'lib/tolaria/form_buildable.rb', line 52

def markdown_composer(method, options = {})
  render(partial:"admin/shared/forms/markdown_composer", locals: {
    f: self,
    method: method,
    options: options,
  })
end

#searchable_select(method, collection, value_method, text_method, options = {}) ⇒ Object

Creates a <select> list that can be filtered by typing word fragments. Uses the jQuery Chosen plugin internally to power the user interface. Parameters are the same as Rails’s collection_select.

Special Options

  • :multiple - if set to true, the select allows more than one choice. The default is false.


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/tolaria/form_buildable.rb', line 19

def searchable_select(method, collection, value_method, text_method, options = {})
  render(partial:"admin/shared/forms/searchable_select", locals: {
    f: self,
    method: method,
    collection: collection,
    value_method: value_method,
    text_method: text_method,
    options: options,
    html_options: options,
  })
end

#slug_field(method, options = {}) ⇒ Object

Returns a text field that parameterizes its input as users type and renders it into the given preview template. Useful for demonstrating the value of a URL or other sluggified text.

Special Options

  • :pattern - Should be a string that includes an asterisk (*) character. As the user types, the asterisk will be replaced with a parameterized version of the text in the text box and shown in a preview area below the field. The default is "/blog-example/*".

Other options are forwarded to text_field.



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/tolaria/form_buildable.rb', line 116

def slug_field(method, options = {})
  pattern = options.delete(:pattern)
  preview_value = self.object.send(method).try(:parameterize).presence || "*"
  render(partial:"admin/shared/forms/slug_field", locals: {
    f: self,
    method: method,
    options: options,
    preview_value: preview_value,
    pattern: (pattern || "/blog-example/*")
  })
end

#swatch_field(method, options = {}) ⇒ Object

Returns a text field that expects to be given a 3 or 6-digit hexadecimal color value. A preview block near the field demonstrates the provided color to the user. Options are forwarded to text_field.



132
133
134
135
136
137
138
# File 'lib/tolaria/form_buildable.rb', line 132

def swatch_field(method, options = {})
  render(partial:"admin/shared/forms/swatch_field", locals: {
    f: self,
    method: method,
    options: options,
  })
end

#timestamp_field(method, options = {}) ⇒ Object

Returns a text field that allows the user to input a date and time. Automatically validates itself and recovers to a template if blanked out. This field uses moment.js to parse the date and set the values on a set of hidden Rails datetime_select fields. Options are forwarded to the hidden datetime_select group.



95
96
97
98
99
100
101
# File 'lib/tolaria/form_buildable.rb', line 95

def timestamp_field(method, options = {})
  render(partial:"admin/shared/forms/timestamp_field", locals: {
    f: self,
    method: method,
    options: options,
  })
end