Sha256: e5628e1eb4c926d3cf27f7a18bc13c60ebc2f1fdb50f0607c7404cc1c6a234cc

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

module Axel
  module Associations
    class BelongsTo < Base

      private

      def included_getter(instance, *args, &block)
        relation_attributes = (instance.try(:attributes) || {})[relation_name]
        build_klass.new(relation_attributes) if relation_attributes
      end

      def find_nested?
        !!options[:find_nested]
      end

      def getter(instance, *args, &block)
        request_options = args.extract_options!
        if find_nested?
          build_klass
            .querier
            .without_default_path
            .at_path(route_path(instance))
            .request_options(request_options)
            .first
        else
          build_klass.find(instance.public_send(association_id_method), request_options)
        end
      end

      def route_path(instance)
        [URI(instance.request_uri).path, association_path(instance)].join("/")
      end

      def association_path(instance) # needs to be belongs_to, some looks has_many
        "#{relation_name.to_s.pluralize}/#{instance.public_send(association_id_method)}"
      end

      def association_id_method
        options[:id_attribute] || "#{relation_name.to_s.singularize.underscore}_id"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
axel-0.0.1 app/models/axel/associations/belongs_to.rb