Skip to content

Commit

Permalink
Merge pull request kennethkalmer#19 from maetl/domain-find-by-name
Browse files Browse the repository at this point in the history
Filter domains by name
  • Loading branch information
Philip Muir committed Apr 18, 2013
2 parents cccf15c + b3714a9 commit 54b4086
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/controllers/domains_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ class DomainsController < InheritedResources::Base
protected

def collection
@domains = Domain.user( current_user ).paginate :page => params[:page]
if params[:name]
@domains = Domain.user( current_user ).by_name(params[:name]).paginate :page => params[:page]
else
@domains = Domain.user( current_user ).paginate :page => params[:page]
end
end

def resource
Expand Down
1 change: 1 addition & 0 deletions app/models/domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class Domain < ActiveRecord::Base

# Scopes
scope :user, lambda { |user| user.admin? ? nil : where(:user_id => user.id) }
scope :by_name, lambda { |name| where(:name => name) }
default_scope order('name')

class << self
Expand Down
9 changes: 9 additions & 0 deletions spec/models/domain_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,17 @@
let(:aaron) { FactoryGirl.create(:aaron) }
let(:quentin_domain) { FactoryGirl.create(:domain, :user => quentin) }
let(:aaron_domain) { FactoryGirl.create(:domain, :name => 'example.org', :user => aaron) }
let(:aaron2_domain) { FactoryGirl.create(:domain, :name => 'example2.org', :user => aaron) }
let(:admin) { FactoryGirl.create(:admin) }

it "should filter by name when domain exists" do
aaron_domain
aaron2_domain

Domain.by_name('example.org').should include(aaron_domain)
Domain.by_name('example.org').should_not include(aaron2_domain)
end

it "should show all domains to an admin" do
quentin_domain
aaron_domain
Expand Down

0 comments on commit 54b4086

Please sign in to comment.