Skip to content

Commit

Permalink
Rails 3.x: Fix to work tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
YOSHIDA Hiroki committed Jul 15, 2014
1 parent c1cf9d0 commit b1f968d
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions lib/utusemi/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,28 +314,16 @@ def load_target(*args)
end

module AssociationMethods
def belongs_to(name, scope = nil, options = {})
check_deplicated_association_warning(:belongs_to, name, scope)
utusemi_flag = scope.try(:delete, :utusemi)
scope = utusemi_association_scope(:belongs_to, name, scope) if utusemi_flag
super if !utusemi_flag || !method_defined?(name)
define_utusemi_association_reader(name, utusemi_flag => true)
def belongs_to(name, *args)
utusemi_association(:belongs_to, name, *args) { |*a| super(*a) }
end

def has_one(name, scope = nil, options = {})
check_deplicated_association_warning(:has_one, name, scope)
utusemi_flag = scope.try(:delete, :utusemi)
scope = utusemi_association_scope(:has_one, name, scope) if utusemi_flag
super if !utusemi_flag || !method_defined?(name)
define_utusemi_association_reader(name, utusemi_flag => true)
def has_one(name, *args)
utusemi_association(:has_one, name, *args) { |*a| super(*a) }
end

def has_many(name, scope = nil, options = {}, &extension)
check_deplicated_association_warning(:has_many, name, scope)
utusemi_flag = scope.try(:delete, :utusemi)
scope = utusemi_association_scope(:has_many, name, scope) if utusemi_flag
super if !utusemi_flag || !method_defined?(name)
define_utusemi_association_reader(name, utusemi_flag => true)
def has_many(name, *args)
utusemi_association(:has_many, name, *args) { |*a| super(*a) }
end

private
Expand All @@ -347,7 +335,20 @@ def check_deplicated_association_warning(association_type, name, scope)
Rails.logger.warn "[Utusemi:WARNING] \"#{association_type} :#{name}\" is duplicated in #{self.name}."
end

def utusemi_association_scope(method_name, name, scope = {})
def utusemi_association(association_type, name, *args)
if args.empty?
yield name, *args
return define_utusemi_association_reader(name)
end
scope = args.shift
check_deplicated_association_warning(association_type, name, scope)
utusemi_flag = scope.try(:delete, :utusemi)
scope = utusemi_association_scope(association_type, name, scope) if utusemi_flag
yield name, scope, *args if !utusemi_flag || !method_defined?(name)
define_utusemi_association_reader(name, utusemi_flag => true)
end

def utusemi_association_scope(method_name, name, scope)
utusemi_map = Utusemi.config.map(name.to_s.singularize)
default_scope = { class_name: utusemi_map.class_name }
default_scope[:foreign_key] = utusemi_map.foreign_key if method_name == :belongs_to
Expand Down

0 comments on commit b1f968d

Please sign in to comment.