Sha256: 2e7bf3c85de8a0654df943717b65df337ac0ac76ae031036e2d0e06111ab5c32

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

module InheritedResources
  # Shallow provides a functionality that goes on pair with Rails' shallow.
  # It is very similar to "optional" but it actually finds all the parents
  # resources instead of leaving them blank. Consider the following example:
  #
  #   belongs_to :post, :shallow => true do
  #     belongs_to :comment
  #   end
  #
  # When accessed as /comments/1, Inherited Resources will automatically get
  # the post resource so both objects are actually accessible through the views.
  #
  # However, when using optional, Inherited Resources wouldn't actually bother
  # with finding the parent object.
  module ShallowHelpers
    private

      def symbols_for_association_chain #:nodoc:
        parent_symbols = parents_symbols.dup
        instance = nil

        if id = params[:id]
          finder_method = resources_configuration[:self][:finder] || :find
          instance      = self.resource_class.send(finder_method, id)
        elsif parents_symbols.size > 1
          config         = resources_configuration[parent_symbols.pop]
          finder_method  = config[:finder] || :find
          instance       = config[:parent_class].send(finder_method, params[config[:param]])
        end

        load_parents(instance, parent_symbols) if instance
        parents_symbols
      end

      def load_parents(instance, parent_symbols)
        parent_symbols.reverse_each do |parent|
          instance = instance.send(parent)
          config   = resources_configuration[parent]
          params[config[:param]] = instance.to_param
        end
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
inherited_resources-2.0.1 lib/inherited_resources/shallow_helpers.rb
inherited_resources-2.0.0 lib/inherited_resources/shallow_helpers.rb