module Dsl::Polymorphs

Public Instance Methods

polymorph(key, possible_types) click to toggle source

Allows to declare a polymorphic relationship

Params:

key

the corresponding key in the params hash

possible_types

A hash that follows the below format

polymorph(:content, {

foo: Bar

})

Int the above example the possible_types hash :

scannable => key
Scannable => model class
# File lib/dsl/polymorphs.rb, line 34
def polymorph(key, possible_types)
  validates(key, polymorph: { keys: possible_types.keys })

  @@polymorphic_keys[name] ||= []
  @@polymorphic_keys[name] << {
    key: key,
    types: possible_types
  }
  send(:attributes, key, "#{key}_id")
end