Sha256: 5837769d5c20f36245d7255915ed9793ad64aa98fc27f8a220d9cd8b5ddbfd30

Contents?: true

Size: 1.27 KB

Versions: 4

Compression:

Stored size: 1.27 KB

Contents

# Nested and Polymorphic Resource Helpers
#
module ResourceController::Helpers::Nested
  protected
    # Returns the relevant association proxy of the parent. (i.e. /posts/1/comments # => @post.comments)
    #
    def parent_association
      @parent_association ||= parent_object.send(model_name.to_s.pluralize.to_sym)
    end
    
    # Returns the type of the current parent
    #
    def parent_type
      @parent_type ||= [*belongs_to].find { |parent| !params["#{parent}_id".to_sym].nil? }
    end
    
    # Returns true/false based on whether or not a parent is present.
    #
    def parent?
      !parent_type.nil?
    end
    
    # Returns the current parent param, if there is a parent. (i.e. params[:post_id])
    def parent_param
      params["#{parent_type}_id".to_sym]
    end
    
    # Like the model method, but for a parent relationship.
    # 
    def parent_model
      parent_type.to_s.camelize.constantize
    end
    
    # Returns the current parent object if a parent object is present.
    #
    def parent_object
      parent? ? parent_model.find(parent_param) : nil
    end
    
    # If there is a parent, returns the relevant association proxy.  Otherwise returns model.
    #
    def end_of_association_chain
      parent? ? parent_association : model
    end
end

Version data entries

4 entries across 4 versions & 4 rubygems

Version Path
giraffesoft-resource_controller-0.4.9 lib/resource_controller/helpers/nested.rb
pager-resource_controller-1.0.20080513 lib/resource_controller/helpers/nested.rb
resource_controller-0.4.9 lib/resource_controller/helpers/nested.rb
spree-0.2.0 vendor/plugins/resource_controller/lib/resource_controller/helpers/nested.rb