Created
February 8, 2010 13:56
-
-
Save smeevil/298153 to your computer and use it in GitHub Desktop.
authlogic single_access_allowed config
This file contains hidden or 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
# 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