Module: Oauned::ControllerMethods

Defined in:
lib/oauned/controller_methods.rb

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/oauned/controller_methods.rb', line 3

def self.included(klass)
  klass.class_eval do
    cattr_accessor :oauth_options, :oauth_options_proc

    protected
    def self.deny_oauth(options = {}, &block)
      raise 'options cannot contain both :only and :except' if options[:only] && options[:except]

      [:only, :except].each do |k|
        if values = options[k]
          options[k] = Array(values).map(&:to_s).to_set
        end
      end
      self.oauth_options = options
      self.oauth_options_proc = block
    end

    def oauth_allowed?
      return true if (oauth_options_proc && !oauth_options_proc.call(self)) || oauth_options.nil?
      return false if oauth_options.empty?
      return true if oauth_options[:only] && !oauth_options[:only].include?(action_name)
      return true if oauth_options[:except] && oauth_options[:except].include?(action_name)
      false
    end
  end
end