Skip to content
This repository has been archived by the owner on Mar 3, 2020. It is now read-only.

Commit

Permalink
Even better Domain.managable query. Fixes #4.
Browse files Browse the repository at this point in the history
  • Loading branch information
nning committed Nov 4, 2014
1 parent d5ab54c commit 96ee131
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
8 changes: 5 additions & 3 deletions app/models/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,11 @@ def self.managable(mailbox)
if mailbox.admin?
Domain
else
Domain.includes(:permissions).where("
(permissions.role = 'editor' or permissions.role = 'owner')
and subject_id = ?",
Domain.joins(:permissions).where('
(permissions.role = ? or permissions.role = ?)
and subject_id = ?',
'editor',
'owner',
mailbox.id
)
end
Expand Down
31 changes: 31 additions & 0 deletions test/unit/domain_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,36 @@ class DomainTest < ActiveSupport::TestCase
assert_equal 'foo.com', @domain.name
end
end

context 'managable' do
setup do
@another_domain = FactoryGirl.create :domain
end

context 'admin' do
setup do
@mailbox = FactoryGirl.create :mailbox, domain: @domain, admin: true
end

should 'return everything' do
assert_equal Domain.managable(@mailbox).all, Domain.all
end
end

%w[owner editor].each do |role|
context role do
setup do
@mailbox = FactoryGirl.create :mailbox, domain: @domain
@domain.permissions.create!(subject: @mailbox, role: role)
@managable = Domain.managable(@mailbox)
end

should 'return correct domain' do
assert_equal @managable.all.size, 1
assert_equal @managable.first, @domain
end
end
end
end
end
end

0 comments on commit 96ee131

Please sign in to comment.