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 ListColumnHelpers

ListColumnHelpers::FORM_UI_WITH_OPTIONS, ListColumnHelpers::INPLACE_EDIT_PLURAL_FORM_UI

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_datetime, #active_scaffold_human_condition_datetime_range_format, #active_scaffold_human_condition_for, #active_scaffold_human_condition_integer, #active_scaffold_human_condition_null, #active_scaffold_human_condition_range, #active_scaffold_human_condition_select, #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_date_field, #active_scaffold_search_datetime, #active_scaffold_search_datetime_comparator_options, #active_scaffold_search_datetime_comparator_tag, #active_scaffold_search_datetime_field, #active_scaffold_search_datetime_numeric_tag, #active_scaffold_search_datetime_range_tag, #active_scaffold_search_datetime_trend_tag, #active_scaffold_search_datetime_trend_units, #active_scaffold_search_decimal, #active_scaffold_search_draggable, #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_select_multiple, #active_scaffold_search_text, #active_scaffold_search_time, #active_scaffold_search_time_field, #active_scaffold_search_timestamp, #column_datetime?, #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

#action_for_validation?, #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_active_storage_has_many, #active_scaffold_input_active_storage_has_one, #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_draggable, #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_select_multiple, #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_new_record_subform, #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_show_new_subform_link, #active_scaffold_subform_attributes, #active_scaffold_text_input, #active_scaffold_translate_select_options, #active_scaffold_translated_option, #column_numerical_constraints, #column_renders_as, #column_scope, #column_show_add_existing, #column_show_add_new, #current_form_columns, #field_attributes, #form_attribute, #form_column_is_hidden?, #form_column_label, #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_active_storage_has_many, #active_scaffold_column_active_storage_has_one, #active_scaffold_column_boolean, #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?, #association_join_text, #cache_association, #clean_column_value, #column_association_size, #column_calculation, #column_heading_attributes, #column_heading_label, #column_heading_value, #column_override, #column_wrap_tag, #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_helper_method, #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_in_params_conditions?, #column_link_authorized?, #configure_column_link, #display_action_link, #display_action_links, #display_dynamic_action_group, #get_action_link_id, #ignore_param_for_nested?, #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?, #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_controller_for(klass) ⇒ Object

Delegates



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

def active_scaffold_controller_for(klass)
  ActiveScaffold::Registry.cache :as_controller, klass do
    controller.class.active_scaffold_controller_for(klass)
  end
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
# 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)

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

    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



119
120
121
122
123
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 119

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_main_div_dataObject



125
126
127
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 125

def as_main_div_data
  params[:eid] ? {eid: id_from_controller(params[:eid])} : {}
end

#as_slider(options) ⇒ Object



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

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

#clean_class_name(name) ⇒ Object



152
153
154
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 152

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

#clean_column_name(name) ⇒ Object



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

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

#column_attributes(column, record) ⇒ Object



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

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



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

def column_class(column, column_value, record)
  classes = ActiveScaffold::Registry.cache :column_classes, column.cache_key do
    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 = classes.dup
  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)


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

def column_empty?(column_value)
  @_empty_values ||= ['&nbsp;', empty_field_text].compact
  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)
  empty ||= false != column_value && column_value.blank? # rubocop:disable Style/YodaCondition
  empty ||= @_empty_values.include? column_value
  empty
end

#column_heading_class(column, sorting) ⇒ Object



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

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



30
31
32
33
34
35
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 30

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.user.highlight_messages)
    message = highlights.inject(message) do |msg, (phrases, highlighter)|
      highlight(msg, phrases, highlighter || {})
    end
  end
  if (format = active_scaffold_config.user.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



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

def empty_field_text
  return @_empty_field_text if defined? @_empty_field_text
  @_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



49
50
51
52
53
54
55
56
57
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 49

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&.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
77
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 71

def link_to_visibility_toggle(id, options = {})
  options[:hide_label] ||= as_(:hide)
  options[:show_label] ||= as_(:show_block)
  label = options[:default_visible].nil? || options[:default_visible] ? options[:hide_label] : options[:show_label]
  data = {:show => options[:show_label], :hide => options[:hide_label], :toggable => id}
  link_to label, '#', :data => data, :style => 'display: none;', :class => 'as-js-button visibility-toggle'
end

#list_row_attributes(tr_class, tr_id, data_refresh) ⇒ Object



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

def list_row_attributes(tr_class, tr_id, data_refresh)
  {class: "record #{tr_class}", id: tr_id, data: {refresh: data_refresh}}
end

#list_row_class(record) ⇒ Object



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

def list_row_class(record)
  class_override_helper = override_helper_per_model(:list_row_class, record.class)
  class_override_helper != :list_row_class ? send(class_override_helper, record) : ''
end

#loading_indicator_tag(options) ⇒ Object

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



60
61
62
63
64
65
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 60

def loading_indicator_tag(options)
  # it's call many times and we can cache same result
  @_loading_indicator_path ||= image_path('active_scaffold/indicator.gif')
  # it's call many times in long lists, image_tag is a bit slower
  tag :img, src: @_loading_indicator_path, style: 'visibility:hidden;', id: loading_indicator_id(options), alt: 'loading indicator', class: 'loading-indicator'
end

#override_helper(column, suffix) ⇒ Object



169
170
171
172
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 169

def override_helper(column, suffix)
  method = "#{clean_column_name(column.name)}_#{suffix}"
  override_helper_per_model(method, column.active_record_class, [suffix, column.cache_key])
end

#override_helper_per_model(method, model, cache_keys = nil) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 156

def override_helper_per_model(method, model, cache_keys = nil)
  cache_keys ||= [method, model.name]
  ActiveScaffold::Registry.cache(*cache_keys) do
    model_names = [model.name]
    model_names << model.base_class.name if model.base_class != model
    method_with_class = model_names.find do |model_name|
      method_with_class = "#{clean_class_name(model_name)}_#{method}"
      break method_with_class if respond_to?(method_with_class)
    end
    method_with_class || (method if respond_to?(method))
  end
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)


39
40
41
42
43
44
45
46
47
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 39

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