Module: ActiveScaffold::Helpers::ViewHelpers

Overview

All extra helpers that should be included in the View. Also a dumping ground for uncategorized helpers.

Instance Method Summary collapse

Methods included from HumanConditionHelpers

#active_scaffold_human_condition_for, #override_human_condition, #override_human_condition?, #override_human_condition_column, #override_human_condition_column?

Methods included from SearchColumnHelpers

#active_scaffold_search_boolean, #active_scaffold_search_date, #active_scaffold_search_datetime, #active_scaffold_search_for, #active_scaffold_search_multi_select, #active_scaffold_search_null, #active_scaffold_search_options, #active_scaffold_search_range, #active_scaffold_search_range_comparator_options, #active_scaffold_search_range_string?, #active_scaffold_search_select, #active_scaffold_search_text, #active_scaffold_search_time, #field_search_datetime_value, #field_search_params_range_values, #include_null_comparators?, #override_search, #override_search?, #override_search_field, #override_search_field_name, #searched_by?, #visibles_and_hiddens

Methods included from FormColumnHelpers

#active_scaffold_add_existing_input, #active_scaffold_add_existing_label, #active_scaffold_checkbox_list, #active_scaffold_input_boolean, #active_scaffold_input_carrierwave, #active_scaffold_input_checkbox, #active_scaffold_input_dragonfly, #active_scaffold_input_enum, #active_scaffold_input_for, #active_scaffold_input_options, #active_scaffold_input_password, #active_scaffold_input_plural_association, #active_scaffold_input_radio, #active_scaffold_input_select, #active_scaffold_input_singular_association, #active_scaffold_input_text_options, #active_scaffold_input_textarea, #active_scaffold_input_virtual, #active_scaffold_render_input, #active_scaffold_translated_option, #column_renders_as, #column_scope, #form_partial_for_column, #onsubmit, #override_form_field, #override_form_field_name, #override_form_field_partial, #override_form_field_partial?, #override_input, #override_input?, #override_subform_partial, #override_subform_partial?, #subform_partial_for_column, #update_columns_options

Methods included from ShowColumnHelpers

#active_scaffold_show_text, #override_show_column_ui, #override_show_column_ui?, #show_column_override, #show_column_override_name, #show_column_value

Methods included from ListColumnHelpers

#action_link_to_inline_form, #active_scaffold_column_carrierwave, #active_scaffold_column_checkbox, #active_scaffold_column_dragonfly, #active_scaffold_column_text, #active_scaffold_inplace_edit, #clean_column_value, #column_heading_value, #column_link_authorized?, #column_override, #column_override_name, #configure_column_link, #format_association_value, #format_column_value, #format_number_value, #format_value, #get_column_value, #inplace_edit?, #inplace_edit_cloning?, #inplace_edit_control, #inplace_edit_control_css_class, #inplace_edit_tag_attributes, #mark_column_heading, #override_column_ui, #override_column_ui?, #render_column_heading, #render_list_column

Methods included from PaginationHelpers

#pagination_ajax_link, #pagination_ajax_links

Methods included from AssociationHelpers

#association_options_count, #association_options_find, #options_for_association, #options_for_association_conditions, #options_for_association_count

Methods included from IdHelpers

#action_iframe_id, #action_link_id, #active_scaffold_calculations_id, #active_scaffold_column_header_id, #active_scaffold_content_id, #active_scaffold_id, #active_scaffold_messages_id, #active_scaffold_tbody_id, #association_subform_id, #before_header_id, #controller_id, #element_cell_id, #element_form_id, #element_messages_id, #element_row_id, #empty_message_id, #id_from_controller, #loading_indicator_id, #scope_id, #search_input_id, #sub_form_id, #sub_form_list_id, #sub_section_id

Instance Method Details



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 172

def action_link_html(link, url, html_options, record)
  # issue 260, use url_options[:link] if it exists. This prevents DB data from being localized.
  label = url.delete(:link) if url.is_a?(Hash) 
  label ||= link.label
  if link.image.nil?
    html = link_to(label, url, html_options)
  else
    html = link_to(image_tag(link.image[:name] , :size => link.image[:size], :alt => label), url, html_options)
  end
  # if url is nil we would like to generate an anchor without href attribute
  url.nil? ? html.sub(/href=".*?"/, '').html_safe : html.html_safe
end


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
153
154
155
156
157
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 128

def action_link_html_options(link, url_options, record, html_options)
  link_id = get_action_link_id(url_options, record, link.column)
  html_options.reverse_merge! link.html_options.merge(:class => link.action.to_s)

  # Needs to be in html_options to as the adding _method to the url is no longer supported by Rails        
  html_options[:method] = link.method if link.method != :get

  html_options[:class] += ' as_action' if link.inline?
  html_options[:data] = {}
  html_options[:data][:confirm] = link.confirm(record.try(:to_label)) if link.confirm?
  html_options[:data][:position] = link.position if link.position and link.inline?
  html_options[:data][:action] = link.action if link.inline?
  if link.popup?
    html_options[:data][:popup] = true
    html_options[:target] = '_blank'
  end
  html_options[:id] = link_id
  html_options[:remote] = true unless link.page? || link.popup?
  if link.dhtml_confirm?
    html_options[:class] += ' as_action' if !link.inline?
    html_options[:page_link] = 'true' if !link.inline?
    html_options[:dhtml_confirm] = link.dhtml_confirm.value
    html_options[:onclick] = link.dhtml_confirm.onclick_function(controller, link_id)
  end
  html_options[:class] += " #{link.html_options[:class]}" unless link.html_options[:class].blank?
  if html_options[:class].split(' ').include?('marked_records_action')
    html_options['data-tbody_id'] = active_scaffold_tbody_id
  end
  html_options
end


113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 113

def action_link_url_options(link, url_options, record, options = {})
  url_options = url_options.clone
  url_options[:action] = link.action
  url_options[:controller] = link.controller.to_s if link.controller
  url_options.delete(:search) if link.controller and link.controller.to_s != params[:controller]
  url_options.merge! link.parameters if link.parameters
  @link_record = record
  url_options.merge! self.instance_eval(&(link.dynamic_parameters)) if link.dynamic_parameters.is_a?(Proc)
  @link_record = nil
  url_options_for_nested_link(link.column, record, link, url_options, options) if link.column
  url_options_for_sti_link(link.column, record, link, url_options, options) unless record.nil? || active_scaffold_config.sti_children.nil?
  url_options[:_method] = link.method if !link.confirm? && link.inline? && link.method != :get
  url_options
end

#active_scaffold_configObject

access to the configuration variable



20
21
22
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 20

def active_scaffold_config
  controller.class.active_scaffold_config
end

#active_scaffold_config_for(*args) ⇒ Object



24
25
26
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 24

def active_scaffold_config_for(*args)
  controller.class.active_scaffold_config_for(*args)
end

#active_scaffold_controller_for(*args) ⇒ Object



28
29
30
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 28

def active_scaffold_controller_for(*args)
  controller.class.active_scaffold_controller_for(*args)
end

#active_scaffold_error_messages_for(*params) ⇒ Object



282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 282

def active_scaffold_error_messages_for(*params)
  options = params.extract_options!.symbolize_keys
  options.reverse_merge!(:container_tag => :div, :list_type => :ul)

  objects = Array.wrap(options.delete(:object) || params).map do |object|
    object = instance_variable_get("@#{object}") unless object.respond_to?(:to_model)
    object = convert_to_model(object)

    if object.class.respond_to?(:model_name)
      options[:object_name] ||= object.class.model_name.human.downcase
    end

    object
  end

  objects.compact!
  count = objects.inject(0) {|sum, object| sum + object.errors.count }

  unless count.zero?
    html = {}
    [:id, :class].each do |key|
      if options.include?(key)
        value = options[key]
        html[key] = value unless value.blank?
      else
        html[key] = 'errorExplanation'
      end
    end
    options[:object_name] ||= params.first

    header_message = if options.include?(:header_message)
      options[:header_message]
    else
      as_('errors.template.header', :count => count, :model => options[:object_name].to_s.gsub('_', ' '))
    end

    message = options.include?(:message) ? options[:message] : as_('errors.template.body')

    error_messages = objects.sum do |object|
      object.errors.full_messages.map do |msg|
        options[:list_type] != :br ? (:li, msg) : msg
      end
    end
    error_messages = if options[:list_type] == :br
      error_messages.join('<br/>').html_safe
    else
      (options[:list_type], error_messages.join.html_safe)
    end

    contents = []
    contents << (options[:header_tag] || :h2, header_message) unless header_message.blank?
    contents << (:p, message) unless message.blank?
    contents << error_messages
    contents = contents.join.html_safe
    options[:container_tag] ? (options[:container_tag], contents, html) : contents
  else
    ''
  end
end

#as_main_div_classObject



231
232
233
234
235
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 231

def as_main_div_class
  classes = ["active-scaffold", "active-scaffold-#{controller_id}", "#{id_from_controller params[:controller]}-view", "#{active_scaffold_config.theme}-theme"]
  classes << "as_touch" if touch_device?
  classes.join(' ')
end

#clean_class_name(name) ⇒ Object



278
279
280
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 278

def clean_class_name(name)
  name.underscore.gsub('/', '_')
end

#clean_column_name(name) ⇒ Object



274
275
276
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 274

def clean_column_name(name)
  name.to_s.gsub('?', '')
end

#column_calculation(column) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 244

def column_calculation(column)
  if column.calculate.instance_of? Proc
    column.calculate.call(@records)
  else
    conditions = controller.send(:all_conditions)
    includes = active_scaffold_config.list.count_includes
    includes ||= controller.send(:active_scaffold_includes) unless conditions.nil?
    controller.send(:append_to_query, beginning_of_chain.dataset, :where => conditions, :includes => includes,
      :joins => controller.send(:joins_for_collection)).get(column.calculate.sql_function(column.name))
  end
end

#column_class(column, column_value, record) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 207

def column_class(column, column_value, record)
  classes = []
  classes << "#{column.name}-column"
  if column.css_class.is_a?(Proc)
    css_class = column.css_class.call(column_value, record)
    classes << css_class unless css_class.nil?
  else
    classes << column.css_class
  end unless column.css_class.nil?
   
  classes << 'empty' if column_empty? column_value
  classes << 'sorted' if active_scaffold_config.list.user.sorting.sorts_on?(column)
  classes << 'numeric' if column.number?
  classes.join(' ').rstrip
end

#column_empty?(column_value) ⇒ Boolean

Returns:

  • (Boolean)


237
238
239
240
241
242
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 237

def column_empty?(column_value)
  empty = column_value.nil?
  empty ||= column_value.blank? if column_value.respond_to? :blank?
  empty ||= ['&nbsp;', active_scaffold_config.list.empty_field_text].include? column_value if String === column_value
  return empty
end

#column_heading_class(column, sorting) ⇒ Object



223
224
225
226
227
228
229
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 223

def column_heading_class(column, sorting)
  classes = []
  classes << "#{column.name}-column_heading"
  classes << "sorted #{sorting.direction_of(column).downcase}" if sorting.sorts_on? column
  classes << column.css_class unless column.css_class.nil? || column.css_class.is_a?(Proc)
  classes.join(' ')
end

#column_show_add_existing(column) ⇒ Object



264
265
266
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 264

def column_show_add_existing(column)
  (column.allow_add_existing and options_for_association_count(column.association) > 0)
end

#column_show_add_new(column, associated, record) ⇒ Object



268
269
270
271
272
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 268

def column_show_add_new(column, associated, record)
  value = column.plural_association? || (column.singular_association? and associated.empty?)
  value = false unless column.association.associated_class.respond_to?(:authorized_for?) and column.association.associated_class.authorized_for?(:crud_type => :create)
  value
end

#controller_path_for_activerecord(klass) ⇒ Object

Uncategorized



36
37
38
39
40
41
42
43
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 36

def controller_path_for_activerecord(klass)
  begin
    controller = active_scaffold_controller_for(klass)
    controller.controller_path
  rescue ActiveScaffold::ControllerNotFound
    controller = nil
  end
end

#form_remote_upload_tag(url_for_options = {}, options = {}) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 71

def form_remote_upload_tag(url_for_options = {}, options = {})
  options[:target] = action_iframe_id(url_for_options)
  options[:multipart] ||= true
  options[:class] = "#{options[:class]} as_remote_upload".strip 
  output=""
  output << form_tag(url_for_options, options)
  (output << "<iframe id='#{action_iframe_id(url_for_options)}' name='#{action_iframe_id(url_for_options)}' style='display:none'></iframe>").html_safe
end

#generate_temporary_idObject



51
52
53
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 51

def generate_temporary_id
  (Time.now.to_f*1000).to_i.to_s
end


159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 159

def get_action_link_id(url_options, record = nil, column = nil)
  id = url_options[:id] || url_options[:parent_id]
  id = "#{column.association[:name]}-#{record.id}" if column && column.plural_association?
  if record.try(column.association[:name]).present?
    id = "#{column.association[:name]}-#{record.send(column.association[:name]).id}-#{record.id}"
  else
    id = "#{column.association[:name]}-#{record.id}" unless record.nil?
  end if column && column.singular_association?
  id = "#{id}-#{url_options[:batch_scope].downcase}" if url_options[:batch_scope]
  action_id = "#{id_from_controller(url_options[:controller]) + '-' if url_options[:parent_controller]}#{url_options[:action].to_s}"
  action_link_id(action_id, id)
end

#in_subform?(column, parent_record) ⇒ Boolean

Should this column be displayed in the subform?

Returns:

  • (Boolean)


66
67
68
69
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 66

def in_subform?(column, parent_record)
  # A column shouldn't be in the subform if it's the reverse association to the parent
  not (column.association and column.association.associated_class == parent_record.class)
end

Creates a javascript-based link that toggles the visibility of some element on the page. By default, it toggles the visibility of the sibling after the one it’s nested in. You may pass custom javascript logic in options to change that, though. For example, you could say :of => ‘$(“my_div_id”)’. You may also flag whether the other element is visible by default or not, and the initial text will adjust accordingly.



88
89
90
91
92
93
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 88

def link_to_visibility_toggle(id, options = {})
  options[:default_visible] = true if options[:default_visible].nil?
  options[:hide_label] = as_(:hide) 
  options[:show_label] = as_(:show)
  javascript_tag("ActiveScaffold.create_visibility_toggle('#{id}', #{options.to_json});")
end

#loading_indicator_tag(options) ⇒ Object

a general-use loading indicator (the “stuff is happening, please wait” feedback)



81
82
83
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 81

def loading_indicator_tag(options)
  image_tag "active_scaffold/indicator.gif", :style => "visibility:hidden;", :id => loading_indicator_id(options), :alt => "loading indicator", :class => "loading-indicator"
end

#option_tags_for(select_options, options = {}) ⇒ Object

Turns [[label, value]] into <option> tags Takes optional parameter of :include_blank



57
58
59
60
61
62
63
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 57

def option_tags_for(select_options, options = {})
  select_options.insert(0,[as_(:_select_),nil]) if options[:include_blank]
  select_options.collect do |option|
    label, value = option[0], option[1]
    value.nil? ? "<option value="">#{label}</option>" : "<option value=\"#{value}\">#{label}</option>"
  end
end


99
100
101
102
103
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 99

def render_action_link(link, url_options, record = nil, html_options = {})
  url_options = action_link_url_options(link, url_options, record)
  html_options = action_link_html_options(link, url_options, record, html_options)
  action_link_html(link, url_options, html_options, record)
end

#render_column_calculation(column) ⇒ Object



256
257
258
259
260
261
262
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 256

def render_column_calculation(column)
  calculation = column_calculation(column)
  override_formatter = "render_#{column.name}_#{column.calculate.is_a?(Proc) ? :calculate : column.calculate}"
  calculation = send(override_formatter, calculation) if respond_to? override_formatter

  "#{"#{as_(column.calculate)}: " unless column.calculate.is_a? Proc}#{format_column_value nil, column, calculation}"
end


105
106
107
108
109
110
111
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 105

def render_group_action_link(link, url_options, options, record = nil)
  if link.type == :member && !options[:authorized]
    action_link_html(link, nil, {:class => "disabled #{link.action}#{link.html_options[:class].blank? ? '' : (' ' + link.html_options[:class])}"}, record)
  else
    render_action_link(link, url_options, record)
  end
end


95
96
97
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 95

def skip_action_link(link, *args)
  (!link.ignore_method.nil? && controller.respond_to?(link.ignore_method) && controller.send(link.ignore_method, *args)) || ((link.security_method_set? or controller.respond_to? link.security_method) and !controller.send(link.security_method, *args))
end

#template_exists?(template_name, partial = false) ⇒ Boolean

This is the template finder logic, keep it updated with however we find stuff in rails currently this very similar to the logic in ActionBase::Base.render for options file

Returns:

  • (Boolean)


47
48
49
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 47

def template_exists?(template_name, partial = false)
  lookup_context.exists? template_name, '', partial
end


185
186
187
188
189
190
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 185

def url_options_for_nested_link(column, record, link, url_options, options = {})
  if column && column.association 
    url_options[column.association[:model].name.foreign_key.to_sym] = url_options.delete(:id)
    url_options[:id] = record.send(column.association[:name]).id if column.singular_association? && record.send(column.association[:name]).present?
  end
end


192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 192

def url_options_for_sti_link(column, record, link, url_options, options = {})
  #need to find out controller of current record type
  #and set parameters
  # its quite difficult to detect an sti link
  # if link.column.nil? we are sure that it is nt an singular association inline autolink
  # howver that will not work if a sti parent is an singular association inline autolink
  if link.column.nil?
    sti_controller_path = controller_path_for_activerecord(record.class)
    if sti_controller_path
      url_options[:controller] = sti_controller_path
      url_options[:parent_sti] = controller_path
    end
  end
end