Class: AdminAssistant::BelongsToColumn

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

Defined Under Namespace

Classes: FormView, IndexView, SearchView, ShowView, View

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Column

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

Constructor Details

#initialize(belongs_to_assoc, opts) ⇒ BelongsToColumn

Returns a new instance of BelongsToColumn.



5
6
7
8
9
10
# File 'lib/admin_assistant/belongs_to_column.rb', line 5

def initialize(belongs_to_assoc, opts)
  @belongs_to_assoc = belongs_to_assoc
  @match_text_fields_in_search = opts[:match_text_fields_in_search]
  @sort_by = opts[:sort_by]
  @association_target = AssociationTarget.new associated_class
end

Instance Attribute Details

#match_text_fields_in_searchObject (readonly)

Returns the value of attribute match_text_fields_in_search.



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

def match_text_fields_in_search
  @match_text_fields_in_search
end

Instance Method Details

#add_to_query_condition(ar_query_condition, search) ⇒ Object



12
13
14
15
16
17
18
19
20
21
# File 'lib/admin_assistant/belongs_to_column.rb', line 12

def add_to_query_condition(ar_query_condition, search)
  if @match_text_fields_in_search
    add_to_query_condition_by_matching_text_fields(
      ar_query_condition, search
    )
  elsif value = search.send(association_foreign_key)
    ar_query_condition.sqls << "#{association_foreign_key} = ?"
    ar_query_condition.bind_vars << value
  end
end

#add_to_query_condition_by_matching_text_fields(ar_query_condition, search) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/admin_assistant/belongs_to_column.rb', line 23

def add_to_query_condition_by_matching_text_fields(
      ar_query_condition, search
    )
  if (value = search.send(name)) and !value.blank?
    ar_query_condition.ar_query.joins << name.to_sym
    searchable_columns = Model.new(associated_class).searchable_columns
    ar_query_condition.add_condition do |sub_cond|
      sub_cond.boolean_join = :or
      searchable_columns.each do |column|
        sub_cond.sqls <<
            "LOWER(#{associated_class.table_name}.#{column.name}) like LOWER(?)"
        sub_cond.bind_vars << "%#{value}%"
      end
    end
  end
end

#associated_classObject



40
41
42
# File 'lib/admin_assistant/belongs_to_column.rb', line 40

def associated_class
  @belongs_to_assoc.klass
end

#association_foreign_keyObject



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

def association_foreign_key
  @belongs_to_assoc.options[:foreign_key] ||
      @belongs_to_assoc.association_foreign_key
end

#attributes_for_search_object(search_params, compare_to_range) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/admin_assistant/belongs_to_column.rb', line 49

def attributes_for_search_object(search_params, compare_to_range)
  atts = {}
  if @match_text_fields_in_search
    atts[name.to_sym] = search_params[name]
  else
    terms = search_params[association_foreign_key]
    associated_id = terms.to_i unless terms.blank?
    atts[association_foreign_key.to_sym] = associated_id
    atts[name.to_sym] = if associated_id
      associated_class.find associated_id
    end
  end
  atts
end

#contains?(column_name) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/admin_assistant/belongs_to_column.rb', line 64

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

#default_name_methodObject



68
69
70
# File 'lib/admin_assistant/belongs_to_column.rb', line 68

def default_name_method
  @association_target.default_name_method
end

#nameObject



72
73
74
# File 'lib/admin_assistant/belongs_to_column.rb', line 72

def name
  @belongs_to_assoc.name.to_s
end

#order_sql_fieldObject



76
77
78
79
80
81
82
83
84
# File 'lib/admin_assistant/belongs_to_column.rb', line 76

def order_sql_field
  if @sort_by
    "#{@belongs_to_assoc.table_name}.#{@sort_by}"
  elsif default_name_method
    "#{@belongs_to_assoc.table_name}.#{default_name_method.to_s}"
  else
    "#{@belongs_to_assoc.active_record.table_name}.#{@belongs_to_assoc.association_foreign_key}"
  end
end

#value_for_search_object(search_params) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/admin_assistant/belongs_to_column.rb', line 86

def value_for_search_object(search_params)
  if @match_text_fields_in_search
    search_params[name]
  else
    terms = search_params[association_foreign_key]
    associated_id = terms.to_i unless terms.blank?
    if associated_id
      associated_class.find(associated_id)
    end
  end
end