Module: ActiveScaffold::Helpers::ViewHelpers
- Includes:
- ActionLinkHelpers, AssociationHelpers, FormColumnHelpers, HumanConditionHelpers, IdHelpers, ListColumnHelpers, PaginationHelpers, SearchColumnHelpers, ShowColumnHelpers, TabsHelpers
- Defined in:
- lib/active_scaffold/helpers/view_helpers.rb
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
-
#active_scaffold_controller_for(klass) ⇒ Object
Delegates.
- #active_scaffold_error_messages_for(*params) ⇒ Object
- #as_main_div_class ⇒ Object
- #as_main_div_data ⇒ Object
- #as_slider(options) ⇒ Object
- #clean_class_name(name) ⇒ Object
- #clean_column_name(name) ⇒ Object
- #column_attributes(column, record) ⇒ Object
- #column_class(column, column_value, record) ⇒ Object
- #column_empty?(column_value) ⇒ Boolean
- #column_heading_class(column, sorting) ⇒ Object
-
#controller_path_for_activerecord(klass) ⇒ Object
Uncategorized.
- #display_message(message) ⇒ Object
- #empty_field_text ⇒ Object
- #form_remote_upload_tag(url_for_options = {}, options = {}) ⇒ Object
- #history_state ⇒ Object
-
#link_to_visibility_toggle(id, options = {}) ⇒ Object
Creates a javascript-based link that toggles the visibility of some element on the page.
- #list_row_attributes(tr_class, tr_id, data_refresh) ⇒ Object
- #list_row_class(record) ⇒ Object
-
#loading_indicator_tag(options) ⇒ Object
a general-use loading indicator (the “stuff is happening, please wait” feedback).
- #override_helper(column, suffix) ⇒ Object
- #override_helper_per_model(method, model, cache_keys = nil) ⇒ Object
-
#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.
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_comparator_options, #active_scaffold_search_select_multiple, #active_scaffold_search_text, #active_scaffold_search_time, #active_scaffold_search_time_field, #active_scaffold_search_timestamp, #build_active_scaffold_search_range_ui, #column_datetime?, #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 TabsHelpers
#active_scaffold_current_tabs, #active_scaffold_input_for_tabbed, #active_scaffold_tab, #active_scaffold_tab_content, #active_scaffold_tab_options, #active_scaffold_tabbed_by, #active_scaffold_tabs_for
Methods included from FormColumnHelpers
#action_for_validation?, #active_scaffold_add_existing_input, #active_scaffold_add_existing_label, #active_scaffold_check_all_buttons, #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, #render_subsection, #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, #list_record_view, #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_link_group, #display_action_link_separator, #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
21 22 23 24 25 |
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 21 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
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 208 def (*params) = params..symbolize_keys .reverse_merge!(container_tag: :div, list_type: :ul) objects = Array.wrap(.delete(:object) || params).map do |object| object = instance_variable_get(:"@#{object}") unless object.respond_to?(:to_model) object = convert_to_model(object) [: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 .include?(key) value = [key] html[key] = value if value.present? else html[key] = 'errorExplanation' end end [:object_name] ||= params.first = if .include?(:header_message) [:header_message] else as_('errors.template.header', count: count, model: [:object_name].to_s.tr('_', ' ')) end = .include?(:message) ? [:message] : as_('errors.template.body') = objects.sum([]) do |object| object.errors..map do |msg| [:list_type] == :br ? msg : content_tag(:li, msg) end end = if [:list_type] == :br safe_join , tag.br else content_tag [:list_type], safe_join() end contents = [] contents << content_tag([:header_tag] || :h2, ) if .present? contents << content_tag(:p, ) if .present? contents << contents = safe_join(contents) [:container_tag] ? content_tag([:container_tag], contents, html) : contents end end |
#as_main_div_class ⇒ Object
120 121 122 123 124 |
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 120 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_data ⇒ Object
126 127 128 |
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 126 def as_main_div_data params[:eid] ? {eid: id_from_controller(params[:eid])} : {} end |
#as_slider(options) ⇒ Object
146 147 148 |
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 146 def () content_tag(:span, '', class: 'as-slider', data: {slider: }) end |
#clean_class_name(name) ⇒ Object
154 155 156 |
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 154 def clean_class_name(name) name.underscore.tr('/', '_') end |
#clean_column_name(name) ⇒ Object
150 151 152 |
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 150 def clean_column_name(name) name.to_s.delete('?') end |
#column_attributes(column, record) ⇒ Object
88 89 90 91 92 93 |
# 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
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 95 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
130 131 132 133 134 135 136 137 138 |
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 130 def column_empty?(column_value) @_empty_values ||= [' ', 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
113 114 115 116 117 118 |
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 113 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
31 32 33 34 35 36 |
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 31 def controller_path_for_activerecord(klass) controller = active_scaffold_controller_for(klass) controller.controller_path rescue ActiveScaffold::ControllerNotFound nil end |
#display_message(message) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 192 def () = safe_join , tag.br if .is_a?(Array) if (highlights = active_scaffold_config.user.) = highlights.inject() do |msg, (phrases, highlighter)| highlight(msg, phrases, highlighter || {}) end end if (format = active_scaffold_config.user.) format = :short if format == true = [content_tag(:div, l(Time.current, format: format), class: 'timestamp')] << content_tag(:div, , class: 'message-content') = safe_join , ' ' end end |
#empty_field_text ⇒ Object
140 141 142 143 144 |
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 140 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
50 51 52 53 54 55 56 57 58 |
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 50 def form_remote_upload_tag( = {}, = {}) [:target] = action_iframe_id() [:multipart] ||= true [:class] = "#{[:class]} as_remote_upload".strip output = [] output << form_tag(, ) output << content_tag(:iframe, '', id: action_iframe_id(), name: action_iframe_id(), style: 'display:none') safe_join output end |
#history_state ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 176 def history_state if active_scaffold_config.store_user_settings 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 |
#link_to_visibility_toggle(id, options = {}) ⇒ Object
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 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, = {}) [:hide_label] ||= as_(:hide) [:show_label] ||= as_(:show_block) label = [:default_visible].nil? || [:default_visible] ? [:hide_label] : [:show_label] data = {show: [:show_label], hide: [: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)
61 62 63 64 65 66 |
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 61 def loading_indicator_tag() # 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(), alt: 'loading indicator', class: 'loading-indicator') end |
#override_helper(column, suffix) ⇒ Object
171 172 173 174 |
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 171 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
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 158 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.respond_to?(:base_class) && 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
40 41 42 43 44 45 46 47 48 |
# File 'lib/active_scaffold/helpers/view_helpers.rb', line 40 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 |