Class: AdminAssistant::ActiveRecordColumn

Inherits:
Column
  • Object
show all
Defined in:
lib/admin_assistant/active_record_column.rb

Defined Under Namespace

Classes: ConditionUpdate, FormView, IndexView, SearchView

Instance Method Summary collapse

Methods inherited from Column

#form_view, #index_view, #search_view, #show_view, #verify_for_search, #view

Constructor Details

#initialize(ar_column) ⇒ ActiveRecordColumn

Returns a new instance of ActiveRecordColumn.



3
4
5
# File 'lib/admin_assistant/active_record_column.rb', line 3

def initialize(ar_column)
  @ar_column = ar_column
end

Instance Method Details

#add_to_query_condition(ar_query_condition, search) ⇒ Object



7
8
9
# File 'lib/admin_assistant/active_record_column.rb', line 7

def add_to_query_condition(ar_query_condition, search)
  ConditionUpdate.new(ar_query_condition, search, name, field_type).run
end

#attributes_for_search_object(search_params, compare_to_range) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/admin_assistant/active_record_column.rb', line 11

def attributes_for_search_object(search_params, compare_to_range)
  attrs = {}
  if compare_to_range
    attrs[name] = range_attribute_for_search_object search_params[name]
  else
    attrs[name] = if field_type == :datetime
      datetime_attributes_for_search_object search_params, name
    else
      terms = search_params[@ar_column.name]
      unless terms.blank?
        case field_type
          when :boolean
            terms.blank? ? nil : (terms == 'true')
          else
            terms
        end
      end
    end
  end
  attrs
end

#contains?(column_name) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/admin_assistant/active_record_column.rb', line 33

def contains?(column_name)
  column_name.to_s == @ar_column.name
end

#datetime_attributes_for_search_object(search_params, prefix) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/admin_assistant/active_record_column.rb', line 37

def datetime_attributes_for_search_object(search_params, prefix)
  begin
    Time.utc(
      *(1..5).to_a.map { |i| search_params["#{prefix}(#{i}i)"].to_i }
    )
  rescue ArgumentError; end
end

#field_typeObject



45
46
47
# File 'lib/admin_assistant/active_record_column.rb', line 45

def field_type
  @ar_column.type
end

#nameObject



49
50
51
# File 'lib/admin_assistant/active_record_column.rb', line 49

def name
  @ar_column.name
end

#range_attribute_for_search_object(column_params) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/admin_assistant/active_record_column.rb', line 53

def range_attribute_for_search_object(column_params)
  range = {}
  if column_params
    %w(gt lt).each do |comparator|
      if column_params.keys.any? { |k| k.to_s =~ /^#{comparator}/ }
        range[comparator.to_sym] = if field_type == :datetime
          datetime_attributes_for_search_object(column_params, comparator)
        else
          column_params[comparator] unless column_params[comparator].blank?
        end
      end
    end
  end
  range
end