Created
August 21, 2020 09:37
-
-
Save equivalent/f9c5bfcea6e0afc5eb0f87510b26a21f to your computer and use it in GitHub Desktop.
Ruby on Rails - Bounded contexts via interface objects - Code Example 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/comment.rb | |
class Comment < ActiveRecord::Base | |
belongs_to :student | |
belongs_to :work | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/student.rb | |
class Student < ActiveRecord::Base | |
has_many :works | |
has_many :comments | |
has_and_belongs_to_many :lessons | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/models/teacher.rb | |
class Teacher < ActiveRecord::Base | |
has_many :lessons | |
def classroom | |
@classroom ||= Classroom::TeacherInterface.new(self) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a GIST for Medium mirror article Ruby on Rails - Bounded contexts via interface object
https://medium.com/@tomas.valent/ruby-on-rails-bounded-contexts-via-interface-objects-4fd61738acaa
Original article: https://blog.eq8.eu/article/rails-bounded-contexts.html
Full list of Article GISTs: