Module: CMS::FormBuilder::Fields
- Included in:
- CMS::FormBuilder
- Defined in:
- lib/cms/form_builder.rb
Instance Method Summary collapse
-
#_apply_default_options(args, defaults) ⇒ Object
simple helper method for extracting and applying default options.
-
#_apply_field_defaults(args) ⇒ Object
apply the default options for all fields.
-
#_extract_field_args(args) ⇒ Object
single use method for parsing options provided by the
field
helper. - #_field_types(type) ⇒ Object
- #actions(options = {}) ⇒ Object
- #autocomplete(name, *args) ⇒ Object
- #boolean(*args) ⇒ Object
- #email(*args) ⇒ Object
- #error_messages ⇒ Object
-
#field(*args, &block) ⇒ Object
def _input_options options [:autocomplete, :placeholder] end.
- #field_wrapper(type, name, options = {}) ⇒ Object
- #hidden(*args) ⇒ Object
- #location(name, options) ⇒ Object
- #modal_actions(options = {}) ⇒ Object
- #password(*args) ⇒ Object
- #radio(name, *args) ⇒ Object
- #search(*args) ⇒ Object
-
#slider(name, *args) ⇒ Object
slider is weird enough that we will not use the default field helper.
- #status(options = {}) ⇒ Object
- #string(*args) ⇒ Object
- #text(*args) ⇒ Object
- #text_with_counter(name, *args) ⇒ Object
- #toggle(name, *args) ⇒ Object
Instance Method Details
#_apply_default_options(args, defaults) ⇒ Object
simple helper method for extracting and applying default options.
226 227 228 229 |
# File 'lib/cms/form_builder.rb', line 226 def args, defaults = args. args << .reverse_merge!(defaults) end |
#_apply_field_defaults(args) ⇒ Object
apply the default options for all fields.
232 233 234 |
# File 'lib/cms/form_builder.rb', line 232 def _apply_field_defaults args args, label: true, wrap_field: true, label_first: true end |
#_extract_field_args(args) ⇒ Object
single use method for parsing options provided by the field
helper
237 238 239 240 241 242 243 244 |
# File 'lib/cms/form_builder.rb', line 237 def _extract_field_args args args = _apply_field_defaults(args) = args. name = args.pop type = args.pop [:label] = name.to_s.humanize if [:label].is_a? TrueClass [type, name, ] end |
#_field_types(type) ⇒ Object
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/cms/form_builder.rb', line 246 def _field_types(type) case type when :string, :location :text_field when :search :search_field when :text :text_area when :email :email_field when :password :password_field when :hidden, :slider :hidden_field when :boolean, :radio :radio_button when :boolean, :check :check_box end end |
#actions(options = {}) ⇒ Object
129 130 131 132 133 134 135 136 |
# File 'lib/cms/form_builder.rb', line 129 def actions = {} .reverse_merge! save: 'Save', saving: 'Saving...', class: 'form-actions', save_class: 'btn btn-primary' @template.content_tag(:div, class: .delete(:class)) do actions = ''.html_safe actions << submit([:save], disable_with: [:saving], class: [:save_class]) actions << status end end |
#autocomplete(name, *args) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/cms/form_builder.rb', line 40 def autocomplete name, *args args = _apply_field_defaults(args) = args. .reverse_merge!(waiting: 'Searching...') option_text = '<div class="options"><div class="inner">'.html_safe option_text.concat @template.content_tag(:div, [:waiting], class: 'waiting') option_text.concat '<div class="results"></div>'.html_safe option_text.concat '</div></div>'.html_safe field_wrapper(:autocomplete, name) do out = ''.html_safe if [:label] == true out.concat label(name) elsif [:label] out.concat label(name, [:label]) end out.concat @template.link_to 'learn more', '#', class: 'tipsy', title: send("#{name}_help_text") if [:help_text] [:help_text] = false out.concat string(name, .merge(autocomplete: false, label: false, append: option_text)) end end |
#boolean(*args) ⇒ Object
74 75 76 |
# File 'lib/cms/form_builder.rb', line 74 def boolean *args field :boolean, *(args, label_first: false) end |
#email(*args) ⇒ Object
90 91 92 |
# File 'lib/cms/form_builder.rb', line 90 def email *args field :email, *args end |
#error_messages ⇒ Object
267 268 269 270 271 |
# File 'lib/cms/form_builder.rb', line 267 def if object.errors.any? @template.render partial: 'shared/form/error_messages', object: object.errors end end |
#field(*args, &block) ⇒ Object
def _input_options options
[:autocomplete, :placeholder]
end
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/cms/form_builder.rb', line 171 def field *args, &block type, name, = _extract_field_args(args) out = ''.html_safe = {} unless [:autocomplete].nil? .delete(:autocomplete) [:autocomplete] = 'off' end unless [:placeholder].nil? [:placeholder] = .delete(:placeholder) end unless [:hidden].nil? [:class] = 'hidden' if [:hidden] == true end unless [:required].nil? [:required] = 'required' if [:required] == true end out.concat [:prepend] if [:prepend] label_html = label(name, [:label], class: 'control-label') out.concat label_html if [:label] && [:label_first] if [:help_text] help_text = send("#{name}_help_text") help_html = "<a class=\"tipsy\" title=\"#{help_text}\" href=\"#\">learn more</a>".html_safe out.concat help_html end out.concat(@template.content_tag(:div, class: 'controls') do controls = send(_field_types(type), name, ) controls.concat @template.content_tag(:div, [:help_block], class: 'help-block') if [:help_block].present? controls end) out.concat label_html if [:label] && ![:label_first] out.concat [:append] if [:append] out.concat yield if block_given? if [:wrap_field] field_wrapper(type, name) { out } else out end end |
#field_wrapper(type, name, options = {}) ⇒ Object
157 158 159 160 161 162 163 164 165 |
# File 'lib/cms/form_builder.rb', line 157 def field_wrapper type, name, = {} classes = "field #{type} #{name.to_s.dasherize} control-group" classes << [:classes] if [:classes] classes << ' error' if object.errors.include? name .merge! class: classes @template.content_tag :div, do yield end end |
#hidden(*args) ⇒ Object
98 99 100 |
# File 'lib/cms/form_builder.rb', line 98 def hidden *args field :hidden, *(args, label: false, wrap_field: false) end |
#location(name, options) ⇒ Object
36 37 38 |
# File 'lib/cms/form_builder.rb', line 36 def location name, autocomplete name, .merge(prepend: @template.content_tag(:div, '', class: 'geolocate')) end |
#modal_actions(options = {}) ⇒ Object
138 139 140 141 142 143 144 145 |
# File 'lib/cms/form_builder.rb', line 138 def modal_actions = {} .reverse_merge! save: 'Save', saving: 'Saving...', class: 'modal-footer' @template.content_tag(:div, class: .delete(:class)) do actions = ''.html_safe actions << @template.link_to('Close', '#close', class: 'btn', data: {dismiss: 'modal'}) actions << submit([:save], disable_with: [:saving], class: 'btn btn-primary') end end |
#password(*args) ⇒ Object
94 95 96 |
# File 'lib/cms/form_builder.rb', line 94 def password *args field :password, *args end |
#radio(name, *args) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/cms/form_builder.rb', line 13 def radio name, *args args = _apply_field_defaults(args) = args. values = args field_wrapper :radio, name do out = ''.html_safe out.concat label(name) if [:label] value_div = @template.content_tag(:span, class: 'values') do values.map do |value| @template.content_tag :span, class: 'value' do if value.is_a? Array (name, value[1]) + label(name, value[0], value: value[1]) else (name, value) + label(name, value, value: value) end end end.join("\n").html_safe end out.concat value_div end end |
#search(*args) ⇒ Object
82 83 84 |
# File 'lib/cms/form_builder.rb', line 82 def search *args field :search, *args end |
#slider(name, *args) ⇒ Object
slider is weird enough that we will not use the default field helper. instead, we will construct our own field that looks like a regular field.
104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/cms/form_builder.rb', line 104 def name, *args args = _apply_field_defaults(args) = args. out = ''.html_safe field_wrapper :slider, name do out.concat label(name, [:label]) if [:label] out.concat @template.content_tag :div, '', class: 'slider-edit' out.concat hidden(name) end end |
#status(options = {}) ⇒ Object
147 148 149 150 151 152 153 154 155 |
# File 'lib/cms/form_builder.rb', line 147 def status = {} .reverse_merge! success: 'Saved!', error: 'Failed!' out = @template.content_tag(:div, class: 'status') do status = ''.html_safe status << @template.content_tag(:div, '', class: 'spinner') # status << @template.content_tag(:div, options[:success], class: 'success') # status << @template.content_tag(:div, options[:error], class: 'error') end end |
#string(*args) ⇒ Object
78 79 80 |
# File 'lib/cms/form_builder.rb', line 78 def string *args field :string, *args end |
#text(*args) ⇒ Object
86 87 88 |
# File 'lib/cms/form_builder.rb', line 86 def text *args field :text, *args end |
#text_with_counter(name, *args) ⇒ Object
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/cms/form_builder.rb', line 63 def text_with_counter name, *args args = _apply_field_defaults(args) = args. unless [:length].blank? counter = "<div class=\"counter\"><span class=\"count\">0</span>/<span class=\"maximum\">#{[:length]}</span></div>".html_safe end text(name, .merge(label: [:label], append: counter)) end |
#toggle(name, *args) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/cms/form_builder.rb', line 116 def toggle name, *args args = _apply_field_defaults(args) = args. out = ''.html_safe field_wrapper :toggle, name, :'data-default' => [:default] do out.concat label(name) if [:label] out.concat(@template.content_tag(:div, class: (object.send(name) ? 'visible' : 'hidden')) do check_box(name) << @template.label_tag(object.send(name) ? 'visible' : 'hidden') end) end end |