Class: Ransack::Search

Inherits:
Object
  • Object
show all
Includes:
Naming
Defined in:
lib/ransack/search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Naming

included, #persisted?, #to_key, #to_model, #to_param

Constructor Details

#initialize(object, params = {}, options = {}) ⇒ Search

Returns a new instance of Search.



16
17
18
19
20
21
22
# File 'lib/ransack/search.rb', line 16

def initialize(object, params = {}, options = {})
  params ||= {}
  @context = Context.for(object, options)
  @context.auth_object = options[:auth_object]
  @base = Nodes::Grouping.new(@context, 'and')
  build(params.with_indifferent_access)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/ransack/search.rb', line 89

def method_missing(method_id, *args)
  method_name = method_id.to_s
  writer = method_name.sub!(/\=$/, '')
  if base.attribute_method?(method_name)
    base.send(method_id, *args)
  else
    super
  end
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



9
10
11
# File 'lib/ransack/search.rb', line 9

def base
  @base
end

#contextObject (readonly)

Returns the value of attribute context.



9
10
11
# File 'lib/ransack/search.rb', line 9

def context
  @context
end

Instance Method Details

#build(params) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ransack/search.rb', line 34

def build(params)
  collapse_multiparameter_attributes!(params).each do |key, value|
    case key
    when 's', 'sorts'
      send("#{key}=", value)
    else
      base.send("#{key}=", value) if base.attribute_method?(key)
    end
  end
  self
end

#build_sort(opts = {}) ⇒ Object



71
72
73
74
75
# File 'lib/ransack/search.rb', line 71

def build_sort(opts = {})
  new_sort(opts).tap do |sort|
    self.sorts << sort
  end
end

#inspectObject



28
29
30
31
32
# File 'lib/ransack/search.rb', line 28

def inspect
  # Replace the inspect method on object's singleton class to call to_s() instead of to_a().
  object.class_eval { alias :inspect :to_s }
  super
end

#new_sort(opts = {}) ⇒ Object



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

def new_sort(opts = {})
  Nodes::Sort.new(@context).build(opts)
end

#respond_to?(method_id, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to?(method_id, include_private = false)
  super or begin
    method_name = method_id.to_s
    writer = method_name.sub!(/\=$/, '')
    base.attribute_method?(method_name) ? true : false
  end
end

#result(opts = {}) ⇒ Object



24
25
26
# File 'lib/ransack/search.rb', line 24

def result(opts = {})
  @context.evaluate(self, opts)
end

#sortsObject Also known as: s



66
67
68
# File 'lib/ransack/search.rb', line 66

def sorts
  @sorts ||= []
end

#sorts=(args) ⇒ Object Also known as: s=



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ransack/search.rb', line 46

def sorts=(args)
  case args
  when Array
    args.each do |sort|
      sort = Nodes::Sort.extract(@context, sort)
      self.sorts << sort
    end
  when Hash
    args.each do |index, attrs|
      sort = Nodes::Sort.new(@context).build(attrs)
      self.sorts << sort
    end
  when String
    self.sorts = [args]
  else
    raise ArgumentError, "Invalid argument (#{args.class}) supplied to sorts="
  end
end