Sha256: 1e9fa6f0e3e3ff18186c00a5d3b31c394b35c8379cb84d200b6851f724fae9fd

Contents?: true

Size: 576 Bytes

Versions: 18

Compression:

Stored size: 576 Bytes

Contents

module Spyke
  module Associations
    class BelongsTo < Association
      def initialize(*args)
        super
        @options.reverse_merge!(uri: "#{@name.to_s.pluralize}/:#{primary_key}", foreign_key: "#{klass.model_name.element}_id")
        @params[primary_key] = primary_key_value
      end

      def find_one
        return unless fetchable?
        super
      end

      private

        def fetchable?
          (primary_key_value || embedded_data).present?
        end

        def primary_key_value
          parent.try(foreign_key)
        end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
spyke-7.2.2 lib/spyke/associations/belongs_to.rb
spyke-7.1.1 lib/spyke/associations/belongs_to.rb
spyke-7.1.0 lib/spyke/associations/belongs_to.rb
spyke-7.0.0 lib/spyke/associations/belongs_to.rb
spyke-6.1.3 lib/spyke/associations/belongs_to.rb
spyke-6.1.2 lib/spyke/associations/belongs_to.rb
spyke-6.1.1 lib/spyke/associations/belongs_to.rb
spyke-6.1.0 lib/spyke/associations/belongs_to.rb
spyke-6.0.0 lib/spyke/associations/belongs_to.rb
spyke-5.4.3 lib/spyke/associations/belongs_to.rb
spyke-5.4.2 lib/spyke/associations/belongs_to.rb
spyke-5.4.1 lib/spyke/associations/belongs_to.rb
spyke-5.4.0 lib/spyke/associations/belongs_to.rb
spyke-5.3.4 lib/spyke/associations/belongs_to.rb
spyke-5.3.3 lib/spyke/associations/belongs_to.rb
spyke-5.3.2 lib/spyke/associations/belongs_to.rb
spyke-5.3.1 lib/spyke/associations/belongs_to.rb
spyke-5.3.0 lib/spyke/associations/belongs_to.rb