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

def initialize(object, params = {}, options = {})
  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



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

def method_missing(method_id, *args)
  method_name = method_id.to_s
  getter_name = method_name.sub(/=$/, Constants::EMPTY)
  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



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

def build(params)
  collapse_multiparameter_attributes!(params).each do |key, value|
    if Constants::S_SORTS.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



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

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

#inspectObject



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

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

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

#new_sort(opts = {}) ⇒ Object



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

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

#result(opts = {}) ⇒ Object



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

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

#sortsObject Also known as: s



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

def sorts
  @sorts
end

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



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

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