Commit 461c4fe9f2e6751136e8f8d92c86fe6f36d04728
1 parent
12761a56
Exists in
master
and in
29 other branches
Replacing Bsc products by a has_many association
* To solve this problem it was necessary to include a monkey patch on will_paginate due to a bug with the finder_sql option of ActiveRecord association. As described in the monkey patch, this bug is already solved in will_paginate 3.x.pre. (ActionItem2100)
Showing
3 changed files
with
22 additions
and
20 deletions
Show diff stats
plugins/bsc/lib/bsc_plugin/bsc.rb
@@ -2,6 +2,7 @@ class BscPlugin::Bsc < Enterprise | @@ -2,6 +2,7 @@ class BscPlugin::Bsc < Enterprise | ||
2 | 2 | ||
3 | has_many :enterprises | 3 | has_many :enterprises |
4 | has_many :enterprise_requests, :class_name => 'BscPlugin::AssociateEnterprise' | 4 | has_many :enterprise_requests, :class_name => 'BscPlugin::AssociateEnterprise' |
5 | + has_many :products, :finder_sql => 'select * from products where enterprise_id in (#{enterprises.map(&:id).join(",")})' | ||
5 | 6 | ||
6 | validates_presence_of :nickname | 7 | validates_presence_of :nickname |
7 | validates_presence_of :company_name | 8 | validates_presence_of :company_name |
@@ -26,11 +27,6 @@ class BscPlugin::Bsc < Enterprise | @@ -26,11 +27,6 @@ class BscPlugin::Bsc < Enterprise | ||
26 | {:title => _('Bsc info and settings'), :icon => 'edit-profile-enterprise'} | 27 | {:title => _('Bsc info and settings'), :icon => 'edit-profile-enterprise'} |
27 | end | 28 | end |
28 | 29 | ||
29 | - def products(reload_flag=false) | ||
30 | - reload if reload_flag | ||
31 | - enterprises.map { |enterprise| enterprise.products }.flatten | ||
32 | - end | ||
33 | - | ||
34 | def create_product? | 30 | def create_product? |
35 | false | 31 | false |
36 | end | 32 | end |
plugins/bsc/test/unit/bsc_plugin/bsc_test.rb
@@ -46,7 +46,7 @@ class BscPlugin::BscTest < Test::Unit::TestCase | @@ -46,7 +46,7 @@ class BscPlugin::BscTest < Test::Unit::TestCase | ||
46 | e1 = fast_create(Enterprise) | 46 | e1 = fast_create(Enterprise) |
47 | e2 = fast_create(Enterprise) | 47 | e2 = fast_create(Enterprise) |
48 | category = fast_create(ProductCategory) | 48 | category = fast_create(ProductCategory) |
49 | - bsc = BscPlugin::Bsc.new() | 49 | + bsc = BscPlugin::Bsc.create!({:business_name => 'Sample Bsc', :identifier => 'sample-bsc', :company_name => 'Sample Bsc Ltda.', :cnpj => VALID_CNPJ}) |
50 | 50 | ||
51 | p1 = fast_create(Product, :product_category_id => category.id) | 51 | p1 = fast_create(Product, :product_category_id => category.id) |
52 | p2 = fast_create(Product, :product_category_id => category.id) | 52 | p2 = fast_create(Product, :product_category_id => category.id) |
@@ -59,25 +59,13 @@ class BscPlugin::BscTest < Test::Unit::TestCase | @@ -59,25 +59,13 @@ class BscPlugin::BscTest < Test::Unit::TestCase | ||
59 | bsc.enterprises << e1 | 59 | bsc.enterprises << e1 |
60 | bsc.enterprises << e2 | 60 | bsc.enterprises << e2 |
61 | 61 | ||
62 | + bsc.reload | ||
63 | + | ||
62 | assert_includes bsc.products, p1 | 64 | assert_includes bsc.products, p1 |
63 | assert_includes bsc.products, p2 | 65 | assert_includes bsc.products, p2 |
64 | assert_includes bsc.products, p3 | 66 | assert_includes bsc.products, p3 |
65 | end | 67 | end |
66 | 68 | ||
67 | - should 'reload products' do | ||
68 | - e = fast_create(Enterprise) | ||
69 | - category = fast_create(ProductCategory) | ||
70 | - bsc = BscPlugin::Bsc.create!(:business_name => 'Sample Bsc', :company_name => 'Sample Bsc', :identifier => 'sample-bsc', :cnpj => VALID_CNPJ) | ||
71 | - p = fast_create(Product, :product_category_id => category.id) | ||
72 | - | ||
73 | - e.bsc = bsc | ||
74 | - e.save! | ||
75 | - e.products << p | ||
76 | - | ||
77 | - assert_equal [], bsc.products | ||
78 | - assert_equal [p], bsc.products(true) | ||
79 | - end | ||
80 | - | ||
81 | should 'not be able to create product' do | 69 | should 'not be able to create product' do |
82 | bsc = BscPlugin::Bsc.new | 70 | bsc = BscPlugin::Bsc.new |
83 | assert !bsc.create_product? | 71 | assert !bsc.create_product? |
@@ -0,0 +1,18 @@ | @@ -0,0 +1,18 @@ | ||
1 | +# monkey patch to fix WillPaginate bug | ||
2 | +# this was solved in will_paginate 3.x.pre, then remove this patch when upgrade to it | ||
3 | +# | ||
4 | +# http://sod.lighthouseapp.com/projects/17958/tickets/120-paginate-association-with-finder_sql-raises-typeerror | ||
5 | +require_dependency 'will_paginate' | ||
6 | + | ||
7 | +WillPaginate::Finder::ClassMethods.module_eval do | ||
8 | + def paginate_with_finder_sql(*args) | ||
9 | + if respond_to?(:proxy_reflection) && !proxy_reflection.options[:finder_sql].nil? | ||
10 | + # note: paginate_by_sql ignores the blocks. So don't pass the block | ||
11 | + paginate_by_sql(@finder_sql, args.extract_options!) | ||
12 | + else | ||
13 | + paginate_without_finder_sql(*args) | ||
14 | + end | ||
15 | + end | ||
16 | + # patch to deal with the custom_sql scenario | ||
17 | + alias_method_chain :paginate, :finder_sql | ||
18 | +end |