Skip to content

Latest commit

 

History

History
66 lines (53 loc) · 966 Bytes

Polymorphs.md

File metadata and controls

66 lines (53 loc) · 966 Bytes

Polymorphic relations

This feature allows to create a resource and its associated polymorphic relation with a single params object.

Declaration

class CommentForm < ActiveFormObjects::Base
  attributes :body
  polymorph :author, {
    admin: AdminForm,
    user: UserForm,
    customer: CustomerForm
  }
end

class UserForm < ActiveFormObjects::Base
  attributes :first_name
end

class AdminForm < ActiveFormObjects::Base
  attributes :login
end

class CustomerForm < ActiveFormObjects::Base
  attributes :last_name
end

Usage

With the above example, you can send the following :

{
  "body": "This form is awesome",
  "author": {
    "type": "user",
    "firstName": "Michaël"
  }
}

or

{
  "body": "This form is awesome",
  "author": {
    "type": "customer",
    "lastName": "Villeneuve"
  }
}

or

{
  "body": "This form is awesome",
  "author": {
    "type": "admin",
    "login": "mylogin"
  }
}