Skip to content

Commit

Permalink
Refactor how to get singleton class
Browse files Browse the repository at this point in the history
  • Loading branch information
YOSHIDA Hiroki committed Jul 24, 2014
1 parent 25f4bd3 commit 17570f1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
16 changes: 5 additions & 11 deletions lib/utusemi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def enable
# for model and relation
this.prepend_to_activerecord_base
this.prepend_to_activerecord_relation
this.prepend_to_activerecord_eigenclass
this.prepend_to_activerecord_singleton_class
this.prepend_to_activerecord_associations_hasmanyassociation
this.prepend_to_activerecord_associations_collectionproxy
end
Expand Down Expand Up @@ -43,12 +43,12 @@ def prepend_to_activerecord_relation
ActiveRecord::Relation.send(:prepend, Core::ActiveRecord::Relation)
end

def prepend_to_activerecord_eigenclass
activerecord_eigenclass.send(:prepend, Core::ActiveRecord::Base::ClassMethods)
def prepend_to_activerecord_singleton_class
ActiveRecord::Base.singleton_class.send(:prepend, Core::ActiveRecord::Base::ClassMethods)
# for rails 3.x
activerecord_eigenclass.send(:prepend, Core::ActiveRecord::RelationMethod) if Rails::VERSION::MAJOR == 3
ActiveRecord::Base.singleton_class.send(:prepend, Core::ActiveRecord::RelationMethod) if Rails::VERSION::MAJOR == 3
# for association
activerecord_eigenclass.send(:prepend, Core::ActiveRecord::AssociationMethods)
ActiveRecord::Base.singleton_class.send(:prepend, Core::ActiveRecord::AssociationMethods)
end

def prepend_to_activerecord_associations_hasmanyassociation
Expand All @@ -58,11 +58,5 @@ def prepend_to_activerecord_associations_hasmanyassociation
def prepend_to_activerecord_associations_collectionproxy
ActiveRecord::Associations::CollectionProxy.send(:prepend, Core::ActiveRecord::CollectionProxy) if Rails::VERSION::MAJOR == 3
end

private

def activerecord_eigenclass
ActiveRecord::Base.class_eval { class << self; self; end }
end
end
end
3 changes: 1 addition & 2 deletions lib/utusemi/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,7 @@ def initialize_copy(original_obj)
def association(name)
truthly_owner = self
association = super
eigenclass = class << association; self; end
eigenclass.send(:define_method, :truthly_owner) { truthly_owner }
association.singleton_class.send(:define_method, :truthly_owner) { truthly_owner }
association
end
end
Expand Down

0 comments on commit 17570f1

Please sign in to comment.