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

Instance Method Details

#active_scaffold_grouped_by_labelObject


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.group_options.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


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

def active_scaffold_human_condition_boolean(column, value)
  attribute = column.active_record_class.human_attribute_name(column.name)
  label = as_(ActiveScaffold::Core.column_type_cast(value, column.column) ? :true : :false) # rubocop:disable Lint/BooleanSymbol
  as_(:boolean, :scope => :human_conditions, :column => attribute, :value => label)
end

#active_scaffold_human_condition_date(column, value) ⇒ Object Also known as: active_scaffold_human_condition_time, active_scaffold_human_condition_datetime, active_scaffold_human_condition_timestamp


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

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

#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


32
33
34
35
36
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 32

def active_scaffold_human_condition_integer(column, value)
  from = format_number_value(controller.class.condition_value_for_numeric(column, value['from']), column.options) if value['from'].present?
  to = "- #{format_number_value(controller.class.condition_value_for_numeric(column, value['to']), column.options)}" if value['opt'] == 'BETWEEN'
  format_human_condition column, value['opt'].downcase, from, to
end

#active_scaffold_human_condition_null(column, value) ⇒ Object


65
66
67
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 65

def active_scaffold_human_condition_null(column, value)
  format_human_condition column, value.to_sym
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


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 69

def active_scaffold_human_condition_select(column, associated)
  attribute = column.active_record_class.human_attribute_name(column.name)
  associated = [associated].compact unless associated.is_a? Array
  if column.association
    method = column.options[:label_method] || :to_label
    associated = column.association.klass.where(:id => associated.map(&:to_i)).map(&method)
  elsif column.options[:options]
    associated = associated.collect do |value|
      text, val = column.options[: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

#active_scaffold_human_condition_string(column, value) ⇒ Object


40
41
42
43
44
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 40

def active_scaffold_human_condition_string(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

#format_human_condition(column, opt, from = nil, to = nil) ⇒ Object


25
26
27
28
29
30
# 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 ? '>=' : '<='
  "#{attribute} #{as_(opt).downcase} #{from} #{to}"
end

#override_human_condition(search_ui) ⇒ Object

the naming convention for overriding human condition search_ui types


100
101
102
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 100

def override_human_condition(search_ui)
  "active_scaffold_human_condition_#{search_ui}"
end

#override_human_condition?(search_ui) ⇒ Boolean

Returns:

  • (Boolean)

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

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


90
91
92
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 90

def override_human_condition_column(column)
  override_helper column, 'human_condition_column'
end