Sha256: ebf8ec86cfbfc2ff7d7065b9a84e90126e1d0b1ef4ec0f5f256b132ba6ca9b48

Contents?: true

Size: 1.66 KB

Versions: 6

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module LinkedRails
  module Helpers
    module ResourceHelper
      def current_resource
        return @current_resource if instance_variable_defined?(:@current_resource)

        @current_resource ||= resolve_current_resource
      end

      def new_resource
        @new_resource ||=
          if requested_resource.try(:singular_resource?)
            requested_resource
          elsif parent_resource
            new_resource_from_parent
          else
            build_new_resource
          end
      end

      def params_for_parent
        params.dup
      end

      def parent_from_params
        @parent_from_params ||= LinkedRails.iri_mapper.parent_from_params(params, user_context)
      end

      def parent_from_params!
        parent_from_params || raise(ActiveRecord::RecordNotFound)
      end

      def parent_resource
        @parent_resource ||= requested_resource_parent || parent_from_params
      end

      def parent_resource!
        parent_resource || raise(ActiveRecord::RecordNotFound)
      end

      def requested_resource_parent
        requested_resource.try(:parent)
      end

      private

      def build_new_resource
        controller_class.build_new(user_context: user_context)
      end

      def new_resource_from_parent
        return requested_resource.child_resource if requested_resource.is_a?(Collection)

        parent_resource.build_child(
          controller_class,
          user_context: user_context
        )
      end

      def resolve_current_resource
        case action_name
        when 'create'
          new_resource
        else
          requested_resource
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
linked_rails-0.0.4.pre.g14b377f91 lib/linked_rails/helpers/resource_helper.rb
linked_rails-0.0.4.pre.g2c53724b3 lib/linked_rails/helpers/resource_helper.rb
linked_rails-0.0.4.pre.g83aa52ab3 lib/linked_rails/helpers/resource_helper.rb
linked_rails-0.0.4.pre.gaa82a222d lib/linked_rails/helpers/resource_helper.rb
linked_rails-0.0.4.pre.g9b7bc9073 lib/linked_rails/helpers/resource_helper.rb
linked_rails-0.0.4.pre.ge1306665a lib/linked_rails/helpers/resource_helper.rb