Sha256: 8f4188234a51bac4510dcd064390569f46189515ac80f21bddc9c3467969d05e

Contents?: true

Size: 758 Bytes

Versions: 19

Compression:

Stored size: 758 Bytes

Contents

module BMC::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

19 entries across 19 versions & 1 rubygems

Version Path
bmc-1.6.1 app/models/concerns/bmc/polymorphic_id.rb
bmc-1.6.0 app/models/concerns/bmc/polymorphic_id.rb
bmc-1.5.1 app/models/concerns/bmc/polymorphic_id.rb
bmc-1.5.0 app/models/concerns/bmc/polymorphic_id.rb
bmc-1.4.3 app/models/concerns/bmc/polymorphic_id.rb
bmc-1.4.2 app/models/concerns/bmc/polymorphic_id.rb
bmc-1.4.1 app/models/concerns/bmc/polymorphic_id.rb
bmc-1.4.0 app/models/concerns/bmc/polymorphic_id.rb
bmc-1.3.5 app/models/concerns/bmc/polymorphic_id.rb
bmc-1.3.4 app/models/concerns/bmc/polymorphic_id.rb
bmc-1.3.3 app/models/concerns/bmc/polymorphic_id.rb
bmc-1.3.2 app/models/concerns/bmc/polymorphic_id.rb
bmc-1.3.1 app/models/concerns/bmc/polymorphic_id.rb
bmc-1.3.0 app/models/concerns/bmc/polymorphic_id.rb
bmc-1.2.1 app/models/concerns/bmc/polymorphic_id.rb
bmc-1.2.0 app/models/concerns/bmc/polymorphic_id.rb
bmc-1.1.0 app/models/concerns/bmc/polymorphic_id.rb
bmc-1.0.1 app/models/concerns/bmc/polymorphic_id.rb
bmc-1.0.0 app/models/concerns/bmc/polymorphic_id.rb