From 461c4fe9f2e6751136e8f8d92c86fe6f36d04728 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Mon, 17 Oct 2011 20:55:22 -0300 Subject: [PATCH] Replacing Bsc products by a has_many association --- plugins/bsc/lib/bsc_plugin/bsc.rb | 6 +----- plugins/bsc/test/unit/bsc_plugin/bsc_test.rb | 18 +++--------------- vendor/plugins/will_paginate_check_finder_sql/init.rb | 18 ++++++++++++++++++ 3 files changed, 22 insertions(+), 20 deletions(-) create mode 100644 vendor/plugins/will_paginate_check_finder_sql/init.rb diff --git a/plugins/bsc/lib/bsc_plugin/bsc.rb b/plugins/bsc/lib/bsc_plugin/bsc.rb index 4991795..6907739 100644 --- a/plugins/bsc/lib/bsc_plugin/bsc.rb +++ b/plugins/bsc/lib/bsc_plugin/bsc.rb @@ -2,6 +2,7 @@ class BscPlugin::Bsc < Enterprise has_many :enterprises has_many :enterprise_requests, :class_name => 'BscPlugin::AssociateEnterprise' + has_many :products, :finder_sql => 'select * from products where enterprise_id in (#{enterprises.map(&:id).join(",")})' validates_presence_of :nickname validates_presence_of :company_name @@ -26,11 +27,6 @@ class BscPlugin::Bsc < Enterprise {:title => _('Bsc info and settings'), :icon => 'edit-profile-enterprise'} end - def products(reload_flag=false) - reload if reload_flag - enterprises.map { |enterprise| enterprise.products }.flatten - end - def create_product? false end diff --git a/plugins/bsc/test/unit/bsc_plugin/bsc_test.rb b/plugins/bsc/test/unit/bsc_plugin/bsc_test.rb index d81b376..6ef248f 100644 --- a/plugins/bsc/test/unit/bsc_plugin/bsc_test.rb +++ b/plugins/bsc/test/unit/bsc_plugin/bsc_test.rb @@ -46,7 +46,7 @@ class BscPlugin::BscTest < Test::Unit::TestCase e1 = fast_create(Enterprise) e2 = fast_create(Enterprise) category = fast_create(ProductCategory) - bsc = BscPlugin::Bsc.new() + bsc = BscPlugin::Bsc.create!({:business_name => 'Sample Bsc', :identifier => 'sample-bsc', :company_name => 'Sample Bsc Ltda.', :cnpj => VALID_CNPJ}) p1 = fast_create(Product, :product_category_id => category.id) p2 = fast_create(Product, :product_category_id => category.id) @@ -59,25 +59,13 @@ class BscPlugin::BscTest < Test::Unit::TestCase bsc.enterprises << e1 bsc.enterprises << e2 + bsc.reload + assert_includes bsc.products, p1 assert_includes bsc.products, p2 assert_includes bsc.products, p3 end - should 'reload products' do - e = fast_create(Enterprise) - category = fast_create(ProductCategory) - bsc = BscPlugin::Bsc.create!(:business_name => 'Sample Bsc', :company_name => 'Sample Bsc', :identifier => 'sample-bsc', :cnpj => VALID_CNPJ) - p = fast_create(Product, :product_category_id => category.id) - - e.bsc = bsc - e.save! - e.products << p - - assert_equal [], bsc.products - assert_equal [p], bsc.products(true) - end - should 'not be able to create product' do bsc = BscPlugin::Bsc.new assert !bsc.create_product? diff --git a/vendor/plugins/will_paginate_check_finder_sql/init.rb b/vendor/plugins/will_paginate_check_finder_sql/init.rb new file mode 100644 index 0000000..5d312e9 --- /dev/null +++ b/vendor/plugins/will_paginate_check_finder_sql/init.rb @@ -0,0 +1,18 @@ +# monkey patch to fix WillPaginate bug +# this was solved in will_paginate 3.x.pre, then remove this patch when upgrade to it +# +# http://sod.lighthouseapp.com/projects/17958/tickets/120-paginate-association-with-finder_sql-raises-typeerror +require_dependency 'will_paginate' + +WillPaginate::Finder::ClassMethods.module_eval do + def paginate_with_finder_sql(*args) + if respond_to?(:proxy_reflection) && !proxy_reflection.options[:finder_sql].nil? + # note: paginate_by_sql ignores the blocks. So don't pass the block + paginate_by_sql(@finder_sql, args.extract_options!) + else + paginate_without_finder_sql(*args) + end + end + # patch to deal with the custom_sql scenario + alias_method_chain :paginate, :finder_sql +end -- libgit2 0.21.2