Created
February 24, 2012 20:04
-
-
Save alindeman/1903397 to your computer and use it in GitHub Desktop.
This file contains 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) | |
# ... | |
can :perform, EventStatusChange do |status_change| | |
user.admin? || ["pending approval"].include?(status_change.new_status) | |
end | |
# ... | |
end | |
end |
This file contains 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 EventStatusChange | |
attr_reader :event, :new_status | |
def initialize(event, new_status) | |
@event = event | |
@new_status = new_status | |
end | |
end |
This file contains 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 EventsController < ActionController::Base | |
load_and_authorize_resource | |
def update | |
# UGH! | |
if @event.status != params[:event][:status] | |
unless current_user.admin? || ["pending approval"].include?(params[:event][:status]) | |
render status: :unauthorized and return | |
end | |
end | |
# ... | |
end | |
end |
This file contains 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 EventsController < ActionController::Base | |
load_and_authorize_resource | |
def update | |
if @event.status != params[:event][:status] | |
authorize! :perform, EventStatusChange.new(@event, params[:event][:status]) | |
end | |
# ... | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Interesting approach, thanks for sharing.