lib/resourcelogic/singleton.rb in resourcelogic-0.0.12 vs lib/resourcelogic/singleton.rb in resourcelogic-0.9.0

- old
+ new

@@ -2,54 +2,72 @@ # module Resourcelogic module Singleton def self.included(klass) klass.class_eval do - add_acts_as_resource_module(Methods) + extend Config end end - module Methods - def object - return @object if defined?(@object) - - if singleton? - if !parent? && respond_to?("current_#{model_name}", true) - @object = send("current_#{model_name}") - elsif parent? && parent_object.send(model_name) - @object = parent_object.send(model_name) - else - super - end - else - super - end + module Config + def singleton(value = nil) + include Methods if value == true + config(:singleton, value) end - def build_object - if singleton? && parent? - scope.send("build_#{model_name}") - else - super - end + def singleton? + singleton == true end - - def scope - if singleton? && parent? - parent_object - else - super + end + + module Methods + def self.included(klass) + klass.class_eval do + methods_to_undefine = [:index, :collection, :load_collection, :collection_url, + :collection_path, :hash_for_collection_url, :hash_for_collection_path] + methods_to_undefine.each { |method| undef_method(method) if method_defined?(method) } end end - # Route alises can only be used for singleton, like account => user. Otherwise the urligence wont work because there is no account model. - def object_url_parts(action = nil, *alternate_object_or_params) - singleton? ? ([action] + contexts_url_parts + [route_name]) : super - end - - # Override me with true to make singleton - def singleton? - false - end + private + # Used to fetch the current object in a singleton controller. + # + # By defult this method is able to fetch the current object for resources nested with the :has_one association only. (i.e. /users/1/image # => @user.image) + # In other cases you should override this method and provide your custom code to fetch a singleton resource object, like using a session hash. + # + # class AccountsController < ResourceController::Singleton + # private + # def object + # @object ||= Account.find(session[:account_id]) + # end + # end + # + def object + @object ||= parent? ? end_of_association_chain : nil + end + + # Returns the :has_one association proxy of the parent. (i.e. /users/1/image # => @user.image) + # + def parent_association + @parent_association ||= parent_object.send(model_name.to_sym) + end + + # Used internally to provide the options to smart_url in a singleton controller. + # + def object_url_options(action_prefix = nil, alternate_object = nil) + [action_prefix] + namespaces + [parent_url_options, route_name.to_sym] + end + + # Builds the object, but doesn't save it, during the new, and create action. + # + def build_object + @object ||= singleton_build_object_base.send parent? ? "build_#{model_name}".to_sym : :new, object_params + end + + # Singleton controllers don't build off of association proxy, so we can't use end_of_association_chain here + # + def singleton_build_object_base + parent? ? parent_object : model + end end end end