Skip to content

Instantly share code, notes, and snippets.

@tsechingho
Last active August 29, 2015 14:13
Show Gist options
  • Save tsechingho/714d6f50865f86d6ac55 to your computer and use it in GitHub Desktop.
Save tsechingho/714d6f50865f86d6ac55 to your computer and use it in GitHub Desktop.

Revisions

  1. tsechingho revised this gist Jan 12, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # app/controllers/application_controller.rb
    # app/controllers/application_controller.rb or any controller
    class ApplicationController < ActionController::Base
    def current_ability
    @current_ability ||= Ability.ability_for current_customer
  2. tsechingho revised this gist Jan 12, 2015. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions application_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    # app/controllers/application_controller.rb
    class ApplicationController < ActionController::Base
    def current_ability
    @current_ability ||= Ability.ability_for current_customer
    end
    end

  3. tsechingho created this gist Jan 12, 2015.
    18 changes: 18 additions & 0 deletions ability.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    # app/models/ability.rb
    module Ability
    class << self
    def ability_for user, options = {}
    abilities = AnonymousAbility.new

    return abilities unless user

    abilities.merge MemberAbility.new user, options

    if user.has_role? 'administrator'
    abilities.merge AdministratorAbility.new
    end

    abilities
    end
    end
    end
    16 changes: 16 additions & 0 deletions member_ability.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    # app/abilities/member_ability.rb
    class MemberAbility
    include CanCan::Ability

    attr_accessor :current_user

    def initialize current_user, options = {}
    can :update, User do |user|
    user.email == current_user.email
    end

    can :update, Profile do |profile|
    profile.owners.include? current_user
    end
    end
    end