Guest User

Untitled

a guest
Apr 22nd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. class Itinerary < ActiveRecord::Base
  2. belongs_to :package
  3. has_and_belongs_to_many :experiences
  4.  
  5. def add_package(package)
  6. self.experience_ids += package.experience_ids
  7. consolidate_experiences
  8. end
  9.  
  10. def remove_package(package)
  11. self.experience_ids -= package.experience_ids
  12. consolidate_experiences
  13. end
  14.  
  15. def add_experience(experience)
  16. self.experiences << experience
  17. consolidate_experiences
  18. end
  19.  
  20. def remove_experience(experience)
  21. self.experiences << experience
  22. consolidate_experiences
  23. end
  24.  
  25. private
  26. def consolidate_experiences
  27. # Look at experiences, are they a package, if so flag itinerary as a package
  28. self.package = (self.experience_ids.sort.uniq == self.experiences.first.package.experience_ids.sort.uniq) ? self.experiences.first.package : nil
  29. end
  30. end
Add Comment
Please, Sign In to add comment