Module: ActiveScaffold::Helpers::HumanConditionHelpers
- Included in:
- ViewHelpers
- Defined in:
- lib/active_scaffold/helpers/human_condition_helpers.rb
Overview
Helpers that assist with rendering of a human readable search statement
Instance Method Summary collapse
- #active_scaffold_grouped_by_label ⇒ Object
-
#active_scaffold_human_condition_boolean(column, value) ⇒ Object
(also: #active_scaffold_human_condition_checkbox)
def active_scaffold_human_condition_date(column, value) conversion = column.column_type == :date ? :to_date : :to_time from = controller.class.condition_value_for_datetime(column, value, conversion) from = I18n.l from if from to = controller.class.condition_value_for_datetime(column, value, conversion) if value == ‘BETWEEN’ || (value.nil? && value) to = “- #to” if to format_human_condition column, value, from, to end.
- #active_scaffold_human_condition_datetime(column, value) ⇒ Object (also: #active_scaffold_human_condition_time, #active_scaffold_human_condition_date, #active_scaffold_human_condition_timestamp)
- #active_scaffold_human_condition_datetime_range_format(range_type, range) ⇒ Object
- #active_scaffold_human_condition_for(column) ⇒ Object
- #active_scaffold_human_condition_integer(column, value) ⇒ Object (also: #active_scaffold_human_condition_decimal, #active_scaffold_human_condition_float)
- #active_scaffold_human_condition_null(column, value) ⇒ Object
- #active_scaffold_human_condition_range(column, value) ⇒ Object (also: #active_scaffold_human_condition_string)
- #active_scaffold_human_condition_select(column, associated) ⇒ Object (also: #active_scaffold_human_condition_multi_select, #active_scaffold_human_condition_record_select, #active_scaffold_human_condition_chosen, #active_scaffold_human_condition_multi_chosen)
- #format_human_condition(column, opt, from = nil, to = nil) ⇒ Object
-
#override_human_condition(search_ui) ⇒ Object
the naming convention for overriding human condition search_ui types.
- #override_human_condition?(search_ui) ⇒ Boolean
-
#override_human_condition_column(column) ⇒ Object
(also: #override_human_condition_column?)
the naming convention for overriding form fields with helpers.
Instance Method Details
#active_scaffold_grouped_by_label ⇒ Object
18 19 20 21 22 23 |
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 18 def active_scaffold_grouped_by_label text, = active_scaffold_config.field_search..find do |text, value| (value || text).to_s == field_search_params['active_scaffold_group'] end active_scaffold_translated_option(active_scaffold_group_column, text).first if text end |
#active_scaffold_human_condition_boolean(column, value) ⇒ Object Also known as: active_scaffold_human_condition_checkbox
def active_scaffold_human_condition_date(column, value)
conversion = column.column_type == :date ? :to_date : :to_time
from = controller.class.condition_value_for_datetime(column, value['from'], conversion)
from = I18n.l from if from
to = controller.class.condition_value_for_datetime(column, value['to'], conversion) if value['opt'] == 'BETWEEN' || (value['opt'].nil? && value['to'])
to = "- #{I18n.l to}" if to
format_human_condition column, value['opt'], from, to
end
95 96 97 98 |
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 95 def active_scaffold_human_condition_boolean(column, value) attribute = column.active_record_class.human_attribute_name(column.name) as_(:boolean, :scope => :human_conditions, :column => attribute, :value => as_(value)) end |
#active_scaffold_human_condition_datetime(column, value) ⇒ Object Also known as: active_scaffold_human_condition_time, active_scaffold_human_condition_date, active_scaffold_human_condition_timestamp
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 48 def active_scaffold_human_condition_datetime(column, value) case value['opt'] when 'RANGE' range_type, range = value['range'].downcase.split('_') format = active_scaffold_human_condition_datetime_range_format(range_type, range) from, = controller.class.datetime_from_to(column, value) "#{column.active_record_class.human_attribute_name(column.name)} = #{as_(value['range'].downcase).downcase} (#{I18n.l(from, :format => format)})" when 'PAST', 'FUTURE' from, to = controller.class.datetime_from_to(column, value) "#{column.active_record_class.human_attribute_name(column.name)} #{as_('BETWEEN'.downcase).downcase} #{I18n.l(from)} - #{I18n.l(to)}" when 'null', 'not_null' "#{column.active_record_class.human_attribute_name(column.name)} #{as_(value['opt'].downcase).downcase}" else from, to = controller.class.datetime_from_to(column, value) "#{column.active_record_class.human_attribute_name(column.name)} #{as_(value['opt'].downcase).downcase} #{I18n.l(from)} #{value['opt'] == 'BETWEEN' ? '- ' + I18n.l(to) : ''}" end end |
#active_scaffold_human_condition_datetime_range_format(range_type, range) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 69 def active_scaffold_human_condition_datetime_range_format(range_type, range) case range when 'week' first_day_of_week = I18n.translate 'active_scaffold.date_picker_options.firstDay' if first_day_of_week == 1 '%W %Y' else '%U %Y' end when 'month' '%b %Y' when 'year' '%Y' else I18n.translate 'date.formats.default' end end |
#active_scaffold_human_condition_for(column) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 5 def active_scaffold_human_condition_for(column) return if (value = field_search_params[column.name.to_s]).nil? search_ui = column.search_ui search_ui ||= column.column_type if column.column if override_human_condition_column?(column) send(override_human_condition_column(column), value, {}) elsif search_ui && override_human_condition?(search_ui) send(override_human_condition(search_ui), column, value) else logger.warn "undefined active_scaffold_human_condition method for search_ui #{search_ui} on column #{column.name}" end end |
#active_scaffold_human_condition_integer(column, value) ⇒ Object Also known as: active_scaffold_human_condition_decimal, active_scaffold_human_condition_float
33 34 35 36 37 |
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 33 def active_scaffold_human_condition_integer(column, value) from = format_number_value(controller.class.condition_value_for_numeric(column, value['from']), column.) if value['from'].present? to = "- #{format_number_value(controller.class.condition_value_for_numeric(column, value['to']), column.)}" if value['opt'] == 'BETWEEN' format_human_condition column, value['opt'].downcase, from, to end |
#active_scaffold_human_condition_null(column, value) ⇒ Object
101 102 103 |
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 101 def active_scaffold_human_condition_null(column, value) format_human_condition column, value.to_sym end |
#active_scaffold_human_condition_range(column, value) ⇒ Object Also known as: active_scaffold_human_condition_string
41 42 43 44 45 |
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 41 def active_scaffold_human_condition_range(column, value) opt = ActiveScaffold::Finder::STRING_COMPARATORS.key(value['opt']) || value['opt'] to = "- #{value['to']}" if opt == 'BETWEEN' format_human_condition column, opt, "'#{value['from']}'", to end |
#active_scaffold_human_condition_select(column, associated) ⇒ Object Also known as: active_scaffold_human_condition_multi_select, active_scaffold_human_condition_record_select, active_scaffold_human_condition_chosen, active_scaffold_human_condition_multi_chosen
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 105 def active_scaffold_human_condition_select(column, associated) attribute = column.active_record_class.human_attribute_name(column.name) if associated.is_a?(Hash) return active_scaffold_human_condition_range(column, associated) unless associated['opt'] == '=' associated = associated['from'] end associated = [associated].compact unless associated.is_a? Array if column.association method = column.[:label_method] || :to_label associated = column.association.klass.where(:id => associated.map(&:to_i)).map(&method) elsif column.[:options] associated = associated.collect do |value| text, val = column.[:options].find { |t, v| (v.nil? ? t : v).to_s == value.to_s } value = active_scaffold_translated_option(column, text, val).first if text value end end as_(:association, :scope => :human_conditions, :column => attribute, :value => associated.join(', ')) end |
#format_human_condition(column, opt, from = nil, to = nil) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 25 def format_human_condition(column, opt, from = nil, to = nil) attribute = column.active_record_class.human_attribute_name(column.name) opt ||= :between if from && to opt ||= from ? '>=' : '<=' from = to = nil if opt&.in? %w[null not_null] "#{attribute} #{as_(opt).downcase} #{from} #{to}" end |
#override_human_condition(search_ui) ⇒ Object
the naming convention for overriding human condition search_ui types
140 141 142 |
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 140 def override_human_condition(search_ui) "active_scaffold_human_condition_#{search_ui}" end |
#override_human_condition?(search_ui) ⇒ Boolean
135 136 137 |
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 135 def override_human_condition?(search_ui) respond_to?(override_human_condition(search_ui)) end |
#override_human_condition_column(column) ⇒ Object Also known as: override_human_condition_column?
the naming convention for overriding form fields with helpers
130 131 132 |
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 130 def override_human_condition_column(column) override_helper column, 'human_condition_column' end |