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, #model_name, #persisted?, #to_key, #to_model, #to_param

Constructor Details

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

Returns a new instance of Search.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ransack/search.rb', line 17

def initialize(object, params = {}, options = {})
  params = params.to_unsafe_h if params.respond_to?(:to_unsafe_h)
  if params.is_a? Hash
    params = params.dup
    params.delete_if { |k, v| [*v].all?{ |i| i.blank? && i != false } }
  else
    params = {}
  end
  @context = options[:context] || Context.for(object, options)
  @context.auth_object = options[:auth_object]
  @base = Nodes::Grouping.new(
    @context, options[:grouping] || Constants::AND
    )
  @scope_args = {}
  @sorts ||= []
  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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/ransack/search.rb', line 94

def method_missing(method_id, *args)
  method_name = method_id.to_s
  getter_name = method_name.sub(/=$/, ''.freeze)
  if base.attribute_method?(getter_name)
    base.send(method_id, *args)
  elsif @context.ransackable_scope?(getter_name, @context.object)
    if method_name =~ /=$/
      add_scope getter_name, args
    else
      @scope_args[method_name]
    end
  else
    super
  end
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



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

def base
  @base
end

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

Instance Method Details

#build(params) ⇒ Object



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

def build(params)
  collapse_multiparameter_attributes!(params).each do |key, value|
    if ['s'.freeze, 'sorts'.freeze].freeze.include?(key)
      send("#{key}=", value)
    elsif base.attribute_method?(key)
      base.send("#{key}=", value)
    elsif @context.ransackable_scope?(key, @context.object)
      add_scope(key, value)
    elsif !Ransack.options[:ignore_unknown_conditions]
      raise ArgumentError, "Invalid search term #{key}"
    end
  end
  self
end

#build_sort(opts = {}) ⇒ Object



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

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

#inspectObject



110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ransack/search.rb', line 110

def inspect
  details = [
    [:class, klass.name],
    ([:scope, @scope_args] if @scope_args.present?),
    [:base, base.inspect]
  ]
  .compact
  .map { |d| d.join(': '.freeze) }
  .join(', '.freeze)

  "Ransack::Search<#{details}>"
end

#new_sort(opts = {}) ⇒ Object



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

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

#result(opts = {}) ⇒ Object



35
36
37
# File 'lib/ransack/search.rb', line 35

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

#sortsObject Also known as: s



79
80
81
# File 'lib/ransack/search.rb', line 79

def sorts
  @sorts
end

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/ransack/search.rb', line 54

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