Skip to content

Instantly share code, notes, and snippets.

@equivalent
Created August 21, 2020 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save equivalent/f9c5bfcea6e0afc5eb0f87510b26a21f to your computer and use it in GitHub Desktop.
Save equivalent/f9c5bfcea6e0afc5eb0f87510b26a21f to your computer and use it in GitHub Desktop.
Ruby on Rails - Bounded contexts via interface objects - Code Example 1
# app/models/comment.rb
class Comment < ActiveRecord::Base
belongs_to :student
belongs_to :work
end
# app/models/lesson.rb
class Lesson < ActiveRecord::Base
belongs_to :teacher
has_many :works
has_and_belongs_to_many :students
def classroom
@classroom ||= Classroom::LessonInterface.new(self)
end
def public_board
@public_board ||= PublicBoard::LessonInterface.new(self)
end
end
# db/schema.rb
ActiveRecord::Schema.define(version: 2019_05_22_134007) do
create_table "lessons" do |t|
t.string "title"
t.bigint "teacher_id"
t.boolean "published", default: false
end
create_table "students" do |t|
t.string "email"
end
create_table "students_lessons" do |t|
t.bigint "lesson_id"
t.bigint "student_id"
end
create_table "teachers" do |t|
t.string "email"
end
create_table "works" do |t|
t.bigint "lesson_id"
t.bigint "student_id"
end
create_table "comments" do |t|
t.bi
# app/models/student.rb
class Student < ActiveRecord::Base
has_many :works
has_many :comments
has_and_belongs_to_many :lessons
end
# app/models/teacher.rb
class Teacher < ActiveRecord::Base
has_many :lessons
def classroom
@classroom ||= Classroom::TeacherInterface.new(self)
end
end
# app/models/work.rb
class Work < ActiveRecord::Base
belongs_to :student
belongs_to :lesson
has_many :comments
def classroom
@classroom ||= Classroom::WorkInterface.new(self)
end
end