Class: AdminAssistant::VirtualColumn

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

Defined Under Namespace

Classes: FormView, SearchView

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Column

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

Constructor Details

#initialize(name, model_class, admin_assistant) ⇒ VirtualColumn

Returns a new instance of VirtualColumn.



5
6
7
8
# File 'lib/admin_assistant/virtual_column.rb', line 5

def initialize(name, model_class, admin_assistant)
  @name, @model_class = name.to_s, model_class
  @search_settings = admin_assistant.search_settings[name]
end

Instance Attribute Details

#model_classObject (readonly)

Returns the value of attribute model_class.



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

def model_class
  @model_class
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#add_to_query_condition(ar_query_condition, search) ⇒ Object



10
11
12
13
14
# File 'lib/admin_assistant/virtual_column.rb', line 10

def add_to_query_condition(ar_query_condition, search)
  if conditions = @search_settings.conditions.call(search.send(@name))
    ar_query_condition.sqls << conditions
  end
end

#attributes_for_search_object(search_params, compare_to_range) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/admin_assistant/virtual_column.rb', line 16

def attributes_for_search_object(search_params, compare_to_range)
  raw_value = search_params[@name.to_s]
  value = if raw_value == 'true'
    true
  elsif raw_value == 'false'
    false
  elsif raw_value.blank?
    nil
  else
    raw_value
  end
  {@name => value}
end

#contains?(column_name) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#field_typeObject



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

def field_type
  @search_settings.field_type
end

#verify_for_searchObject



38
39
40
41
42
# File 'lib/admin_assistant/virtual_column.rb', line 38

def verify_for_search
  unless @search_settings.conditions
    raise "Virtual search column #{@name.to_sym.inspect} needs a conditions block"
  end
end