Sha256: 71d0e362e409aad6a025cd95ad242d532da0849932bd160ef7754de06606f83b

Contents?: true

Size: 808 Bytes

Versions: 2

Compression:

Stored size: 808 Bytes

Contents

module Dorsale::PolymorphicId
  extend ActiveSupport::Concern
  included do

    def self.polymorphic_id_for(relation_name)
      module_src = File.read(__FILE__).split("__END__").last
      module_src = module_src.gsub("relation", relation_name.to_s)
      send :include, eval(module_src)
    end

    def guid
      return nil if new_record?

      "#{self.class.base_class}-#{self.id}"
    end
  end # included
end # module

ActiveRecord::Base.send(:include, Dorsale::PolymorphicId)

# __END__

Module.new do
  def relation_guid
    return nil if relation_type.blank? || relation_id.blank?

    "#{relation_type}-#{relation_id}"
  end

  def relation_guid=(guid)
    return self.relation = nil if guid.blank?

    type, id      = guid.split("-", 2)
    self.relation = type.constantize.find(id)
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dorsale-3.3.0 lib/dorsale/polymorphic_id.rb
dorsale-3.2.0 lib/dorsale/polymorphic_id.rb