Skip to content

Instantly share code, notes, and snippets.

@mallain
Created May 25, 2010 12:39
Show Gist options
  • Save mallain/413079 to your computer and use it in GitHub Desktop.
Save mallain/413079 to your computer and use it in GitHub Desktop.
######### Recuperation du premier collaborateur
>> Collaborator.first
=> #<Collaborator id: 3, firstname: "Madisen", lastname: "Altenwerth", email: "[email protected]", created_at: "2010-05-25 10:03:42", updated_at: "2010-05-25 12:27:40", function_id: 5, agency_id: 5>
######### Insertion d un évènement "changement de fonction" sur le collaborateur.
>> HrCollaboratorFunction.create!(:feedback_id => 13, :agency_id => 1, :collaborator_id => 3, :function_id => 2)
=> #<HrCollaboratorFunction id: 1, feedback_id: 13, agency_id: 1, function_id: 2, collaborator_id: 3, created_at: "2010-05-25 12:31:06", updated_at: "2010-05-25 12:31:06">
######### Affichage du premier collaborateur (il a changé de fonction)
>> Collaborator.first
=> #<Collaborator id: 3, firstname: "Madisen", lastname: "Altenwerth", email: "[email protected]", created_at: "2010-05-25 10:03:42", updated_at: "2010-05-25 12:31:06", function_id: 2, agency_id: 5>
######################################################################################################
######### Insertion d un évènement "changement de fonction" sur le collaborateur (antérieure en terme de temps)
>> HrCollaboratorFunction.create!(:feedback_id => 1, :agency_id => 1, :collaborator_id => 3, :function_id => 9)
=> #<HrCollaboratorFunction id: 2, feedback_id: 1, agency_id: 1, function_id: 9, collaborator_id: 3, created_at: "2010-05-25 12:31:24", updated_at: "2010-05-25 12:31:24">
######### Affichage du premier collaborateur (il n'a pas changé de fonction vu que la donnée est antérieure à la précédente)
>> Collaborator.first
=> #<Collaborator id: 3, firstname: "Madisen", lastname: "Altenwerth", email: "[email protected]", created_at: "2010-05-25 10:03:42", updated_at: "2010-05-25 12:31:06", function_id: 2, agency_id: 5>
class HrCollaboratorFunction < ActiveRecord::Base
before_create :callback_function
belongs_to :feedback
belongs_to :agency
belongs_to :function
belongs_to :collaborator
validates_presence_of :feedback, :agency, :function, :collaborator
validates_uniqueness_of(:collaborator_id, :scope => :feedback_id)
def callback_function
if last_time_function_change(self.collaborator) < self.feedback.feedback_time.to_s
c = Collaborator.find(self.collaborator_id)
c.function = self.function
c.save!
end
end
private
# last_time_function_change(collaborator)
# Retrieve feedback_time where the last time
# which collaborator have change his function
#
# collaborator as Collaborator Object
# return feedback_time in String
def last_time_function_change(collaborator)
feedback_time = HrCollaboratorFunction.maximum( 'feedbacks.feedback_time',
:include => :feedback,
:conditions => {:collaborator_id => collaborator.id}
)
feedback_time ? feedback_time : "1970-01-01 00:00:00"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment