Class: Ransack::Nodes::Grouping

Inherits:
Node
  • Object
show all
Defined in:
lib/ransack/nodes/grouping.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#context

Instance Method Summary collapse

Methods inherited from Node

i18n_alias, i18n_word

Constructor Details

#initialize(context, combinator = nil) ⇒ Grouping


14
15
16
17
# File 'lib/ransack/nodes/grouping.rb', line 14

def initialize(context, combinator = nil)
  super(context)
  self.combinator = combinator.to_s if combinator
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args) ⇒ Object


115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ransack/nodes/grouping.rb', line 115

def method_missing(method_id, *args)
  method_name = method_id.to_s
  writer = method_name.sub!(/\=$/, Constants::EMPTY)
  if attribute_method?(method_name)
    if writer
      write_attribute(method_name, *args)
    else
      read_attribute(method_name)
    end
  else
    super
  end
end

Instance Attribute Details

#combinatorObject Also known as: m

Returns the value of attribute combinator


5
6
7
# File 'lib/ransack/nodes/grouping.rb', line 5

def combinator
  @combinator
end

#conditionsObject Also known as: c

Returns the value of attribute conditions


4
5
6
# File 'lib/ransack/nodes/grouping.rb', line 4

def conditions
  @conditions
end

Instance Method Details

#[](key) ⇒ Object


51
52
53
54
55
56
57
# File 'lib/ransack/nodes/grouping.rb', line 51

def [](key)
  if condition = conditions.detect { |c| c.key == key.to_s }
    condition
  else
    nil
  end
end

#[]=(key, value) ⇒ Object


59
60
61
62
# File 'lib/ransack/nodes/grouping.rb', line 59

def []=(key, value)
  conditions.reject! { |c| c.key == key.to_s }
  self.conditions << value
end

#attribute_method?(name) ⇒ Boolean


129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ransack/nodes/grouping.rb', line 129

def attribute_method?(name)
  stripped_name = strip_predicate_and_index(name)
  return true if @context.attribute_method?(stripped_name) ||
                 @context.attribute_method?(name)
  case stripped_name
  when /^(g|c|m|groupings|conditions|combinator)=?$/
    true
  else
    stripped_name
    .split(/_and_|_or_/)
    .none? { |n| !@context.attribute_method?(n) }
  end
end

#build(params) ⇒ Object


154
155
156
157
158
159
160
161
162
163
164
# File 'lib/ransack/nodes/grouping.rb', line 154

def build(params)
  params.with_indifferent_access.each do |key, value|
    case key
    when /^(g|c|m)$/
      self.send("#{key}=", value)
    else
      write_attribute(key.to_s, value)
    end
  end
  self
end

#build_condition(opts = {}) ⇒ Object


76
77
78
79
80
# File 'lib/ransack/nodes/grouping.rb', line 76

def build_condition(opts = {})
  new_condition(opts).tap do |condition|
    self.conditions << condition
  end
end

#build_grouping(params = {}) ⇒ Object


143
144
145
146
147
148
# File 'lib/ransack/nodes/grouping.rb', line 143

def build_grouping(params = {})
  params ||= {}
  new_grouping(params).tap do |new_grouping|
    self.groupings << new_grouping
  end
end

#groupingsObject Also known as: g


91
92
93
# File 'lib/ransack/nodes/grouping.rb', line 91

def groupings
  @groupings ||= []
end

#groupings=(groupings) ⇒ Object Also known as: g=


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ransack/nodes/grouping.rb', line 96

def groupings=(groupings)
  case groupings
  when Array
    groupings.each do |attrs|
      grouping_object = new_grouping(attrs)
      self.groupings << grouping_object if grouping_object.values.any?
    end
  when Hash
    groupings.each do |index, attrs|
      grouping_object = new_grouping(attrs)
      self.groupings << grouping_object if grouping_object.values.any?
    end
  else
    raise ArgumentError,
      "Invalid argument (#{groupings.class}) supplied to groupings="
  end
end

#inspectObject


166
167
168
169
170
171
172
173
174
# File 'lib/ransack/nodes/grouping.rb', line 166

def inspect
  data = [
    ['conditions'.freeze, conditions], [Constants::COMBINATOR, combinator]
  ]
  .reject { |e| e[1].blank? }
  .map { |v| "#{v[0]}: #{v[1]}" }
  .join(Constants::COMMA_SPACE)
  "Grouping <#{data}>"
end

#new_condition(opts = {}) ⇒ Object


82
83
84
85
86
87
88
89
# File 'lib/ransack/nodes/grouping.rb', line 82

def new_condition(opts = {})
  attrs = opts[:attributes] || 1
  vals = opts[:values] || 1
  condition = Condition.new(@context)
  attrs.times { condition.build_attribute }
  vals.times { condition.build_value }
  condition
end

#new_grouping(params = {}) ⇒ Object


150
151
152
# File 'lib/ransack/nodes/grouping.rb', line 150

def new_grouping(params = {})
  Grouping.new(@context).build(params)
end

#persisted?Boolean


19
20
21
# File 'lib/ransack/nodes/grouping.rb', line 19

def persisted?
  false
end

#respond_to?(method_id) ⇒ Boolean


68
69
70
71
72
73
74
# File 'lib/ransack/nodes/grouping.rb', line 68

def respond_to?(method_id)
  super or begin
    method_name = method_id.to_s
    writer = method_name.sub!(/\=$/, Constants::EMPTY)
    attribute_method?(method_name) ? true : false
  end
end

#translate(key, options = {}) ⇒ Object


23
24
25
26
27
# File 'lib/ransack/nodes/grouping.rb', line 23

def translate(key, options = {})
  super or Translate.attribute(
    key.to_s, options.merge(:context => context)
  )
end

#valuesObject


64
65
66
# File 'lib/ransack/nodes/grouping.rb', line 64

def values
  conditions + groupings
end