I have an Events
Controller on which I want to skip authentication incase the event is public.
In my ApplicationController
I have this call to devise's authenticate_user!
class ApplicationController < ActionController::Base
before_action :authenticate_user!
end
now, Inside my Events table, I have a boolean field called public
. I use that to check if event is public or not. Like this in EventsController
class EventsController < ApplicationController
skip_before_action :authenticate_user!, only: :show, if: Proc.new { :is_public? }
end
But for some reason, this didn't work. so I had to do this:
class EventsController < ApplicationController
skip_before_action :authenticate_user!, only: :show
before_action :authenticate_user!, unless: :is_public?
def is_public?
@event.present? && @event.is_public
end
end
This works as expects and skip authentication if @event.public = true
because the above repeats the before_filter
with the inverse condition after skipping.
I am wondering:
- what I did is correct?
- Does this have any performance impact. if yes, then Is there a better way?