lib/trust/controller/resource.rb in trust-0.7.0 vs lib/trust/controller/resource.rb in trust-0.8.0
- old
+ new
@@ -22,25 +22,26 @@
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
module Trust
module Controller
+ # = Trust::Controller::Resource
+ #
+ # Collects information about the current resource and relations.
+ # Handles the loading of the resource and its possible parent, i.e. setting the relevant instance variables
+ # It assumes the name of the resource is built on the controllers name, but this can be overridden in your
+ # controller by setting the +model+
+ #
+ # Examples:
+ #
+ # # controller name AccountsController
+ # resource.instance # => @account
+ #
+ # # controller name Customer::AccountsController
+ # resource.instance # => @customer_account
+ #
class Resource
-# = Trust::Controller::Resource
-# Collects information about the current resource and relations.
-# Handles the loading of the resource and its possible parent, i.e. setting the relevant instance variables
-# It assumes the name of the resource is built on the controllers name, but this can be overridden in your
-# controller by setting the +model+
-#
-# Examples:
-#
-# # controller name AccountsController
-# resource.instance # => @account
-#
-# # controller name Customer::AccountsController
-# resource.instance # => @customer_account
-#
delegate :logger, :to => Rails
attr_reader :properties, :params, :action
attr_reader :info, :parent_info, :relation
@@ -114,10 +115,18 @@
# Returns the class for the resource
def klass
info.klass
end
+ # Returns a collection that can be used for index, new and creation actions
+ #
+ # See Trust::Controller::ResourceInfo.collection which controls the behavior of this method.
+ def collection(instance = nil)
+ @info.collection(@parent_info, instance)
+ end
+
+
# Loads the resource
#
# See Trust::Controller::Properties which controls the behavior of this method.
#
# It will normally find the instance variable for existing object or initialize them as new.
@@ -166,11 +175,14 @@
# resource.parent_name # => :customer
def parent_name
parent_info && parent_info.name
end
-
+ # Returns the association name with the parent
+ def association_name
+ parent_info && info.association_name(parent_info)
+ end
private
def extract_resource_info(model, params) # nodoc
ResourceInfo.new(model, params)
end
@@ -232,11 +244,11 @@
end
# = Resource::ResorceInfo
#
# Resolves the resource in subject
- # (see #ResourceInfo)
+ # (see #Resource::Info)
class Resource::ResourceInfo < Resource::Info
def initialize(model, params) #:nodoc:
@path, params = model, params
@klass = model.to_s.classify.constantize
@@ -263,22 +275,46 @@
# if association is named lottery_prizes, then that association is returned
# if association is named prizes, then that association is returned
#
def relation(associated_resource)
if associated_resource && associated_resource.object
- name = associated_resource.as || plural_name
- associated_resource.object.class.reflect_on_association(name) ?
- associated_resource.object.send(name) : associated_resource.object.send(klass.to_s.demodulize.underscore.pluralize)
+ associated_resource.object.send(association_name(associated_resource))
else
klass
end
end
+
+ # Returns a collection that can be used for index, new and creation actions.
+ #
+ # If specifying an instance, returns the full path for that instance. Can be used when not using shallow routes
+ #
+ # === Example
+ #
+ # Assumption
+ # resource is instance of Lottery::Package #1 (@lottery_package)
+ # association is Lottery::Prizes
+ # if association is named lottery_prizes, then [@lottery_package, :lottery_prizes] is returned
+ # if association is named prizes, then [@lottery_package, :prizes] is returned
+ # if you specify an instance, then [@lottery_package, @prize] is returned
+ #
+ def collection(associated_resource, instance = nil)
+ if associated_resource && associated_resource.object
+ [associated_resource.object, instance || association_name(associated_resource)]
+ else
+ klass
+ end
+ end
+
+ def association_name(associated_resource) # :nodoc
+ name = associated_resource.as || plural_name
+ associated_resource.object.class.reflect_on_association(name) ? name : klass.to_s.demodulize.underscore.pluralize
+ end
end
# = Resource::ParentInfo
#
# Resolves the parent resource in subject
- # (see #ResourceInfo)
+ # (see #Resource::ResourceInfo)
class Resource::ParentInfo < Resource::Info
attr_reader :object,:as
def initialize(resources, params, request)
ptr = resources.detect do |r,as|
@klass = classify(r)