Sha256: 89a099158cd60bee5aaed4752c8abf222783d64f8974918a06ed4f3818547bc3

Contents?: true

Size: 1.93 KB

Versions: 4

Compression:

Stored size: 1.93 KB

Contents

module InheritedResources #:nodoc:
  module BelongsToHelpers #:nodoc:

    # Private helpers, you probably don't have to worry with them.
    private

      def parent?
        true
      end

      # Evaluate the parent given. This is used to nest parents in the
      # association chain.
      #
      def evaluate_parent(parent_config, chain = nil)
        scoped_parent = if chain
          chain.send(parent_config[:collection_name])
        else
          parent_config[:parent_class]
        end

        scoped_parent = scoped_parent.send(parent_config[:finder], params[parent_config[:param]])

        instance_variable_set("@#{parent_config[:instance_name]}", scoped_parent)
      end

      # Overwrites the end_of_association_chain method.
      #
      # This methods gets your begin_of_association_chain, join it with your
      # parents chain and returns the scoped association.
      #
      def end_of_association_chain
        chain = symbols_for_chain.inject(begin_of_association_chain) do |chain, symbol|
          evaluate_parent(resources_configuration[symbol], chain)
        end

        return resource_class unless chain

        chain = chain.send(method_for_association_chain) if method_for_association_chain
        return chain
      end

      # If current controller is singleton, returns instance name to
      # end_of_association_chain. This means that we will have the following
      # chain:
      #
      #   Project.find(params[:project_id]).manager
      #
      # Instead of:
      #
      #   Project.find(params[:project_id]).managers
      #
      def method_for_association_chain
        singleton ? nil : resource_collection_name
      end

      # Maps parents_symbols to build association chain. In this case, it
      # simply return the parent_symbols, however on polymorphic belongs to,
      # it has some customization to deal with properly.
      #
      def symbols_for_chain
        parents_symbols
      end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
josevalim-inherited_resources-0.4.1 lib/inherited_resources/belongs_to_helpers.rb
josevalim-inherited_resources-0.4.2 lib/inherited_resources/belongs_to_helpers.rb
josevalim-inherited_resources-0.4.3 lib/inherited_resources/belongs_to_helpers.rb
josevalim-inherited_resources-0.4 lib/inherited_resources/belongs_to_helpers.rb