Last active
December 16, 2015 02:59
-
-
Save ansonhoyt/5366298 to your computer and use it in GitHub Desktop.
Authorizing polymorphic association with CanCan 1.6.9
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Ability | |
| include CanCan::Ability | |
| def initialize(user, session=nil) | |
| @user = user | |
| @session = session | |
| @user ? user_rules : public_rules | |
| end | |
| def user_rules | |
| can :update, Building, coach_id: @user.id | |
| can :manage, Assignment do |assignment| | |
| self.can? :update, assignment.assignable | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Assignment < ActiveRecord::Base | |
| belongs_to :assignable, polymorphic: true | |
| belongs_to :user | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class AssignmentsController < ApplicationController | |
| before_filter :load_assignments | |
| authorize_resource | |
| private | |
| def load_assignable | |
| if params[:program_id] | |
| @assignable = @program = Program.find(params[:program_id]) | |
| elsif params[:building_id] | |
| @assignable = @building = Building.find(params[:building_id]) | |
| elsif params[:classroom_id] | |
| @assignable = @classroom = Classroom.find(params[:classroom_id]) | |
| end | |
| end | |
| def load_assignments | |
| load_assignable | |
| if @assignable | |
| @assignments = @assignable.assignments | |
| elsif params[:user_id] | |
| @user = User.find(params[:user_id]) | |
| @assignments = @user.assignments | |
| else | |
| @assignments = Assignment.all.select {|a| can? :read, a} | |
| end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Building < ActiveRecord::Base | |
| belongs_to :coach, class_name: "User" | |
| has_many :assignments, as: :assignable, dependent: :destroy | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Program < ActiveRecord::Base | |
| has_many :assignments, as: :assignable, dependent: :destroy | |
| has_many :buildings | |
| belongs_to :coach, class_name: "User" | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment