Guest User

Untitled

a guest
Apr 25th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. class Unit < ApplicationRecord
  2. # concerns
  3. include UnitStatus
  4. include InstructionType
  5.  
  6. # relations
  7. has_ancestry
  8. belongs_to :city
  9. has_many :responsibles, foreign_key: 'unit_id', class_name: 'Responsibility'
  10.  
  11. # validations
  12. validates :name, :yoksis_id, :status, :type, :instruction_type,
  13. presence: true, strict: true
  14. validates :yoksis_id,
  15. uniqueness: true, strict: true
  16. validates :name,
  17. uniqueness: { scope: %i[ancestry status] }
  18.  
  19. # callbacks
  20. before_validation do
  21. self.name = name.capitalize_all
  22. end
  23.  
  24. # STI helpers
  25. def self.types
  26. descendants.map(&:name)
  27. end
  28.  
  29. # dynamically generate scopes for STI
  30. types.each do |type|
  31. define_method("#{type.underscore.pluralize}") { Unit.where(type: type) }
  32. # scope type.underscore, where(type: type)
  33. end
  34.  
  35.  
  36. scope :universities, -> { where(type: 'University') }
  37. scope :faculties, -> { where(type: 'Faculty') }
  38. scope :institutes, -> { where(type: 'Institute') }
  39. scope :vocational_schools, -> { where(type: 'VocationalSchool') }
  40. scope :academies, -> { where(type: 'Academy') }
  41. scope :departments, -> { where(type: 'Department') }
  42. scope :science_disciplines, -> { where(type: 'ScienceDiscipline') }
  43. scope :art_disciplines, -> { where(type: 'ArtDiscipline') }
  44. scope :interdisciplinary_disciplines, -> { where(type: 'InterdisciplinaryDiscipline') }
  45. scope :disciplines, -> { where(type: 'Discipline') }
  46. scope :rectorships, -> { where(type: 'Rectorship') }
  47. scope :research_centers, -> { where(type: 'ResearchCenter') }
  48. scope :undergraduate_programs, -> { where(type: 'UndergraduateProgram') }
  49. scope :master_programs, -> { where(type: 'MasterProgram') }
  50. scope :doctoral_programs, -> { where(type: 'DoctoralProgram') }
  51. scope :interdisciplinary_master_programs, -> { where(type: 'InterdisciplinaryMasterProgram') }
  52. scope :interdisciplinary_doctoral_programs, -> { where(type: 'InterdisciplinaryDoctoralProgram') }
  53. scope :proficiency_in_art_programs, -> { where(type: 'ProficiencyInArtProgram') }
  54. end
Add Comment
Please, Sign In to add comment