Skip to content

Instantly share code, notes, and snippets.

@alindeman
Created February 24, 2012 20:04
Show Gist options
  • Save alindeman/1903397 to your computer and use it in GitHub Desktop.
Save alindeman/1903397 to your computer and use it in GitHub Desktop.
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
class EventStatusChange
attr_reader :event, :new_status
def initialize(event, new_status)
@event = event
@new_status = new_status
end
end
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
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
@sgringwe
Copy link

sgringwe commented Feb 7, 2014

Interesting approach, thanks for sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment