Sha256: f74a45c925dcb54a59844776a7262f1c669c4e236f2c308ca17e212e3c49c165

Contents?: true

Size: 765 Bytes

Versions: 72

Compression:

Stored size: 765 Bytes

Contents

module Agilibox::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}-#{id}"
    end
  end # included
end # module

# __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)
    if guid.blank?
      self.relation = nil
      return
    end

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

Version data entries

72 entries across 72 versions & 1 rubygems

Version Path
agilibox-2.0.0 app/models/concerns/agilibox/polymorphic_id.rb
agilibox-1.11.0 app/models/concerns/agilibox/polymorphic_id.rb
agilibox-1.10.5 app/models/concerns/agilibox/polymorphic_id.rb
agilibox-1.10.2 app/models/concerns/agilibox/polymorphic_id.rb
agilibox-1.10.1 app/models/concerns/agilibox/polymorphic_id.rb
agilibox-1.10.0 app/models/concerns/agilibox/polymorphic_id.rb
agilibox-1.9.20 app/models/concerns/agilibox/polymorphic_id.rb
agilibox-1.9.19 app/models/concerns/agilibox/polymorphic_id.rb
agilibox-1.9.18 app/models/concerns/agilibox/polymorphic_id.rb
agilibox-1.9.17 app/models/concerns/agilibox/polymorphic_id.rb
agilibox-1.9.16 app/models/concerns/agilibox/polymorphic_id.rb
agilibox-1.9.15 app/models/concerns/agilibox/polymorphic_id.rb
agilibox-1.9.14 app/models/concerns/agilibox/polymorphic_id.rb
agilibox-1.9.13 app/models/concerns/agilibox/polymorphic_id.rb
agilibox-1.9.12 app/models/concerns/agilibox/polymorphic_id.rb
agilibox-1.9.11 app/models/concerns/agilibox/polymorphic_id.rb
agilibox-1.9.10 app/models/concerns/agilibox/polymorphic_id.rb
agilibox-1.9.9 app/models/concerns/agilibox/polymorphic_id.rb
agilibox-1.9.8 app/models/concerns/agilibox/polymorphic_id.rb
agilibox-1.9.7 app/models/concerns/agilibox/polymorphic_id.rb