Skip to content

Instantly share code, notes, and snippets.

@terenceponce
Created April 10, 2012 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terenceponce/2349494 to your computer and use it in GitHub Desktop.
Save terenceponce/2349494 to your computer and use it in GitHub Desktop.
Having trouble in testing CanCan
<% if can? :update, article %>
<%= link_to 'Edit', edit_article_path(article) %>
<% end %>
<% if can? :destroy, article %>
<%= link_to 'Destroy', article_path(article), :confirm => 'Are you sure?', :method => :delete %>
<% end %>
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # Guest user
if user.role? :admin
can :manage, :all
else
can :read, :all
if user.role?(:author)
can :create, Article
can :update, Article do |article|
article.try(:author) == user
end
can :destroy, Article do |article|
article.try(:author) == user
end
end
end
end
end
Fabricator(:article) do
title 'This is a title'
content 'This is the content'
author! { Fabricate(:author) }
categories(:count => 1)
end
before do
@author = Fabricate(:author)
visit new_user_session_path
fill_in 'Email', :with => @author.email
fill_in 'Password', :with => @author.password
click_button 'Sign in'
@article = Fabricate(:article, :author_id => @author.id)
visit articles_path
end
it { should have_link 'Edit', :href => edit_article_path(@article) }
it { should have_link 'Destroy', :method => :delete, :href => article_path(@article) }
# I keep getting this error
#
# Failure/Error: it { should have_link 'Edit', :href => edit_article_path(@article) }
# expected link "Edit" to return something
#
# Failure/Error: it { should have_link 'Destroy', :method => :delete, :href => article_path(@article) }
# expected link "Destroy" to return something
#
# It works in the browser though :(
<%= render @articles %>
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
attr_accessible :name, :roles
has_many :articles, :foreign_key => 'author_id', :dependent => :destroy
ROLES = %w[admin moderator author]
def roles=(roles)
self.roles_mask = (roles & ROLES).map { |r| 2**ROLES.index(r) }.sum
end
def roles
ROLES.reject { |r| ((roles_mask || 0) & 2**ROLES.index(r)).zero? }
end
def role?(role)
roles.include?(role.to_s)
end
end
Fabricator(:user) do
email { sequence(:email) { |i| "user#{i}@example.com" } }
name { sequence(:name) { |i| "Example User-#{i}" } }
password 'foobar'
end
Fabricator(:admin, :from => :user) do
roles ['admin']
end
Fabricator(:author, :from => :user) do
roles ['author']
end
@bryanbibat
Copy link

See https://github.com/terenceponce/devcon/pull/14
Basically the fabricator for :article doesn't have an author_id entry so the :author_id => @author.id is ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment