├── .gitattributes ├── .gitignore ├── .rubocop.yml ├── .ruby-version ├── Gemfile ├── Gemfile.lock ├── README.md ├── Rakefile ├── app ├── channels │ └── application_cable │ │ ├── channel.rb │ │ └── connection.rb ├── controllers │ ├── application_controller.rb │ └── concerns │ │ └── .keep ├── domains │ ├── analytics │ │ ├── client.rb │ │ ├── configuration.rb │ │ ├── model │ │ │ └── event.rb │ │ └── service │ │ │ ├── track_event_version1.rb │ │ │ ├── track_event_version2.rb │ │ │ ├── track_event_version3.rb │ │ │ └── track_event_version4.rb │ ├── car_catalog │ │ ├── domain │ │ │ └── car.rb │ │ └── infrastructure │ │ │ ├── db_car.rb │ │ │ └── db_car_repository.rb │ └── customer │ │ └── model │ │ ├── orm_driven │ │ ├── customer.rb │ │ ├── email_syntax_validator.rb │ │ ├── phone_syntax_validator.rb │ │ └── rate.rb │ │ ├── with_person_model │ │ ├── address.rb │ │ ├── address_validator.rb │ │ ├── customer.rb │ │ ├── person.rb │ │ ├── person_validator.rb │ │ ├── phone_syntax_validator.rb │ │ └── rate.rb │ │ ├── with_vo │ │ ├── address.rb │ │ ├── address_validator.rb │ │ ├── customer.rb │ │ ├── email_syntax_validator.rb │ │ ├── personal_information.rb │ │ ├── personal_information_validator.rb │ │ ├── phone_syntax_validator.rb │ │ └── rate.rb │ │ └── with_vo_and_identity_and_behavior │ │ ├── address.rb │ │ ├── address_validator.rb │ │ ├── customer.rb │ │ ├── personal_information.rb │ │ ├── personal_information_validator.rb │ │ ├── phone_syntax_validator.rb │ │ └── rate.rb ├── jobs │ └── application_job.rb ├── mailers │ └── application_mailer.rb ├── models │ ├── application_record.rb │ ├── concerns │ │ └── .keep │ └── user.rb └── views │ └── layouts │ ├── mailer.html.erb │ └── mailer.text.erb ├── bin ├── bundle ├── rails ├── rake └── setup ├── config.ru ├── config ├── application.rb ├── boot.rb ├── cable.yml ├── credentials.yml.enc ├── database.yml ├── environment.rb ├── environments │ ├── development.rb │ ├── production.rb │ └── test.rb ├── initializers │ ├── cors.rb │ ├── filter_parameter_logging.rb │ └── inflections.rb ├── locales │ └── en.yml ├── puma.rb ├── routes.rb └── storage.yml ├── db ├── migrate │ ├── 20220821195526_create_users.rb │ ├── 20220822114828_create_customers_orm_driven_customers.rb │ ├── 20220822115002_create_customers_orm_driven_rates.rb │ ├── 20220822174958_create_customers_with_vo_customers.rb │ ├── 20220822175005_create_customers_with_vo_rates.rb │ ├── 20220822175058_create_customers_with_vo_and_identity_and_behavior_customers.rb │ ├── 20220822175123_create_customers_with_vo_and_identity_and_behavior_rates.rb │ ├── 20220822215125_create_customers_with_person_model_customers.rb │ ├── 20220822215130_create_customers_with_person_model_rates.rb │ ├── 20220822215136_create_customers_with_person_model_persons.rb │ └── 20221207084108_create_car_catalog_cars.rb ├── schema.rb └── seeds.rb ├── lib └── tasks │ └── .keep ├── log └── .keep ├── public └── robots.txt ├── storage └── .keep ├── test ├── analytics │ └── service.rb ├── car_catalog │ └── car_repository_test.rb └── customer │ └── model │ ├── orm_driven.rb │ ├── with_person_model.rb │ ├── with_vo.rb │ └── with_vo_and_identity_and_behavior.rb ├── tmp ├── .keep ├── pids │ └── .keep └── storage │ └── .keep └── vendor └── .keep /.gitattributes: -------------------------------------------------------------------------------- 1 | # See https://git-scm.com/docs/gitattributes for more about git attribute files. 2 | 3 | # Mark the database schema as having been generated. 4 | db/schema.rb linguist-generated 5 | 6 | # Mark any vendored files as having been vendored. 7 | vendor/* linguist-vendored 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files for more about ignoring files. 2 | # 3 | # If you find yourself ignoring temporary files generated by your text editor 4 | # or operating system, you probably want to add a global ignore instead: 5 | # git config --global core.excludesfile '~/.gitignore_global' 6 | 7 | # Ignore bundler config. 8 | /.bundle 9 | 10 | # Ignore the default SQLite database. 11 | /db/*.sqlite3 12 | /db/*.sqlite3-* 13 | 14 | # Ignore all logfiles and tempfiles. 15 | /log/* 16 | /tmp/* 17 | !/log/.keep 18 | !/tmp/.keep 19 | 20 | # Ignore pidfiles, but keep the directory. 21 | /tmp/pids/* 22 | !/tmp/pids/ 23 | !/tmp/pids/.keep 24 | 25 | # Ignore uploaded files in development. 26 | /storage/* 27 | !/storage/.keep 28 | /tmp/storage/* 29 | !/tmp/storage/ 30 | !/tmp/storage/.keep 31 | 32 | # Ignore master key for decrypting credentials and more. 33 | /config/master.key 34 | -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | AllCops: 2 | NewCops: enable 3 | DisplayCopNames: true 4 | DisplayStyleGuide: true 5 | Exclude: 6 | - '**/config.ru' 7 | - '**/Rakefile' 8 | - 'bin/**/*' 9 | - 'config/**/*' 10 | - 'db/schema.rb' 11 | - 'db/seeds.rb' 12 | - 'log/**/*' 13 | - 'tmp/**/*' 14 | - 'vendor/**/*' 15 | 16 | Layout/LineLength: 17 | Max: 120 18 | Style/Documentation: 19 | Enabled: false 20 | Style/FrozenStringLiteralComment: 21 | Enabled: false 22 | Style/StringLiterals: 23 | EnforcedStyle: double_quotes 24 | -------------------------------------------------------------------------------- /.ruby-version: -------------------------------------------------------------------------------- 1 | ruby-3.0.3 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | git_source(:github) { |repo| "https://github.com/#{repo}.git" } 3 | 4 | ruby "3.0.3" 5 | 6 | gem "bootsnap", require: false 7 | gem "puma", "~> 5.0" 8 | gem "rails", "~> 7.0.3" 9 | gem "sqlite3", "~> 1.4" 10 | gem "tzinfo-data", platforms: %i[mingw mswin x64_mingw jruby] 11 | 12 | group :development, :test do 13 | gem "pry" 14 | end 15 | 16 | group :development do 17 | gem "rubocop" 18 | end 19 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | actioncable (7.0.3.1) 5 | actionpack (= 7.0.3.1) 6 | activesupport (= 7.0.3.1) 7 | nio4r (~> 2.0) 8 | websocket-driver (>= 0.6.1) 9 | actionmailbox (7.0.3.1) 10 | actionpack (= 7.0.3.1) 11 | activejob (= 7.0.3.1) 12 | activerecord (= 7.0.3.1) 13 | activestorage (= 7.0.3.1) 14 | activesupport (= 7.0.3.1) 15 | mail (>= 2.7.1) 16 | net-imap 17 | net-pop 18 | net-smtp 19 | actionmailer (7.0.3.1) 20 | actionpack (= 7.0.3.1) 21 | actionview (= 7.0.3.1) 22 | activejob (= 7.0.3.1) 23 | activesupport (= 7.0.3.1) 24 | mail (~> 2.5, >= 2.5.4) 25 | net-imap 26 | net-pop 27 | net-smtp 28 | rails-dom-testing (~> 2.0) 29 | actionpack (7.0.3.1) 30 | actionview (= 7.0.3.1) 31 | activesupport (= 7.0.3.1) 32 | rack (~> 2.0, >= 2.2.0) 33 | rack-test (>= 0.6.3) 34 | rails-dom-testing (~> 2.0) 35 | rails-html-sanitizer (~> 1.0, >= 1.2.0) 36 | actiontext (7.0.3.1) 37 | actionpack (= 7.0.3.1) 38 | activerecord (= 7.0.3.1) 39 | activestorage (= 7.0.3.1) 40 | activesupport (= 7.0.3.1) 41 | globalid (>= 0.6.0) 42 | nokogiri (>= 1.8.5) 43 | actionview (7.0.3.1) 44 | activesupport (= 7.0.3.1) 45 | builder (~> 3.1) 46 | erubi (~> 1.4) 47 | rails-dom-testing (~> 2.0) 48 | rails-html-sanitizer (~> 1.1, >= 1.2.0) 49 | activejob (7.0.3.1) 50 | activesupport (= 7.0.3.1) 51 | globalid (>= 0.3.6) 52 | activemodel (7.0.3.1) 53 | activesupport (= 7.0.3.1) 54 | activerecord (7.0.3.1) 55 | activemodel (= 7.0.3.1) 56 | activesupport (= 7.0.3.1) 57 | activestorage (7.0.3.1) 58 | actionpack (= 7.0.3.1) 59 | activejob (= 7.0.3.1) 60 | activerecord (= 7.0.3.1) 61 | activesupport (= 7.0.3.1) 62 | marcel (~> 1.0) 63 | mini_mime (>= 1.1.0) 64 | activesupport (7.0.3.1) 65 | concurrent-ruby (~> 1.0, >= 1.0.2) 66 | i18n (>= 1.6, < 2) 67 | minitest (>= 5.1) 68 | tzinfo (~> 2.0) 69 | ast (2.4.2) 70 | bootsnap (1.13.0) 71 | msgpack (~> 1.2) 72 | builder (3.2.4) 73 | coderay (1.1.3) 74 | concurrent-ruby (1.1.10) 75 | crass (1.0.6) 76 | digest (3.1.0) 77 | erubi (1.11.0) 78 | globalid (1.0.0) 79 | activesupport (>= 5.0) 80 | i18n (1.12.0) 81 | concurrent-ruby (~> 1.0) 82 | json (2.6.2) 83 | loofah (2.18.0) 84 | crass (~> 1.0.2) 85 | nokogiri (>= 1.5.9) 86 | mail (2.7.1) 87 | mini_mime (>= 0.1.1) 88 | marcel (1.0.2) 89 | method_source (1.0.0) 90 | mini_mime (1.1.2) 91 | minitest (5.16.3) 92 | msgpack (1.5.4) 93 | net-imap (0.2.3) 94 | digest 95 | net-protocol 96 | strscan 97 | net-pop (0.1.1) 98 | digest 99 | net-protocol 100 | timeout 101 | net-protocol (0.1.3) 102 | timeout 103 | net-smtp (0.3.1) 104 | digest 105 | net-protocol 106 | timeout 107 | nio4r (2.5.8) 108 | nokogiri (1.13.8-x86_64-darwin) 109 | racc (~> 1.4) 110 | parallel (1.22.1) 111 | parser (3.1.2.1) 112 | ast (~> 2.4.1) 113 | pry (0.14.1) 114 | coderay (~> 1.1) 115 | method_source (~> 1.0) 116 | puma (5.6.4) 117 | nio4r (~> 2.0) 118 | racc (1.6.0) 119 | rack (2.2.4) 120 | rack-test (2.0.2) 121 | rack (>= 1.3) 122 | rails (7.0.3.1) 123 | actioncable (= 7.0.3.1) 124 | actionmailbox (= 7.0.3.1) 125 | actionmailer (= 7.0.3.1) 126 | actionpack (= 7.0.3.1) 127 | actiontext (= 7.0.3.1) 128 | actionview (= 7.0.3.1) 129 | activejob (= 7.0.3.1) 130 | activemodel (= 7.0.3.1) 131 | activerecord (= 7.0.3.1) 132 | activestorage (= 7.0.3.1) 133 | activesupport (= 7.0.3.1) 134 | bundler (>= 1.15.0) 135 | railties (= 7.0.3.1) 136 | rails-dom-testing (2.0.3) 137 | activesupport (>= 4.2.0) 138 | nokogiri (>= 1.6) 139 | rails-html-sanitizer (1.4.3) 140 | loofah (~> 2.3) 141 | railties (7.0.3.1) 142 | actionpack (= 7.0.3.1) 143 | activesupport (= 7.0.3.1) 144 | method_source 145 | rake (>= 12.2) 146 | thor (~> 1.0) 147 | zeitwerk (~> 2.5) 148 | rainbow (3.1.1) 149 | rake (13.0.6) 150 | regexp_parser (2.5.0) 151 | rexml (3.2.5) 152 | rubocop (1.35.0) 153 | json (~> 2.3) 154 | parallel (~> 1.10) 155 | parser (>= 3.1.2.1) 156 | rainbow (>= 2.2.2, < 4.0) 157 | regexp_parser (>= 1.8, < 3.0) 158 | rexml (>= 3.2.5, < 4.0) 159 | rubocop-ast (>= 1.20.1, < 2.0) 160 | ruby-progressbar (~> 1.7) 161 | unicode-display_width (>= 1.4.0, < 3.0) 162 | rubocop-ast (1.21.0) 163 | parser (>= 3.1.1.0) 164 | ruby-progressbar (1.11.0) 165 | sqlite3 (1.4.4) 166 | strscan (3.0.4) 167 | thor (1.2.1) 168 | timeout (0.3.0) 169 | tzinfo (2.0.5) 170 | concurrent-ruby (~> 1.0) 171 | unicode-display_width (2.2.0) 172 | websocket-driver (0.7.5) 173 | websocket-extensions (>= 0.1.0) 174 | websocket-extensions (0.1.5) 175 | zeitwerk (2.6.0) 176 | 177 | PLATFORMS 178 | x86_64-darwin-20 179 | 180 | DEPENDENCIES 181 | bootsnap 182 | pry 183 | puma (~> 5.0) 184 | rails (~> 7.0.3) 185 | rubocop 186 | sqlite3 (~> 1.4) 187 | tzinfo-data 188 | 189 | RUBY VERSION 190 | ruby 3.0.3p157 191 | 192 | BUNDLED WITH 193 | 2.2.32 194 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # README 2 | 3 | ## Description 4 | 5 | Code examples for concepts discussed in series of titled "DDD in Ruby on Rails", available at https://www.visuality.pl/posts/introduction-to-ddd-in-ruby-on-rails 6 | 7 | ## Setup 8 | 9 | 1. clone repo 10 | 2. run `bundle` 11 | 3. rub `rails db:prepare` 12 | 13 | ## Testing 14 | 15 | This repo has no testing framework. It contains interactive tests, which can be run against your development database. 16 | 17 | Test scripts can be run using rails runner. Example: 18 | 19 | ```sh 20 | $ rails r test/customer/model/orm_driven.rb 21 | ``` 22 | 23 | The test are not aimed to give 100% code coverage (or in fact - no coverage at all). They are here to give you an easy entry point to the implemented concepts. Run them and adjust to see the logic in action. 24 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # Add your own tasks in files placed in lib/tasks ending in .rake, 2 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. 3 | 4 | require_relative "config/application" 5 | 6 | Rails.application.load_tasks 7 | -------------------------------------------------------------------------------- /app/channels/application_cable/channel.rb: -------------------------------------------------------------------------------- 1 | module ApplicationCable 2 | class Channel < ActionCable::Channel::Base 3 | end 4 | end 5 | -------------------------------------------------------------------------------- /app/channels/application_cable/connection.rb: -------------------------------------------------------------------------------- 1 | module ApplicationCable 2 | class Connection < ActionCable::Connection::Base 3 | end 4 | end 5 | -------------------------------------------------------------------------------- /app/controllers/application_controller.rb: -------------------------------------------------------------------------------- 1 | class ApplicationController < ActionController::API 2 | end 3 | -------------------------------------------------------------------------------- /app/controllers/concerns/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pstrzalk/ddd-in-ruby-on-rails/157ede1d252d6e7b45a2cea1045b2351f5f251bd/app/controllers/concerns/.keep -------------------------------------------------------------------------------- /app/domains/analytics/client.rb: -------------------------------------------------------------------------------- 1 | module Analytics 2 | class Client 3 | def self.track(params) 4 | # sent event outside and return 5 | Rails.logger.info("[Analytics::Client] Tracking #{params}") 6 | 7 | true 8 | end 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /app/domains/analytics/configuration.rb: -------------------------------------------------------------------------------- 1 | module Analytics 2 | class Configuration 3 | def self.enabled? 4 | ENV.fetch("ANALYTICS_ENABLED", "0") == "1" 5 | end 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /app/domains/analytics/model/event.rb: -------------------------------------------------------------------------------- 1 | module Analytics 2 | module Model 3 | class Event 4 | attr_reader :name 5 | 6 | def initialize(name:) 7 | @name = name.to_s 8 | end 9 | 10 | def self.combination_of(noun:, verb:) 11 | new(name: "#{noun} #{verb}") 12 | end 13 | 14 | def ==(other) 15 | name == other.name 16 | end 17 | 18 | def hash 19 | name.hash 20 | end 21 | end 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /app/domains/analytics/service/track_event_version1.rb: -------------------------------------------------------------------------------- 1 | # Usage example 2 | # TrackEventVersion1.new(user_id: 6, event: 'Item added').call 3 | 4 | module Analytics 5 | module Service 6 | class TrackEventVersion1 7 | def initialize(user_id:, event:) 8 | self.user_id = user_id 9 | self.event = event 10 | end 11 | 12 | def call 13 | return false unless Configuration.enabled? 14 | 15 | Client.track( 16 | user_id: user_id, 17 | event: event, 18 | properties: properties 19 | ) 20 | end 21 | 22 | private 23 | 24 | attr_accessor :user_id, :event 25 | 26 | def properties 27 | { 28 | foo: :bar, 29 | moo: :too 30 | } 31 | end 32 | end 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /app/domains/analytics/service/track_event_version2.rb: -------------------------------------------------------------------------------- 1 | # Usage example 2 | # TrackEventVersion2.new(user_id: 6, noun: 'Job', verb: 'Created').call 3 | 4 | module Analytics 5 | module Service 6 | class TrackEventVersion2 7 | def initialize(user_id:, noun:, verb:) 8 | self.user_id = user_id 9 | self.event = "#{noun} #{verb}" 10 | end 11 | 12 | def call 13 | return false unless Configuration.enabled? 14 | 15 | Client.track( 16 | user_id: user_id, 17 | event: event, 18 | properties: properties 19 | ) 20 | end 21 | 22 | private 23 | 24 | attr_accessor :user_id, :event 25 | 26 | def properties 27 | { 28 | foo: :bar, 29 | moo: :too 30 | } 31 | end 32 | end 33 | end 34 | end 35 | -------------------------------------------------------------------------------- /app/domains/analytics/service/track_event_version3.rb: -------------------------------------------------------------------------------- 1 | # Usage example 2 | # TrackEventVersion3.new(user_id: 6, noun: 'Job', verb: 'Created').call 3 | # TrackEventVersion3.new(user_id: 6, event: 'Cloned and archived related clients').call 4 | 5 | module Analytics 6 | module Service 7 | class TrackEventVersion3 8 | def initialize(user_id:, noun: nil, verb: nil, event: nil) 9 | self.user_id = user_id 10 | self.event = event.presence || "#{noun} #{verb}" 11 | end 12 | 13 | def call 14 | return false unless Configuration.enabled? 15 | 16 | Client.track( 17 | user_id: user_id, 18 | event: event, 19 | properties: properties 20 | ) 21 | end 22 | 23 | private 24 | 25 | attr_accessor :user_id, :event 26 | 27 | def properties 28 | { 29 | foo: :bar, 30 | moo: :too 31 | } 32 | end 33 | end 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /app/domains/analytics/service/track_event_version4.rb: -------------------------------------------------------------------------------- 1 | # Usage example 2 | # TrackEventVersion4.new(user_id: 6, event: Analytics::Model::Event.combination_of(noun: "Jon", verb: "Created")) 3 | # TrackEventVersion4.new(user_id: 6, event: Analytics::Model::Event.new(name: "Cloned and archived related clients")) 4 | 5 | module Analytics 6 | module Service 7 | class TrackEventVersion4 8 | def initialize(user_id:, event: nil) 9 | self.user_id = user_id 10 | self.event = event.name 11 | end 12 | 13 | def call 14 | return false unless Configuration.enabled? 15 | 16 | Client.track( 17 | user_id: user_id, 18 | event: event, 19 | properties: properties 20 | ) 21 | end 22 | 23 | private 24 | 25 | attr_accessor :user_id, :event 26 | 27 | def properties 28 | { 29 | foo: :bar, 30 | moo: :too 31 | } 32 | end 33 | end 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /app/domains/car_catalog/domain/car.rb: -------------------------------------------------------------------------------- 1 | module CarCatalog 2 | module Domain 3 | class Car 4 | attr_reader :registration_number, :color, :id 5 | 6 | def initialize(registration_number:, color:) 7 | @registration_number = registration_number 8 | @color = color 9 | end 10 | 11 | def repaint_to(new_color) 12 | @color = new_color 13 | end 14 | 15 | def to_s 16 | "I am a #{color} car. My registration_number is #{registration_number} and my ID is #{id.presence || 'nil'}" 17 | end 18 | 19 | private 20 | 21 | attr_writer :id 22 | end 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /app/domains/car_catalog/infrastructure/db_car.rb: -------------------------------------------------------------------------------- 1 | module CarCatalog 2 | module Infrastructure 3 | class DbCar < ApplicationRecord 4 | self.table_name = "car_catalog_cars" 5 | end 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /app/domains/car_catalog/infrastructure/db_car_repository.rb: -------------------------------------------------------------------------------- 1 | module CarCatalog 2 | module Infrastructure 3 | class DbCarRepository 4 | def of_registration_number(registration_number) 5 | db_car = DbCar.find_by(registration_number: registration_number) 6 | return unless db_car 7 | 8 | to_domain_car(db_car) 9 | end 10 | 11 | def save(car) 12 | db_car = car.id ? DbCar.find(car.id) : DbCar.new 13 | db_car.assign_attributes( 14 | registration_number: car.registration_number, 15 | color: car.color 16 | ) 17 | 18 | db_car.save! 19 | end 20 | 21 | def registered_in_ddd 22 | db_cars = DbCar.where('registration_number LIKE "DDD%"') 23 | db_cars.map { |db_car| to_domain_car(db_car) } 24 | end 25 | 26 | private 27 | 28 | def to_domain_car(db_car) 29 | car = Domain::Car.new( 30 | registration_number: db_car[:registration_number], 31 | color: db_car[:color] 32 | ) 33 | car.send("id=", db_car[:id]) 34 | car 35 | end 36 | end 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /app/domains/customer/model/orm_driven/customer.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module OrmDriven 4 | class Customer < ApplicationRecord 5 | self.table_name = "customers_orm_driven_customers" 6 | 7 | validates :city, :street, :name, :birthday, presence: true 8 | 9 | validates_with PhoneSyntaxValidator 10 | validates :phone, uniqueness: true 11 | 12 | validates_with EmailSyntaxValidator 13 | validates :email, uniqueness: true 14 | 15 | validate :birthday_in_the_past 16 | 17 | has_many :rates 18 | 19 | private 20 | 21 | def birthday_in_the_past 22 | errors.add(:birthday, "is in the future") if birthday&.future? 23 | end 24 | end 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /app/domains/customer/model/orm_driven/email_syntax_validator.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module OrmDriven 4 | class EmailSyntaxValidator < ActiveModel::Validator 5 | def validate(record) 6 | return if record.email.present? && record.email.include?("@") 7 | 8 | record.errors.add :email, "Email is invalid" 9 | end 10 | end 11 | end 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /app/domains/customer/model/orm_driven/phone_syntax_validator.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module OrmDriven 4 | class PhoneSyntaxValidator < ActiveModel::Validator 5 | def validate(record) 6 | return if record.phone.present? && record.phone.start_with?("+") 7 | 8 | record.errors.add :phone, "Phone is invalid" 9 | end 10 | end 11 | end 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /app/domains/customer/model/orm_driven/rate.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module OrmDriven 4 | class Rate < ApplicationRecord 5 | self.table_name = "customers_orm_driven_rates" 6 | 7 | belongs_to :customer, dependent: :destroy 8 | belongs_to :rated_customer, class_name: "Customer::Model::OrmDriven::Customer" 9 | end 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /app/domains/customer/model/with_person_model/address.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithPersonModel 4 | class Address 5 | attr_reader :city, :street 6 | 7 | def initialize(city, street) 8 | @city = city 9 | @street = street 10 | end 11 | end 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /app/domains/customer/model/with_person_model/address_validator.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithPersonModel 4 | class AddressValidator < ActiveModel::Validator 5 | def validate(record) 6 | record.errors.add(:city, "City is blank") if record.address.city.blank? 7 | record.errors.add(:street, "Street is blank") if record.address.street.blank? 8 | end 9 | end 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /app/domains/customer/model/with_person_model/customer.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithPersonModel 4 | class Customer < ApplicationRecord 5 | self.table_name = "customers_with_person_model_customers" 6 | 7 | AlreadyRated = Class.new(StandardError) 8 | CannotRateSelf = Class.new(StandardError) 9 | 10 | composed_of :address, 11 | class_name: "Customer::Model::WithPersonModel::Address", 12 | mapping: [%w[city city], %w[street street]] 13 | validates_with AddressValidator 14 | 15 | validates :phone, uniqueness: true 16 | validates_with PhoneSyntaxValidator 17 | 18 | has_one :person 19 | has_many :rates 20 | end 21 | end 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /app/domains/customer/model/with_person_model/person.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithPersonModel 4 | class Person < ApplicationRecord 5 | self.table_name = "customers_with_person_model_persons" 6 | 7 | validates_with PersonValidator 8 | end 9 | end 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /app/domains/customer/model/with_person_model/person_validator.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithPersonModel 4 | class PersonValidator < ActiveModel::Validator 5 | def validate(record) 6 | validate_email(record) 7 | validate_name(record) 8 | validate_birthday(record) 9 | end 10 | 11 | private 12 | 13 | def validate_email(record) 14 | record.errors.add(:email, "Email is blank") if record.email.blank? 15 | end 16 | 17 | def validate_name(record) 18 | record.errors.add(:name, "Name is blank") if record.name.blank? 19 | end 20 | 21 | def validate_birthday(record) 22 | if record.birthday.blank? 23 | record.errors.add :birthday, "Birthday is blank" 24 | elsif record.birthday.future? 25 | record.errors.add :birthday, "is in the future" 26 | end 27 | end 28 | end 29 | end 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /app/domains/customer/model/with_person_model/phone_syntax_validator.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithPersonModel 4 | class PhoneSyntaxValidator < ActiveModel::Validator 5 | def validate(record) 6 | return if record.phone.present? && record.phone.start_with?("+") 7 | 8 | record.errors.add :phone, "Phone is invalid" 9 | end 10 | end 11 | end 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /app/domains/customer/model/with_person_model/rate.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithPersonModel 4 | class Rate < ApplicationRecord 5 | self.table_name = "customers_with_person_model_rates" 6 | 7 | belongs_to :customer, dependent: :destroy 8 | belongs_to :rated_customer, class_name: "Customer::Model::WithPersonModel::Customer" 9 | end 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /app/domains/customer/model/with_vo/address.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithVo 4 | class Address 5 | attr_reader :city, :street 6 | 7 | def initialize(city, street) 8 | @city = city 9 | @street = street 10 | end 11 | end 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /app/domains/customer/model/with_vo/address_validator.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithVo 4 | class AddressValidator < ActiveModel::Validator 5 | def validate(record) 6 | record.errors.add(:city, "City is blank") if record.address.city.blank? 7 | record.errors.add(:street, "Street is blank") if record.address.street.blank? 8 | end 9 | end 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /app/domains/customer/model/with_vo/customer.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithVo 4 | class Customer < ApplicationRecord 5 | self.table_name = "customers_with_vo_customers" 6 | 7 | composed_of :address, 8 | class_name: "Customer::Model::WithVo::Address", 9 | mapping: [%w[city city], %w[street street]] 10 | validates_with AddressValidator 11 | 12 | composed_of :personal_information, 13 | class_name: "Customer::Model::WithVo::PersonalInformation", 14 | mapping: [%w[name name], %w[birthday birthday]] 15 | validates_with PersonalInformationValidator 16 | 17 | validates_with PhoneSyntaxValidator 18 | validates :phone, uniqueness: true 19 | 20 | validates_with EmailSyntaxValidator 21 | validates :email, uniqueness: true 22 | 23 | has_many :rates 24 | end 25 | end 26 | end 27 | end 28 | -------------------------------------------------------------------------------- /app/domains/customer/model/with_vo/email_syntax_validator.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithVo 4 | class EmailSyntaxValidator < ActiveModel::Validator 5 | def validate(record) 6 | return if record.email.present? && record.email.include?("@") 7 | 8 | record.errors.add :email, "Email is invalid" 9 | end 10 | end 11 | end 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /app/domains/customer/model/with_vo/personal_information.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithVo 4 | class PersonalInformation 5 | attr_reader :name, :birthday 6 | 7 | def initialize(name, birthday) 8 | @name = name 9 | @birthday = birthday 10 | end 11 | end 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /app/domains/customer/model/with_vo/personal_information_validator.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithVo 4 | class PersonalInformationValidator < ActiveModel::Validator 5 | def validate(record) 6 | validate_name(record) 7 | validate_birthday(record) 8 | end 9 | 10 | private 11 | 12 | def validate_name(record) 13 | record.errors.add(:name, "Name is blank") if record.personal_information.name.blank? 14 | end 15 | 16 | def validate_birthday(record) 17 | if record.personal_information.birthday.blank? 18 | record.errors.add :birthday, "Birthday is blank" 19 | elsif record.personal_information.birthday.future? 20 | record.errors.add :birthday, "is in the future" 21 | end 22 | end 23 | end 24 | end 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /app/domains/customer/model/with_vo/phone_syntax_validator.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithVo 4 | class PhoneSyntaxValidator < ActiveModel::Validator 5 | def validate(record) 6 | return if record.phone.present? && record.phone.start_with?("+") 7 | 8 | record.errors.add :phone, "Phone is invalid" 9 | end 10 | end 11 | end 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /app/domains/customer/model/with_vo/rate.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithVo 4 | class Rate < ApplicationRecord 5 | self.table_name = "customers_with_vo_rates" 6 | 7 | belongs_to :customer, dependent: :destroy 8 | belongs_to :rated_customer, class_name: "Customer::Model::WithVo::Customer" 9 | end 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /app/domains/customer/model/with_vo_and_identity_and_behavior/address.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithVoAndIdentityAndBehavior 4 | class Address 5 | attr_reader :city, :street 6 | 7 | def initialize(city, street) 8 | @city = city 9 | @street = street 10 | end 11 | end 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /app/domains/customer/model/with_vo_and_identity_and_behavior/address_validator.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithVoAndIdentityAndBehavior 4 | class AddressValidator < ActiveModel::Validator 5 | def validate(record) 6 | record.errors.add(:city, "City is blank") if record.address.city.blank? 7 | record.errors.add(:street, "Street is blank") if record.address.street.blank? 8 | end 9 | end 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /app/domains/customer/model/with_vo_and_identity_and_behavior/customer.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithVoAndIdentityAndBehavior 4 | class Customer < ApplicationRecord 5 | self.table_name = "customers_with_vo_and_identity_and_behavior_customers" 6 | 7 | AlreadyRated = Class.new(StandardError) 8 | CannotRateSelf = Class.new(StandardError) 9 | 10 | composed_of :address, 11 | class_name: "Customer::Model::WithVoAndIdentityAndBehavior::Address", 12 | mapping: [%w[city city], %w[street street]] 13 | validates_with AddressValidator 14 | 15 | composed_of :personal_information, 16 | class_name: "Customer::Model::WithVoAndIdentityAndBehavior::PersonalInformation", 17 | mapping: [%w[name name], %w[birthday birthday], %w[email email]] 18 | validates_with PersonalInformationValidator 19 | 20 | validates_with PhoneSyntaxValidator 21 | validates :phone, uniqueness: true 22 | 23 | has_many :rates 24 | 25 | def give_negative_rate(other) 26 | give_rate(other, -1) 27 | end 28 | 29 | def give_neutral_rate(other) 30 | give_rate(other, 0) 31 | end 32 | 33 | def give_positive_rate(other) 34 | give_rate(other, 1) 35 | end 36 | 37 | private 38 | 39 | def give_rate(other, mark) 40 | raise CannotRateSelf if other == self 41 | raise AlreadyRated if rates.any? { |rate| rate.rated_customer == other } 42 | 43 | rates.build(rated_customer: other, mark: mark) 44 | end 45 | end 46 | end 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /app/domains/customer/model/with_vo_and_identity_and_behavior/personal_information.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithVoAndIdentityAndBehavior 4 | class PersonalInformation 5 | attr_reader :name, :birthday, :email 6 | 7 | def initialize(name, birthday, email) 8 | @name = name 9 | @birthday = birthday 10 | @email = email 11 | end 12 | end 13 | end 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /app/domains/customer/model/with_vo_and_identity_and_behavior/personal_information_validator.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithVoAndIdentityAndBehavior 4 | class PersonalInformationValidator < ActiveModel::Validator 5 | def validate(record) 6 | validate_email(record) 7 | validate_name(record) 8 | validate_birthday(record) 9 | end 10 | 11 | private 12 | 13 | def validate_email(record) 14 | record.errors.add(:email, "Email is blank") if record.personal_information.email.blank? 15 | end 16 | 17 | def validate_name(record) 18 | record.errors.add(:name, "Name is blank") if record.personal_information.name.blank? 19 | end 20 | 21 | def validate_birthday(record) 22 | if record.personal_information.birthday.blank? 23 | record.errors.add :birthday, "Birthday is blank" 24 | elsif record.personal_information.birthday.future? 25 | record.errors.add :birthday, "is in the future" 26 | end 27 | end 28 | end 29 | end 30 | end 31 | end 32 | -------------------------------------------------------------------------------- /app/domains/customer/model/with_vo_and_identity_and_behavior/phone_syntax_validator.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithVoAndIdentityAndBehavior 4 | class PhoneSyntaxValidator < ActiveModel::Validator 5 | def validate(record) 6 | return if record.phone.present? && record.phone.start_with?("+") 7 | 8 | record.errors.add :phone, "Phone is invalid" 9 | end 10 | end 11 | end 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /app/domains/customer/model/with_vo_and_identity_and_behavior/rate.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithVoAndIdentityAndBehavior 4 | class Rate < ApplicationRecord 5 | self.table_name = "customers_with_vo_and_identity_and_behavior_rates" 6 | 7 | belongs_to :customer, dependent: :destroy 8 | belongs_to :rated_customer, class_name: "Customer::Model::WithVoAndIdentityAndBehavior::Customer" 9 | end 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /app/jobs/application_job.rb: -------------------------------------------------------------------------------- 1 | class ApplicationJob < ActiveJob::Base 2 | # Automatically retry jobs that encountered a deadlock 3 | # retry_on ActiveRecord::Deadlocked 4 | 5 | # Most jobs are safe to ignore if the underlying records are no longer available 6 | # discard_on ActiveJob::DeserializationError 7 | end 8 | -------------------------------------------------------------------------------- /app/mailers/application_mailer.rb: -------------------------------------------------------------------------------- 1 | class ApplicationMailer < ActionMailer::Base 2 | default from: "from@example.com" 3 | layout "mailer" 4 | end 5 | -------------------------------------------------------------------------------- /app/models/application_record.rb: -------------------------------------------------------------------------------- 1 | class ApplicationRecord < ActiveRecord::Base 2 | primary_abstract_class 3 | end 4 | -------------------------------------------------------------------------------- /app/models/concerns/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pstrzalk/ddd-in-ruby-on-rails/157ede1d252d6e7b45a2cea1045b2351f5f251bd/app/models/concerns/.keep -------------------------------------------------------------------------------- /app/models/user.rb: -------------------------------------------------------------------------------- 1 | class User < ApplicationRecord 2 | end 3 | -------------------------------------------------------------------------------- /app/views/layouts/mailer.html.erb: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 8 | 9 | 10 | 11 | <%= yield %> 12 | 13 | 14 | -------------------------------------------------------------------------------- /app/views/layouts/mailer.text.erb: -------------------------------------------------------------------------------- 1 | <%= yield %> 2 | -------------------------------------------------------------------------------- /bin/bundle: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # frozen_string_literal: true 3 | 4 | # 5 | # This file was generated by Bundler. 6 | # 7 | # The application 'bundle' is installed as part of a gem, and 8 | # this file is here to facilitate running it. 9 | # 10 | 11 | require "rubygems" 12 | 13 | m = Module.new do 14 | module_function 15 | 16 | def invoked_as_script? 17 | File.expand_path($0) == File.expand_path(__FILE__) 18 | end 19 | 20 | def env_var_version 21 | ENV["BUNDLER_VERSION"] 22 | end 23 | 24 | def cli_arg_version 25 | return unless invoked_as_script? # don't want to hijack other binstubs 26 | return unless "update".start_with?(ARGV.first || " ") # must be running `bundle update` 27 | bundler_version = nil 28 | update_index = nil 29 | ARGV.each_with_index do |a, i| 30 | if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN 31 | bundler_version = a 32 | end 33 | next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/ 34 | bundler_version = $1 35 | update_index = i 36 | end 37 | bundler_version 38 | end 39 | 40 | def gemfile 41 | gemfile = ENV["BUNDLE_GEMFILE"] 42 | return gemfile if gemfile && !gemfile.empty? 43 | 44 | File.expand_path("../../Gemfile", __FILE__) 45 | end 46 | 47 | def lockfile 48 | lockfile = 49 | case File.basename(gemfile) 50 | when "gems.rb" then gemfile.sub(/\.rb$/, gemfile) 51 | else "#{gemfile}.lock" 52 | end 53 | File.expand_path(lockfile) 54 | end 55 | 56 | def lockfile_version 57 | return unless File.file?(lockfile) 58 | lockfile_contents = File.read(lockfile) 59 | return unless lockfile_contents =~ /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/ 60 | Regexp.last_match(1) 61 | end 62 | 63 | def bundler_requirement 64 | @bundler_requirement ||= 65 | env_var_version || cli_arg_version || 66 | bundler_requirement_for(lockfile_version) 67 | end 68 | 69 | def bundler_requirement_for(version) 70 | return "#{Gem::Requirement.default}.a" unless version 71 | 72 | bundler_gem_version = Gem::Version.new(version) 73 | 74 | requirement = bundler_gem_version.approximate_recommendation 75 | 76 | return requirement unless Gem::Version.new(Gem::VERSION) < Gem::Version.new("2.7.0") 77 | 78 | requirement += ".a" if bundler_gem_version.prerelease? 79 | 80 | requirement 81 | end 82 | 83 | def load_bundler! 84 | ENV["BUNDLE_GEMFILE"] ||= gemfile 85 | 86 | activate_bundler 87 | end 88 | 89 | def activate_bundler 90 | gem_error = activation_error_handling do 91 | gem "bundler", bundler_requirement 92 | end 93 | return if gem_error.nil? 94 | require_error = activation_error_handling do 95 | require "bundler/version" 96 | end 97 | return if require_error.nil? && Gem::Requirement.new(bundler_requirement).satisfied_by?(Gem::Version.new(Bundler::VERSION)) 98 | warn "Activating bundler (#{bundler_requirement}) failed:\n#{gem_error.message}\n\nTo install the version of bundler this project requires, run `gem install bundler -v '#{bundler_requirement}'`" 99 | exit 42 100 | end 101 | 102 | def activation_error_handling 103 | yield 104 | nil 105 | rescue StandardError, LoadError => e 106 | e 107 | end 108 | end 109 | 110 | m.load_bundler! 111 | 112 | if m.invoked_as_script? 113 | load Gem.bin_path("bundler", "bundle") 114 | end 115 | -------------------------------------------------------------------------------- /bin/rails: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | APP_PATH = File.expand_path("../config/application", __dir__) 3 | require_relative "../config/boot" 4 | require "rails/commands" 5 | -------------------------------------------------------------------------------- /bin/rake: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require_relative "../config/boot" 3 | require "rake" 4 | Rake.application.run 5 | -------------------------------------------------------------------------------- /bin/setup: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | require "fileutils" 3 | 4 | # path to your application root. 5 | APP_ROOT = File.expand_path("..", __dir__) 6 | 7 | def system!(*args) 8 | system(*args) || abort("\n== Command #{args} failed ==") 9 | end 10 | 11 | FileUtils.chdir APP_ROOT do 12 | # This script is a way to set up or update your development environment automatically. 13 | # This script is idempotent, so that you can run it at any time and get an expectable outcome. 14 | # Add necessary setup steps to this file. 15 | 16 | puts "== Installing dependencies ==" 17 | system! "gem install bundler --conservative" 18 | system("bundle check") || system!("bundle install") 19 | 20 | # puts "\n== Copying sample files ==" 21 | # unless File.exist?("config/database.yml") 22 | # FileUtils.cp "config/database.yml.sample", "config/database.yml" 23 | # end 24 | 25 | puts "\n== Preparing database ==" 26 | system! "bin/rails db:prepare" 27 | 28 | puts "\n== Removing old logs and tempfiles ==" 29 | system! "bin/rails log:clear tmp:clear" 30 | 31 | puts "\n== Restarting application server ==" 32 | system! "bin/rails restart" 33 | end 34 | -------------------------------------------------------------------------------- /config.ru: -------------------------------------------------------------------------------- 1 | # This file is used by Rack-based servers to start the application. 2 | 3 | require_relative "config/environment" 4 | 5 | run Rails.application 6 | Rails.application.load_server 7 | -------------------------------------------------------------------------------- /config/application.rb: -------------------------------------------------------------------------------- 1 | require_relative "boot" 2 | 3 | require "rails" 4 | # Pick the frameworks you want: 5 | require "active_model/railtie" 6 | require "active_job/railtie" 7 | require "active_record/railtie" 8 | require "active_storage/engine" 9 | require "action_controller/railtie" 10 | require "action_mailer/railtie" 11 | require "action_mailbox/engine" 12 | require "action_text/engine" 13 | require "action_view/railtie" 14 | require "action_cable/engine" 15 | # require "rails/test_unit/railtie" 16 | 17 | # Require the gems listed in Gemfile, including any gems 18 | # you've limited to :test, :development, or :production. 19 | Bundler.require(*Rails.groups) 20 | 21 | module DddInRubyOnRails 22 | class Application < Rails::Application 23 | # Initialize configuration defaults for originally generated Rails version. 24 | config.load_defaults 7.0 25 | 26 | # Configuration for the application, engines, and railties goes here. 27 | # 28 | # These settings can be overridden in specific environments using the files 29 | # in config/environments, which are processed later. 30 | # 31 | # config.time_zone = "Central Time (US & Canada)" 32 | # config.eager_load_paths << Rails.root.join("extras") 33 | 34 | # Only loads a smaller set of middleware suitable for API only apps. 35 | # Middleware like session, flash, cookies can be added back manually. 36 | # Skip views, helpers and assets when generating a new resource. 37 | config.api_only = true 38 | end 39 | end 40 | -------------------------------------------------------------------------------- /config/boot.rb: -------------------------------------------------------------------------------- 1 | ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) 2 | 3 | require "bundler/setup" # Set up gems listed in the Gemfile. 4 | require "bootsnap/setup" # Speed up boot time by caching expensive operations. 5 | -------------------------------------------------------------------------------- /config/cable.yml: -------------------------------------------------------------------------------- 1 | development: 2 | adapter: async 3 | 4 | test: 5 | adapter: test 6 | 7 | production: 8 | adapter: redis 9 | url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %> 10 | channel_prefix: ddd_in_ruby_on_rails_production 11 | -------------------------------------------------------------------------------- /config/credentials.yml.enc: -------------------------------------------------------------------------------- 1 | XIlzXWAEGPiu3z0nMhhop2W2GuLljIu3NKgm+BbHUx205lUUo6xEInqA42nzk2+2TVZRtG+W4Xect/sJe0fx4pYRgwtR5R5oMbqj7RrgsxpJWpmnGUX2/ZueZkqtH7fBiVs4jfOw0FACc1RyQ6uhfnQtXOG2J8eZmBXvXmy3Zuv03XvsXO2R33GhFIytOdqD3Lm7BdX/MG+Zj24SbH2WV8DAwp9mSzAEXb/IgMGtQJafjirOSAdHquAOQ4HlJCrnIHN8H+tLt4KcvzCzBqz7k600lt7IHd8GgslcHanh9wr7EQB0miW4kGB7XsIZIN9CQArXgs5tWAKIvuy3mSp1Zamc5KFslO2Kc7oKupJ2g8YVy2Epxs8y9QSvoqKZfGmXh2uGOdwM2imOW4zrOOcYjnyW5AQAXYESAm6L--f3JVRpa4FRt6721d--0tMbTRbgPtFUmYGJSWFx9Q== -------------------------------------------------------------------------------- /config/database.yml: -------------------------------------------------------------------------------- 1 | # SQLite. Versions 3.8.0 and up are supported. 2 | # gem install sqlite3 3 | # 4 | # Ensure the SQLite 3 gem is defined in your Gemfile 5 | # gem "sqlite3" 6 | # 7 | default: &default 8 | adapter: sqlite3 9 | pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 10 | timeout: 5000 11 | 12 | development: 13 | <<: *default 14 | database: db/development.sqlite3 15 | 16 | # Warning: The database defined as "test" will be erased and 17 | # re-generated from your development database when you run "rake". 18 | # Do not set this db to the same as development or production. 19 | test: 20 | <<: *default 21 | database: db/test.sqlite3 22 | 23 | production: 24 | <<: *default 25 | database: db/production.sqlite3 26 | -------------------------------------------------------------------------------- /config/environment.rb: -------------------------------------------------------------------------------- 1 | # Load the Rails application. 2 | require_relative "application" 3 | 4 | # Initialize the Rails application. 5 | Rails.application.initialize! 6 | -------------------------------------------------------------------------------- /config/environments/development.rb: -------------------------------------------------------------------------------- 1 | require "active_support/core_ext/integer/time" 2 | 3 | Rails.application.configure do 4 | # Settings specified here will take precedence over those in config/application.rb. 5 | 6 | # In the development environment your application's code is reloaded any time 7 | # it changes. This slows down response time but is perfect for development 8 | # since you don't have to restart the web server when you make code changes. 9 | config.cache_classes = false 10 | 11 | # Do not eager load code on boot. 12 | config.eager_load = false 13 | 14 | # Show full error reports. 15 | config.consider_all_requests_local = true 16 | 17 | # Enable server timing 18 | config.server_timing = true 19 | 20 | # Enable/disable caching. By default caching is disabled. 21 | # Run rails dev:cache to toggle caching. 22 | if Rails.root.join("tmp/caching-dev.txt").exist? 23 | config.cache_store = :memory_store 24 | config.public_file_server.headers = { 25 | "Cache-Control" => "public, max-age=#{2.days.to_i}" 26 | } 27 | else 28 | config.action_controller.perform_caching = false 29 | 30 | config.cache_store = :null_store 31 | end 32 | 33 | # Store uploaded files on the local file system (see config/storage.yml for options). 34 | config.active_storage.service = :local 35 | 36 | # Don't care if the mailer can't send. 37 | config.action_mailer.raise_delivery_errors = false 38 | 39 | config.action_mailer.perform_caching = false 40 | 41 | # Print deprecation notices to the Rails logger. 42 | config.active_support.deprecation = :log 43 | 44 | # Raise exceptions for disallowed deprecations. 45 | config.active_support.disallowed_deprecation = :raise 46 | 47 | # Tell Active Support which deprecation messages to disallow. 48 | config.active_support.disallowed_deprecation_warnings = [] 49 | 50 | # Raise an error on page load if there are pending migrations. 51 | config.active_record.migration_error = :page_load 52 | 53 | # Highlight code that triggered database queries in logs. 54 | config.active_record.verbose_query_logs = true 55 | 56 | 57 | # Raises error for missing translations. 58 | # config.i18n.raise_on_missing_translations = true 59 | 60 | # Annotate rendered view with file names. 61 | # config.action_view.annotate_rendered_view_with_filenames = true 62 | 63 | # Uncomment if you wish to allow Action Cable access from any origin. 64 | # config.action_cable.disable_request_forgery_protection = true 65 | end 66 | -------------------------------------------------------------------------------- /config/environments/production.rb: -------------------------------------------------------------------------------- 1 | require "active_support/core_ext/integer/time" 2 | 3 | Rails.application.configure do 4 | # Settings specified here will take precedence over those in config/application.rb. 5 | 6 | # Code is not reloaded between requests. 7 | config.cache_classes = true 8 | 9 | # Eager load code on boot. This eager loads most of Rails and 10 | # your application in memory, allowing both threaded web servers 11 | # and those relying on copy on write to perform better. 12 | # Rake tasks automatically ignore this option for performance. 13 | config.eager_load = true 14 | 15 | # Full error reports are disabled and caching is turned on. 16 | config.consider_all_requests_local = false 17 | 18 | # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"] 19 | # or in config/master.key. This key is used to decrypt credentials (and other encrypted files). 20 | # config.require_master_key = true 21 | 22 | # Disable serving static files from the `/public` folder by default since 23 | # Apache or NGINX already handles this. 24 | config.public_file_server.enabled = ENV["RAILS_SERVE_STATIC_FILES"].present? 25 | 26 | # Enable serving of images, stylesheets, and JavaScripts from an asset server. 27 | # config.asset_host = "http://assets.example.com" 28 | 29 | # Specifies the header that your server uses for sending files. 30 | # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for Apache 31 | # config.action_dispatch.x_sendfile_header = "X-Accel-Redirect" # for NGINX 32 | 33 | # Store uploaded files on the local file system (see config/storage.yml for options). 34 | config.active_storage.service = :local 35 | 36 | # Mount Action Cable outside main process or domain. 37 | # config.action_cable.mount_path = nil 38 | # config.action_cable.url = "wss://example.com/cable" 39 | # config.action_cable.allowed_request_origins = [ "http://example.com", /http:\/\/example.*/ ] 40 | 41 | # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 42 | # config.force_ssl = true 43 | 44 | # Include generic and useful information about system operation, but avoid logging too much 45 | # information to avoid inadvertent exposure of personally identifiable information (PII). 46 | config.log_level = :info 47 | 48 | # Prepend all log lines with the following tags. 49 | config.log_tags = [ :request_id ] 50 | 51 | # Use a different cache store in production. 52 | # config.cache_store = :mem_cache_store 53 | 54 | # Use a real queuing backend for Active Job (and separate queues per environment). 55 | # config.active_job.queue_adapter = :resque 56 | # config.active_job.queue_name_prefix = "ddd_in_ruby_on_rails_production" 57 | 58 | config.action_mailer.perform_caching = false 59 | 60 | # Ignore bad email addresses and do not raise email delivery errors. 61 | # Set this to true and configure the email server for immediate delivery to raise delivery errors. 62 | # config.action_mailer.raise_delivery_errors = false 63 | 64 | # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 65 | # the I18n.default_locale when a translation cannot be found). 66 | config.i18n.fallbacks = true 67 | 68 | # Don't log any deprecations. 69 | config.active_support.report_deprecations = false 70 | 71 | # Use default logging formatter so that PID and timestamp are not suppressed. 72 | config.log_formatter = ::Logger::Formatter.new 73 | 74 | # Use a different logger for distributed setups. 75 | # require "syslog/logger" 76 | # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new "app-name") 77 | 78 | if ENV["RAILS_LOG_TO_STDOUT"].present? 79 | logger = ActiveSupport::Logger.new(STDOUT) 80 | logger.formatter = config.log_formatter 81 | config.logger = ActiveSupport::TaggedLogging.new(logger) 82 | end 83 | 84 | # Do not dump schema after migrations. 85 | config.active_record.dump_schema_after_migration = false 86 | end 87 | -------------------------------------------------------------------------------- /config/environments/test.rb: -------------------------------------------------------------------------------- 1 | require "active_support/core_ext/integer/time" 2 | 3 | # The test environment is used exclusively to run your application's 4 | # test suite. You never need to work with it otherwise. Remember that 5 | # your test database is "scratch space" for the test suite and is wiped 6 | # and recreated between test runs. Don't rely on the data there! 7 | 8 | Rails.application.configure do 9 | # Settings specified here will take precedence over those in config/application.rb. 10 | 11 | # Turn false under Spring and add config.action_view.cache_template_loading = true. 12 | config.cache_classes = true 13 | 14 | # Eager loading loads your whole application. When running a single test locally, 15 | # this probably isn't necessary. It's a good idea to do in a continuous integration 16 | # system, or in some way before deploying your code. 17 | config.eager_load = ENV["CI"].present? 18 | 19 | # Configure public file server for tests with Cache-Control for performance. 20 | config.public_file_server.enabled = true 21 | config.public_file_server.headers = { 22 | "Cache-Control" => "public, max-age=#{1.hour.to_i}" 23 | } 24 | 25 | # Show full error reports and disable caching. 26 | config.consider_all_requests_local = true 27 | config.action_controller.perform_caching = false 28 | config.cache_store = :null_store 29 | 30 | # Raise exceptions instead of rendering exception templates. 31 | config.action_dispatch.show_exceptions = false 32 | 33 | # Disable request forgery protection in test environment. 34 | config.action_controller.allow_forgery_protection = false 35 | 36 | # Store uploaded files on the local file system in a temporary directory. 37 | config.active_storage.service = :test 38 | 39 | config.action_mailer.perform_caching = false 40 | 41 | # Tell Action Mailer not to deliver emails to the real world. 42 | # The :test delivery method accumulates sent emails in the 43 | # ActionMailer::Base.deliveries array. 44 | config.action_mailer.delivery_method = :test 45 | 46 | # Print deprecation notices to the stderr. 47 | config.active_support.deprecation = :stderr 48 | 49 | # Raise exceptions for disallowed deprecations. 50 | config.active_support.disallowed_deprecation = :raise 51 | 52 | # Tell Active Support which deprecation messages to disallow. 53 | config.active_support.disallowed_deprecation_warnings = [] 54 | 55 | # Raises error for missing translations. 56 | # config.i18n.raise_on_missing_translations = true 57 | 58 | # Annotate rendered view with file names. 59 | # config.action_view.annotate_rendered_view_with_filenames = true 60 | end 61 | -------------------------------------------------------------------------------- /config/initializers/cors.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Avoid CORS issues when API is called from the frontend app. 4 | # Handle Cross-Origin Resource Sharing (CORS) in order to accept cross-origin AJAX requests. 5 | 6 | # Read more: https://github.com/cyu/rack-cors 7 | 8 | # Rails.application.config.middleware.insert_before 0, Rack::Cors do 9 | # allow do 10 | # origins "example.com" 11 | # 12 | # resource "*", 13 | # headers: :any, 14 | # methods: [:get, :post, :put, :patch, :delete, :options, :head] 15 | # end 16 | # end 17 | -------------------------------------------------------------------------------- /config/initializers/filter_parameter_logging.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Configure parameters to be filtered from the log file. Use this to limit dissemination of 4 | # sensitive information. See the ActiveSupport::ParameterFilter documentation for supported 5 | # notations and behaviors. 6 | Rails.application.config.filter_parameters += [ 7 | :passw, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn 8 | ] 9 | -------------------------------------------------------------------------------- /config/initializers/inflections.rb: -------------------------------------------------------------------------------- 1 | # Be sure to restart your server when you modify this file. 2 | 3 | # Add new inflection rules using the following format. Inflections 4 | # are locale specific, and you may define rules for as many different 5 | # locales as you wish. All of these examples are active by default: 6 | # ActiveSupport::Inflector.inflections(:en) do |inflect| 7 | # inflect.plural /^(ox)$/i, "\\1en" 8 | # inflect.singular /^(ox)en/i, "\\1" 9 | # inflect.irregular "person", "people" 10 | # inflect.uncountable %w( fish sheep ) 11 | # end 12 | 13 | # These inflection rules are supported but not enabled by default: 14 | # ActiveSupport::Inflector.inflections(:en) do |inflect| 15 | # inflect.acronym "RESTful" 16 | # end 17 | -------------------------------------------------------------------------------- /config/locales/en.yml: -------------------------------------------------------------------------------- 1 | # Files in the config/locales directory are used for internationalization 2 | # and are automatically loaded by Rails. If you want to use locales other 3 | # than English, add the necessary files in this directory. 4 | # 5 | # To use the locales, use `I18n.t`: 6 | # 7 | # I18n.t "hello" 8 | # 9 | # In views, this is aliased to just `t`: 10 | # 11 | # <%= t("hello") %> 12 | # 13 | # To use a different locale, set it with `I18n.locale`: 14 | # 15 | # I18n.locale = :es 16 | # 17 | # This would use the information in config/locales/es.yml. 18 | # 19 | # The following keys must be escaped otherwise they will not be retrieved by 20 | # the default I18n backend: 21 | # 22 | # true, false, on, off, yes, no 23 | # 24 | # Instead, surround them with single quotes. 25 | # 26 | # en: 27 | # "true": "foo" 28 | # 29 | # To learn more, please read the Rails Internationalization guide 30 | # available at https://guides.rubyonrails.org/i18n.html. 31 | 32 | en: 33 | hello: "Hello world" 34 | -------------------------------------------------------------------------------- /config/puma.rb: -------------------------------------------------------------------------------- 1 | # Puma can serve each request in a thread from an internal thread pool. 2 | # The `threads` method setting takes two numbers: a minimum and maximum. 3 | # Any libraries that use thread pools should be configured to match 4 | # the maximum value specified for Puma. Default is set to 5 threads for minimum 5 | # and maximum; this matches the default thread size of Active Record. 6 | # 7 | max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 } 8 | min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count } 9 | threads min_threads_count, max_threads_count 10 | 11 | # Specifies the `worker_timeout` threshold that Puma will use to wait before 12 | # terminating a worker in development environments. 13 | # 14 | worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development" 15 | 16 | # Specifies the `port` that Puma will listen on to receive requests; default is 3000. 17 | # 18 | port ENV.fetch("PORT") { 3000 } 19 | 20 | # Specifies the `environment` that Puma will run in. 21 | # 22 | environment ENV.fetch("RAILS_ENV") { "development" } 23 | 24 | # Specifies the `pidfile` that Puma will use. 25 | pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" } 26 | 27 | # Specifies the number of `workers` to boot in clustered mode. 28 | # Workers are forked web server processes. If using threads and workers together 29 | # the concurrency of the application would be max `threads` * `workers`. 30 | # Workers do not work on JRuby or Windows (both of which do not support 31 | # processes). 32 | # 33 | # workers ENV.fetch("WEB_CONCURRENCY") { 2 } 34 | 35 | # Use the `preload_app!` method when specifying a `workers` number. 36 | # This directive tells Puma to first boot the application and load code 37 | # before forking the application. This takes advantage of Copy On Write 38 | # process behavior so workers use less memory. 39 | # 40 | # preload_app! 41 | 42 | # Allow puma to be restarted by `bin/rails restart` command. 43 | plugin :tmp_restart 44 | -------------------------------------------------------------------------------- /config/routes.rb: -------------------------------------------------------------------------------- 1 | Rails.application.routes.draw do 2 | # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html 3 | 4 | # Defines the root path route ("/") 5 | # root "articles#index" 6 | end 7 | -------------------------------------------------------------------------------- /config/storage.yml: -------------------------------------------------------------------------------- 1 | test: 2 | service: Disk 3 | root: <%= Rails.root.join("tmp/storage") %> 4 | 5 | local: 6 | service: Disk 7 | root: <%= Rails.root.join("storage") %> 8 | 9 | # Use bin/rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key) 10 | # amazon: 11 | # service: S3 12 | # access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %> 13 | # secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %> 14 | # region: us-east-1 15 | # bucket: your_own_bucket-<%= Rails.env %> 16 | 17 | # Remember not to checkin your GCS keyfile to a repository 18 | # google: 19 | # service: GCS 20 | # project: your_project 21 | # credentials: <%= Rails.root.join("path/to/gcs.keyfile") %> 22 | # bucket: your_own_bucket-<%= Rails.env %> 23 | 24 | # Use bin/rails credentials:edit to set the Azure Storage secret (as azure_storage:storage_access_key) 25 | # microsoft: 26 | # service: AzureStorage 27 | # storage_account_name: your_account_name 28 | # storage_access_key: <%= Rails.application.credentials.dig(:azure_storage, :storage_access_key) %> 29 | # container: your_container_name-<%= Rails.env %> 30 | 31 | # mirror: 32 | # service: Mirror 33 | # primary: local 34 | # mirrors: [ amazon, google, microsoft ] 35 | -------------------------------------------------------------------------------- /db/migrate/20220821195526_create_users.rb: -------------------------------------------------------------------------------- 1 | class CreateUsers < ActiveRecord::Migration[7.0] 2 | def change 3 | create_table :users do |t| 4 | t.string :name 5 | t.timestamps 6 | end 7 | end 8 | end 9 | -------------------------------------------------------------------------------- /db/migrate/20220822114828_create_customers_orm_driven_customers.rb: -------------------------------------------------------------------------------- 1 | class CreateCustomersOrmDrivenCustomers < ActiveRecord::Migration[7.0] 2 | def change 3 | create_table :customers_orm_driven_customers do |t| 4 | t.string :phone 5 | t.string :email 6 | t.string :name 7 | t.string :city 8 | t.string :street 9 | t.date :birthday 10 | 11 | t.timestamps 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /db/migrate/20220822115002_create_customers_orm_driven_rates.rb: -------------------------------------------------------------------------------- 1 | class CreateCustomersOrmDrivenRates < ActiveRecord::Migration[7.0] 2 | def change 3 | create_table :customers_orm_driven_rates do |t| 4 | t.references :customer 5 | t.references :rated_customer, foreign_key: { to_table: :customers_orm_driven_customers } 6 | t.integer :mark 7 | 8 | t.timestamps 9 | end 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /db/migrate/20220822174958_create_customers_with_vo_customers.rb: -------------------------------------------------------------------------------- 1 | class CreateCustomersWithVoCustomers < ActiveRecord::Migration[7.0] 2 | def change 3 | create_table :customers_with_vo_customers do |t| 4 | t.string :phone 5 | t.string :email 6 | t.string :name 7 | t.string :city 8 | t.string :street 9 | t.date :birthday 10 | 11 | t.timestamps 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /db/migrate/20220822175005_create_customers_with_vo_rates.rb: -------------------------------------------------------------------------------- 1 | class CreateCustomersWithVoRates < ActiveRecord::Migration[7.0] 2 | def change 3 | create_table :customers_with_vo_rates do |t| 4 | t.references :customer 5 | t.references :rated_customer, foreign_key: { to_table: :customers_with_vo_customers } 6 | t.integer :mark 7 | 8 | t.timestamps 9 | end 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /db/migrate/20220822175058_create_customers_with_vo_and_identity_and_behavior_customers.rb: -------------------------------------------------------------------------------- 1 | class CreateCustomersWithVoAndIdentityAndBehaviorCustomers < ActiveRecord::Migration[7.0] 2 | def change 3 | create_table :customers_with_vo_and_identity_and_behavior_customers do |t| 4 | t.string :phone 5 | t.string :email 6 | t.string :name 7 | t.string :city 8 | t.string :street 9 | t.date :birthday 10 | 11 | t.timestamps 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /db/migrate/20220822175123_create_customers_with_vo_and_identity_and_behavior_rates.rb: -------------------------------------------------------------------------------- 1 | class CreateCustomersWithVoAndIdentityAndBehaviorRates < ActiveRecord::Migration[7.0] 2 | def change 3 | create_table :customers_with_vo_and_identity_and_behavior_rates do |t| 4 | t.references :customer, 5 | index: { name: "index_customers_with_vo_and_identity_and_behavior_rates_c_id" } 6 | t.references :rated_customer, 7 | foreign_key: { to_table: :customers_with_vo_and_identity_and_behavior_customers }, 8 | index: { name: "index_customers_with_vo_and_identity_and_behavior_rates_oc_id" } 9 | t.integer :mark 10 | 11 | t.timestamps 12 | end 13 | end 14 | end 15 | -------------------------------------------------------------------------------- /db/migrate/20220822215125_create_customers_with_person_model_customers.rb: -------------------------------------------------------------------------------- 1 | class CreateCustomersWithPersonModelCustomers < ActiveRecord::Migration[7.0] 2 | def change 3 | create_table :customers_with_person_model_customers do |t| 4 | t.string :phone 5 | t.string :city 6 | t.string :street 7 | 8 | t.timestamps 9 | end 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /db/migrate/20220822215130_create_customers_with_person_model_rates.rb: -------------------------------------------------------------------------------- 1 | class CreateCustomersWithPersonModelRates < ActiveRecord::Migration[7.0] 2 | def change 3 | create_table :customers_with_person_model_rates do |t| 4 | t.references :customer 5 | t.references :rated_customer, foreign_key: { to_table: :customers_with_person_model_customers } 6 | t.integer :mark 7 | 8 | t.timestamps 9 | 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /db/migrate/20220822215136_create_customers_with_person_model_persons.rb: -------------------------------------------------------------------------------- 1 | class CreateCustomersWithPersonModelPersons < ActiveRecord::Migration[7.0] 2 | def change 3 | create_table :customers_with_person_model_persons do |t| 4 | t.string :email 5 | t.string :name 6 | t.date :birthday 7 | 8 | t.integer :customer_id 9 | 10 | t.timestamps 11 | end 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /db/migrate/20221207084108_create_car_catalog_cars.rb: -------------------------------------------------------------------------------- 1 | class CreateCarCatalogCars < ActiveRecord::Migration[7.0] 2 | def change 3 | create_table :car_catalog_cars do |t| 4 | t.string :registration_number 5 | t.string :color 6 | 7 | t.timestamps 8 | end 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /db/schema.rb: -------------------------------------------------------------------------------- 1 | # This file is auto-generated from the current state of the database. Instead 2 | # of editing this file, please use the migrations feature of Active Record to 3 | # incrementally modify your database, and then regenerate this schema definition. 4 | # 5 | # This file is the source Rails uses to define your schema when running `bin/rails 6 | # db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to 7 | # be faster and is potentially less error prone than running all of your 8 | # migrations from scratch. Old migrations may fail to apply correctly if those 9 | # migrations use external dependencies or application code. 10 | # 11 | # It's strongly recommended that you check this file into your version control system. 12 | 13 | ActiveRecord::Schema[7.0].define(version: 2022_12_07_084108) do 14 | create_table "car_catalog_cars", force: :cascade do |t| 15 | t.string "registration_number" 16 | t.string "color" 17 | t.datetime "created_at", null: false 18 | t.datetime "updated_at", null: false 19 | end 20 | 21 | create_table "customers_orm_driven_customers", force: :cascade do |t| 22 | t.string "phone" 23 | t.string "email" 24 | t.string "name" 25 | t.string "city" 26 | t.string "street" 27 | t.date "birthday" 28 | t.datetime "created_at", null: false 29 | t.datetime "updated_at", null: false 30 | end 31 | 32 | create_table "customers_orm_driven_rates", force: :cascade do |t| 33 | t.integer "customer_id" 34 | t.integer "rated_customer_id" 35 | t.integer "mark" 36 | t.datetime "created_at", null: false 37 | t.datetime "updated_at", null: false 38 | t.index ["customer_id"], name: "index_customers_orm_driven_rates_on_customer_id" 39 | t.index ["rated_customer_id"], name: "index_customers_orm_driven_rates_on_rated_customer_id" 40 | end 41 | 42 | create_table "customers_with_person_model_customers", force: :cascade do |t| 43 | t.string "phone" 44 | t.string "city" 45 | t.string "street" 46 | t.datetime "created_at", null: false 47 | t.datetime "updated_at", null: false 48 | end 49 | 50 | create_table "customers_with_person_model_persons", force: :cascade do |t| 51 | t.string "email" 52 | t.string "name" 53 | t.date "birthday" 54 | t.integer "customer_id" 55 | t.datetime "created_at", null: false 56 | t.datetime "updated_at", null: false 57 | end 58 | 59 | create_table "customers_with_person_model_rates", force: :cascade do |t| 60 | t.integer "customer_id" 61 | t.integer "rated_customer_id" 62 | t.integer "mark" 63 | t.datetime "created_at", null: false 64 | t.datetime "updated_at", null: false 65 | t.index ["customer_id"], name: "index_customers_with_person_model_rates_on_customer_id" 66 | t.index ["rated_customer_id"], name: "index_customers_with_person_model_rates_on_rated_customer_id" 67 | end 68 | 69 | create_table "customers_with_vo_and_identity_and_behavior_customers", force: :cascade do |t| 70 | t.string "phone" 71 | t.string "email" 72 | t.string "name" 73 | t.string "city" 74 | t.string "street" 75 | t.date "birthday" 76 | t.datetime "created_at", null: false 77 | t.datetime "updated_at", null: false 78 | end 79 | 80 | create_table "customers_with_vo_and_identity_and_behavior_rates", force: :cascade do |t| 81 | t.integer "customer_id" 82 | t.integer "rated_customer_id" 83 | t.integer "mark" 84 | t.datetime "created_at", null: false 85 | t.datetime "updated_at", null: false 86 | t.index ["customer_id"], name: "index_customers_with_vo_and_identity_and_behavior_rates_c_id" 87 | t.index ["rated_customer_id"], name: "index_customers_with_vo_and_identity_and_behavior_rates_oc_id" 88 | end 89 | 90 | create_table "customers_with_vo_customers", force: :cascade do |t| 91 | t.string "phone" 92 | t.string "email" 93 | t.string "name" 94 | t.string "city" 95 | t.string "street" 96 | t.date "birthday" 97 | t.datetime "created_at", null: false 98 | t.datetime "updated_at", null: false 99 | end 100 | 101 | create_table "customers_with_vo_rates", force: :cascade do |t| 102 | t.integer "customer_id" 103 | t.integer "rated_customer_id" 104 | t.integer "mark" 105 | t.datetime "created_at", null: false 106 | t.datetime "updated_at", null: false 107 | t.index ["customer_id"], name: "index_customers_with_vo_rates_on_customer_id" 108 | t.index ["rated_customer_id"], name: "index_customers_with_vo_rates_on_rated_customer_id" 109 | end 110 | 111 | create_table "users", force: :cascade do |t| 112 | t.string "name" 113 | t.datetime "created_at", null: false 114 | t.datetime "updated_at", null: false 115 | end 116 | 117 | add_foreign_key "customers_orm_driven_rates", "customers_orm_driven_customers", column: "rated_customer_id" 118 | add_foreign_key "customers_with_person_model_rates", "customers_with_person_model_customers", column: "rated_customer_id" 119 | add_foreign_key "customers_with_vo_and_identity_and_behavior_rates", "customers_with_vo_and_identity_and_behavior_customers", column: "rated_customer_id" 120 | add_foreign_key "customers_with_vo_rates", "customers_with_vo_customers", column: "rated_customer_id" 121 | end 122 | -------------------------------------------------------------------------------- /db/seeds.rb: -------------------------------------------------------------------------------- 1 | # This file should contain all the record creation needed to seed the database with its default values. 2 | # The data can then be loaded with the bin/rails db:seed command (or created alongside the database with db:setup). 3 | # 4 | # Examples: 5 | # 6 | # movies = Movie.create([{ name: "Star Wars" }, { name: "Lord of the Rings" }]) 7 | # Character.create(name: "Luke", movie: movies.first) 8 | -------------------------------------------------------------------------------- /lib/tasks/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pstrzalk/ddd-in-ruby-on-rails/157ede1d252d6e7b45a2cea1045b2351f5f251bd/lib/tasks/.keep -------------------------------------------------------------------------------- /log/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pstrzalk/ddd-in-ruby-on-rails/157ede1d252d6e7b45a2cea1045b2351f5f251bd/log/.keep -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | # See https://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file 2 | -------------------------------------------------------------------------------- /storage/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pstrzalk/ddd-in-ruby-on-rails/157ede1d252d6e7b45a2cea1045b2351f5f251bd/storage/.keep -------------------------------------------------------------------------------- /test/analytics/service.rb: -------------------------------------------------------------------------------- 1 | module Analytics 2 | module Service 3 | class Test 4 | def self.run # rubocop:disable all 5 | service = TrackEventVersion1.new(user_id: 6, event: "Item added") 6 | result = service.call 7 | pp "TrackEventVersion1 result: #{result}" 8 | 9 | service = TrackEventVersion2.new(user_id: 6, noun: "Job", verb: "Done") 10 | result = service.call 11 | pp "TrackEventVersion2 result: #{result}" 12 | 13 | service = TrackEventVersion3.new(user_id: 6, noun: "Job", verb: "Done") 14 | result = service.call 15 | pp "TrackEventVersion3 using noun+verb result: #{result}" 16 | 17 | service = TrackEventVersion3.new(user_id: 6, event: "Thing Done Well") 18 | result = service.call 19 | pp "TrackEventVersion3 using noun+verb result: #{result}" 20 | 21 | event = Analytics::Model::Event.combination_of(noun: "Jon", verb: "Created") 22 | service = TrackEventVersion4.new(user_id: 6, event: event) 23 | result = service.call 24 | pp "TrackEventVersion4 using event built using noun+verb: #{result}" 25 | 26 | event = Analytics::Model::Event.new(name: "Cloned and archived related clients") 27 | service = TrackEventVersion4.new(user_id: 6, event: event) 28 | result = service.call 29 | pp "TrackEventVersion4 using event built using name: #{result}" 30 | end 31 | end 32 | end 33 | end 34 | 35 | Analytics::Service::Test.run 36 | -------------------------------------------------------------------------------- /test/car_catalog/car_repository_test.rb: -------------------------------------------------------------------------------- 1 | module CarCatalog 2 | class CarRepositoryTest 3 | def self.run # rubocop:disable all 4 | car = Domain::Car.new(registration_number: "DDD 2013", color: "red") 5 | puts "Domain car: #{car}" 6 | 7 | repository = Infrastructure::DbCarRepository.new 8 | repository.save(car) 9 | puts "Persisted..." 10 | 11 | car = repository.of_registration_number("FOO 2033") 12 | puts "Car of registration number FOO 2033: #{car}" 13 | 14 | car = repository.of_registration_number("DDD 2013") 15 | puts "Car of registration number DDD 2033: #{car}" 16 | 17 | car.repaint_to("purple") 18 | puts "Domain car with new paint: #{car}" 19 | 20 | repository.save(car) 21 | puts "Persisted..." 22 | 23 | car = repository.of_registration_number("DDD 2013") 24 | puts "Car of registration number DDD 2033: #{car}" 25 | 26 | car = Domain::Car.new(registration_number: "DDD 2014", color: "blue") 27 | puts "Domain car: #{car}" 28 | repository.save(car) 29 | puts "Persisted..." 30 | 31 | car = Domain::Car.new(registration_number: "WWW 2015", color: "silver") 32 | puts "Domain car: #{car}" 33 | repository.save(car) 34 | puts "Persisted..." 35 | 36 | cars = repository.registered_in_ddd 37 | puts "Car registered in DDD:" 38 | puts cars.map(&:to_s).join("\n") 39 | end 40 | end 41 | end 42 | 43 | CarCatalog::CarRepositoryTest.run 44 | -------------------------------------------------------------------------------- /test/customer/model/orm_driven.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module OrmDriven 4 | class Test 5 | def self.run # rubocop:disable all 6 | invalid_customer = Customer.new 7 | invalid_customer.valid? 8 | pp invalid_customer.errors 9 | 10 | customer = Customer.create!( 11 | city: "Warsaw", 12 | street: "Odolanska", 13 | name: "Joe Doe", 14 | birthday: Date.parse("1980-07-15"), 15 | phone: "+48 112 555 339", 16 | email: "joedoe@domain.com" 17 | ) 18 | 19 | other_customer = Customer.create!( 20 | city: "Krakow", 21 | street: "Wawelska", 22 | name: "Jim Bom", 23 | birthday: Date.parse("1990-02-19"), 24 | phone: "+48 339 555 112", 25 | email: "jimbom@domain.com" 26 | ) 27 | 28 | rate = Rate.create!( 29 | customer: customer, 30 | rated_customer: other_customer, 31 | mark: 3 32 | ) 33 | 34 | pp rate 35 | pp customer 36 | pp other_customer 37 | 38 | rate.destroy! 39 | customer.destroy! 40 | other_customer.destroy! 41 | end 42 | end 43 | end 44 | end 45 | end 46 | 47 | Customer::Model::OrmDriven::Test.run 48 | -------------------------------------------------------------------------------- /test/customer/model/with_person_model.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithPersonModel 4 | class Test 5 | def self.run # rubocop:disable all 6 | invalid_customer = Customer.new 7 | invalid_customer.valid? 8 | pp invalid_customer.errors 9 | 10 | address = Address.new("Warsaw", "Odolanska") 11 | person = Person.create!( 12 | name: "Bo Mo", 13 | email: "bomo@domain.com", 14 | birthday: Date.parse("1990-02-19") 15 | ) 16 | customer = Customer.create!( 17 | address: address, 18 | person: person, 19 | phone: "+48 112 555 339" 20 | ) 21 | 22 | other_person = Person.create!( 23 | name: "Po Ro", 24 | email: "poro@domain.com", 25 | birthday: Date.parse("1975-11-20") 26 | ) 27 | other_customer = Customer.create!( 28 | address: Address.new("Krakow", "Wawelska"), 29 | person: other_person, 30 | phone: "+48 339 555 112" 31 | ) 32 | 33 | Rate.create!( 34 | customer: customer, 35 | rated_customer: other_customer, 36 | mark: 3 37 | ) 38 | 39 | pp customer 40 | pp customer.person 41 | pp customer.address 42 | pp customer.rates 43 | 44 | pp other_customer 45 | pp other_customer.person 46 | pp other_customer.address 47 | pp other_customer.rates 48 | 49 | customer.rates.destroy_all 50 | customer.destroy! 51 | other_customer.destroy! 52 | end 53 | end 54 | end 55 | end 56 | end 57 | 58 | Customer::Model::WithPersonModel::Test.run 59 | -------------------------------------------------------------------------------- /test/customer/model/with_vo.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithVo 4 | class Test 5 | def self.run # rubocop:disable all 6 | invalid_customer = Customer.new 7 | invalid_customer.valid? 8 | pp invalid_customer.errors 9 | 10 | address = Address.new("Warsaw", "Odolanska") 11 | personal_information = PersonalInformation.new("Joe Doe", Date.parse("1980-07-15")) 12 | customer = Customer.create!( 13 | address: address, 14 | personal_information: personal_information, 15 | phone: "+48 112 555 339", 16 | email: "joedoe@domain.com" 17 | ) 18 | 19 | other_customer = Customer.create!( 20 | address: Address.new("Krakow", "Wawelska"), 21 | personal_information: PersonalInformation.new("Jim Bom", Date.parse("1990-02-19")), 22 | phone: "+48 339 555 112", 23 | email: "jimbom@domain.com" 24 | ) 25 | 26 | rate = Rate.create!( 27 | customer: customer, 28 | rated_customer: other_customer, 29 | mark: 3 30 | ) 31 | 32 | pp rate 33 | pp customer 34 | pp customer.address 35 | pp customer.personal_information 36 | pp other_customer 37 | 38 | rate.destroy! 39 | customer.destroy! 40 | other_customer.destroy! 41 | end 42 | end 43 | end 44 | end 45 | end 46 | 47 | Customer::Model::WithVo::Test.run 48 | -------------------------------------------------------------------------------- /test/customer/model/with_vo_and_identity_and_behavior.rb: -------------------------------------------------------------------------------- 1 | module Customer 2 | module Model 3 | module WithVoAndIdentityAndBehavior 4 | class Test 5 | def self.run # rubocop:disable all 6 | invalid_customer = Customer.new 7 | invalid_customer.valid? 8 | pp invalid_customer.errors 9 | 10 | address = Address.new("Warsaw", "Odolanska") 11 | personal_information = PersonalInformation.new("Joe Doe", Date.parse("1980-07-15"), "joedoe@domain.com") 12 | customer = Customer.create!( 13 | address: address, 14 | personal_information: personal_information, 15 | phone: "+48 112 555 339" 16 | ) 17 | 18 | other_customer = Customer.create!( 19 | address: Address.new("Krakow", "Wawelska"), 20 | personal_information: PersonalInformation.new("Jim Bom", Date.parse("1990-02-19"), "jimbom@domain.com"), 21 | phone: "+48 339 555 112" 22 | ) 23 | 24 | pp customer 25 | pp customer.address 26 | pp customer.personal_information 27 | 28 | customer.give_positive_rate(other_customer) 29 | customer.save! 30 | 31 | pp customer.rates 32 | 33 | begin 34 | customer.give_positive_rate(customer) 35 | rescue Customer::CannotRateSelf => e 36 | pp "rescued from #{e} when giving a positive rate to self" 37 | end 38 | 39 | begin 40 | customer.give_negative_rate(other_customer) 41 | rescue Customer::AlreadyRated => e 42 | pp "rescued from #{e} when giving duplicated rate" 43 | end 44 | 45 | pp other_customer 46 | 47 | customer.rates.destroy_all 48 | customer.destroy! 49 | other_customer.destroy! 50 | end 51 | end 52 | end 53 | end 54 | end 55 | 56 | Customer::Model::WithVoAndIdentityAndBehavior::Test.run 57 | -------------------------------------------------------------------------------- /tmp/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pstrzalk/ddd-in-ruby-on-rails/157ede1d252d6e7b45a2cea1045b2351f5f251bd/tmp/.keep -------------------------------------------------------------------------------- /tmp/pids/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pstrzalk/ddd-in-ruby-on-rails/157ede1d252d6e7b45a2cea1045b2351f5f251bd/tmp/pids/.keep -------------------------------------------------------------------------------- /tmp/storage/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pstrzalk/ddd-in-ruby-on-rails/157ede1d252d6e7b45a2cea1045b2351f5f251bd/tmp/storage/.keep -------------------------------------------------------------------------------- /vendor/.keep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pstrzalk/ddd-in-ruby-on-rails/157ede1d252d6e7b45a2cea1045b2351f5f251bd/vendor/.keep --------------------------------------------------------------------------------