Skip to content

Commit

Permalink
Models: Re-order acts_as_tenant declaration.
Browse files Browse the repository at this point in the history
When acts_as_tenant comes before other associations, it does not seem to
properly check the tenant when associating model instances with eachother. (WTF)

I debugged this for hours. See ErwinM/acts_as_tenant#255.

References #163.
  • Loading branch information
Yong Bakos committed Mar 16, 2021
1 parent c3e462f commit 69369f7
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion app/models/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ class Activity < ApplicationRecord
validate :date_must_be_valid_activity_day, unless: Proc.new { date.nil? }
validates_uniqueness_of :room, scope: [:date, :name], case_sensitive: false

acts_as_tenant(:school)
has_many :registrations, dependent: :destroy

acts_as_tenant :school

def self.for_week(date)
Week::ACTIVITY_DAYS.reduce({}) do |week, day|
week[date.send(day)] = Activity.where(date: date.send(day)).order('name').to_a
Expand Down
3 changes: 2 additions & 1 deletion app/models/registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ class Registration < ApplicationRecord

belongs_to :activity, counter_cache: true
belongs_to :creator, class_name: 'User'
acts_as_tenant(:school)
belongs_to :student, class_name: 'User'
belongs_to :teacher

acts_as_tenant :school

validates :activity, uniqueness: {scope: :student}
validate :student_must_be_student
validate :student_not_registered_for_another_activity_on_same_date, unless: :updating_activity
Expand Down
3 changes: 2 additions & 1 deletion app/models/teacher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ class Teacher < ApplicationRecord
validates :name, uniqueness: { scope: :title, case_sensitive: false }
validates :active, inclusion: { in: [true, false] }

acts_as_tenant(:school)
has_many :students, class_name: 'User', dependent: :restrict_with_error
has_many :registrations, dependent: :restrict_with_error

acts_as_tenant :school

default_scope { order(:name) }
scope :active, -> { where(active: true) }
scope :deactivated, -> { where(active: false) }
Expand Down
3 changes: 2 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ class User < ApplicationRecord
validates_presence_of :last_name
validates :active, inclusion: { in: [true, false] }

acts_as_tenant(:school)
belongs_to :teacher, optional: true
validates_presence_of :teacher_id, unless: Proc.new { |u| u.new_record? || !u.student? || !u.active || u.active_changed?(from: false) }
validate :school_of_teacher_matches, if: -> { teacher.present? }
Expand All @@ -20,6 +19,8 @@ class User < ApplicationRecord

has_many :activities, through: :registrations

acts_as_tenant :school

default_scope { order(:last_name) }
scope :active, -> { where(active: true) }
scope :deactivated, -> { where(active: false) }
Expand Down

0 comments on commit 69369f7

Please sign in to comment.