Skip to content

Commit

Permalink
Fixes #16389 - enable taxable object creation in specific context
Browse files Browse the repository at this point in the history
  • Loading branch information
ares committed Sep 1, 2016
1 parent d301d11 commit 90d58ee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 4 additions & 2 deletions app/models/concerns/taxonomix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ def with_taxonomy_scope(loc = Location.current, org = Organization.current, inne
self.which_location = Location.expand(loc) if SETTINGS[:locations_enabled]
self.which_organization = Organization.expand(org) if SETTINGS[:organizations_enabled]
scope = block_given? ? yield : where('1=1')
scope = scope.where(:id => taxable_ids) if taxable_ids
tax_ids = taxable_ids
# we need to generate part of SQL query as a string, otherwise the default scope would set id on each
# new instance and the same taxable_id on taxable_taxonomy objects
scope = scope.where("#{self.table_name}.id IN (#{tax_ids.join(',')})") if tax_ids
scope.readonly(false)
end

Expand Down Expand Up @@ -115,7 +118,6 @@ def admin_ids

def set_current_taxonomy
if self.new_record? && self.errors.empty?
self.id = nil #fix for rails 3.2.8 bug that sets id = 1 on after_initialize. This can later be removed.
self.locations << Location.current if add_current_location?
self.organizations << Organization.current if add_current_organization?
end
Expand Down
10 changes: 10 additions & 0 deletions test/unit/taxonomix_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,14 @@ def setup
assert_includes resource.organizations, org2
assert_includes resource.organizations, org3
end

test "default scope does not set create scope attributes" do
org = FactoryGirl.create :organization
FactoryGirl.create(:domain, :organizations => [ org ])
original_org, Organization.current = Organization.current, org
new_dom = Domain.new(:organization_ids => [ org.id ])
Organization.current = original_org
new_dom.taxable_taxonomies.must_be :present?
assert new_dom.taxable_taxonomies.all?(&:valid?)
end
end

0 comments on commit 90d58ee

Please sign in to comment.