Skip to content

Commit

Permalink
Refactor: condition handles binding of its attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
avit committed Feb 12, 2016
1 parent 3258d25 commit 235eae3
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/ransack/adapters/active_record/ransack/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def initialize(object, options = {})
@base.table_name, as: @base.aliased_table_name, type_caster: self
)
@bind_pairs = Hash.new do |hash, key|
parent, attr_name = get_parent_and_attribute_name(key.to_s)
parent, attr_name = get_parent_and_attribute_name(key)
if parent && attr_name
hash[key] = [parent, attr_name]
end
Expand Down
1 change: 1 addition & 0 deletions lib/ransack/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def scope_arity(scope)
end

def bind(object, str)
return nil unless str
object.parent, object.attr_name = @bind_pairs[str]
end

Expand Down
1 change: 0 additions & 1 deletion lib/ransack/nodes/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def initialize(context, name = nil, ransacker_args = [])

def name=(name)
@name = name
context.bind(self, name) unless name.blank?
end

def valid?
Expand Down
15 changes: 7 additions & 8 deletions lib/ransack/nodes/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,12 @@ def attributes
def attributes=(args)
case args
when Array
args.each do |attr|
attr = Attribute.new(@context, attr)
self.attributes << attr if attr.valid?
args.each do |name|
build_attribute(name)
end
when Hash
args.each do |index, attrs|
attr = Attribute.new(@context, attrs[:name], attrs[:ransacker_args])
self.attributes << attr if attr.valid?
build_attribute(attrs[:name], attrs[:ransacker_args])
end
else
raise ArgumentError,
Expand Down Expand Up @@ -129,9 +127,10 @@ def combinator=(val)
alias :m= :combinator=
alias :m :combinator

def build_attribute(name = nil)
Attribute.new(@context, name).tap do |attribute|
self.attributes << attribute
def build_attribute(name = nil, ransacker_args = [])
Attribute.new(@context, name, ransacker_args).tap do |attribute|
@context.bind(attribute, attribute.name)
self.attributes << attribute if attribute.valid?
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/ransack/nodes/sort.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def valid?

def name=(name)
@name = name
context.bind(self, name) unless name.blank?
context.bind(self, name)
end

def dir=(dir)
Expand Down

0 comments on commit 235eae3

Please sign in to comment.