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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
@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
Transcript
15 slides
コード構成素案 主にビジネスロジックの話 @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
More Decks by @amyroi
See All by @amyroi
リリースレトロスペクティブ/release-retrospective
amyroi
0
400
Featured
See All Featured
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.8k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
970
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
550
Reality Check: Gamification 10 Years Later
codingconduct
0
2.4k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.9k
A Soul's Torment
seathinner
9
4k
Tell your own story through comics
letsgokoyo
1
1.1k
Making Projects Easy
brettharned
120
6.9k
Navigating Team Friction
lara
192
16k
Large-scale JavaScript Application Architecture
addyosmani
516
110k
What Being in a Rock Band Can Teach Us About Real World SEO
427marketing
0
1.1k