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
23
# File 'lib/ransack/search.rb', line 16

def initialize(object, params = {}, options = {})
  params = {} unless params.is_a?(Hash)
  params.delete_if { |k, v| v.blank? && v != false }
  @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



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

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



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ransack/search.rb', line 29

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



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

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

#inspectObject



90
91
92
# File 'lib/ransack/search.rb', line 90

def inspect
  "Ransack::Search<class: #{klass.name}, base: #{base.inspect}>"
end

#new_sort(opts = {}) ⇒ Object



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

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

#result(opts = {}) ⇒ Object



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

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

#sortsObject Also known as: s



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

def sorts
  @sorts ||= []
end

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



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

def sorts=(args)
  case args
  when Array
    args.each do |sort|
      if sort.kind_of? Hash
        sort = Nodes::Sort.new(@context).build(sort)
      else
        sort = Nodes::Sort.extract(@context, sort)
      end
      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