Sha256: 4ed2f5a69683661b12ba8b1f1990f88968addc8d0e557354ebd86bd48214bcc3

Contents?: true

Size: 1.47 KB

Versions: 52

Compression:

Stored size: 1.47 KB

Contents

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

        if @sideload.type == :polymorphic_belongs_to
          type = @model.send(@sideload.grouper.field_name)
          @sideload = @sideload.children.values.find do |c|
            c.group_name == type.to_sym
          end
        end
      end

      def generate
        on_demand_links(raw_url)
      end

      private

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

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

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

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

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

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

Version data entries

52 entries across 52 versions & 1 rubygems

Version Path
graphiti-1.0.rc.9 lib/graphiti/util/link.rb
graphiti-1.0.rc.8 lib/graphiti/util/link.rb
graphiti-1.0.rc.7 lib/graphiti/util/link.rb
graphiti-1.0.rc.6 lib/graphiti/util/link.rb
graphiti-1.0.rc.5 lib/graphiti/util/link.rb
graphiti-1.0.rc.4 lib/graphiti/util/link.rb
graphiti-1.0.rc.3 lib/graphiti/util/link.rb
graphiti-1.0.rc.2 lib/graphiti/util/link.rb
graphiti-1.0.rc.1 lib/graphiti/util/link.rb
graphiti-1.0.beta.23 lib/graphiti/util/link.rb
graphiti-1.0.beta.22 lib/graphiti/util/link.rb
graphiti-1.0.beta.21 lib/graphiti/util/link.rb
graphiti-1.0.beta.20 lib/graphiti/util/link.rb
graphiti-1.0.beta.19 lib/graphiti/util/link.rb
graphiti-1.0.beta.18 lib/graphiti/util/link.rb
graphiti-1.0.beta.17 lib/graphiti/util/link.rb
graphiti-1.0.beta.16 lib/graphiti/util/link.rb
graphiti-1.0.beta.15 lib/graphiti/util/link.rb
graphiti-1.0.beta.14 lib/graphiti/util/link.rb
graphiti-1.0.beta.13 lib/graphiti/util/link.rb