Sha256: c743cb29dcff797d264c647736ac0dd7672b397cea370fbf95b0e3c581c2dfcf

Contents?: true

Size: 1.07 KB

Versions: 2

Compression:

Stored size: 1.07 KB

Contents

require 'metabolical/meta_datum'
require 'metabolical/scopes'

module Metabolical
  class Error < StandardError; end
  
  def self.included(klass)
    klass.extend ClassMethods
  end  
  
  module ClassMethods
    def metabolize!
      if self.respond_to?(:named_scope)
        include Metabolical::AR3Scopes
      else
        include Metabolical::AR4Scopes
      end

      class_eval do
        has_many :metas, :as => :metabolized, :class_name => 'Metabolical::MetaDatum' do
          def [](key)
            owner = (self.respond_to?(:proxy_association) ? self.proxy_association.owner : self.proxy_owner)
            (owner.metas.loaded? && owner.metas.detect{|m| m.key == key} ) || find_by(key: key) || owner.metas.build(:key => key)
          end
          
          def []=(key, data)
            owner = (self.respond_to?(:proxy_association) ? self.proxy_association.owner : self.proxy_owner)
            meta = self[key]
            meta.data = data
            meta.save unless owner.new_record?
            meta
          end
        end
      end
      
    end
  end
    
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
metabolical-1.2.1 lib/metabolical.rb
metabolical-1.2.0 lib/metabolical.rb