Skip to content

Commit

Permalink
Changed field name, modified the User model, added unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasiek committed Jan 6, 2011
1 parent 3750a06 commit 3b80c18
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class User < ActiveRecord::Base
def can?(action, page)
UserPagePermission.count(:conditions => ['user_id = ? AND page_id = ? AND action = ?', self.id, page.id, action.to_s]) > 0
end
end
2 changes: 1 addition & 1 deletion db/migrate/20110102045601_create_user_page_permissions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def self.up
create_table :user_page_permissions do |t|
t.integer :user_id
t.integer :page_id
t.string :permission
t.string :action
t.timestamps
end
end
Expand Down
15 changes: 15 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe User, 'permissions' do
before(:each) do
User.delete_observers
@user = User.create!
@page = Page.create!(:title => 'sample_title', :slug => 'sample_title', :breadcrumb => '...', :status => Status.find(1))
@user_page_permission = UserPagePermission.create!(:user_id => @user.id, :page_id => @page.id, :action => 'create')
end

it "should allow the user to create a page under the given page by a given user" do
@user.can?(:create, @page).should be_true
@user.can?(:destroy, @page).should be_false
end
end

0 comments on commit 3b80c18

Please sign in to comment.