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_human_condition_for(column) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 6

def active_scaffold_human_condition_for(column)
  value = field_search_params[column.name]
  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 and override_human_condition?(column.search_ui)
    send(override_human_condition(column.search_ui), column, value)
  else
    case search_ui
    when :integer, :decimal, :float
      "#{column.active_record_class.human_attribute_name(column.name)} #{as_(value[:opt].downcase).downcase} #{format_number_value(controller.class.condition_value_for_numeric(column, value[:from]), column.options)} #{value[:opt] == 'BETWEEN' ? '- ' + format_number_value(controller.class.condition_value_for_numeric(column, value[:to]), column.options).to_s : ''}"
    when :string
      opt = ActiveScaffold::Finder::StringComparators.index(value[:opt]) || value[:opt]
      "#{column.active_record_class.human_attribute_name(column.name)} #{as_(opt).downcase} '#{value[:from]}' #{opt == 'BETWEEN' ? '- ' + value[:to].to_s : ''}"
    when :date, :time, :datetime, :timestamp
      conversion = column.column[:type] == :date ? :to_date : :to_time
      from = controller.condition_value_for_datetime(value[:from], conversion)
      to = controller.condition_value_for_datetime(value[:to], conversion)
      "#{column.active_record_class.human_attribute_name(column.name)} #{as_(value[:opt])} #{I18n.l(from)} #{value[:opt] == 'BETWEEN' ? '- ' + I18n.l(to) : ''}"
    when :select, :multi_select, :record_select
      associated = value
      associated = [associated].compact unless associated.is_a? Array
      if column.association
        klass = column.association.associated_class
        associated = column.association.associated_class.filter(klass.primary_key => associated.map(&:to_i)).collect(&:to_label)
      end
      "#{column.active_record_class.human_attribute_name(column.name)} = #{associated.join(', ')}"
    when :boolean, :checkbox
      label = column.active_record_class.db.send(:typecast_value_boolean, value) ? as_(:true) : as_(:false)
      "#{column.active_record_class.human_attribute_name(column.name)} = #{label}"
    when :null
      "#{column.active_record_class.human_attribute_name(column.name)} #{as_(value.to_sym)}"
    end
  end unless value.nil?
end

#override_human_condition(search_ui) ⇒ Object

the naming convention for overriding human condition search_ui types



57
58
59
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 57

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

#override_human_condition?(search_ui) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 52

def override_human_condition?(search_ui)
  respond_to?(override_human_condition(search_ui))
end

#override_human_condition_column(column) ⇒ Object

the naming convention for overriding form fields with helpers



48
49
50
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 48

def override_human_condition_column(column)
  "#{column.name}_human_condition_column"
end

#override_human_condition_column?(column) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/active_scaffold/helpers/human_condition_helpers.rb', line 43

def override_human_condition_column?(column)
  respond_to?(override_human_condition_column(column))
end