Skip to content

Commit

Permalink
Make sure we don't have duplicate permission records in the DB. [#34]
Browse files Browse the repository at this point in the history
  • Loading branch information
marnen committed Oct 20, 2009
1 parent 222e30e commit f7eb0c4
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions app/models/permission.rb
Expand Up @@ -6,4 +6,5 @@ class Permission < ActiveRecord::Base
validates_presence_of :user_id
validates_presence_of :calendar_id
validates_presence_of :role_id
validates_uniqueness_of :calendar_id, :scope => [:role_id, :user_id]
end
9 changes: 9 additions & 0 deletions db/migrate/20091020154940_add_unique_index_to_permissions.rb
@@ -0,0 +1,9 @@
class AddUniqueIndexToPermissions < ActiveRecord::Migration
def self.up
add_index :permissions, [:user_id, :calendar_id, :role_id], :unique => true
end

def self.down
remove_index :permissions, [:user_id, :calendar_id, :role_id]
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Expand Up @@ -9,7 +9,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20091012223503) do
ActiveRecord::Schema.define(:version => 20091020154940) do

create_table "calendars", :force => true do |t|
t.column "name", :string, :null => false
Expand Down Expand Up @@ -53,6 +53,8 @@
t.column "show_in_report", :boolean, :default => true, :null => false
end

add_index "permissions", ["user_id", "calendar_id", "role_id"], :name => "index_permissions_on_user_id_and_calendar_id_and_role_id", :unique => true

create_table "roles", :force => true do |t|
t.column "name", :string
t.column "created_at", :timestamp
Expand Down
14 changes: 12 additions & 2 deletions spec/controllers/permissions_controller_spec.rb
Expand Up @@ -116,11 +116,21 @@
response.should be_redirect
end

it 'should create a new permission for the current user and the given calendar' do
it "should create a new permission for the current user and the given calendar, if there isn't one already" do
conditions = {:calendar_id => @calendar.id, :user_id => @current_user.id, :role_id => @user.id}
Permission.destroy(Permission.find(:all, :conditions => conditions).collect{|p| p.id})
@pcount = Permission.count
get :subscribe, :calendar_id => @calendar.id
Permission.count.should == @pcount + 1
Permission.find(:first, :conditions => {:calendar_id => @calendar.id, :user_id => @current_user.id, :role_id => @user.id}).should_not be_nil
Permission.find(:first, :conditions => conditions).should_not be_nil
end

it "should not create a new permission if there's already one (at user level) for the current user and given calendar" do
opts = {:calendar_id => @calendar.id, :user_id => @current_user.id, :role_id => @user.id}
Permission.create opts
@pcount = Permission.count
get :subscribe, :calendar_id => @calendar.id
Permission.count.should == @pcount
end
end

Expand Down
14 changes: 14 additions & 0 deletions spec/models/permission_spec.rb
Expand Up @@ -36,5 +36,19 @@
@permission.role_id = nil
@permission.should_not be_valid
end

it "should be unique across all three attributes" do
opts = Permission.plan
@one = Permission.new opts
@one.should be_valid
@one.save!
@two = Permission.new opts
@two.should_not be_valid
[:calendar_id, :user_id, :role_id].each do |attr|
@three = Permission.new opts
@three[attr] += 1
@three.should be_valid
end
end
end

0 comments on commit f7eb0c4

Please sign in to comment.