class Ransack::Nodes::Grouping

Attributes

c[R]
combinator[RW]
conditions[R]
m[RW]
m=[RW]

Public Class Methods

new(context, combinator = nil) click to toggle source
Calls superclass method
# File lib/ransack/nodes/grouping.rb, line 14
def initialize(context, combinator = nil)
  super(context)
  self.combinator = combinator.to_s if combinator
end

Public Instance Methods

[](key) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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
c=(conditions)
Alias for: conditions=
conditions=(conditions) click to toggle source
# File lib/ransack/nodes/grouping.rb, line 34
def conditions=(conditions)
  case conditions
  when Array
    conditions.each do |attrs|
      condition = Condition.new(@context).build(attrs)
      self.conditions << condition if condition.valid?
    end
  when Hash
    conditions.each do |index, attrs|
      condition = Condition.new(@context).build(attrs)
      self.conditions << condition if condition.valid?
    end
  end
  self.conditions.uniq!
end
Also aliased as: c=
g()
Alias for: groupings
g=(groupings)
Alias for: groupings=
groupings() click to toggle source
# File lib/ransack/nodes/grouping.rb, line 91
def groupings
  @groupings ||= []
end
Also aliased as: g
groupings=(groupings) click to toggle source
# 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
Also aliased as: g=
inspect() click to toggle source
# 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
method_missing(method_id, *args) click to toggle source
Calls superclass method
# 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
new_condition(opts = {}) click to toggle source
# 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 = {}) click to toggle source
# File lib/ransack/nodes/grouping.rb, line 150
def new_grouping(params = {})
  Grouping.new(@context).build(params)
end
persisted?() click to toggle source
# File lib/ransack/nodes/grouping.rb, line 19
def persisted?
  false
end
respond_to?(method_id) click to toggle source
Calls superclass method
# 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 = {}) click to toggle source
Calls superclass method
# File lib/ransack/nodes/grouping.rb, line 23
def translate(key, options = {})
  super or Translate.attribute(
    key.to_s, options.merge(:context => context)
  )
end
values() click to toggle source
# File lib/ransack/nodes/grouping.rb, line 64
def values
  conditions + groupings
end

Private Instance Methods

read_attribute(name) click to toggle source
# File lib/ransack/nodes/grouping.rb, line 185
def read_attribute(name)
  if self[name].respond_to?(:value)
    self[name].value
  else
    self[name]
  end
end
strip_predicate_and_index(str) click to toggle source
# File lib/ransack/nodes/grouping.rb, line 193
def strip_predicate_and_index(str)
  string = str.split(/\(/).first
  Predicate.detect_and_strip_from_string!(string)
  string
end
write_attribute(name, val) click to toggle source
# File lib/ransack/nodes/grouping.rb, line 178
def write_attribute(name, val)
  # TODO: Methods
  if condition = Condition.extract(@context, name, val)
    self[name] = condition
  end
end