Sha256: c8b50c66d0c681efa6fec3bc32ca53ed1d2e54e5b4a5193d34f2394bf3e1b25f

Contents?: true

Size: 1.93 KB

Versions: 11

Compression:

Stored size: 1.93 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 request_path_to_url(path)
        return path unless path.present? && URI(path).relative?

        port = [80, 443].include?(request.port) ? nil : request.port
        URI::Generic.new(request.scheme, nil, request.host, port, nil, path, nil, nil, nil).to_s
      end

      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

11 entries across 11 versions & 1 rubygems

Version Path
linked_rails-0.0.4.pre.g96885856b lib/linked_rails/helpers/resource_helper.rb
linked_rails-0.0.4.pre.g1faca9643 lib/linked_rails/helpers/resource_helper.rb
linked_rails-0.0.4.pre.g4c8e21c9f lib/linked_rails/helpers/resource_helper.rb
linked_rails-0.0.4.pre.g76dedd8ba lib/linked_rails/helpers/resource_helper.rb
linked_rails-0.0.4.pre.g7123943aa lib/linked_rails/helpers/resource_helper.rb
linked_rails-0.0.4.pre.g72bb595fe lib/linked_rails/helpers/resource_helper.rb
linked_rails-0.0.4.pre.gd8464437f lib/linked_rails/helpers/resource_helper.rb
linked_rails-0.0.4.pre.g661cde413 lib/linked_rails/helpers/resource_helper.rb
linked_rails-0.0.4.pre.ge09501794 lib/linked_rails/helpers/resource_helper.rb
linked_rails-0.0.4.pre.gc3dfc6914 lib/linked_rails/helpers/resource_helper.rb
linked_rails-0.0.4.pre.g92825d924 lib/linked_rails/helpers/resource_helper.rb