Skip to content

Commit b1f2cf2

Browse files
tuzzfloehopper
authored andcommitted
Add Lesson#projects and Project#lesson associations
1 parent 2f16573 commit b1f2cf2

File tree

4 files changed

+8
-0
lines changed

4 files changed

+8
-0
lines changed

app/models/lesson.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class Lesson < ApplicationRecord
55
belongs_to :school_class, optional: true
66
belongs_to :parent, optional: true, class_name: :Lesson, foreign_key: :copied_from_id, inverse_of: :copies
77
has_many :copies, dependent: :nullify, class_name: :Lesson, foreign_key: :copied_from_id, inverse_of: :parent
8+
has_many :projects, dependent: :nullify
89

910
before_validation :assign_school_from_school_class
1011
before_destroy -> { throw :abort }

app/models/project.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
class Project < ApplicationRecord
44
belongs_to :school, optional: true
5+
belongs_to :lesson, optional: true
56
belongs_to :parent, optional: true, class_name: :Project, foreign_key: :remixed_from_id, inverse_of: :remixes
67
has_many :remixes, dependent: :nullify, class_name: :Project, foreign_key: :remixed_from_id, inverse_of: :parent
78
has_many :components, -> { order(default: :desc, name: :asc) }, dependent: :destroy, inverse_of: :project

spec/models/lesson_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
lesson = create(:lesson, copies: [build(:lesson), build(:lesson)])
3030
expect(lesson.copies.size).to eq(2)
3131
end
32+
33+
it 'has many projects' do
34+
lesson = create(:lesson, projects: [build(:project), build(:project)])
35+
expect(lesson.projects.size).to eq(2)
36+
end
3237
end
3338

3439
describe 'callbacks' do

spec/models/project_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
RSpec.describe Project do
66
describe 'associations', :sample_words do
77
it { is_expected.to belong_to(:school).optional(true) }
8+
it { is_expected.to belong_to(:lesson).optional(true) }
89
it { is_expected.to belong_to(:parent).optional(true) }
910
it { is_expected.to have_many(:remixes).dependent(:nullify) }
1011
it { is_expected.to have_many(:components) }

0 commit comments

Comments
 (0)