james2m / canard

Makes role based authorization in Rails really simple. Wraps CanCan and RoleModel up with a smattering of syntactic sugar, some generators and scopes.
MIT License
125 stars 28 forks source link

Tweak generators for rails 4+ #30

Closed jondkinney closed 7 years ago

jondkinney commented 7 years ago

And adjust some of the spacing that is output, along with FactoryGirl config and rspec let vars.

Tweak documentation to point to CanCanCan

Here is what the resulting output looks like now:

Without Definitions

rails g canard:ability person
Canard::Abilities.for(:person) do
  # Define abilities for the user role here. For example:
  #
  #   if user.admin?
  #     can :manage, :all
  #   else
  #     can :read, :all
  #   end
  #
  # The first argument to `can` is the action you are giving the user permission to do.
  # If you pass :manage it will apply to every action. Other common actions here are
  # :read, :create, :update and :destroy.
  #
  # The second argument is the resource the user can perform the action on. If you pass
  # :all it will apply to every resource. Otherwise pass a Ruby class of the resource.
  #
  # The third argument is an optional hash of conditions to further filter the objects.
  # For example, here the user can only update published articles.
  #
  #   can :update, Article, published: true
  #
  # See the wiki for details: https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities
end
require 'rails_helper'
require 'cancan/matchers'

describe Canard::Abilities, '#people' do
  let(:person) { FactoryGirl.create(:person) }

  subject(:person_ability) { Ability.new(person) }

#   # Define your ability tests thus;
#   describe 'on Person' do
#     it { is_expected.to be_able_to(:index,   Person) }
#     it { is_expected.to be_able_to(:show,    person) }
#     it { is_expected.to be_able_to(:read,    person) }
#     it { is_expected.to be_able_to(:new,     person) }
#     it { is_expected.to be_able_to(:create,  person) }
#     it { is_expected.to be_able_to(:edit,    person) }
#     it { is_expected.to be_able_to(:update,  person) }
#     it { is_expected.to be_able_to(:destroy, person) }
#   end
#   # on Person
end

With Definitions

rails g canard:ability person can:manage:person cannot:destroy:person
Canard::Abilities.for(:person) do
  can [:manage], Person
  cannot [:destroy], Person
end
require 'rails_helper'
require 'cancan/matchers'

describe Canard::Abilities, '#people' do
  let(:person) { FactoryGirl.create(:person) }

  subject(:person_ability) { Ability.new(person) }

  describe 'on Person' do
    it { is_expected.to be_able_to(:manage, person) }
    it { is_expected.to_not be_able_to(:destroy, person) }
  end
  # on Person
end