activerecord-hackery--ransack/lib/ransack/nodes/bindable.rb

53 lines
1.0 KiB
Ruby
Raw Normal View History

module Ransack
module Nodes
module Bindable
attr_accessor :parent, :attr_name
def attr
@attr ||= get_arel_attribute
end
alias :arel_attribute :attr
def ransacker
klass._ransackers[attr_name]
2011-04-11 16:04:31 +00:00
end
def klass
@klass ||= context.klassify(parent)
end
def bound?
attr_name.present? && parent.present?
end
def reset_binding!
@parent = @attr_name = @attr = @klass = nil
end
private
2015-09-23 14:03:04 +00:00
def get_arel_attribute
if ransacker
ransacker.attr_from(self)
else
get_attribute
end
2015-09-23 14:03:04 +00:00
end
2015-09-23 14:03:04 +00:00
def get_attribute
if is_alias_attribute?
context.table_for(parent)[parent.base_klass.attribute_aliases[attr_name]]
2015-09-23 14:03:04 +00:00
else
context.table_for(parent)[attr_name]
end
end
def is_alias_attribute?
Ransack::SUPPORTS_ATTRIBUTE_ALIAS &&
parent.base_klass.attribute_aliases.key?(attr_name)
2015-09-23 14:03:04 +00:00
end
end
end
end