Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Rails構成とオブジェクト指向/ rails-structure-and-object-or...
Search
@amyroi
June 08, 2018
1.7k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Rails構成とオブジェクト指向/ rails-structure-and-object-oriented
@amyroi
June 08, 2018
More Decks by @amyroi
See All by @amyroi
リリースレトロスペクティブ/release-retrospective
amyroi
0
390
Featured
See All Featured
The Cost Of JavaScript in 2023
addyosmani
55
10k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
550
Unsuck your backbone
ammeep
672
58k
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.6k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
The Limits of Empathy - UXLibs8
cassininazir
1
680
Art, The Web, and Tiny UX
lynnandtonic
304
22k
BBQ
matthewcrist
89
10k
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
2
280
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
28
3.6k
RailsConf 2023
tenderlove
30
1.5k
We Are The Robots
honzajavorek
0
380
Transcript
コード構成素案 主にビジネスロジックの話 @amyroi
コード配置例 # 例 app/ ├ lib/ # もしくは lib/ 配下、autoload
│ └ aiml/ # プロダクトコードに影響しないコンポーネン │ └ aws/ # プロダクトコードに影響しないコンポーネント │ └ nlu/ # gem 化想定 │ ├ models/ │ └ concerns/ # ActiveSupport::Concern │ └ user.rb # ActiveRelation 継承 │ └ validators/ # 独自validation クラス ├ behaviors/ │ └ user_search_form.rb # Form オブジェクト │ # | Service クラス │ # | Decorator クラス │ └ nlu_manage/ # プロジェクト内で行うNLU への │ # アクセス関するクラスをまとめるnamespace │ └ project.rb # 振る舞いのクラス │ └ bot.rb # 振る舞いのクラス 2
継承と結合をなくしていく module, callback, 継承の使用を極力さける 継承より委譲 密結合 → 疎結合へ lib配下のライブラリは対象外 3
メリット 振る舞いに合わせたオブジェクト設計 疎結合にすることによりユニットテストの見通しがよくなる デメリット デザインパターンの乱用 振る舞いにおけるファイル数増 4
継承 class A def save end end class B <
A end b = B.new b.save 5
委譲 class A def initialize(object) end def save object.save end
end class B # object を汚染しない end class C end a = A.new(B.new) a.save a = A.new(C.new) a.save 6
モジュール インスタンスを生成しない 例:helper グローバルでアクセスが可能。 moduleではなくclassにしていく。もしくはnamespaceとして使 う。 7
concernについて ActiveSupport::Concern 使用用途:ApplicationRecordに共通のクラスメソッド、インスタン スメソッドを追加する クラスのトップレベルの共通化。 使用頻度を少なく 8
クラス インスタンス化 メソッドのスコープを限定できる オブジェクト! models配下に振る舞いのクラスを生成していく ActiveModel、デザインパターン、委譲 transactionは明示的に行う 9
FormObject (ActiveModel) フォーム表示するための振る舞いを分離したクラス validationも配置可能 Serviceクラスでも可 10
モジュール module Seachable extend ActiveSupport::Concern def self.seach(params) end end class
User include Seachable end # Usage User.search(params) 11
Formオブジェクト class UserSearchForm include ActiveModel::Model def self.search(search_params) end end class
User # object を汚染しない end # Usage UserSearchForm.search(params) 12
Callback Railsのcallbackの影響 モデル同士の結合度の高さ 完結したユニットテストがかけない ActiveModel::Callbacksを使用 transactionは定義 Decoratorパターンで作成したクラスで処理を完結させる。 13
ActiveRecordのcallback class User before_create :join_project def join_project self.project = Project.create()
end end user = User.new user.save 14
Decoratorパターンで役割を分離した例 class UserCreateDecorator include ActiveModel::Model attr_accessor :user define_model_callback :save before_save
:set_project def save User.transaction do run_callbacks(:save) do @user.save end end end def set_project @user.project = Project.create end end user = UserCreateDecorator.new(User.new) user.save 15