Sha256: b3c991cdc187c761c1f14da168f84dc53dad0e8a9f615ec8921eb0b282e040db

Contents?: true

Size: 1.9 KB

Versions: 11

Compression:

Stored size: 1.9 KB

Contents

module Graphiti
  module Util
    class Link
      def initialize(sideload, model)
        @sideload = sideload
        @model = model
        @linkable = true

        if @sideload.type == :polymorphic_belongs_to
          if (type = @model.send(@sideload.grouper.field_name))
            @sideload = @sideload.children.values.find { |c|
              c.group_name == type.to_sym
            }
            @polymorphic_sideload_not_found = true unless @sideload
          else
            @linkable = false
          end
        end
      end

      def generate
        if linkable?
          on_demand_links(raw_url)
        end
      end

      private

      def linkable?
        return false if @polymorphic_sideload_not_found

        if @sideload.type == :belongs_to
          !@model.send(@sideload.foreign_key).nil?
        else
          @linkable
        end
      end

      def raw_url
        if @sideload.link_proc
          @sideload.link_proc.call(@model)
        elsif params.empty?
          path
        else
          "#{path}?#{CGI.unescape(params.to_query)}"
        end
      end

      def on_demand_links(url)
        return url unless Graphiti.config.links_on_demand

        url << if url.include?("?")
          "&links=true"
        else
          "?links=true"
        end
        url
      end

      def params
        @params ||= {}.tap do |params|
          if @sideload.type != :belongs_to || @sideload.remote?
            params[:filter] = @sideload.link_filter([@model])
          end

          @sideload.params_proc&.call(params, [@model], context)
        end
      end

      def path
        @path ||=
          path = @sideload.resource.endpoint[:url].to_s
        if @sideload.type == :belongs_to && !@sideload.remote?
          path = "#{path}/#{@model.send(@sideload.foreign_key)}"
        end
        path
      end

      def context
        Graphiti.context[:object]
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
graphiti-1.2.33 lib/graphiti/util/link.rb
graphiti-1.2.32 lib/graphiti/util/link.rb
graphiti-1.2.31 lib/graphiti/util/link.rb
graphiti-1.2.30 lib/graphiti/util/link.rb
graphiti-1.2.29 lib/graphiti/util/link.rb
graphiti-1.2.28 lib/graphiti/util/link.rb
graphiti-1.2.27 lib/graphiti/util/link.rb
graphiti-1.2.26 lib/graphiti/util/link.rb
graphiti-1.2.25 lib/graphiti/util/link.rb
graphiti-1.2.21 lib/graphiti/util/link.rb
graphiti-1.2.20 lib/graphiti/util/link.rb