Skip to content

Commit

Permalink
use FriendlyId pattern for overriding relation method in order to avo…
Browse files Browse the repository at this point in the history
…id conflict, should solve issue in globalizejs#288.
  • Loading branch information
shioyama committed Nov 5, 2013
1 parent 885ec67 commit a370308
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions lib/globalize/active_record/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,6 @@ def translated_column_name(name)
"#{translation_class.table_name}.#{name}"
end

def relation
relation = Relation.new(self, arel_table)

if finder_needs_type_condition?
relation.where(type_condition).create_with(inheritance_column.to_sym => sti_name)
else
relation
end
end

def respond_to_missing?(method_id, include_private = false)
supported_on_missing?(method_id) || super
end
Expand Down Expand Up @@ -135,7 +125,27 @@ def find_or_instantiator_by_attributes(match, attributes, *args)
record
end

protected
private

# Override the default relation method in order to return a subclass
# of ActiveRecord::Relation with custom finder methods for translated
# attributes.
def relation
relation = relation_class.new(self, arel_table)

if finder_needs_type_condition?
relation.where(type_condition).create_with(inheritance_column.to_sym => sti_name)
else
relation
end
end

# Use pattern defined in FriendlyId (4.x) to avoid conflict.
def relation_class
Relation
end

protected

def translated_attr_accessor(name)
define_method(:"#{name}=") do |value|
Expand Down

0 comments on commit a370308

Please sign in to comment.