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

Returns a new instance of 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



114
115
116
117
118
119
120
121
122
# File 'lib/ransack/nodes/grouping.rb', line 114

def method_missing(method_id, *args)
  method_name = method_id.to_s
  writer = method_name.sub!(/\=$/, '')
  if attribute_method?(method_name)
    writer ? write_attribute(method_name, *args) : read_attribute(method_name)
  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



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

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

#[]=(key, value) ⇒ Object



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

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

#attribute_method?(name) ⇒ Boolean

Returns:

  • (Boolean)


124
125
126
127
128
129
130
131
132
# File 'lib/ransack/nodes/grouping.rb', line 124

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

#build(params) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/ransack/nodes/grouping.rb', line 145

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



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

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

#build_grouping(params = {}) ⇒ Object



134
135
136
137
138
139
# File 'lib/ransack/nodes/grouping.rb', line 134

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
# 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



157
158
159
160
161
162
# File 'lib/ransack/nodes/grouping.rb', line 157

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

#new_condition(opts = {}) ⇒ Object



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

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

#new_grouping(params = {}) ⇒ Object



141
142
143
# File 'lib/ransack/nodes/grouping.rb', line 141

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

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  false
end

#respond_to?(method_id) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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



23
24
25
# 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



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

def values
  conditions + groupings
end