Created
January 18, 2013 04:02
-
-
Save spacecowb0y/4562253 to your computer and use it in GitHub Desktop.
God bless AR!
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 Order < ActiveRecord::Base | |
## Setup accessible (or protected) attributes for your model | |
attr_accessible :project_id, :developer_id, :due_date, :status, :notes, :is_delivered, :deliveted_at, :completed_at | |
## Virtual attributes | |
attr_accessor :pages | |
## Associations | |
belongs_to :project | |
belongs_to :developer, :inverse_of => :assigned_orders, | |
:class_name => "User" | |
## Callbacks | |
before_save :assign_developer, :if => :developer_id_changed? | |
## Custom methods | |
def assign_developer | |
# we create an assignment for the new developer on the project | |
# if it doesn't have one already | |
self.project.assignments.create!(:user_id => self.developer_id) if self.project.assignments.find_by_user_id(self.developer_id).nil? | |
# if the old developer doesn't have any other order assigned on this project | |
# we should (?) delete the assignment to it | |
self.project.assignments.find_by_user_id(self.developer_id_was).destroy unless self.project.orders.find_by_developer_id(self.developer_id_was).nil? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment