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



83
84
85
86
87
88
89
90
91
# File 'lib/ransack/search.rb', line 83

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



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

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



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

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

#new_sort(opts = {}) ⇒ Object



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

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

#respond_to?(method_id) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
# File 'lib/ransack/search.rb', line 75

def respond_to?(method_id)
  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



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

def sorts
  @sorts ||= []
end

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



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

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