Module: ActiveScaffold::Helpers::FormColumnHelpers
- Included in:
- ViewHelpers
- Defined in:
- lib/active_scaffold/bridges/file_column/form_ui.rb,
lib/active_scaffold/bridges/dragonfly/form_ui.rb,
lib/active_scaffold/bridges/paperclip/form_ui.rb,
lib/active_scaffold/bridges/carrierwave/form_ui.rb,
lib/active_scaffold/helpers/form_column_helpers.rb
Overview
Helpers that assist with the rendering of a Form Column
Instance Method Summary collapse
- #active_scaffold_add_existing_input(options) ⇒ Object
- #active_scaffold_add_existing_label ⇒ Object
- #active_scaffold_checkbox_list(column, select_options, associated_ids, options) ⇒ Object
- #active_scaffold_checkbox_option(option, label_method, associated_ids, checkbox_options, li_options = {}) ⇒ Object
- #active_scaffold_enum_options(column, record = nil) ⇒ Object
-
#active_scaffold_grouped_options(column, select_options, optgroup) ⇒ Object
Form input methods.
-
#active_scaffold_input_boolean(column, options) ⇒ Object
Column.type-based inputs.
- #active_scaffold_input_carrierwave(column, options) ⇒ Object
- #active_scaffold_input_checkbox(column, options) ⇒ Object
- #active_scaffold_input_dragonfly(column, options) ⇒ Object
-
#active_scaffold_input_email(column, options) ⇒ Object
A text box, that accepts only valid email address (in-browser validation).
- #active_scaffold_input_enum(column, html_options) ⇒ Object
- #active_scaffold_input_file_column(column, options) ⇒ Object
-
#active_scaffold_input_for(column, scope = nil, options = nil) ⇒ Object
This method decides which input to use for the given column.
-
#active_scaffold_input_number(column, options) ⇒ Object
A spinbox control for number values (in-browser validation).
-
#active_scaffold_input_options(column, scope = nil, options = {}) ⇒ Object
the standard active scaffold options used for class, name and scope.
- #active_scaffold_input_paperclip(column, options) ⇒ Object
- #active_scaffold_input_password(column, options) ⇒ Object
- #active_scaffold_input_plural_association(column, options) ⇒ Object
- #active_scaffold_input_radio(column, html_options) ⇒ Object
-
#active_scaffold_input_range(column, options) ⇒ Object
A slider control for number values (in-browser validation).
- #active_scaffold_input_select(column, html_options) ⇒ Object
- #active_scaffold_input_singular_association(column, html_options) ⇒ Object
-
#active_scaffold_input_telephone(column, options) ⇒ Object
A text box, that accepts only valid phone-number (in-browser validation).
-
#active_scaffold_input_text_options(options = {}) ⇒ Object
the standard active scaffold options used for textual inputs.
- #active_scaffold_input_textarea(column, options) ⇒ Object
-
#active_scaffold_input_url(column, options) ⇒ Object
A text box, that accepts only valid URI (in-browser validation).
- #active_scaffold_input_virtual(column, options) ⇒ Object
- #active_scaffold_plural_association_options(column, record = nil) ⇒ Object
- #active_scaffold_refresh_link(column, html_options, record) ⇒ Object
- #active_scaffold_render_input(column, options) ⇒ Object
- #active_scaffold_render_subform_column(column, scope, crud_type, readonly, add_class = false, record = nil) ⇒ Object
- #active_scaffold_subform_attributes(column, column_css_class = nil) ⇒ Object
- #active_scaffold_translate_select_options(options) ⇒ Object
- #active_scaffold_translated_option(column, text, value = nil) ⇒ Object
-
#column_renders_as(column) ⇒ Object
Macro-level rendering decisions for columns.
- #column_scope(column, scope = nil, record = nil) ⇒ Object
- #column_show_add_existing(column, record = nil) ⇒ Object
- #column_show_add_new(column, associated, record) ⇒ Object
- #current_form_columns(record, scope, subform_controller = nil) ⇒ Object
- #field_attributes(column, record) ⇒ Object
- #form_attribute(column, record, scope = nil, only_value = false, col_class = nil) ⇒ Object
- #form_hidden_attribute(column, record, scope = nil) ⇒ Object
-
#in_subform?(column, parent_record) ⇒ Boolean
Should this column be displayed in the subform?.
- #label_for(column, options) ⇒ Object
-
#numerical_constraints_for_column(column, options) ⇒ Object
Try to get numerical constraints from model’s validators.
- #onsubmit ⇒ Object
- #override_form_field(column) ⇒ Object (also: #override_form_field?)
-
#override_form_field_partial(column) ⇒ Object
the naming convention for overriding form fields with helpers.
- #override_form_field_partial?(column) ⇒ Boolean
-
#override_input(form_ui) ⇒ Object
(also: #override_input?)
the naming convention for overriding form input types with helpers.
- #override_subform_partial(column, subform_partial) ⇒ Object
-
#override_subform_partial?(column, subform_partial) ⇒ Boolean
add functionality for overriding subform partials from association class path.
- #render_column(column, record, renders_as, scope = nil, only_value = false, col_class = nil) ⇒ Object
- #subform_label(column, hidden) ⇒ Object
- #subform_partial_for_column(column) ⇒ Object
- #update_columns_options(column, scope, options, force = false) ⇒ Object
Instance Method Details
#active_scaffold_add_existing_input(options) ⇒ Object
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 534 def active_scaffold_add_existing_input() record = .delete(:object) ActiveSupport::Deprecation.warn 'Relying on @record is deprecated, include :object in options with record.', caller if record.nil? # TODO: Remove when relying on @record is removed record ||= @record # TODO: Remove when relying on @record is removed if !ActiveScaffold.js_framework.nil? && controller.respond_to?(:record_select_config, true) remote_controller = active_scaffold_controller_for(record_select_config.model).controller_path .merge!(:controller => remote_controller) .merge!() record_select_field([:name], record, ) else = (nested.association, nil, record) ||= active_scaffold_config.model.all = (, :id, :to_label) select_tag 'associated_id', ('<option value="">' + as_(:_select_) + '</option>' + ).html_safe unless .empty? end end |
#active_scaffold_add_existing_label ⇒ Object
551 552 553 554 555 556 557 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 551 def active_scaffold_add_existing_label if controller.respond_to?(:record_select_config, true) record_select_config.model.model_name.human else active_scaffold_config.model.model_name.human end end |
#active_scaffold_checkbox_list(column, select_options, associated_ids, options) ⇒ Object
320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 320 def active_scaffold_checkbox_list(column, , associated_ids, ) label_method = column.[:label_method] || :to_label html = hidden_field_tag("#{[:name]}[]", '', :id => nil) html << content_tag(:ul, .merge(:class => "#{[:class]} checkbox-list#{' draggable-lists' if column.[:draggable_lists]}")) do content = ''.html_safe .each_with_index do |option, i| content << active_scaffold_checkbox_option(option, label_method, associated_ids, :name => "#{[:name]}[]", :id => "#{[:id]}_#{i}_id") end content end html end |
#active_scaffold_checkbox_option(option, label_method, associated_ids, checkbox_options, li_options = {}) ⇒ Object
311 312 313 314 315 316 317 318 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 311 def active_scaffold_checkbox_option(option, label_method, associated_ids, , = {}) content_tag(:li, ) do option_id = option.is_a?(Array) ? option[1] : option.id label = option.is_a?(Array) ? option[0] : option.send(label_method) check_box_tag([:name], option_id, associated_ids.include?(option_id), ) << content_tag(:label, label, :for => [:id]) end end |
#active_scaffold_enum_options(column, record = nil) ⇒ Object
338 339 340 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 338 def (column, record = nil) column.[:options] end |
#active_scaffold_grouped_options(column, select_options, optgroup) ⇒ Object
Form input methods
233 234 235 236 237 238 239 240 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 233 def (column, , optgroup) group_column = active_scaffold_config_for(column.association.klass).columns[optgroup] group_label = group_column.[:label_method] if group_column group_label ||= group_column.try(:association) ? :to_label : :to_s .group_by(&optgroup.to_sym).collect do |group, | [group.send(group_label), .collect { |r| [r.send(column.[:label_method] || :to_label), r.id] }] end end |
#active_scaffold_input_boolean(column, options) ⇒ Object
Column.type-based inputs
450 451 452 453 454 455 456 457 458 459 460 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 450 def active_scaffold_input_boolean(column, ) record = .delete(:object) ActiveSupport::Deprecation.warn 'Relying on @record is deprecated, include :object in options with record.', caller if record.nil? # TODO: Remove when relying on @record is removed record ||= @record # TODO: Remove when relying on @record is removed = [] << [as_(:_select_), nil] if !column.virtual? && column.column.null << [as_(:true), true] << [as_(:false), false] select_tag([:name], (, record.send(column.name)), ) end |
#active_scaffold_input_carrierwave(column, options) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/active_scaffold/bridges/carrierwave/form_ui.rb', line 4 def active_scaffold_input_carrierwave(column, ) record = [:object] ActiveSupport::Deprecation.warn 'Relying on @record is deprecated, include :object in html_options with record.', caller if record.nil? # TODO: Remove when relying on @record is removed record ||= @record # TODO: Remove when relying on @record is removed = (.merge(column.)) carrierwave = record.send("#{column.name}") if !carrierwave.file.blank? required = .delete(:required) = { :name => [:name].gsub(/\[#{column.name}\]$/, "[remove_#{column.name}]"), :id => 'remove_' + [:id], :value => false } = { :name => [:name].gsub(/\[#{column.name}\]$/, "[#{column.name}_cache]"), :id => [:id] + '_cache' } case ActiveScaffold.js_framework when :jquery js_remove_file_code = "jQuery(this).prev('input#remove_#{[:id]}').val('true'); jQuery(this).parent().hide().next().show()#{".find('input').attr('required', 'required')" if required}; return false;" js_dont_remove_file_code = "jQuery(this).parents('div.carrierwave_controls').find('input#remove_#{[:id]}').val('false'); return false;" when :prototype js_remove_file_code = "$(this).previous('input#remove_#{[:id]}').value='true'; $(this).up().hide().next().show()#{".down().writeAttribute('required', 'required')" if required}; return false;" js_dont_remove_file_code = "$(this).up('div.carrierwave_controls').down('input#remove_#{[:id]}').value='false'; return false;" end input = file_field(:record, column.name, .merge(:onchange => js_dont_remove_file_code)) content_tag( :div, content_tag(:div, ( get_column_value(record, column) + ' | ' + hidden_field(:record, "#{column.name}_cache", ) + hidden_field(:record, "remove_#{column.name}", ) + content_tag(:a, as_(:remove_file), :href => '#', :onclick => js_remove_file_code) ).html_safe ) + content_tag(:div, input, :style => 'display: none'), :class => 'carrierwave_controls' ) else file_field(:record, column.name, ) end end |
#active_scaffold_input_checkbox(column, options) ⇒ Object
393 394 395 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 393 def active_scaffold_input_checkbox(column, ) check_box(:record, column.name, .merge(column.)) end |
#active_scaffold_input_dragonfly(column, options) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/active_scaffold/bridges/dragonfly/form_ui.rb', line 4 def active_scaffold_input_dragonfly(column, ) record = [:object] ActiveSupport::Deprecation.warn 'Relying on @record is deprecated, include :object in html_options with record.', caller if record.nil? # TODO: Remove when relying on @record is removed record ||= @record # TODO: Remove when relying on @record is removed = (.merge(column.)) dragonfly = record.send("#{column.name}") required = .delete(:required) if dragonfly.present? input = file_field(:record, column.name, ) if dragonfly.present? case ActiveScaffold.js_framework when :jquery js_remove_file_code = "jQuery(this).prev().val('true'); jQuery(this).parent().hide().next().show()#{".find('input').attr('required', 'required')" if required}; return false;" when :prototype js_remove_file_code = "$(this).previous().value='true'; $(this).up().hide().next().show()#{".down().writeAttribute('required', 'required')" if required}; return false;" end content = active_scaffold_column_dragonfly(record, column) content_tag( :div, content + ' | ' + hidden_field(:record, "remove_#{column.name}", :value => 'false') + content_tag(:a, as_(:remove_file), :href => '#', :onclick => js_remove_file_code) ) + content_tag(:div, input, :style => 'display: none') else input end end |
#active_scaffold_input_email(column, options) ⇒ Object
A text box, that accepts only valid email address (in-browser validation)
415 416 417 418 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 415 def active_scaffold_input_email(column, ) = () email_field :record, column.name, .merge(column.) end |
#active_scaffold_input_enum(column, html_options) ⇒ Object
342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 342 def active_scaffold_input_enum(column, ) record = .delete(:object) ActiveSupport::Deprecation.warn 'Relying on @record is deprecated, include :object in html_options with record.', caller if record.nil? # TODO: Remove when relying on @record is removed record ||= @record # TODO: Remove when relying on @record is removed = {:selected => record.send(column.name), :object => record} = (column, record).collect do |text, value| active_scaffold_translated_option(column, text, value) end .merge!(column.[:html_options] || {}) .merge!(column.) [:name] = "#{[:name]}[]" if [:multiple] == true && ![:name].to_s.ends_with?('[]') () select(:record, column.name, , , ) end |
#active_scaffold_input_file_column(column, options) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/active_scaffold/bridges/file_column/form_ui.rb', line 5 def active_scaffold_input_file_column(column, ) record = [:object] ActiveSupport::Deprecation.warn 'Relying on @record is deprecated, include :object in html_options with record.', caller if record.nil? # TODO: Remove when relying on @record is removed record ||= @record # TODO: Remove when relying on @record is removed if record.send(column.name) # we already have a value? display the form for deletion. case ActiveScaffold.js_framework when :jquery remove_file_js = "jQuery(this).prev().val('true'); jQuery(this).parent().hide().next().show(); return false;" when :prototype remove_file_js = "$(this).previous().value='true'; p=$(this).up(); p.hide(); p.next().show(); return false;" end = .merge(:id => [:id] + '_delete', :name => [:name].sub("[#{column.name}]", "[delete_#{column.name}]"), :value => 'false') custom_hidden_field_tag = hidden_field(:record, column.name, ) content_tag(:div) do content_tag(:div) do get_column_value(record, column) + " #{custom_hidden_field_tag} | ".html_safe << content_tag(:a, as_(:remove_file), :href => '#', :onclick => remove_file_js) << content_tag(:div, file_column_field('record', column.name, ), :style => 'display: none') end end else file_column_field('record', column.name, ) end end |
#active_scaffold_input_for(column, scope = nil, options = nil) ⇒ Object
This method decides which input to use for the given column. It does not do any rendering. It only decides which method is responsible for rendering.
7 8 9 10 11 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 7 def active_scaffold_input_for(column, scope = nil, = nil) ||= (column, scope) = (column, scope, ) active_scaffold_render_input(column, ) end |
#active_scaffold_input_number(column, options) ⇒ Object
A spinbox control for number values (in-browser validation)
433 434 435 436 437 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 433 def active_scaffold_input_number(column, ) = numerical_constraints_for_column(column, ) = () number_field :record, column.name, .merge(column.) end |
#active_scaffold_input_options(column, scope = nil, options = {}) ⇒ Object
the standard active scaffold options used for class, name and scope
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 100 def (column, scope = nil, = {}) name = scope ? "record#{scope}[#{column.name}]" : "record[#{column.name}]" record = [:object] # Add some HTML5 attributes for in-browser validation and better user experience if column.required? && (!@disable_required_for_new || scope.nil? || record.try(:persisted?)) [:required] = true end [:placeholder] = column.placeholder if column.placeholder.present? # Fix for keeping unique IDs in subform id_control = "record_#{column.name}_#{[params[:eid], params[:parent_id] || params[:id]].compact.join '_'}" id_control += scope_id(scope) if scope classes = "#{column.name}-input" classes += ' numeric-input' if column.number? {:name => name, :class => classes, :id => id_control}.merge() end |
#active_scaffold_input_paperclip(column, options) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/active_scaffold/bridges/paperclip/form_ui.rb', line 4 def active_scaffold_input_paperclip(column, ) record = [:object] ActiveSupport::Deprecation.warn 'Relying on @record is deprecated, include :object in html_options with record.', caller if record.nil? # TODO: Remove when relying on @record is removed record ||= @record # TODO: Remove when relying on @record is removed = (.merge(column.)) paperclip = record.send("#{column.name}") required = .delete(:required) if paperclip.file? input = file_field(:record, column.name, ) if paperclip.file? case ActiveScaffold.js_framework when :jquery js_remove_file_code = "jQuery(this).prev().val('true'); jQuery(this).parent().hide().next().show()#{".find('input').attr('required', 'required')" if required}; return false;" when :prototype js_remove_file_code = "$(this).previous().value='true'; $(this).up().hide().next().show()#{".down().writeAttribute('required', 'required')" if required}; return false;" end object_name, method = [:name].split(/\[(#{column.name})\]/) method.sub!(/#{column.name}/, 'delete_\0') content = active_scaffold_column_paperclip(record, column) content_tag( :div, content + ' | ' + hidden_field(object_name, method, :value => 'false') + content_tag(:a, as_(:remove_file), :href => '#', :onclick => js_remove_file_code) ) + content_tag(:div, input, :style => 'display: none') else input end end |
#active_scaffold_input_password(column, options) ⇒ Object
397 398 399 400 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 397 def active_scaffold_input_password(column, ) = () password_field :record, column.name, .merge(column.) end |
#active_scaffold_input_plural_association(column, options) ⇒ Object
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 295 def active_scaffold_input_plural_association(column, ) record = .delete(:object) ActiveSupport::Deprecation.warn 'Relying on @record is deprecated, include :object in options with record.', caller if record.nil? # TODO: Remove when relying on @record is removed record ||= @record # TODO: Remove when relying on @record is removed , = (column, record) html = if .empty? content_tag(:span, as_(:no_options), :class => "#{[:class]} no-options", :id => [:id]) else active_scaffold_checkbox_list(column, , .collect(&:id), ) end html << active_scaffold_refresh_link(column, , record) if column.[:refresh_link] html end |
#active_scaffold_input_radio(column, html_options) ⇒ Object
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 367 def active_scaffold_input_radio(column, ) record = [:object] .merge!(column.[:html_options] || {}) = if column.association (column.association, nil, record) else (column, record) end id_key = [:"data-id"] ? :"data-id" : :id label_method = column.[:label_method] || :to_label if column.association .each_with_object('') do |(text, value), html| if column.association text, value = [text.send(label_method), text.id] checked = {:checked => [:object].send(column.association.name).try(:id) == value} else text, value = active_scaffold_translated_option(column, text, value) end = .merge(id_key => [id_key] + '-' + value.to_s.parameterize) .merge!(checked) if checked html << content_tag(:label, (:record, column.name, value, ) + text) end.html_safe end |
#active_scaffold_input_range(column, options) ⇒ Object
A slider control for number values (in-browser validation)
440 441 442 443 444 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 440 def active_scaffold_input_range(column, ) = numerical_constraints_for_column(column, ) = () range_field :record, column.name, .merge(column.) end |
#active_scaffold_input_select(column, html_options) ⇒ Object
357 358 359 360 361 362 363 364 365 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 357 def active_scaffold_input_select(column, ) if column.singular_association? active_scaffold_input_singular_association(column, ) elsif column.plural_association? active_scaffold_input_plural_association(column, ) else active_scaffold_input_enum(column, ) end end |
#active_scaffold_input_singular_association(column, html_options) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 248 def active_scaffold_input_singular_association(column, ) record = .delete(:object) ActiveSupport::Deprecation.warn 'Relying on @record is deprecated, include :object in html_options with record.', caller if record.nil? # TODO: Remove when relying on @record is removed record ||= @record # TODO: Remove when relying on @record is removed associated = record.send(column.association.name) = (column.association, nil, record) .unshift(associated) unless associated.nil? || .include?(associated) method = column.name = {:selected => associated.try(:id), :include_blank => as_(:_select_), :object => record} .merge!(column.[:html_options] || {}) .merge!(column.) [:name] = "#{[:name]}[]" if [:multiple] == true && ![:name].to_s.ends_with?('[]') () html = if (optgroup = .delete(:optgroup)) select(:record, method, (column, , optgroup), , ) else collection_select(:record, method, , :id, column.[:label_method] || :to_label, , ) end html << active_scaffold_refresh_link(column, , record) if column.[:refresh_link] html end |
#active_scaffold_input_telephone(column, options) ⇒ Object
A text box, that accepts only valid phone-number (in-browser validation)
427 428 429 430 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 427 def active_scaffold_input_telephone(column, ) = () telephone_field :record, column.name, .merge(column.) end |
#active_scaffold_input_text_options(options = {}) ⇒ Object
the standard active scaffold options used for textual inputs
93 94 95 96 97 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 93 def ( = {}) [:autocomplete] = 'off' [:class] = "#{[:class]} text-input".strip end |
#active_scaffold_input_textarea(column, options) ⇒ Object
402 403 404 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 402 def active_scaffold_input_textarea(column, ) text_area(:record, column.name, .merge(:cols => column.[:cols], :rows => column.[:rows], :size => column.[:size])) end |
#active_scaffold_input_url(column, options) ⇒ Object
A text box, that accepts only valid URI (in-browser validation)
421 422 423 424 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 421 def active_scaffold_input_url(column, ) = () url_field :record, column.name, .merge(column.) end |
#active_scaffold_input_virtual(column, options) ⇒ Object
406 407 408 409 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 406 def active_scaffold_input_virtual(column, ) = () text_field :record, column.name, .merge(column.) end |
#active_scaffold_plural_association_options(column, record = nil) ⇒ Object
288 289 290 291 292 293 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 288 def (column, record = nil) ActiveSupport::Deprecation.warn 'Relying on @record is deprecated, call with record.', caller if record.nil? # TODO: Remove when relying on @record is removed record ||= @record # TODO: Remove when relying on @record is removed = record.send(column.association.name) [, | (column.association, nil, record)] end |
#active_scaffold_refresh_link(column, html_options, record) ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 275 def active_scaffold_refresh_link(column, , record) = {:object => record} if ['data-update_url'] ['data-update_send_form'] = ['data-update_send_form'] ['data-update_send_form_selector'] = ['data-update_send_form_selector'] else scope = [:name].scan(/^record((\[[^\]]*\])*)\[#{column.name}\]/)[0].try(:first) if [:name] = (column, scope.presence, , true) end [:class] = 'refresh-link' link_to(as_(:refresh), .delete('data-update_url') || ['data-update_url'], ) end |
#active_scaffold_render_input(column, options) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 13 def active_scaffold_render_input(column, ) record = [:object] ActiveSupport::Deprecation.warn 'Relying on @record is deprecated, include :object in options with record.', caller if record.nil? # TODO: Remove when relying on @record is removed record ||= @record # TODO: Remove when relying on @record is removed # first, check if the dev has created an override for this specific field if (method = override_form_field(column)) send(method, record, ) # second, check if the dev has specified a valid form_ui for this column elsif column.form_ui && (method = override_input(column.form_ui)) send(method, column, ) # fallback: we get to make the decision else if column.association if column.form_ui.nil? # its an association and nothing is specified, we will assume form_ui :select active_scaffold_input_select(column, ) else # if we get here, it's because the column has a form_ui but not one ActiveScaffold knows about. raise "Unknown form_ui `#{column.form_ui}' for column `#{column.name}'" end elsif column.virtual? [:value] = format_number_value(record.send(column.name), column.) if column.number? active_scaffold_input_virtual(column, ) else # regular model attribute column # if we (or someone else) have created a custom render option for the column type, use that if (method = override_input(column.column.type)) send(method, column, ) # final ultimate fallback: use rails' generic input method else # for textual fields we pass different options text_types = [:text, :string, :integer, :float, :decimal, :date, :time, :datetime] = () if text_types.include?(column.column.type) if column.column.type == :string && [:maxlength].blank? [:maxlength] = column.column.limit [:size] ||= [:maxlength].to_i > 30 ? 30 : [:maxlength] end [:include_blank] = true if column.column.null && [:date, :datetime, :time].include?(column.column.type) [:value] = format_number_value(record.send(column.name), column.) if column.number? text_field(:record, column.name, .merge(column.)) end end end rescue StandardError => e logger.error "#{e.class.name}: #{e.} -- on the ActiveScaffold column = :#{column.name} in #{controller.class}" raise e end |
#active_scaffold_render_subform_column(column, scope, crud_type, readonly, add_class = false, record = nil) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 62 def active_scaffold_render_subform_column(column, scope, crud_type, readonly, add_class = false, record = nil) ActiveSupport::Deprecation.warn 'Relying on @record is deprecated, call with record.', caller if record.nil? # TODO: Remove when relying on @record is removed record ||= @record # TODO: Remove when relying on @record is removed if add_class col_class = [] col_class << 'required' if column.required? col_class << column.css_class unless column.css_class.nil? || column.css_class.is_a?(Proc) col_class << 'hidden' if column_renders_as(column) == :hidden col_class << 'checkbox' if column.form_ui == :checkbox col_class = col_class.join(' ') end if readonly && !record.new_record? || !record.(:crud_type => crud_type, :column => column.name) = (column, scope).except(:name) [:class] = "#{[:class]} #{col_class}" if col_class content_tag :span, get_column_value(record, column), else renders_as = column_renders_as(column) html = render_column(column, record, renders_as, scope, false, col_class) html = content_tag(:div, html, active_scaffold_subform_attributes(column)) if renders_as == :subform html end end |
#active_scaffold_subform_attributes(column, column_css_class = nil) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 85 def active_scaffold_subform_attributes(column, column_css_class = nil) { :class => "sub-form #{active_scaffold_config_for(column.association.klass).subform.layout}-sub-form #{column_css_class} #{column.name}-sub-form", :id => sub_form_id(:association => column.name) } end |
#active_scaffold_translate_select_options(options) ⇒ Object
242 243 244 245 246 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 242 def () [:include_blank] = as_([:include_blank].to_s) if [:include_blank].is_a? Symbol [:prompt] = as_([:prompt].to_s) if [:prompt].is_a? Symbol end |
#active_scaffold_translated_option(column, text, value = nil) ⇒ Object
333 334 335 336 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 333 def active_scaffold_translated_option(column, text, value = nil) value = text if value.nil? [(text.is_a?(Symbol) ? column.active_record_class.human_attribute_name(text) : text), value] end |
#column_renders_as(column) ⇒ Object
Macro-level rendering decisions for columns
513 514 515 516 517 518 519 520 521 522 523 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 513 def column_renders_as(column) if column.respond_to? :each return :subsection elsif column.active_record_class.locking_column.to_s == column.name.to_s || column.form_ui == :hidden return :hidden elsif column.association.nil? || column.form_ui || !active_scaffold_config_for(column.association.klass).actions.include?(:subform) || override_form_field?(column) return :field else return :subform end end |
#column_scope(column, scope = nil, record = nil) ⇒ Object
525 526 527 528 529 530 531 532 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 525 def column_scope(column, scope = nil, record = nil) ActiveSupport::Deprecation.warn 'Relying on @record is deprecated, call with record.', caller if record.nil? # TODO: Remove when relying on @record is removed if column.plural_association? "#{scope}[#{column.name}][#{record.id || generate_temporary_id(record)}]" else "#{scope}[#{column.name}]" end end |
#column_show_add_existing(column, record = nil) ⇒ Object
217 218 219 220 221 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 217 def column_show_add_existing(column, record = nil) ActiveSupport::Deprecation.warn 'Relying on @record is deprecated, call with record.', caller if record.nil? # TODO: Remove when relying on @record is removed record ||= @record # TODO: Remove when relying on @record is removed (column.allow_add_existing && (column.association, record) > 0) end |
#column_show_add_new(column, associated, record) ⇒ Object
223 224 225 226 227 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 223 def column_show_add_new(column, associated, record) value = (column.plural_association? && !column.readonly_association?) || column.singular_association? value &&= false unless column.association.klass.(:crud_type => :create) value end |
#current_form_columns(record, scope, subform_controller = nil) ⇒ Object
120 121 122 123 124 125 126 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 120 def current_form_columns(record, scope, subform_controller = nil) if scope subform_controller.active_scaffold_config.subform.columns.names elsif [:new, :create, :edit, :update, :render_field].include? action_name.to_sym active_scaffold_config.send(record.new_record? ? :create : :update).columns.names end end |
#field_attributes(column, record) ⇒ Object
154 155 156 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 154 def field_attributes(column, record) {} end |
#form_attribute(column, record, scope = nil, only_value = false, col_class = nil) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 170 def form_attribute(column, record, scope = nil, only_value = false, col_class = nil) = (column, scope, :object => record) attributes = field_attributes(column, record) attributes[:class] = "#{attributes[:class]} #{col_class}" if col_class.present? field = if only_value content_tag(:span, get_column_value(record, column), .except(:name, :object)) << hidden_field(:record, column.association ? column.association.foreign_key : column.name, ) else active_scaffold_input_for column, scope, end content_tag :dl, attributes do %(<dt>#{label_tag label_for(column, ), column.label}</dt><dd>#{field} #{loading_indicator_tag(:action => :render_field, :id => params[:id]) if column.update_columns} #{content_tag :span, column.description, :class => 'description' if column.description.present?} </dd>).html_safe end end |
#form_hidden_attribute(column, record, scope = nil) ⇒ Object
198 199 200 201 202 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 198 def form_hidden_attribute(column, record, scope = nil) %(<dl style="display: none;"><dt></dt><dd> #{hidden_field :record, column.name, (column, scope).merge(:object => record)} </dd></dl>).html_safe end |
#in_subform?(column, parent_record) ⇒ Boolean
Should this column be displayed in the subform?
205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 205 def in_subform?(column, parent_record) return true unless column.association # Polymorphic associations can't appear because they *might* be the reverse association, and because you generally don't assign an association from the polymorphic side ... I think. return false if column.polymorphic_association? # A column shouldn't be in the subform if it's the reverse association to the parent return false if column.association.inverse_for?(parent_record.class) true end |
#label_for(column, options) ⇒ Object
190 191 192 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 190 def label_for(column, ) [:id] unless column.form_ui == :select && column.plural_association? end |
#numerical_constraints_for_column(column, options) ⇒ Object
Try to get numerical constraints from model’s validators
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 560 def numerical_constraints_for_column(column, ) if column.numerical_constraints.nil? numerical_constraints = {} validators = column.active_record_class.validators.select do |v| v.is_a?(ActiveModel::Validations::NumericalityValidator) && v.attributes.include?(column.name) end equal_to = (val = validators.find { |v| v.[:equal_to] }) ? val.[:equal_to] : nil # If there is equal_to constraint - use it (unless otherwise specified by user) if equal_to && !([:min] || [:max]) numerical_constraints[:min] = numerical_constraints[:max] = equal_to else # find minimum and maximum from validators # we can safely modify :min and :max by 1 for :greater_tnan or :less_than value only for integer values only_integer = column.column.type == :integer if column.column only_integer ||= validators.find { |v| v.[:only_integer] }.present? margin = only_integer ? 1 : 0 # Minimum unless [:min] min = validators.map { |v| v.[:greater_than_or_equal] }.compact.max greater_than = validators.map { |v| v.[:greater_than] }.compact.max numerical_constraints[:min] = [min, (greater_than + margin if greater_than)].compact.max end # Maximum unless [:max] max = validators.map { |v| v.[:less_than_or_equal] }.compact.min less_than = validators.map { |v| v.[:less_than] }.compact.min numerical_constraints[:max] = [max, (less_than - margin if less_than)].compact.min end # Set step = 2 for column values restricted to be odd or even (but only if minimum is set) unless [:step] only_odd_valid = validators.any? { |v| v.[:odd] } only_even_valid = validators.any? { |v| v.[:even] } unless only_odd_valid if !only_integer numerical_constraints[:step] ||= "0.#{'0' * (column.column.scale - 1)}1" if column.column && column.column.scale.to_i > 0 elsif [:min] && [:min].respond_to?(:even?) && (only_odd_valid || only_even_valid) numerical_constraints[:step] = 2 numerical_constraints[:min] += 1 if only_odd_valid && ![:min].odd? numerical_constraints[:min] += 1 if only_even_valid && ![:min].even? end numerical_constraints[:step] ||= 'any' unless only_integer end end column.numerical_constraints = numerical_constraints end column.numerical_constraints.merge() end |
#onsubmit ⇒ Object
462 463 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 462 def onsubmit end |
#override_form_field(column) ⇒ Object Also known as: override_form_field?
488 489 490 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 488 def override_form_field(column) override_helper column, 'form_column' end |
#override_form_field_partial(column) ⇒ Object
the naming convention for overriding form fields with helpers
483 484 485 486 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 483 def override_form_field_partial(column) path = active_scaffold_controller_for(column.active_record_class).controller_path File.join(path, "#{clean_column_name(column.name)}_form_column") end |
#override_form_field_partial?(column) ⇒ Boolean
478 479 480 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 478 def override_form_field_partial?(column) template_exists?(override_form_field_partial(column), true) end |
#override_input(form_ui) ⇒ Object Also known as: override_input?
the naming convention for overriding form input types with helpers
494 495 496 497 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 494 def override_input(form_ui) method = "active_scaffold_input_#{form_ui}" method if respond_to? method end |
#override_subform_partial(column, subform_partial) ⇒ Object
474 475 476 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 474 def override_subform_partial(column, subform_partial) File.join(active_scaffold_controller_for(column.association.klass).controller_path, subform_partial) if column_renders_as(column) == :subform end |
#override_subform_partial?(column, subform_partial) ⇒ Boolean
add functionality for overriding subform partials from association class path
470 471 472 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 470 def override_subform_partial?(column, subform_partial) template_exists?(override_subform_partial(column, subform_partial), true) end |
#render_column(column, record, renders_as, scope = nil, only_value = false, col_class = nil) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 158 def render_column(column, record, renders_as, scope = nil, only_value = false, col_class = nil) if override_form_field_partial?(column) render :partial => override_form_field_partial(column), :locals => {:column => column, :only_value => only_value, :scope => scope, :col_class => col_class, :record => record} elsif renders_as == :field || override_form_field?(column) form_attribute(column, record, scope, only_value, col_class) elsif renders_as == :subform render :partial => 'form_association', :locals => {:column => column, :scope => scope, :parent_record => record} else form_hidden_attribute(column, record, scope) end end |
#subform_label(column, hidden) ⇒ Object
194 195 196 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 194 def subform_label(column, hidden) column.label unless hidden end |
#subform_partial_for_column(column) ⇒ Object
500 501 502 503 504 505 506 507 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 500 def subform_partial_for_column(column) subform_partial = "#{active_scaffold_config_for(column.association.klass).subform.layout}_subform" if override_subform_partial?(column, subform_partial) override_subform_partial(column, subform_partial) else subform_partial end end |
#update_columns_options(column, scope, options, force = false) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 128 def (column, scope, , force = false) record = [:object] ActiveSupport::Deprecation.warn 'Relying on @record is deprecated, include :object in options with record.', caller if record.nil? # TODO: Remove when relying on @record is removed record ||= @record # TODO: Remove when relying on @record is removed subform_controller = controller.class.active_scaffold_controller_for(record.class) if scope form_columns = @main_columns.try(:names) if scope.nil? || subform_controller == controller.class form_columns ||= current_form_columns(record, scope, subform_controller) if force || (form_columns && column.update_columns && (column.update_columns & form_columns).present?) url_params = params_for(:action => 'render_field', :column => column.name, :id => record.to_param) url_params = url_params.except(:parent_scaffold, :association, nested.param_name) if nested? && scope url_params[:eid] = params[:eid] if params[:eid] if scope url_params[:parent_controller] ||= url_params[:controller].gsub(/^\//, '') url_params[:controller] = subform_controller.controller_path url_params[:scope] = scope url_params[:parent_id] = params[:parent_id] || params[:id] end [:class] = "#{[:class]} update_form".strip ['data-update_url'] = url_for(url_params.merge(:_added => nil, :_removed => nil)) ['data-update_send_form'] = column.send_form_on_update_column ['data-update_send_form_selector'] = column.[:send_form_selector] if column.[:send_form_selector] end end |