Skip to content

Commit f09effc

Browse files
committed
Add ability to pass :model option to filter_resource_access, as in filter_access_to. Also see #164
1 parent cdfa2e9 commit f09effc

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

lib/declarative_authorization/in_controller.rb

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ def filter_access_filter # :nodoc:
137137
end
138138
end
139139

140-
def load_controller_object (context_without_namespace = nil) # :nodoc:
140+
def load_controller_object (context_without_namespace = nil, model = nil) # :nodoc:
141141
instance_var = :"@#{context_without_namespace.to_s.singularize}"
142-
model = context_without_namespace.to_s.classify.constantize
142+
model = model ? model.classify.constantize : context_without_namespace.to_s.classify.constantize
143143
instance_variable_set(instance_var, model.find(params[:id]))
144144
end
145145

@@ -158,10 +158,14 @@ def new_controller_object_from_params (context_without_namespace, parent_context
158158
model_or_proxy.new(params[context_without_namespace.to_s.singularize]))
159159
end
160160

161-
def new_blank_controller_object (context_without_namespace, parent_context_without_namespace, strong_params) # :nodoc:
162-
model_or_proxy = parent_context_without_namespace ?
163-
instance_variable_get(:"@#{parent_context_without_namespace.to_s.singularize}").send(context_without_namespace.to_sym) :
164-
context_without_namespace.to_s.classify.constantize
161+
def new_blank_controller_object (context_without_namespace, parent_context_without_namespace, strong_params, model) # :nodoc:
162+
if model
163+
model_or_proxy = model.to_s.classify.constantize
164+
else
165+
model_or_proxy = parent_context_without_namespace ?
166+
instance_variable_get(:"@#{parent_context_without_namespace.to_s.singularize}").send(context_without_namespace.to_sym) :
167+
context_without_namespace.to_s.classify.constantize
168+
end
165169
instance_var = :"@#{context_without_namespace.to_s.singularize}"
166170
instance_variable_set(instance_var,
167171
model_or_proxy.new())
@@ -489,6 +493,7 @@ def filter_resource_access(options = {})
489493
#:load_method => nil, # only symbol method name
490494
:no_attribute_check => nil,
491495
:context => nil,
496+
:model => nil,
492497
:nested_in => nil,
493498
:strong_parameters => nil
494499
}.merge(options)
@@ -548,7 +553,7 @@ def filter_resource_access(options = {})
548553
controller.send(new_object_method)
549554
else
550555
controller.send(:new_blank_controller_object,
551-
options[:context] || controller_name, options[:nested_in], options[:strong_parameters])
556+
options[:context] || controller_name, options[:nested_in], options[:strong_parameters], options[:model])
552557
end
553558
end
554559
end
@@ -559,17 +564,18 @@ def filter_resource_access(options = {})
559564
if controller.respond_to?(load_method, true)
560565
controller.send(load_method)
561566
else
562-
controller.send(:load_controller_object, options[:context] || controller_name)
567+
controller.send(:load_controller_object, options[:context] || controller_name, options[:model])
563568
end
564569
end
565-
filter_access_to :all, :attribute_check => true, :context => options[:context]
570+
filter_access_to :all, :attribute_check => true, :context => options[:context], :model => options[:model]
566571

567572
members.merge(new_actions).merge(collections).each do |action, privilege|
568573
if action != privilege or (options[:no_attribute_check] and options[:no_attribute_check].include?(action))
569574
filter_options = {
570575
:strong_parameters => options[:strong_parameters],
571576
:context => options[:context],
572-
:attribute_check => !options[:no_attribute_check] || !options[:no_attribute_check].include?(action)
577+
:attribute_check => !options[:no_attribute_check] || !options[:no_attribute_check].include?(action),
578+
:model => options[:model]
573579
}
574580
filter_options[:require] = privilege if action != privilege
575581
filter_access_to(action, filter_options)
@@ -675,7 +681,8 @@ def load_object(contr)
675681
else
676682
load_object_model = @load_object_model ||
677683
(@context ? @context.to_s.classify.constantize : contr.class.controller_name.classify.constantize)
678-
instance_var = "@#{load_object_model.name.underscore}"
684+
load_object_model = load_object_model.classify.constantize if load_object_model.is_a?(String)
685+
instance_var = "@#{load_object_model.name.demodulize.underscore}"
679686
object = contr.instance_variable_get(instance_var)
680687
unless object
681688
begin

0 commit comments

Comments
 (0)