Sha256: 22653db75532555560367371337d33955c2f87d32c242abf09a42c91f35f7589

Contents?: true

Size: 994 Bytes

Versions: 5

Compression:

Stored size: 994 Bytes

Contents

module GreyscaleRecord
  module Associations
    module Hasable
      extend ActiveSupport::Concern

      included do

        def query( instance )
          if through?
            { id: target_ids( instance ) }
          elsif as?
            { "#{as}_id" => instance.id }
          else
            { key => instance.id }
          end
        end

        def additional_options
          [ :as, :through ]
        end

        protected

        def through
          opts[:through]
        end

        def through?
          opts.has_key? :through
        end

        def as
          opts[:as]
        end

        def as?
          opts.has_key? :as
        end

        def target_ids( instance )
          intermadiates = Array(instance.send( through ) )
          
          intermadiates.map do | intermediate | 
            intermediate.send( through_key )
          end
        end
        
        def through_key
          idify klass
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
greyscale_record-1.0.3 lib/greyscale_record/associations/hasable.rb
greyscale_record-1.0.2 lib/greyscale_record/associations/hasable.rb
greyscale_record-1.0.1 lib/greyscale_record/associations/hasable.rb
greyscale_record-1.0.0 lib/greyscale_record/associations/hasable.rb
greyscale_record-0.0.1 lib/greyscale_record/associations/hasable.rb