Last active
December 16, 2015 08:59
-
-
Save arenoir/5409904 to your computer and use it in GitHub Desktop.
ember-data polymorphic and rails model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.List = DS.Model.extend | |
project: DS.BelongsTo(App.Project) | |
user: DS.BelongsTo(App.User) | |
task: DS.BelongsTo(App.Task) | |
proposal: DS.BelongsTo(App.Proposal) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class List < ActiveRecord::Base | |
attr_accessible :name, :project_id, :user_id, :task_id, proposal_id | |
belongs_to :listable, :polymorphic => true | |
%w(user project user proposal).each do |name| | |
class_eval %( | |
def #{name}_id | |
listable_id if listable_type == '#{name.classify}' | |
end | |
def #{name}_id=(_value) | |
self.listable_id = _value | |
self.listable_type = '#{name.classify}' | |
end | |
) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ListSerializer < ActiveModel::Serializer | |
attributes :id, :name, :project_id, :user_id, :task_id, proposal_id | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment