Class: AdminAssistant::HasManyColumn

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

Defined Under Namespace

Classes: FormView, SearchView

Instance Method Summary collapse

Methods inherited from Column

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

Constructor Details

#initialize(has_many_assoc, opts) ⇒ HasManyColumn

Returns a new instance of HasManyColumn.



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

def initialize(has_many_assoc, opts)
  @has_many_assoc = has_many_assoc
  @match_text_fields_in_search = opts[:match_text_fields_in_search]
end

Instance Method Details

#add_to_query_condition(ar_query_condition, search) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/admin_assistant/has_many_column.rb', line 8

def add_to_query_condition(ar_query_condition, search)
  if @match_text_fields_in_search
    if (value = search.send(name)) and !value.blank?
      searchable_columns = Model.new(associated_class).searchable_columns
      model_class = @has_many_assoc.active_record
      sanitized_match_value = ActiveRecord::Base.sanitize("%#{value}%")
      searchable_column_comparisons = searchable_columns.map { |column|
        "LOWER(#{associated_class.table_name}.#{column.name}) like LOWER(#{sanitized_match_value})"
      }
      foreign_key = if @has_many_assoc.respond_to?(:foreign_key)
        @has_many_assoc.foreign_key
      else
        @has_many_assoc.primary_key_name
      end
      condition_sql = <<-CONDITION_SQL
      #{model_class.table_name}.#{model_class.primary_key} in (select #{foreign_key} from #{associated_class.table_name} where #{searchable_column_comparisons.join(' or ')})
      CONDITION_SQL
      ar_query_condition.sqls << condition_sql
    end
  end
end

#associated_classObject



30
31
32
# File 'lib/admin_assistant/has_many_column.rb', line 30

def associated_class
  @has_many_assoc.klass
end

#attributes_for_search_object(search_params, compare_to_range) ⇒ Object



34
35
36
# File 'lib/admin_assistant/has_many_column.rb', line 34

def attributes_for_search_object(search_params, compare_to_range)
  {name.to_sym => search_params[name]}
end

#contains?(column_name) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/admin_assistant/has_many_column.rb', line 38

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

#model_classObject



42
43
44
# File 'lib/admin_assistant/has_many_column.rb', line 42

def model_class
  @has_many_assoc.active_record
end

#nameObject



46
47
48
# File 'lib/admin_assistant/has_many_column.rb', line 46

def name
  @has_many_assoc.name.to_s
end