Skip to content

Instantly share code, notes, and snippets.

@smeevil
Created February 8, 2010 13:56
Show Gist options
  • Save smeevil/298153 to your computer and use it in GitHub Desktop.
Save smeevil/298153 to your computer and use it in GitHub Desktop.
authlogic single_access_allowed config
# Allows you to define "allow_single_access" in multiple ways :
#
# single_access_allowed #all methods are accessable
# single_access_allowed :only=>:index
# single_access_allowed :only=>[:index, :show]
# single_access_allowed :except=>[:edit]
# single_access_allowed :except=>[:edit, :delete]
#
### example
class UsersController < ApplicationController
single_access_allowed :only=>:show
end
###code
ApplicationController < ActionController::Base
def self.inherited(klass)
super
klass.extend(ClassMethods)
class << klass
attr_accessor :single_access_options
end
end
module ClassMethods
def single_access_allowed(options=nil)
self.single_access_options=options
include(SingleAccessAllowed)
end
end
module SingleAccessAllowed
def single_access_allowed?
options=self.class.single_access_options
return true unless options.kind_of?(Hash)
return [options[:except]].flatten.compact.index(params[:action].to_sym).nil? if options[:except].present?
return [options[:only]].flatten.compact.include?(params[:action].to_sym)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment