Module: ActiveScaffold::Helpers::ListColumnHelpers
- Included in:
- ViewHelpers
- Defined in:
- lib/active_scaffold/helpers/list_column_helpers.rb,
lib/active_scaffold/bridges/dragonfly/list_ui.rb,
lib/active_scaffold/bridges/carrierwave/list_ui.rb
Overview
Helpers that assist with the rendering of a List Column
Instance Method Summary collapse
-
#action_link_to_inline_form(column, record, associated, text) ⇒ Object
setup the action link to inline form.
- #active_scaffold_column_carrierwave(column, record) ⇒ Object
- #active_scaffold_column_checkbox(column, record) ⇒ Object
- #active_scaffold_column_dragonfly(column, record) ⇒ Object
-
#active_scaffold_column_text(column, record) ⇒ Object
Overrides.
- #active_scaffold_inplace_edit(record, column, options = {}) ⇒ Object
-
#clean_column_value(v) ⇒ Object
There are two basic ways to clean a column’s value: h() and sanitize().
- #column_heading_value(column, sorting, sort_direction) ⇒ Object
- #column_link_authorized?(link, column, record, associated) ⇒ Boolean
- #column_override(column) ⇒ Object (also: #column_override?)
- #column_override_name(column, class_prefix = false) ⇒ Object
- #configure_column_link(link, associated, actions) ⇒ Object
- #format_association_value(value, column, size) ⇒ Object
-
#format_column_value(record, column, value = nil) ⇒ Object
Formatting.
- #format_number_value(value, options = {}) ⇒ Object
- #format_value(column_value, options = {}) ⇒ Object
- #get_column_value(record, column) ⇒ Object
-
#inplace_edit?(record, column) ⇒ Boolean
= Inline Edit = ==========.
- #inplace_edit_cloning?(column) ⇒ Boolean
- #inplace_edit_control(column) ⇒ Object
- #inplace_edit_control_css_class ⇒ Object
- #inplace_edit_tag_attributes(column) ⇒ Object
- #mark_column_heading ⇒ Object
-
#override_column_ui(list_ui) ⇒ Object
the naming convention for overriding column types with helpers.
- #override_column_ui?(list_ui) ⇒ Boolean
- #render_column_heading(column, sorting, sort_direction) ⇒ Object
-
#render_list_column(text, column, record) ⇒ Object
TODO: move empty_field_text and logic in here? TODO: we need to distinguish between the automatic links we create and the ones that the dev specified.
Instance Method Details
#action_link_to_inline_form(column, record, associated, text) ⇒ Object
setup the action link to inline form
60 61 62 63 64 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 60 def action_link_to_inline_form(column, record, associated, text) link = column.link.clone link.label = text configure_column_link(link, associated, column.actions_for_association_links) end |
#active_scaffold_column_carrierwave(column, record) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/active_scaffold/bridges/carrierwave/list_ui.rb', line 4 def active_scaffold_column_carrierwave(column, record) carrierwave = record.send("#{column.name}") return nil unless !carrierwave.file.blank? thumbnail_style = ActiveScaffold::Bridges::Carrierwave::CarrierwaveBridgeHelpers.thumbnail_style content = if carrierwave.versions.keys.include?(thumbnail_style) image_tag(carrierwave.url(thumbnail_style), :border => 0).html_safe else record.send(record.send(:_mounter, column.name).send(:serialization_column)) end link_to(content, carrierwave.url, :target => '_blank') end |
#active_scaffold_column_checkbox(column, record) ⇒ Object
121 122 123 124 125 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 121 def active_scaffold_column_checkbox(column, record) = {:disabled => true, :id => nil, :object => record} .delete(:disabled) if inplace_edit?(record, column) check_box(:record, column.name, ) end |
#active_scaffold_column_dragonfly(column, record) ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/active_scaffold/bridges/dragonfly/list_ui.rb', line 4 def active_scaffold_column_dragonfly(column, record) = record.send("#{column.name}") return nil unless .present? content = if .image? image_tag(.thumb(column.[:thumb] || ActiveScaffold::Bridges::Dragonfly::DragonflyBridgeHelpers.thumbnail_style).url, :border => 0) else .name end link_to(content, .remote_url, {'data-popup' => true, :target => '_blank'}) end |
#active_scaffold_column_text(column, record) ⇒ Object
Overrides
117 118 119 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 117 def active_scaffold_column_text(column, record) clean_column_value(truncate(record.send(column.name), :length => column.[:truncate] || 50)) end |
#active_scaffold_inplace_edit(record, column, options = {}) ⇒ Object
238 239 240 241 242 243 244 245 246 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 238 def active_scaffold_inplace_edit(record, column, = {}) formatted_column = [:formatted_column] || format_column_value(record, column) klass = (column.name == :marked ? 'mark_record_field' : 'in_place_editor_field') = {:id => record.id.to_s, :action => 'update_column', :name => column.name.to_s} = {:id => element_cell_id(), :class => klass, :title => as_(:click_to_edit), 'data-ie_id' => record.id.to_s} content_tag(:span, formatted_column, ) end |
#clean_column_value(v) ⇒ Object
There are two basic ways to clean a column’s value: h() and sanitize(). The latter is useful when the column contains valid html data, and you want to just disable any scripting. People can always use field overrides to clean data one way or the other, but having this override lets people decide which way it should happen by default.
Why is it not a configuration option? Because it seems like a somewhat rare request. But it could eventually be an option in config.list (and config.show, I guess).
110 111 112 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 110 def clean_column_value(v) h(v) end |
#column_heading_value(column, sorting, sort_direction) ⇒ Object
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 304 def column_heading_value(column, sorting, sort_direction) if column.sortable? = {:id => nil, :class => "as_sort", 'data-page-history' => controller_id, :remote => true, :method => :get} = params_for(:action => :index, :page => 1, :sort => column.name, :sort_direction => sort_direction) link_to column.label, , else if column.name == :marked mark_column_heading else content_tag(:p, column.label) end end end |
#column_link_authorized?(link, column, record, associated) ⇒ Boolean
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 86 def (link, column, record, associated) if column.association = if associated.nil? || (associated.respond_to?(:blank?) && associated.blank?) column.association.associated_class elsif column.plural_association? associated.first else associated end = .(:crud_type => link.crud_type) = and record.(:crud_type => :update, :column => column.name) if link.crud_type == :create else record.(:crud_type => link.crud_type) end end |
#column_override(column) ⇒ Object Also known as: column_override?
131 132 133 134 135 136 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 131 def column_override(column) method_with_class = column_override_name(column, true) return method_with_class if respond_to?(method_with_class) method = column_override_name(column) method if respond_to?(method) end |
#column_override_name(column, class_prefix = false) ⇒ Object
127 128 129 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 127 def column_override_name(column, class_prefix = false) "#{clean_class_name(column.active_record_class.name) + '_' if class_prefix}#{clean_column_name(column.name)}_column" end |
#configure_column_link(link, associated, actions) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 66 def configure_column_link(link, associated, actions) if column_empty?(associated) # if association is empty, we only can link to create form if actions.include?(:new) link.action = 'new' link.crud_type = :create link.label = as_(:create_new) end elsif actions.include?(:edit) link.action = 'edit' link.crud_type = :update elsif actions.include?(:show) link.action = 'show' link.crud_type = :read elsif actions.include?(:list) link.action = 'index' link.crud_type = :read end link if link.action.present? end |
#format_association_value(value, column, size) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 187 def format_association_value(value, column, size) format_value case when column.singular_association? value.to_label when column.plural_association? if column.associated_limit.nil? firsts = value.collect { |v| v.to_label } else firsts = value.first(column.associated_limit) firsts.collect! { |v| v.to_label } firsts[column.associated_limit] = '…' if value.size > column.associated_limit end if column.associated_limit == 0 size if column.associated_number? else joined_associated = firsts.join(active_scaffold_config.list.association_join_text) joined_associated << " (#{size})" if column.associated_number? and column.associated_limit and value.size > column.associated_limit joined_associated end end end |
#format_column_value(record, column, value = nil) ⇒ Object
Formatting
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 151 def format_column_value(record, column, value = nil) value ||= record.send(column.name) unless record.nil? if value && column.association # cache association size before calling column_empty? associated_size = value.size if column.plural_association? and column.associated_number? # get count before cache association end if column.association.nil? or column_empty?(value) if column.form_ui == :select && column.[:options] text, val = column.[:options].find {|text, val| (val.nil? ? text : val).to_s == value.to_s} value = active_scaffold_translated_option(column, text, val).first if text end if value.is_a? Numeric format_number_value(value, column.) else format_value(value, column.) end else format_association_value(value, column, associated_size) end end |
#format_number_value(value, options = {}) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 171 def format_number_value(value, = {}) value = case [:format] when :size number_to_human_size(value, [:i18n_options] || {}) when :percentage number_to_percentage(value, [:i18n_options] || {}) when :currency number_to_currency(value, [:i18n_options] || {}) when :i18n_number number_with_delimiter(value, [:i18n_options] || {}) else value end clean_column_value(value) end |
#format_value(column_value, options = {}) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 209 def format_value(column_value, = {}) value = if column_empty?(column_value) active_scaffold_config.list.empty_field_text elsif column_value.is_a?(Time) || column_value.is_a?(Date) l(column_value, :format => [:format] || :default) elsif [FalseClass, TrueClass].include?(column_value.class) as_(column_value.to_s.to_sym) else column_value.to_s end clean_column_value(value) end |
#get_column_value(record, column) ⇒ Object
6 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/active_scaffold/helpers/list_column_helpers.rb', line 6 def get_column_value(record, column) begin # check for an override helper value = if column_override? column # we only pass the record as the argument. we previously also passed the formatted_value, # but mike perham pointed out that prohibited the usage of overrides to improve on the # performance of our default formatting. see issue #138. send(column_override(column), record) # second, check if the dev has specified a valid list_ui for this column elsif column.list_ui and override_column_ui?(column.list_ui) send(override_column_ui(column.list_ui), column, record) elsif column.column and override_column_ui?(column.column[:type]) send(override_column_ui(column.column[:type]), column, record) else format_column_value(record, column) end value = ' '.html_safe if value.nil? or (value.respond_to?(:empty?) and value.empty?) # fix for IE 6 return value rescue Exception => e logger.error Time.now.to_s + "#{e.inspect} -- on the ActiveScaffold column = :#{column.name} in #{controller.class}" raise e end end |
#inplace_edit?(record, column) ⇒ Boolean
Inline Edit =
226 227 228 229 230 231 232 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 226 def inplace_edit?(record, column) if column.inplace_edit editable = controller.send(:update_authorized?, record) if controller.respond_to?(:update_authorized?) editable = record.(:crud_type => :update, :column => column.name) if editable.nil? || editable == true editable end end |
#inplace_edit_cloning?(column) ⇒ Boolean
234 235 236 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 234 def inplace_edit_cloning?(column) column.inplace_edit != :ajax and (override_form_field?(column) or column.form_ui or (column.column and override_input?(column.column[:type]))) end |
#inplace_edit_control(column) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 248 def inplace_edit_control(column) if inplace_edit?(active_scaffold_config.model, column) and inplace_edit_cloning?(column) old_record, @record = @record, new_model column = column.clone column. = column..clone column.form_ui = :select if (column.association && column.form_ui.nil?) content_tag(:div, active_scaffold_input_for(column), :style => "display:none;", :class => inplace_edit_control_css_class).tap do @record = old_record end end end |
#inplace_edit_control_css_class ⇒ Object
260 261 262 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 260 def inplace_edit_control_css_class "as_inplace_pattern" end |
#inplace_edit_tag_attributes(column) ⇒ Object
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 264 def inplace_edit_tag_attributes(column) = {} if column.name == :marked ['data-ie_url'] = '#' else ['data-ie_url'] = url_for({:controller => params_for[:controller], :action => 'update_column', :column => column.name, :id => '__id__'}) end ['data-ie_cancel_text'] = column.[:cancel_text] || as_(:cancel) ['data-ie_loading_text'] = column.[:loading_text] || as_(:loading) ['data-ie_save_text'] = column.[:save_text] || as_(:update) ['data-ie_saving_text'] = column.[:saving_text] || as_(:saving) ['data-ie_rows'] = column.[:rows] || 5 if column.column.try(:type) == :text ['data-ie_cols'] = column.[:cols] if column.[:cols] ['data-ie_size'] = column.[:size] if column.[:size] if column.list_ui == :checkbox ['data-ie_mode'] = :inline_checkbox elsif inplace_edit_cloning?(column) ['data-ie_mode'] = :clone elsif column.inplace_edit == :ajax url = url_for(:controller => params_for[:controller], :action => 'render_field', :id => '__id__', :column => column.name, :update_column => column.name, :in_place_editing => true) plural = column.plural_association? && !override_form_field?(column) && [:select, :record_select].include?(column.form_ui) ['data-ie_render_url'] = url ['data-ie_mode'] = :ajax ['data-ie_plural'] = plural end end |
#mark_column_heading ⇒ Object
293 294 295 296 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 293 def mark_column_heading = {:id => "#{controller_id}_mark_heading", :class => 'mark_heading', 'data-ie_url' => '#', 'data-tbody_id' => active_scaffold_tbody_id} content_tag(:span, check_box_tag("#{controller_id}_mark_heading_span_input", true, false), ) end |
#override_column_ui(list_ui) ⇒ Object
the naming convention for overriding column types with helpers
144 145 146 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 144 def override_column_ui(list_ui) "active_scaffold_column_#{list_ui}" end |
#override_column_ui?(list_ui) ⇒ Boolean
139 140 141 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 139 def override_column_ui?(list_ui) respond_to?(override_column_ui(list_ui)) end |
#render_column_heading(column, sorting, sort_direction) ⇒ Object
298 299 300 301 302 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 298 def render_column_heading(column, sorting, sort_direction) = {:id => active_scaffold_column_header_id(column), :class => column_heading_class(column, sorting), :title => column.description} .merge!(inplace_edit_tag_attributes(column)) if column.inplace_edit content_tag(:th, column_heading_value(column, sorting, sort_direction) + inplace_edit_control(column), ) end |
#render_list_column(text, column, record) ⇒ Object
TODO: move empty_field_text and logic in here? TODO: we need to distinguish between the automatic links we create and the ones that the dev specified. some logic may not apply if the dev specified the link.
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 |
# File 'lib/active_scaffold/helpers/list_column_helpers.rb', line 33 def render_list_column(text, column, record) if column.link link = column.link associated = column.association ? record.send(column.association[:name]) : nil = params_for(:action => nil, :id => record.id) # setup automatic link if column.autolink? && column.singular_association? # link to inline form link = action_link_to_inline_form(column, record, associated, text) return text if link.nil? else [:link] = text end if (link, column, record, associated) render_action_link(link, , record) else "<a class='disabled'>#{text}</a>".html_safe end elsif inplace_edit?(record, column) active_scaffold_inplace_edit(record, column, {:formatted_column => text}) else text end end |