Module: ActiveScaffold::Helpers::FormColumnHelpers
- Included in:
- ViewHelpers
- Defined in:
- lib/active_scaffold/helpers/form_column_helpers.rb,
lib/active_scaffold/bridges/dragonfly/lib/form_ui.rb,
lib/active_scaffold/bridges/paperclip/lib/form_ui.rb,
lib/active_scaffold/bridges/carrierwave/lib/form_ui.rb,
lib/active_scaffold/bridges/file_column/lib/form_ui.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_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_enum(column, html_options) ⇒ Object
- #active_scaffold_input_file_column(column, options) ⇒ Object
-
#active_scaffold_input_for(column, scope = nil, options = {}) ⇒ Object
(also: #form_column)
This method decides which input to use for the given column.
-
#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_select(column, html_options) ⇒ Object
-
#active_scaffold_input_singular_association(column, html_options) ⇒ Object
Form input methods.
-
#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_virtual(column, options) ⇒ Object
- #active_scaffold_render_input(column, 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) ⇒ Object
- #form_partial_for_column(column, renders_as = nil) ⇒ Object
- #onsubmit ⇒ Object
- #override_form_field(column) ⇒ Object (also: #override_form_field?)
-
#override_form_field_name(column, class_prefix = false) ⇒ Object
the naming convention for overriding form fields with helpers.
-
#override_form_field_partial(column) ⇒ Object
the naming convention for overriding form fields with partials.
- #override_form_field_partial?(column) ⇒ Boolean
-
#override_input(form_ui) ⇒ Object
the naming convention for overriding form input types with helpers.
- #override_input?(form_ui) ⇒ Boolean
- #override_subform_partial(column, subform_partial) ⇒ Object
-
#override_subform_partial?(column, subform_partial) ⇒ Boolean
add functionality for overriding subform partials from association class path.
- #subform_partial_for_column(column) ⇒ Object
- #update_columns_options(column, scope, options) ⇒ Object
Instance Method Details
#active_scaffold_add_existing_input(options) ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 299 def active_scaffold_add_existing_input() if ActiveScaffold.js_framework == :prototype && controller.respond_to?(:record_select_config) 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)) #unless column.through_association? ||= (active_scaffold_config.model.all.collect {|c| [h(c.to_label), c.id]}) select_tag 'associated_id', ('<option value="">' + as_(:_select_) + '</option>' + ).html_safe unless .empty? end end |
#active_scaffold_add_existing_label ⇒ Object
312 313 314 315 316 317 318 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 312 def active_scaffold_add_existing_label if controller.respond_to?(:record_select_config) 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
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 124 def active_scaffold_checkbox_list(column, , associated_ids, ) html = "<ul class=\"checkbox-list\" id=\"#{[:id]}\">" .each_with_index do |option, i| label, id = option this_id = "#{[:id]}_#{i}_id" html << content_tag(:li) do check_box_tag("#{[:name]}[]", id, associated_ids.include?(id), :id => this_id) << content_tag(:label, h(label), :for => this_id) end end html << '</ul>' html << javascript_tag("new DraggableLists('#{[:id]}')") if column.[:draggable_lists] html.html_safe end |
#active_scaffold_input_boolean(column, options) ⇒ Object
Column.type-based inputs
195 196 197 198 199 200 201 202 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 195 def active_scaffold_input_boolean(column, ) = [] << [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 |
# File 'lib/active_scaffold/bridges/carrierwave/lib/form_ui.rb', line 4 def active_scaffold_input_carrierwave(column, ) = () carrierwave = @record.send("#{column.name}") if !carrierwave.file.blank? = { :name => [:name].gsub(/\[#{column.name}\]$/, "[remove_#{column.name}]"), :id => 'remove_' + [:id], :value => false } = { :name => [:name].gsub(/\[#{column.name}\]$/, "[#{column.name}_cache]"), :id => [:id] + '_cache' } if ActiveScaffold.js_framework == :jquery js_remove_file_code = "$(this).prev('input#remove_#{[:id]}').val('true'); $(this).parent().hide().next().show(); return false;"; js_dont_remove_file_code = "$(this).parents('div.carrierwave_controls').find('input#remove_#{[:id]}').val('false'); return false;"; else js_remove_file_code = "$(this).previous('input#remove_#{[:id]}').value='true'; $(this).up().hide().next().show(); 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
173 174 175 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 173 def active_scaffold_input_checkbox(column, ) check_box(:record, column.name, ) 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 |
# File 'lib/active_scaffold/bridges/dragonfly/lib/form_ui.rb', line 4 def active_scaffold_input_dragonfly(column, ) = () input = file_field(:record, column.name, ) dragonfly = @record.send("#{column.name}") if dragonfly.present? if ActiveScaffold.js_framework == :jquery js_remove_file_code = "$(this).prev().val('true'); $(this).parent().hide().next().show(); return false;"; else js_remove_file_code = "$(this).previous().value='true'; $(this).up().hide().next().show(); return false;"; end content = active_scaffold_column_dragonfly(column, @record) 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_enum(column, html_options) ⇒ Object
145 146 147 148 149 150 151 152 153 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 145 def active_scaffold_input_enum(column, ) = { :selected => @record.send(column.name) } = column.[:options].collect do |text, value| active_scaffold_translated_option(column, text, value) end .update(column.[:html_options] || {}) .update(column.) 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 32 33 34 |
# File 'lib/active_scaffold/bridges/file_column/lib/form_ui.rb', line 5 def active_scaffold_input_file_column(column, ) if @record.send(column.name) # we already have a value? display the form for deletion. if ActiveScaffold.js_framework == :jquery js_remove_file_code = "$(this).prev().val('true'); $(this).parent().hide().next().show(); return false;"; else js_remove_file_code = "$(this).previous().value='true'; p=$(this).up(); p.hide(); p.next().show(); return false;"; end content_tag( :div, content_tag( :div, get_column_value(@record, column) + " " + hidden_field(:record, "delete_#{column.name}", :value => "false") + " | " + content_tag(:a, as_(:remove_file), {:href => '#', :onclick => js_remove_file_code}), {} ) + content_tag( :div, file_column_field("record", column.name, ), :style => "display: none" ), {} ) else # no, just display the file_column_field file_column_field("record", column.name, ) end end |
#active_scaffold_input_for(column, scope = nil, options = {}) ⇒ Object Also known as: form_column
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, = {}) = (column, scope, ) = (column, scope, ) active_scaffold_render_input(column, ) end |
#active_scaffold_input_options(column, scope = nil, options = {}) ⇒ Object
the standard active scaffold options used for class, name and scope
70 71 72 73 74 75 76 77 78 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 70 def (column, scope = nil, = {}) name = scope ? "record#{scope}[#{column.name}]" : "record[#{column.name}]" # Fix for keeping unique IDs in subform id_control = "record_#{column.name}_#{[params[:eid], params[:id]].compact.join '_'}" id_control += scope.gsub(/(\[|\])/, '_').gsub('__', '_').gsub(/_$/, '') if scope { :name => name, :class => "#{column.name}-input", :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 |
# File 'lib/active_scaffold/bridges/paperclip/lib/form_ui.rb', line 4 def active_scaffold_input_paperclip(column, ) = () input = file_field(:record, column.name, ) paperclip = @record.send("#{column.name}") if paperclip.file? if ActiveScaffold.js_framework == :jquery js_remove_file_code = "$(this).prev().val('true'); $(this).parent().hide().next().show(); return false;"; else js_remove_file_code = "$(this).previous().value='true'; $(this).up().hide().next().show(); return false;"; end content = active_scaffold_column_paperclip(column, @record) content_tag(:div, content + " | " + hidden_field(:record, "delete_#{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_password(column, options) ⇒ Object
177 178 179 180 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 177 def active_scaffold_input_password(column, ) = () password_field :record, column.name, .merge(column.) end |
#active_scaffold_input_plural_association(column, options) ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 116 def active_scaffold_input_plural_association(column, ) = @record.send(column.association.name).collect {|r| [r.to_label, r.id]} = | (column.association) return content_tag(:span, as_(:no_options), :id => [:id]) if .empty? active_scaffold_checkbox_list(column, , .collect {|a| a[1]}, ) end |
#active_scaffold_input_radio(column, html_options) ⇒ Object
165 166 167 168 169 170 171 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 165 def active_scaffold_input_radio(column, ) .update(column.[:html_options] || {}) column.[:options].inject('') do |html, (text, value)| text, value = active_scaffold_translated_option(column, text, value) html << content_tag(:label, (:record, column.name, value, .merge(:id => [:id] + '-' + value.to_s)) + text) end.html_safe end |
#active_scaffold_input_select(column, html_options) ⇒ Object
155 156 157 158 159 160 161 162 163 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 155 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
Form input methods
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 99 def active_scaffold_input_singular_association(column, ) associated = @record.send(column.association.name) = (column.association) .unshift([ associated.to_label, associated.id ]) unless associated.nil? or .find {|label, id| id == associated.id} selected = associated.nil? ? nil : associated.id method = column.name #html_options[:name] += '[id]' = {:selected => selected, :include_blank => as_(:_select_)} .update(column.[:html_options] || {}) .update(column.) [:name] = "#{[:name]}[]" if ([:multiple] == true && ![:name].to_s.ends_with?("[]")) select(:record, method, .uniq, , ) end |
#active_scaffold_input_text_options(options = {}) ⇒ Object
the standard active scaffold options used for textual inputs
63 64 65 66 67 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 63 def ( = {}) [:autocomplete] = 'off' [:class] = "#{[:class]} text-input".strip end |
#active_scaffold_input_textarea(column, options) ⇒ Object
182 183 184 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 182 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_virtual(column, options) ⇒ Object
186 187 188 189 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 186 def active_scaffold_input_virtual(column, ) = () text_field :record, column.name, .merge(column.) end |
#active_scaffold_render_input(column, options) ⇒ Object
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 15 def active_scaffold_render_input(column, ) begin # first, check if the dev has created an override for this specific field if override_form_field?(column) send(override_form_field(column), @record, ) # second, check if the dev has specified a valid form_ui for this column elsif column.form_ui and override_input?(column.form_ui) send(override_input(column.form_ui), 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 override_input?(column.column.type) send(override_input(column.column.type), 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] ||= ActionView::Helpers::InstanceTag::DEFAULT_FIELD_OPTIONS["size"] end [:include_blank] = true if column.column.null and [: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 Exception => e logger.error Time.now.to_s + "#{e.inspect} -- on the ActiveScaffold column = :#{column.name} in #{controller.class}" raise e end end |
#active_scaffold_translated_option(column, text, value = nil) ⇒ Object
140 141 142 143 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 140 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
279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 279 def column_renders_as(column) if column.is_a? ActiveScaffold::DataStructures::ActionColumns return :subsection elsif column.active_record_class.locking_column.to_s == column.name.to_s or column.form_ui == :hidden return :hidden elsif column.association.nil? or column.form_ui or !active_scaffold_config_for(column.association.klass).actions.include?(:subform) return :field else return :subform end end |
#column_scope(column) ⇒ Object
291 292 293 294 295 296 297 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 291 def column_scope(column) if column.plural_association? "[#{column.name}][#{@record.id || generate_temporary_id}]" else "[#{column.name}]" end end |
#form_partial_for_column(column, renders_as = nil) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 253 def form_partial_for_column(column, renders_as = nil) renders_as ||= column_renders_as(column) if override_form_field_partial?(column) override_form_field_partial(column) elsif renders_as == :field or override_form_field?(column) "form_attribute" elsif renders_as == :subform "form_association" elsif renders_as == :hidden "form_hidden_attribute" end end |
#onsubmit ⇒ Object
204 205 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 204 def onsubmit end |
#override_form_field(column) ⇒ Object Also known as: override_form_field?
231 232 233 234 235 236 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 231 def override_form_field(column) method_with_class = override_form_field_name(column, true) return method_with_class if respond_to?(method_with_class) method = override_form_field_name(column) method if respond_to?(method) end |
#override_form_field_name(column, class_prefix = false) ⇒ Object
the naming convention for overriding form fields with helpers
240 241 242 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 240 def override_form_field_name(column, class_prefix = false) "#{clean_class_name(column.active_record_class.name) + '_' if class_prefix}#{clean_column_name(column.name)}_form_column" end |
#override_form_field_partial(column) ⇒ Object
the naming convention for overriding form fields with partials
227 228 229 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 227 def override_form_field_partial(column) "#{column.name}_form_column" end |
#override_form_field_partial?(column) ⇒ Boolean
221 222 223 224 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 221 def override_form_field_partial?(column) path, partial_name = partial_pieces(override_form_field_partial(column)) template_exists?(partial_name, path, true) end |
#override_input(form_ui) ⇒ Object
the naming convention for overriding form input types with helpers
249 250 251 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 249 def override_input(form_ui) "active_scaffold_input_#{form_ui}" end |
#override_input?(form_ui) ⇒ Boolean
244 245 246 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 244 def override_input?(form_ui) respond_to?(override_input(form_ui)) end |
#override_subform_partial(column, subform_partial) ⇒ Object
217 218 219 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 217 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
212 213 214 215 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 212 def override_subform_partial?(column, subform_partial) path, partial_name = partial_pieces(override_subform_partial(column, subform_partial)) template_exists?(partial_name, path, true) end |
#subform_partial_for_column(column) ⇒ Object
266 267 268 269 270 271 272 273 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 266 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) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/active_scaffold/helpers/form_column_helpers.rb', line 80 def (column, scope, ) if column.update_columns form_action = params[:action] == 'edit' ? :update : :create url_params = {:action => 'render_field', :id => params[:id], :column => column.name} url_params[:eid] = params[:eid] if params[:eid] url_params[:controller] = controller.class.active_scaffold_controller_for(@record.class).controller_path if scope url_params[:scope] = scope if scope [:class] = "#{[:class]} update_form".strip ['data-update_url'] = url_for(url_params) ['data-update_send_form'] = true if column.send_form_on_update_column end end |