From b3714a9b5cfa54e4cdff6cd9d35cd17132320f79 Mon Sep 17 00:00:00 2001 From: maetl Date: Thu, 18 Apr 2013 18:34:31 +1000 Subject: [PATCH] Filter domains by name --- app/controllers/domains_controller.rb | 6 +++++- app/models/domain.rb | 1 + spec/models/domain_spec.rb | 9 +++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/app/controllers/domains_controller.rb b/app/controllers/domains_controller.rb index 9ecd2b58..b314dd5a 100644 --- a/app/controllers/domains_controller.rb +++ b/app/controllers/domains_controller.rb @@ -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 diff --git a/app/models/domain.rb b/app/models/domain.rb index 7b2a24f6..c42b172e 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -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 diff --git a/spec/models/domain_spec.rb b/spec/models/domain_spec.rb index c8c5a311..04c1ee52 100644 --- a/spec/models/domain_spec.rb +++ b/spec/models/domain_spec.rb @@ -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