diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2012-11-13 13:52:01 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2012-11-13 16:35:16 -0800 |
commit | 46284a183e4c5a96f43542d26e86194c47347d2a (patch) | |
tree | 76cf252c09f14fba5d7bfbe209a5a426275d048e /actionpack | |
parent | d7c72706007151a4a140a6c9af4547843bf7a9df (diff) | |
download | rails-46284a183e4c5a96f43542d26e86194c47347d2a.tar.gz rails-46284a183e4c5a96f43542d26e86194c47347d2a.tar.bz2 rails-46284a183e4c5a96f43542d26e86194c47347d2a.zip |
model name is lazily evaluated in the options object
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/lib/action_controller/metal/params_wrapper.rb | 64 |
1 files changed, 37 insertions, 27 deletions
diff --git a/actionpack/lib/action_controller/metal/params_wrapper.rb b/actionpack/lib/action_controller/metal/params_wrapper.rb index 65ed7ce7fd..9dc7469bd7 100644 --- a/actionpack/lib/action_controller/metal/params_wrapper.rb +++ b/actionpack/lib/action_controller/metal/params_wrapper.rb @@ -73,7 +73,11 @@ module ActionController EXCLUDE_PARAMETERS = %w(authenticity_token _method utf8) - Options = Struct.new(:name, :format, :include, :exclude, :klass, :model) do # :nodoc: + require 'mutex_m' + + class Options < Struct.new(:name, :format, :include, :exclude, :klass, :model) # :nodoc: + include Mutex_m + def self.from_hash(hash) name = hash[:name] format = Array(hash[:format]) @@ -81,6 +85,36 @@ module ActionController exclude = hash[:exclude] && Array(hash[:exclude]).collect(&:to_s) new name, format, include, exclude end + + def model + super || synchronize { super || self.model = _default_wrap_model } + end + + private + # Determine the wrapper model from the controller's name. By convention, + # this could be done by trying to find the defined model that has the + # same singularize name as the controller. For example, +UsersController+ + # will try to find if the +User+ model exists. + # + # This method also does namespace lookup. Foo::Bar::UsersController will + # try to find Foo::Bar::User, Foo::User and finally User. + def _default_wrap_model #:nodoc: + return nil if klass.anonymous? + model_name = klass.name.sub(/Controller$/, '').classify + + begin + if model_klass = model_name.safe_constantize + model_klass + else + namespaces = model_name.split("::") + namespaces.delete_at(-2) + break if namespaces.last == model_name + model_name = namespaces.join("::") + end + end until model_klass + + model_klass + end end included do @@ -155,41 +189,17 @@ module ActionController protected - # Determine the wrapper model from the controller's name. By convention, - # this could be done by trying to find the defined model that has the - # same singularize name as the controller. For example, +UsersController+ - # will try to find if the +User+ model exists. - # - # This method also does namespace lookup. Foo::Bar::UsersController will - # try to find Foo::Bar::User, Foo::User and finally User. - def _default_wrap_model #:nodoc: - return nil if self.anonymous? - model_name = self.name.sub(/Controller$/, '').classify - - begin - if model_klass = model_name.safe_constantize - model_klass - else - namespaces = model_name.split("::") - namespaces.delete_at(-2) - break if namespaces.last == model_name - model_name = namespaces.join("::") - end - end until model_klass - - model_klass - end def _set_wrapper_defaults(opts) unless opts.include || opts.exclude - model = (opts.model ||= _default_wrap_model) + model = opts.model if model.respond_to?(:attribute_names) && model.attribute_names.any? opts.include = model.attribute_names end end unless opts.name || opts.klass.anonymous? - model = (opts.model ||= _default_wrap_model) + model = opts.model opts.name = model ? model.to_s.demodulize.underscore : controller_name.singularize end |