Class: AdminAssistant::Search

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(admin_assistant, params) ⇒ Search

Returns a new instance of Search.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/admin_assistant/search.rb', line 5

def initialize(admin_assistant, params)
  @admin_assistant, @params = admin_assistant, params
  @params ||= {}
  @attributes = HashWithIndifferentAccess.new
  columns.each do |c|
    c.verify_for_search
    compare_to_range = compare_to_range?(c.name) if c.respond_to?(:name)
    attributes = c.attributes_for_search_object(@params, compare_to_range)
    attributes.each do |key, value|
      @attributes[key] = value
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/admin_assistant/search.rb', line 81

def method_missing(meth, *args)
  if @attributes.has_key?(meth)
    @attributes[meth]
  else
    super
  end
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

Instance Method Details

#[](name) ⇒ Object



19
20
21
# File 'lib/admin_assistant/search.rb', line 19

def [](name)
  @params[name]
end

#add_to_query(ar_query) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/admin_assistant/search.rb', line 23

def add_to_query(ar_query)
  unless @params.empty?
    ar_query.add_condition do |cond|
      if match_any_conditions?
        cond.boolean_join = :or
      end
      columns.each do |column|
        column.add_to_query_condition cond, self
      end
    end
  end
end

#blank?(column_name) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/admin_assistant/search.rb', line 36

def blank?(column_name)
  @params["#{column_name}(blank)"] == '1'
end

#column_views(action_view) ⇒ Object



54
55
56
57
58
# File 'lib/admin_assistant/search.rb', line 54

def column_views(action_view)
  columns.map { |c|
    c.search_view action_view, @admin_assistant, :search => self
  }
end

#columnsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/admin_assistant/search.rb', line 40

def columns
  column_names = settings.column_names
  if column_names.empty?
    [DefaultSearchColumn.new(
      @admin_assistant.model_class,
      :fields_to_match => @admin_assistant.default_search_matches_on
    )]
  else
    column_names.map { |column_name| 
      @admin_assistant.column(column_name.to_s)
    }
  end
end

#comparator(column_name) ⇒ Object



60
61
62
63
# File 'lib/admin_assistant/search.rb', line 60

def comparator(column_name)
  c = @params["#{column_name}(comparator)"]
  c if %w(< <= = >= >).include?(c)
end

#compare_to_range?(column_name) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/admin_assistant/search.rb', line 65

def compare_to_range?(column_name)
  settings[column_name].compare_to_range
end

#idObject



69
70
71
# File 'lib/admin_assistant/search.rb', line 69

def id
  @attributes[:id] if @attributes.has_key?(:id)
end

#match_all_conditions?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/admin_assistant/search.rb', line 73

def match_all_conditions?
  !match_any_conditions?
end

#match_any_conditions?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/admin_assistant/search.rb', line 77

def match_any_conditions?
  @params["(all_or_any)"] == 'any'
end

#model_classObject



89
90
91
# File 'lib/admin_assistant/search.rb', line 89

def model_class
  @admin_assistant.model_class
end

#settingsObject



93
94
95
# File 'lib/admin_assistant/search.rb', line 93

def settings
  @admin_assistant.search_settings
end