cancan abilities in separate file

南笙酒味 提交于 2019-12-01 00:53:02

Just Include the Module and Call the Method in initialize

The trick here is to create modules for each of your abilities, include them in your base ability.rb file and then run the specific method in your initialize method, like so:

In your ability.rb file:

class Ability

  include CanCan::Ability

  include ProjectAbilities 

  def initialize user
    # Your "base" abilities are defined here.

    project_abilities user
  end  

end

In your lib/project_abilities.rb file:

module ProjectAbilities

  def project_abilities user
    # New abilities go here and later get added to the initialize method
    # of the base Ability class.

    can :read, Project do |project|
      user.can? :read, project.client || user.is_an_admin?
    end 
  end

end

Using this pattern, you can break out your abilities into various modules (perhaps, one for each model that you have to define user abilities for).

Take a Look at Pundit

Also of note, take a look at the (relatively) new gem called Pundit, which provides a much more scalable pattern for authorization of larger sites.

Cheers,

JP

With more modern Rubies, you can achieve this with prepend

module CashBalance
  attr_accessor :balance

  def deposit(amount)
    self.balance += amount
  end

  def withdraw(amount)
    self.balance -= amount
  end

  def initialize(*args)
    self.balance = 0.0
    super
  end  

end

class Bank
  prepend CashBalance

  def initialize(name)
    @name = name
  end

  def dump
    puts "%s has a balance of %0.2f" % [ @name, balance ]
  end


end


b = Bank.new("Fleet")

b.deposit(20)
b.dump
b.withdraw(10)
b.dump

yields

$ ruby blarg.rb
Fleet has a balance of 20.00
Fleet has a balance of 10.00
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!