Skip to content

Instantly share code, notes, and snippets.

@sscirrus
Created May 30, 2017 19:34
Show Gist options
  • Save sscirrus/726a1f89119850feb50ff7656b67f6ee to your computer and use it in GitHub Desktop.
Save sscirrus/726a1f89119850feb50ff7656b67f6ee to your computer and use it in GitHub Desktop.
CanCanCan Issue
gemfile(true) do
source 'https://rubygems.org'
gem 'rails', '4.2.7'
gem 'cancancan', '1.12.0'
gem 'pg'
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'poltergeist'
gem 'puma'
end
class Computer < ActiveRecord::Base
belongs_to :device
end
class Device < ActiveRecord::Base
has_one :computer
end
class Ability
include CanCan::Ability
def initialize(user)
if user.admin || user.group_admin
can :crud, [Computer, Device], company_id: company.id
end
end
end
# factories.rb
FactoryGirl.define do
factory :company do
sequence :name do |n|
"Company #{n}, inc."
end
trait :with_everything do
after(:create) do |c|
create(:user, :admin, company: c)
create(:user, :non_admin, company: c)
create(:user, :group_admin, company: c)
d1 = create(:device, :active, :model_1, company: c)
d2 = create(:device, :active, :model_2, company: c)
d3 = create(:device, :inactive, :model_1, company: c)
create(:computer, company: c, device: d1)
create(:computer, company: c, device: d3)
create(:computer, company: c, device: nil)
end
end
end
# spec/features/computer_management_spec.rb
feature "Computer management", type: :feature do
scenario "admin can access all computers" do
# Given we have a company with computers and devices...
@company = create(:company, :with_everything)
@computers = @company.computers
@computer = @computers.sample
@devices = @company.devices
# Given I am logged in as an admin...
@user = @company.users.where("role = ?", "admin").first
login_as @user
@current_ability ||= Ability.new(@user)
visit computers_path
within("#row_computer_#{@computer.id}") do # FAILS (unable to find css "#row_computer_271")
expect(page).to have_content(@computer.name)
end
end
end
# View
computers/index.html.erb
# Debugging messages added at the top of view
<% @current_ability = Ability.new(current_user) %>
*** Company Computers: <%= @company.computers.collect(&:id) %><br />
*** Accessible Computers: <%= @company.computers.accessible_by(@current_ability).collect(&:id) %><br />
*** SQL: <%= @company.computers.accessible_by(Ability.new(current_user)).to_sql %>
<br /><br />
*** Company Devices: <%= @company.devices.collect(&:id) %><br />
*** Accessible devices: <%= @company.devices.accessible_by(@current_ability).collect(&:id) %><br />
*** SQL: <%= @company.devices.accessible_by(Ability.new(current_user)).to_sql %>
# (HOW IT SHOULD LOOK) Load the page in development:
*** Company Computers: [2, 14, 17, 4, 1, 3]
*** Accessible Computers: [2, 14, 17, 4, 1, 3]
*** SQL: SELECT "computers".* FROM "computers" WHERE "computers"."company_id" = 1 AND "computers"."company_id" = 1
*** Company Devices: [3, 9, 4, 2, 1, 7, 11, 5, 6]
*** Accessible devices: [3, 9, 4, 2, 1, 7, 11, 5, 6]
*** SQL: SELECT "devices".* FROM "devices" WHERE "devices"."company_id" = 1 AND "devices"."company_id" = 1
# (HOW IT LOOKS DURING TESTING) Screenshot of page during Rspec feature test:
*** Company Computers: [271, 272, 273, 275]
*** Accessible Computers: []
*** SQL: SELECT "computers".* FROM "computers" WHERE "computers"."company_id" = 437 AND "computers"."company_id" = 437
*** Company Devices: [330, 327, 326, 325]
*** Accessible devices: []
*** SQL: SELECT "devices".* FROM "devices" WHERE "devices"."company_id" = 437 AND "devices"."company_id" = 437
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment