Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix problems for Active Record Association on Rails 3 #2

Merged
merged 3 commits into from
Jul 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/utusemi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def enable
this.prepend_to_activerecord_relation
this.prepend_to_activerecord_eigenclass
this.prepend_to_activerecord_associations_hasmanyassociation
this.prepend_to_activerecord_associations_collectionproxy
end
end

Expand Down Expand Up @@ -54,6 +55,10 @@ def prepend_to_activerecord_associations_hasmanyassociation
ActiveRecord::Associations::HasManyAssociation.send(:prepend, Core::ActiveRecord::Associations)
end

def prepend_to_activerecord_associations_collectionproxy
ActiveRecord::Associations::CollectionProxy.send(:prepend, Core::ActiveRecord::CollectionProxy) if Rails::VERSION::MAJOR == 3
end

private

def activerecord_eigenclass
Expand Down
24 changes: 12 additions & 12 deletions lib/utusemi/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,18 @@ def warning_checker
#
module RelationMethod
def relation(*args, &block)
return super unless current_scope
return super unless current_scope.utusemi_values
return super unless current_scope.utusemi_values[:flag]
super.utusemi(current_scope.utusemi_values[:type], utusemi_values[:options])
utusemi_values = current_scope.try(:utusemi_values) || {}
return super unless utusemi_values[:flag]
super.utusemi(utusemi_values[:type], utusemi_values[:options])
end
end

module CollectionProxy
def scoped(*args, &block)
association = @association
utusemi_values = association.truthly_owner.utusemi_values
return super unless utusemi_values[:flag]
super.utusemi!(association.klass.model_name.singular, utusemi_values[:options])
end
end

Expand Down Expand Up @@ -343,18 +351,10 @@ def utusemi_association(association_type, name, *args)
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
default_scope.merge(scope)
end

def define_utusemi_association_reader(name, options = {})
return if method_defined?("#{name}_with_utusemi")
define_method "#{name}_with_utusemi" do |*args|
Expand Down
2 changes: 2 additions & 0 deletions spec/dummy/app/models/stock.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class Stock < ActiveRecord::Base
belongs_to :product

scope :unsold, -> { where('quantity > ?', 0) }
end
9 changes: 9 additions & 0 deletions spec/lib/utusemi/core_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@
it { should respond_to(:quantity) }
it { expect(subject.units).to eq(subject.quantity) }
end

describe 'named scope of associations' do
let(:product) { FactoryGirl.create(:product, :with_stock) }
subject { product.utusemi(:product).stocks.unsold }
it { expect(subject.count).to eq(1) }
it { expect(subject.first).to respond_to(:units) }
it { expect(subject.first).to respond_to(:quantity) }
it { expect(subject.first.units).to eq(subject.first.quantity) }
end
end

describe ActiveRecord::Base::ClassMethods do
Expand Down