Module: Checkin::Filters::ClassMethods

Defined in:
lib/checkin/filters.rb

Instance Method Summary collapse

Instance Method Details

#checkin(opts = {}) ⇒ Object

checkin(:subject => :user_subject, :scope => nil, :skip_authorization => false, :object => :object, rescue_with => lambda

if subject.guest?
  redirect_to new_user_session_path
else
  render :text => "Not Authorized", :status => 403
end

)



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/checkin/filters.rb', line 14

def checkin(opts = {})
  opts.symbolize_keys!
  from_subject = :"#{(opts[:subject] || :user_subject)}"
  subject_name = :"#{(opts[:as] || :subject)}"
  subject_model = :"#{(opts[:from] || :current_user)}"
  subject_class = from_subject.to_s.camelize.constantize
  object_method = :"#{(opts[:object] || :fetch_object)}"
  find_method   =  opts[:find_object]


  define_method "#{subject_name}" do
    if !instance_variable_get("@checkin_#{subject_name}")
      instance_variable_set("@checkin_#{subject_name}", subject_class.new(self.send(subject_model), :scope => opts[:scope]))
    else
      instance_variable_get("@checkin_#{subject_name}")
    end       
  end
  
  helper_method :"#{subject_name}"

  if opts[:rescue_with]
    block = opts[:rescue_with]
    define_method :rescue_from_checkin_access_denied, &block
    rescue_from Checkin::AccessDenied, :with => :rescue_from_checkin_access_denied        
  end

  if !opts[:skip_authorization]
    define_method :"#{object_method}" do   

      
      if params[:id]  
        model_class   = self.controller_name.singularize.camelize.constantize
        singular_name = :"#{model_class.name.underscore.singularize}"
        if !instance_variable_get("@#{singular_name}")
          find_method = if find_method.nil?
             Proc.new {|model_class, params|
              model_class.find(params[:id])
            }
          elsif find_method.is_a?(Proc) || find_method.is_a?(Method)
            find_method
          elsif find_method.is_a?(Symbol) || find_method.is_a?(String)
            self.method(find_method)
          else
            raise "'#{find_method.class.name}' is an invalid type for find method"
          end
          instance_variable_set("@#{singular_name}", find_method.call(model_class, params))
        else
          instance_variable_get("@#{singular_name}")
        end
      end
    end
  
    before_filter do |controller|
      if Rails.env.development?
        controller.send(subject_name).explain!
      end
      controller.send(subject_name).checkin!( :"#{controller.action_name}", (controller.send(:"#{object_method}") || {:for => :"#{controller.controller_name}"}) )
      controller.send(subject_name).delete_denied_params( :"#{controller.action_name}", (controller.send(:"#{object_method}") || {:for => :"#{controller.controller_name}"}), (params[:"#{controller.controller_name.singularize}"] || {}) )
    end
  end      
end