Class: AdminAssistant::PolymorphicBelongsToColumn

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

Defined Under Namespace

Classes: 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(belongs_to_assoc) ⇒ PolymorphicBelongsToColumn

Returns a new instance of PolymorphicBelongsToColumn.



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

def initialize(belongs_to_assoc)
  @belongs_to_assoc = belongs_to_assoc
end

Instance Method Details

#add_to_query_condition(ar_query_condition, search) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/admin_assistant/polymorphic_belongs_to_column.rb', line 7

def add_to_query_condition(ar_query_condition, search)
  kv = (key_value = search.send(association_foreign_key)) && 
       !key_value.blank?
  tv = (type_value = search.send(foreign_type_field)) &&
       !type_value.blank?
  if kv and tv
    ar_query_condition.add_condition do |subcond|
      subcond.boolean_join = :and
      subcond.sqls << "#{association_foreign_key} = ?"
      subcond.bind_vars << key_value
      subcond.sqls << "#{foreign_type_field} = ?"
      subcond.bind_vars << type_value
    end
  end
end

#association_foreign_keyObject



23
24
25
# File 'lib/admin_assistant/polymorphic_belongs_to_column.rb', line 23

def association_foreign_key
  @belongs_to_assoc.association_foreign_key
end

#attributes_for_search_object(search_params, compare_to_range) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/admin_assistant/polymorphic_belongs_to_column.rb', line 27

def attributes_for_search_object(search_params, compare_to_range)
  atts = {}
  atts[association_foreign_key.to_sym] = 
      search_params[association_foreign_key]
  atts[foreign_type_field.to_sym] = search_params[foreign_type_field]
  if !atts[foreign_type_field.to_sym].blank?
    atts[name.to_sym] = Module.const_get(
      search_params[foreign_type_field]
    ).find_by_id(search_params[association_foreign_key])
  else
    atts[name.to_sym] = nil
  end
  atts
end

#contains?(column_name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#foreign_type_fieldObject



46
47
48
49
50
51
52
# File 'lib/admin_assistant/polymorphic_belongs_to_column.rb', line 46

def foreign_type_field
  if @belongs_to_assoc.respond_to?(:foreign_type)
    @belongs_to_assoc.foreign_type
  else
    @belongs_to_assoc.options[:foreign_type]
  end
end

#match_text_fields_in_searchObject



54
55
56
# File 'lib/admin_assistant/polymorphic_belongs_to_column.rb', line 54

def match_text_fields_in_search
  false
end

#nameObject



58
59
60
# File 'lib/admin_assistant/polymorphic_belongs_to_column.rb', line 58

def name
  @belongs_to_assoc.name.to_s
end