Sha256: 82b68a784a2f92349aa774e0e91bd896e1efc5e4e314c2f0cfeb17c084d01b6c

Contents?: true

Size: 891 Bytes

Versions: 1

Compression:

Stored size: 891 Bytes

Contents

require 'og/relation'

module Og

class RefersTo < Relation

  def enchant
    self[:foreign_key] = "#{foreign_name || target_singular_name}_#{target_pk}"

    if self[:field]
      field = ", :field => :#{self[:field]}"
    end 

    owner_class.module_eval %{
      attr_accessor :#{target_singular_name}
      prop_accessor :#{foreign_key}, #{target_pkclass}#{field}

      def #{target_singular_name}(reload = false)
        return nil if @#{foreign_key}.nil? 

        # will reload if forced or first load or
        if reload or not @#{target_singular_name}
          @#{target_singular_name} = #{target_class}[@#{foreign_key}]
        end
        @#{target_singular_name}
      end

      def #{target_singular_name}=(obj)
        @#{foreign_key} = obj.#{target_pk} if obj
      end
    }
  end

end

end

# * George Moschovitis <gm@navel.gr>
# * Michael Neumann <mneumann@ntecs.de>

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
og-0.22.0 lib/og/relation/refers_to.rb