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


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

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


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ransack/search.rb', line 101

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


18
19
20
# File 'lib/ransack/search.rb', line 18

def base
  @base
end

#contextObject (readonly)

Returns the value of attribute context


18
19
20
# File 'lib/ransack/search.rb', line 18

def context
  @context
end

Instance Method Details

#build(params) ⇒ Object


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

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


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

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

#inspectObject


117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ransack/search.rb', line 117

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


97
98
99
# File 'lib/ransack/search.rb', line 97

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

#result(opts = {}) ⇒ Object


42
43
44
# File 'lib/ransack/search.rb', line 42

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

#sortsObject Also known as: s


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

def sorts
  @sorts
end

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


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/ransack/search.rb', line 61

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