Module: CMS::FormBuilder::Fields
- Included in:
- CMS::FormBuilder
- Defined in:
- lib/cms/form_builder.rb
Constant Summary collapse
- LABEL_WIDTH =
'col-lg-2'
- FIELD_WIDTH =
'col-lg-4'
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
- #choices(attribute, choices, *args) ⇒ Object
- #email(*args) ⇒ Object
- #error_messages ⇒ Object
-
#field(*args, &block) ⇒ Object
def _input_options options [:autocomplete, :placeholder] end.
- #field_options ⇒ Object
- #field_wrapper(type, name, options = {}) ⇒ Object
- #file(*args) ⇒ 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.
238 239 240 241 |
# File 'lib/cms/form_builder.rb', line 238 def args, defaults = args. args << .reverse_merge!(defaults) end |
#_apply_field_defaults(args) ⇒ Object
apply the default options for all fields.
244 245 246 |
# File 'lib/cms/form_builder.rb', line 244 def _apply_field_defaults args args, .reverse_merge(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
253 254 255 256 257 258 259 260 |
# File 'lib/cms/form_builder.rb', line 253 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
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/cms/form_builder.rb', line 262 def _field_types(type) case type when :string, :location :text_field when :file :file_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 when :choices :select end end |
#actions(options = {}) ⇒ Object
132 133 134 135 136 137 138 139 |
# File 'lib/cms/form_builder.rb', line 132 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
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/cms/form_builder.rb', line 35 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
69 70 71 |
# File 'lib/cms/form_builder.rb', line 69 def boolean *args field :boolean, *(args, label_first: false) end |
#choices(attribute, choices, *args) ⇒ Object
101 102 103 |
# File 'lib/cms/form_builder.rb', line 101 def choices attribute, choices, *args field :choices, *(args << attribute, choices: choices) end |
#email(*args) ⇒ Object
89 90 91 |
# File 'lib/cms/form_builder.rb', line 89 def email *args field :email, *args end |
#error_messages ⇒ Object
287 288 289 290 291 |
# File 'lib/cms/form_builder.rb', line 287 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
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 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/cms/form_builder.rb', line 174 def field *args, &block type, name, = _extract_field_args(args) out = ''.html_safe = {} input_args = [] input_classes = [:class].try(:split, ' ') || [] unless [:autocomplete].nil? .delete(:autocomplete) [:autocomplete] = 'off' end unless [:placeholder].nil? [:placeholder] = if (placeholder = .delete(:placeholder)) == true then name.to_s.humanize else placeholder end end unless [:hidden].nil? input_classes << 'hidden' if [:hidden] == true end unless [:required].nil? [:required] = 'required' if [:required] == true end unless [:choices].nil? input_args << [:choices] end out.concat [:prepend] if [:prepend] label_html = label(name, [:label], class: "#{LABEL_WIDTH} control-label") out.concat label_html if [:label] && [:label_first] if [:help_text] help_text = send("#{name}_help_text") help_html = %Q(<a class="tipsy" title="#{help_text}" href="#">learn more</a>).html_safe out.concat help_html end input_classes << 'form-control' [:class] = input_classes.join(' ') out.concat(@template.content_tag(:div, class: "#{FIELD_WIDTH} #{type}") do merged_input_args = input_args << controls = send(_field_types(type), name, *merged_input_args) 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_options ⇒ Object
248 249 250 |
# File 'lib/cms/form_builder.rb', line 248 def [:fields] || {} end |
#field_wrapper(type, name, options = {}) ⇒ Object
160 161 162 163 164 165 166 167 168 |
# File 'lib/cms/form_builder.rb', line 160 def field_wrapper type, name, = {} classes = "field #{type} #{name.to_s.dasherize} form-group" classes << [:classes] if [:classes] classes << ' error' if object.errors.include? name .merge! class: classes @template.content_tag :div, do yield end end |
#file(*args) ⇒ Object
77 78 79 |
# File 'lib/cms/form_builder.rb', line 77 def file *args field :file, *args end |
#hidden(*args) ⇒ Object
97 98 99 |
# File 'lib/cms/form_builder.rb', line 97 def hidden *args field :hidden, *(args, label: false, wrap_field: false) end |
#location(name, options) ⇒ Object
31 32 33 |
# File 'lib/cms/form_builder.rb', line 31 def location name, autocomplete name, .merge(prepend: @template.content_tag(:div, '', class: 'geolocate')) end |
#modal_actions(options = {}) ⇒ Object
141 142 143 144 145 146 147 148 |
# File 'lib/cms/form_builder.rb', line 141 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
93 94 95 |
# File 'lib/cms/form_builder.rb', line 93 def password *args field :password, *args end |
#radio(name, *args) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/cms/form_builder.rb', line 7 def radio name, *args args = _apply_field_defaults(args) = args. values = args field_wrapper :radio, name do out = ''.html_safe out.concat label(name, class: "#{LABEL_WIDTH} control-label") if [:label] value_div = @template.content_tag(:div, class: "#{FIELD_WIDTH} radio") do values.map do |value| @template.content_tag :span, class: 'value' do if value.is_a? Array (name, value[1].html_safe) + label(name, value[0].html_safe, 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
81 82 83 |
# File 'lib/cms/form_builder.rb', line 81 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.
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/cms/form_builder.rb', line 107 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
150 151 152 153 154 155 156 157 158 |
# File 'lib/cms/form_builder.rb', line 150 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
73 74 75 |
# File 'lib/cms/form_builder.rb', line 73 def string *args field :string, *args end |
#text(*args) ⇒ Object
85 86 87 |
# File 'lib/cms/form_builder.rb', line 85 def text *args field :text, *args end |
#text_with_counter(name, *args) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/cms/form_builder.rb', line 58 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
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/cms/form_builder.rb', line 119 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 |