Module: ActiveScaffold::Helpers::ViewHelpers

Overview

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

Constant Summary

Constants included from ActionLinkHelpers

ActionLinkHelpers::NESTED_PARAMS

Instance Method Summary collapse

Methods included from HumanConditionHelpers

#active_scaffold_grouped_by_label, #active_scaffold_human_condition_boolean, #active_scaffold_human_condition_date, #active_scaffold_human_condition_for, #active_scaffold_human_condition_integer, #active_scaffold_human_condition_null, #active_scaffold_human_condition_select, #active_scaffold_human_condition_string, #format_human_condition, #override_human_condition, #override_human_condition?, #override_human_condition_column

Methods included from SearchColumnHelpers

#active_scaffold_group_column, #active_scaffold_group_search_column, #active_scaffold_group_search_options, #active_scaffold_search_boolean, #active_scaffold_search_date, #active_scaffold_search_datetime, #active_scaffold_search_decimal, #active_scaffold_search_for, #active_scaffold_search_integer, #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_field, #search_attribute, #search_column_label, #search_label_for, #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_checkbox_option, #active_scaffold_enum_options, #active_scaffold_file_with_content, #active_scaffold_file_with_remove_link, #active_scaffold_grouped_options, #active_scaffold_input_boolean, #active_scaffold_input_carrierwave, #active_scaffold_input_checkbox, #active_scaffold_input_color, #active_scaffold_input_date, #active_scaffold_input_datetime, #active_scaffold_input_dragonfly, #active_scaffold_input_email, #active_scaffold_input_enum, #active_scaffold_input_file_column, #active_scaffold_input_for, #active_scaffold_input_month, #active_scaffold_input_number, #active_scaffold_input_options, #active_scaffold_input_paperclip, #active_scaffold_input_password, #active_scaffold_input_plural_association, #active_scaffold_input_radio, #active_scaffold_input_range, #active_scaffold_input_select, #active_scaffold_input_singular_association, #active_scaffold_input_telephone, #active_scaffold_input_text_options, #active_scaffold_input_textarea, #active_scaffold_input_time, #active_scaffold_input_url, #active_scaffold_input_virtual, #active_scaffold_input_week, #active_scaffold_number_input, #active_scaffold_plural_association_options, #active_scaffold_radio_option, #active_scaffold_refresh_link, #active_scaffold_render_input, #active_scaffold_render_subform_column, #active_scaffold_select_name_with_multiple, #active_scaffold_subform_attributes, #active_scaffold_text_input, #active_scaffold_translate_select_options, #active_scaffold_translated_option, #column_renders_as, #column_scope, #column_show_add_existing, #column_show_add_new, #current_form_columns, #field_attributes, #form_attribute, #form_hidden_attribute, #form_hidden_field, #in_subform?, #label_for, #numerical_constraints_for_column, #override_form_field, #override_form_field_partial, #override_input, #override_subform_partial, #partial_for_model, #render_column, #subform_label, #subform_partial_for_column, #update_columns_options

Methods included from ShowColumnHelpers

#active_scaffold_show_horizontal, #active_scaffold_show_text, #active_scaffold_show_vertical, #display_link_in_show?, #override_show_column_ui, #show_column_override, #show_column_value, #show_columns_for, #show_label

Methods included from ListColumnHelpers

#active_scaffold_column_carrierwave, #active_scaffold_column_checkbox, #active_scaffold_column_download_link, #active_scaffold_column_download_link_with_filename, #active_scaffold_column_dragonfly, #active_scaffold_column_fulltext, #active_scaffold_column_marked, #active_scaffold_column_month, #active_scaffold_column_paperclip, #active_scaffold_column_percentage, #active_scaffold_column_telephone, #active_scaffold_column_text, #active_scaffold_column_thumbnail, #active_scaffold_column_week, #active_scaffold_inplace_edit, #active_scaffold_inplace_edit_tag_options, #all_marked?, #cache_association, #clean_column_value, #column_calculation, #column_heading_attributes, #column_heading_label, #column_heading_value, #column_override, #dragonfly_url_for_attachment, #format_association_value, #format_collection_association_value, #format_column_calculation, #format_column_value, #format_grouped_search_column, #format_number_value, #format_singular_association_value, #format_value, #get_column_method, #get_column_value, #inplace_edit?, #inplace_edit_cloning?, #inplace_edit_control, #inplace_edit_control_css_class, #inplace_edit_data, #mark_column_heading, #override_column_ui, #render_column_calculation, #render_column_heading, #render_list_column, #tel_to

Methods included from PaginationHelpers

#pagination_ajax_link, #pagination_ajax_links, #pagination_url_options

Methods included from AssociationHelpers

#association_klass_scoped, #association_options_count, #association_options_find, #cache_association_options, #column_for_association, #options_for_association_conditions, #options_for_association_count, #record_select_params_for_add_existing, #sorted_association_options_find

Methods included from ActionLinkHelpers

#action_link_authorized?, #action_link_html, #action_link_html_options, #action_link_selected?, #action_link_text, #action_link_to_inline_form, #action_link_url, #action_link_url_options, #add_query_string_to_cached_url, #cache_action_link_url?, #cache_action_link_url_options?, #cached_action_link_url, #cached_action_link_url_options, #column_link_authorized?, #configure_column_link, #display_action_link, #display_action_links, #display_dynamic_action_group, #get_action_link_id, #query_string_for_action_links, #render_action_link, #replace_id_params_in_action_link_url, #replaced_action_link_url_options, #skip_action_link?, #sti_record?, #url_options_for_nested_link, #url_options_for_sti_link

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, #nested_id, #nested_parent_id, #scope_id, #search_input_id, #sub_form_id, #sub_form_list_id, #sub_section_id

Instance Method Details

#active_scaffold_configObject

access to the configuration variable



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

def active_scaffold_config
  controller.class.active_scaffold_config
end

#active_scaffold_config_for(*args) ⇒ Object



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

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

#active_scaffold_controller_for(*args) ⇒ Object



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

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

#active_scaffold_error_messages_for(*params) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 206

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 }

  if count.zero?
    ''
  else
    html = {}
    %i[id class].each do |key|
      if options.include?(key)
        value = options[key]
        html[key] = value if value.present?
      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.tr('_', ' '))
      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
        safe_join error_messages, tag(:br)
      else
         options[:list_type], safe_join(error_messages)
      end

    contents = []
    contents << (options[:header_tag] || :h2, header_message) if header_message.present?
    contents << (:p, message) if message.present?
    contents << error_messages
    contents = safe_join(contents)
    options[:container_tag] ? (options[:container_tag], contents, html) : contents
  end
end

#as_main_div_classObject



121
122
123
124
125
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 121

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
end

#as_slider(options) ⇒ Object



141
142
143
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 141

def as_slider(options)
  (:span, '', class: 'as-slider', data: {slider: options})
end

#clean_class_name(name) ⇒ Object



149
150
151
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 149

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

#clean_column_name(name) ⇒ Object



145
146
147
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 145

def clean_column_name(name)
  name.to_s.delete('?')
end

#column_attributes(column, record) ⇒ Object



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

def column_attributes(column, record)
  method = override_helper column, 'column_attributes'
  return send(method, record) if method
  {}
end

#column_class(column, column_value, record) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 95

def column_class(column, column_value, record)
  @_column_classes ||= {}
  @_column_classes[column.name] ||= begin
    classes = "#{column.name}-column "
    classes << 'sorted ' if active_scaffold_config.actions.include?(:list) && active_scaffold_config.list.user.sorting.sorts_on?(column)
    classes << 'numeric ' if column.number?
    classes << column.css_class unless column.css_class.nil? || column.css_class.is_a?(Proc)
    classes
  end
  classes = "#{@_column_classes[column.name]} "
  classes << 'empty ' if column_empty? column_value
  classes << 'in_place_editor_field ' if inplace_edit?(record, column) || column.list_ui == :marked
  if column.css_class.is_a?(Proc)
    css_class = column.css_class.call(column_value, record)
    classes << css_class unless css_class.nil?
  end
  classes
end

#column_empty?(column_value) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
130
131
132
133
134
135
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 127

def column_empty?(column_value)
  empty = column_value.nil?
  # column_value != false would force boolean to be cast to integer
  # when comparing to column_value of IPAddr class (PostgreSQL inet column type)
  # rubocop:disable Style/YodaCondition
  empty ||= false != column_value && column_value.blank?
  empty ||= ['&nbsp;', empty_field_text].include? column_value if column_value.is_a? String
  empty
end

#column_heading_class(column, sorting) ⇒ Object



114
115
116
117
118
119
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 114

def column_heading_class(column, sorting)
  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
end

#controller_path_for_activerecord(klass) ⇒ Object

Uncategorized



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

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

#display_message(message) ⇒ Object



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

def display_message(message)
  message = safe_join message, tag(:br) if message.is_a?(Array)
  if (highlights = active_scaffold_config.highlight_messages)
    message = highlights.inject(message) do |msg, (phrases, highlighter)|
      highlight(msg, phrases, highlighter || {})
    end
  end
  if (format = active_scaffold_config.timestamped_messages)
    format = :short if format == true
    messages = [(:div, l(Time.current, :format => format), :class => 'timestamp')]
    messages << (:div, message, :class => 'message-content')
    message = safe_join messages, ' '
  end
  message
end

#empty_field_textObject



137
138
139
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 137

def empty_field_text
  active_scaffold_config.list.empty_field_text if active_scaffold_config.actions.include?(:list)
end

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



53
54
55
56
57
58
59
60
61
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 53

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')
  safe_join output
end

#history_stateObject



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 174

def history_state
  if active_scaffold_config.
    state = {page: @page.try(:number)}
    state[:search] = search_params if respond_to?(:search_params) && search_params.present?
    if active_scaffold_config.list.user.user_sorting?
      column, state[:sort_direction] = active_scaffold_config.list.user.sorting.first
      state[:sort] = column.name
    else
      state.merge sort: '', sort_direction: ''
    end
    state
  else
    {}
  end
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.



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

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_block)
  link_to options[:default_visible] ? options[:hide_label] : options[:show_label], '#', :data => {:show => options[:show_label], :hide => options[:hide_label], :toggable => id}, :style => 'display: none;', :class => 'as-js-button visibility-toggle'
end

#list_row_class(record) ⇒ Object



84
85
86
87
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 84

def list_row_class(record)
  class_override_helper = list_row_class_method(record)
  class_override_helper ? send(class_override_helper, record) : ''
end

#list_row_class_method(record) ⇒ Object



78
79
80
81
82
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 78

def list_row_class_method(record)
  return @_list_row_class_method if defined? @_list_row_class_method
  class_override_helper = "#{clean_class_name(record.class.name)}_list_row_class"
  @_list_row_class_method = (class_override_helper if respond_to?(class_override_helper))
end

#loading_indicator_tag(options) ⇒ Object

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



64
65
66
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 64

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

#override_helper(column, suffix) ⇒ Object



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

def override_helper(column, suffix)
  hash = @_override_helpers ||= {}
  hash = hash[suffix] ||= {}
  hash = hash[column.active_record_class.name] ||= {}
  return hash[column.name] if hash.include? column.name
  hash[column.name] = begin
    method_with_class = override_helper_name(column, suffix, true)
    if respond_to?(method_with_class)
      method_with_class
    else
      method = override_helper_name(column, suffix)
      method if respond_to?(method)
    end
  end
end

#override_helper_name(column, suffix, class_prefix = false) ⇒ Object

the naming convention for overriding with helpers



154
155
156
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 154

def override_helper_name(column, suffix, class_prefix = false)
  "#{clean_class_name(column.active_record_class.name) + '_' if class_prefix}#{clean_column_name(column.name)}_#{suffix}"
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)


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

def template_exists?(template_name, partial = false)
  restore_view_paths, lookup_context.view_paths = lookup_context.view_paths, @_view_paths if @_view_paths
  lookup_context.exists?(template_name, '', partial).tap do
    lookup_context.view_paths = restore_view_paths if @_view_paths
  end
end