Rails 4: CanCanCan features with has_many: through union

I have a Rails application with the following models:

class User < ActiveRecord::Base
  has_many :administrations
  has_many :calendars, through: :administrations
end

class Calendar < ActiveRecord::Base
  has_many :administrations
  has_many :users, through: :administrations
end

class Administration < ActiveRecord::Base
  belongs_to :user
  belongs_to :calendar
end

For a given calendara userhas role, which is defined in the connection model administration.

The user can only have one of the following three roles for each calendar: Owner, Editoror Viewer.

These roles are not currently stored in a dictionary or constant and are assigned only to the administrator as strings ("Ower", "Editor", "Viewer") using different methods.

Authentication of the model is userhandled through Devise, and the method works current_user.

, before_action :authenticate_user! calendars administrations.

, CanCanCan.

:

  • ( ) user calendar s.
  • a user Owner a calendar, manage calendar administration, calendar, administration.
  • Editor calendar, read update administration.
  • a user Viewer of calendar, read calendar destroy administration.

, ability.rb:

class Ability
  include CanCan::Ability

  def initialize(user, calendar)
    user ||= User.new
    calendar = Calendar.find(params[:id])
    user can :create, :calendar
    if user.role?(:owner)
      can :manage, :calendar, :user_id => user.id
      can :manage, :administration, :user_id => user.id
      can :manage, :administration, :calendar_id => calendar.id
    elsif user.role?(:editor)
      can [:read, :update], :calendar, :user_id => user.id
      can :destroy, :administration, :user_id => user.id
    elsif user.role?(:viewer)
      can [:read], :calendar, :user_id => user.id
      can :destroy, :administration, :user_id => user.id
    end    
  end
end

Rails, , CanCanCan, .

, , , ?

UPDATE: , # show , , .

, , .

, ?

2. , , :model Model, user Model.

.

, ?

3: , if user.role?(:owner), , ​​ , "" ( )?

4: , , .

  • load_and_authorize_resource calendars administrations.

  • - initialize(user, calendar) - initialize.

, , capability.rb :

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new
    if user.role?(:owner)
      can :manage, Calendar, :user_id => user.id
      can :manage, Administration, :user_id => user.id
      can :manage, Administration, :calendar_id => calendar.id
    elsif user.role?(:editor)
      can [:read, :update], Calendar, :user_id => user.id
      can :destroy, Administration, :user_id => user.id
    elsif user.role?(:viewer)
      can [:read], Calendar, :user_id => user.id
      can :destroy, Administration, :user_id => user.id
    end    
  end
end

, , current_user, :

NoMethodError in CalendarsController#show
undefined method `role?' for #<User:0x007fd003dff860>
def initialize(user)
    user ||= User.new
    if user.role?(:owner)
      can :manage, Calendar, :user_id => user.id
      can :manage, Administration, :user_id => user.id
      can :manage, Administration, :calendar_id => calendar.id

?

+2
1

role? . Cancancan , , .

, :

if user.role == 'Owner'
  ...
elsif user.role == 'Editor'
  ...
elsif user.role == 'Viewer'
  ...
+1

All Articles