Skip to content

Instantly share code, notes, and snippets.

@lazybios
Forked from ordoghl/polymorphic_ass_grape_entity
Created February 14, 2017 06:49
Show Gist options
  • Select an option

  • Save lazybios/a1d9be9f6671ab6aea96a84d2240f403 to your computer and use it in GitHub Desktop.

Select an option

Save lazybios/a1d9be9f6671ab6aea96a84d2240f403 to your computer and use it in GitHub Desktop.

Revisions

  1. @ordoghl ordoghl created this gist Sep 19, 2013.
    28 changes: 28 additions & 0 deletions polymorphic_ass_grape_entity
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    require_relative 'customer.rb'
    require_relative 'contact.rb'
    require_relative 'agent.rb'

    module Api
    module V1
    module Entities

    class Ticket < Grape::Entity
    expose :id
    expose :subject
    expose :description
    expose :customer, using: Api::V1::Entities::Customer
    expose :contact, using: Api::V1::Entities::Contact
    expose :created_by do |ticket, options|
    if ticket.created_by.class.to_s == 'Contact'
    { "contact" => Api::V1::Entities::Contact.represent(ticket.created_by) }
    else
    { "agent" => Api::V1::Entities::Agent.represent(ticket.created_by) }
    end
    end
    expose :created_at
    expose :updated_at
    end

    end
    end
    end