Slide 1

Slide 1 text

Ϩʔϧͷ৳͹͠ํ !XJMMOFU

Slide 2

Slide 2 text

3BJMT%FWFMPQFST .FFUVQ

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

$MFBO5FTU$PEF

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

IUUQTXJMMOFUHJUCPPLTJPSTQFDTUZMFHVJEF

Slide 7

Slide 7 text

ӳޠ൛΋͋Γ·͢ 5IBOLT!HB[BZBT

Slide 8

Slide 8 text

*❤$MFBO

Slide 9

Slide 9 text

Shinichi Maeshima ! " # @netwillnet @willnet http://blog.willnet.in Rails Consultant

Slide 10

Slide 10 text

8FBSFOPUIJSJOH

Slide 11

Slide 11 text

͔͜͜Βຊ୊

Slide 12

Slide 12 text

8PSLJOHXJUI3BJMT XIJMFGFFMJOHTUSFTT

Slide 13

Slide 13 text

։ൃॳظ

Slide 14

Slide 14 text

։ൃதظ

Slide 15

Slide 15 text

։ൃޙظ

Slide 16

Slide 16 text

Ͳ͏ͨ͠ΒͭΒ͘ͳ͍͔

Slide 17

Slide 17 text

ద੾ͳந৅Խ

Slide 18

Slide 18 text

ద੾ͳந৅Խ Կͯ͠Δͷ͔ͻͱ໨Ͱ ෼͔ΔΑ͏ʹ͢Δ

Slide 19

Slide 19 text

ద੾ͳந৅Խͷྫ CFGPSF class Registration def save ApplicationRecord.transaction do case @auth_hash[:provider] when 'twitter' then register_by_twitter! when 'facebook' then register_by_facebook! when 'github' then register_by_github! end Friendship.create!(user: invitation.sender, friend: user) Friendship.create!(user: user, friend: invitation.sender) invitation.joined_at = Time.zone.now invitation.receiver = user invitation.save! end true rescue ActiveRecord::RecordInvalid false end

Slide 20

Slide 20 text

ద੾ͳந৅Խͷྫ BGUFS class Registration def save ApplicationRecord.transaction do register! make_friends! finish_invite! end true rescue ActiveRecord::RecordInvalid false end

Slide 21

Slide 21 text

ద੾ͳந৅Խ

Slide 22

Slide 22 text

ద੾Ͱͳ͍ந৅Խͷྫ class IssuesController < ApplicationController before_filter :find_issue, :only => [:show, :edit, :update] before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy] before_filter :find_project, :only => [:new, :create, :update_form] before_filter :authorize, :except => [:index] before_filter :find_optional_project, :only => [:index] before_filter :check_for_default_issue_status, :only => [:new, :create] before_filter :build_new_issue_from_params, :only => [:new, :create, :update_form]

Slide 23

Slide 23 text

3BJMT.7$

Slide 24

Slide 24 text

3BJMTͷ.7$ʹ͓͚Δ՝୊ w.7$ɺਖ਼͔ͭ͑ͯ͘͠·͔͢ʁ w.7$͚ͩͰ͸ϨΠϠ଍Γͳ͍ͷͰ͸ʁ

Slide 25

Slide 25 text

$POUSPMMFSΛਖ਼͘͠࢖͏

Slide 26

Slide 26 text

ѱ͍ྫ 'BU$POUSPMMFS IUUQTHJUIVCDPNTIBLBDPEFGBUDPEFSFGBDUPSJOHUFDIOJRVFT def create @micropost = Micropost.new(micropost_params.merge(user: current_user)) if current_user.minor? && (profane_words_used = profane_words_in(@micropost.content)) current_user.increment(:profanity_count, profane_words_used.size) current_user.save(validate: false) send_parent_notifcation_of_profanity(profane_words_used) flash.now[:error] = <<-MSG.html_safe

Profanity: '#{profane_words_used.join(", ")}' not allowed! You've tried to use profanity #{view_context.pluralize(current_user.profanity_count, "time")}!

Your parents have been notified!

MSG render 'static_pages/home' else if @micropost.save flash[:success] = "Micropost created!" redirect_to root_url else render 'static_pages/home' end end end end

Slide 27

Slide 27 text

'BU$POUSPMMFS㲈ద੾ͳ ந৅Խ͕Ͱ͖͍ͯͳ͍

Slide 28

Slide 28 text

मਖ਼ޙ def create @micropost = Micropost.new(micropost_params.merge(user: current_user)) if @micropost.save_with_profanity_callbacks flash[:success] = "Micropost created!" redirect_to root_url else adjust_micropost_profanity_message render 'static_pages/home' end end

Slide 29

Slide 29 text

$POUSPMMFSͷ੹຿ w ϞσϧͷϝιουΛݺͿ w Πϯελϯεม਺΁ͷ୅ೖ w Ϗϡʔͷબ୒ ΋͘͠͸ϦμΠϨΫτઌͷબ୒

Slide 30

Slide 30 text

ϩδοΫͷ͢΂ͯΛ Ϟσϧʹ೚ͤΔؾ࣋ͪͰ

Slide 31

Slide 31 text

class UsersController < ApplicationController def create @user = User.new(user_params) if @user.save UserMailer.thanks(@user).deliver_later redirect_to @user, notice: '登録完了しました' else render :new end end ͜ͷ͘Β͍͸ڐ༰

Slide 32

Slide 32 text

ͺͬͱݟͯ෼͔ΔൣғͳΒ ίϯτϩʔϥʹॻ͍ͯ΋େৎ෉

Slide 33

Slide 33 text

.PEFMΛਖ਼͘͠࢖͏

Slide 34

Slide 34 text

࠶ܝ ϩδοΫͷ͢΂ͯΛ Ϟσϧʹ೚ͤΔؾ࣋ͪͰ

Slide 35

Slide 35 text

ϩδοΫͷ͢΂ͯΛ Ϟσϧʹ೚ͤΔ 'BU.PEFM

Slide 36

Slide 36 text

'BU.PEFMͷԿ͕ྑ͘ͳ͍ͷ w େ఍ͷ৔߹ɺϞσϧಉ͕࢜ີ݁߹ʹͳ͍ͬͯΔ w ີ݁߹͸मਖ਼͕࿈࠯͠΍͍͢ w "ΫϥεͷϝιουΛमਖ਼ͨ͠Β#Ϋϥεͷमਖ਼͕ඞཁ ʹͳͬͯɺͦΕΛमਖ਼ͨ͠Β$Ϋϥεͷमਖ਼͕ʜ w ˠόάͷݪҼʹͳΓ΍͍͢

Slide 37

Slide 37 text

'BU.PEFMͷྫ class User < ApplicationRecord has_many :posts has_many :friendships has_many :friends, through: :friendships def create_post_with_notifications!(body) transaction do posts.create!(body: body) friends.each do |friend| friend.notifications.create!("#{name}さんが投稿しました") end end end # 大量の似たようなメソッド end

Slide 38

Slide 38 text

ղܾࡦ

Slide 39

Slide 39 text

1030ʹ੾Γग़͢ class PostWithNotifications def self.create!(creator:, body:) new(creator: creator, body: body).create! end def initialize(creator:, body:) @creator = creator @body = body end def create! ActiveRecord::Base.transaction do create_post! create_notifications! end end private attr_reader :creator, :body def create_post! creator.posts.create!(body: body) end def create_notifications! creator.friends.each { |friend| create_notification!(friend) } end def create_notification!(friend) friend.notifications.create!( “#{creator.name}さんが投稿しました" ) end end

Slide 40

Slide 40 text

IUUQUFDINFEQFFSDPKQFOUSZ

Slide 41

Slide 41 text

7JFXΛਖ਼͘͠࢖͏

Slide 42

Slide 42 text

7JFXʹ େ͖͍՝୊͸ͳ͍ ͸ͣ

Slide 43

Slide 43 text

͜͜·Ͱͷ·ͱΊ w ϩδοΫΛϞσϧʹدͤΔ w 1030 "DUJWF3FDPSEҎ֎ͷϞσϧ Λ׆༻͢Δ

Slide 44

Slide 44 text

͜Ε͚ͩͰ΋ฏۉҎ্ ͷՄಡੑ ౰ࣾൺ

Slide 45

Slide 45 text

͔࣍͠͠ͷΑ͏ͳέʔεͰ ·ͩࠔΔ w େྔͷCFGPSF@pMUFS w WBMJEBUJPO΍DBMMCBDLͷ৔߹෼͚ w ͻͱͭͷΞΫγϣϯͰͨ͘͞Μ΍Δ͜ͱ͕͋Δ

Slide 46

Slide 46 text

େྔͷCFGPSF@pMUFS class IssuesController < ApplicationController before_filter :find_issue, :only => [:show, :edit, :update] before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy] before_filter :find_project, :only => [:new, :create, :update_form] before_filter :authorize, :except => [:index] before_filter :find_optional_project, :only => [:index] before_filter :check_for_default_issue_status, :only => [:new, :create] before_filter :build_new_issue_from_params, :only => [:new, :create, :update_form]

Slide 47

Slide 47 text

CFGPSF@pMUFS͕ଟ͍ͱ Կ͕໰୊͔ w ࣗ෼͕ؾʹ͍ͯ͠ΔίϯςΩετ ΞΫγϣϯ Ͱͳ ʹ͕࣮ߦ͞Ε͍ͯΔͷ͔͕Θ͔ΓͮΒ͍ w ࠷ऴతʹͲΜͳΠϯελϯεม਺͕ઃఆ͞Ε͍ͯΔ ͷ͔Θ͔ΓͮΒ͍

Slide 48

Slide 48 text

આ໌༻ͷ΋ͬͱ؆୯ͳྫ class PostsController < ApplicationController before_filter :set_posts, only: :index before_filter :set_post, only: :show before_filter :set_recommendations def index end def show end private def set_posts @posts = post.page(params[:page).per(10) end def set_post @post = post.find(params[:id]) end def set_recommendations @recommendation_categories = Category.recommendations @recommendations = @recommendation_categories.posts end end

Slide 49

Slide 49 text

CFGPSF@pMUFSBDUJPO class PostsController < ApplicationController def index set_recommendations set_posts end def show set_recommendations set_post end private def set_posts @posts = post.page(params[:page).per(10) end def set_post @post = post.find(params[:id]) end def set_recommendations @recommendation_categories = Category.recommendations @recommendations = Recommendations.new(@recommendation_categories) end end

Slide 50

Slide 50 text

w ࣗ෼͕ؾʹ͍ͯ͠ΔίϯςΩετ ΞΫγϣϯ Ͱͳʹ͕࣮ߦ ͞Ε͍ͯΔͷ͔͕Θ͔ΓͮΒ͍ˠ w ࠷ऴతʹͲΜͳΠϯελϯεม਺͕ઃఆ͞Ε͍ͯΔͷ͔Θ ͔ΓͮΒ͍ˠ

Slide 51

Slide 51 text

ΫΤϦϝιουԽ class PostsController < ApplicationController def index @recommendation_categories = fetch_recommendation_categories @recommendations = fetch_recommendations(@recommendation_categories) @posts = fetch_posts end def show @recommendation_categories = fetch_recommendation_categories @recommendations = fetch_recommendations(@recommendation_categories) @post = fetch_post end private def fetch_posts post.page(params[:page).per(10) end def fetch_post post.find(params[:id]) end def fetch_recommendation_categories Category.recommendations end def fetch_recommendations(categories) Recommenadtions.new(categories) end end

Slide 52

Slide 52 text

w ࣗ෼͕ؾʹ͍ͯ͠ΔίϯςΩετ ΞΫγϣϯ Ͱͳʹ͕࣮ߦ ͞Ε͍ͯΔͷ͔͕Θ͔ΓͮΒ͍ˠ w ࠷ऴతʹͲΜͳΠϯελϯεม਺͕ઃఆ͞Ε͍ͯΔͷ͔Θ ͔ΓͮΒ͍ˠ

Slide 53

Slide 53 text

Ұݟ໰୊ͳͦ͞͏ʹ ݟ͑Δ

Slide 54

Slide 54 text

Πϯελϯεม਺͕ଟ͍ͱʜ class PostsController < ApplicationController def index @recommendation_categories = fetch_recommendation_categories @recommendations = fetch_recommendations(@recommendation_categories) @posts = fetch_posts @ad = fetch_ad @widget = fetch_widget @sidebar = fetch_sidebar @title = fetch_title @cart = current_cart @search = build_search_form end def show # indexと同じようにたくさんのインスタンス変数への代入がある end private # たくさんのプライベートメソッド end

Slide 55

Slide 55 text

Πϯελϯεม਺͕ଟ͍ͱʜ w ϓϥΠϕʔτϝιου͕ଟ͘ͳΓݟ௨͕͠ѱ͘ͳΔ w ˢΛϞσϧ΁Ҡ͢ͱଟগ؇࿨͞ΕΔ

Slide 56

Slide 56 text

Ϗϡʔʹ౉͢ΦϒδΣΫτ͕ ଟ͍ͱݟͮΒ͍

Slide 57

Slide 57 text

ղܾࡦ ʮϏϡʔʹ౉͢ม਺Λந৅Խʯ

Slide 58

Slide 58 text

7JFX.PEFM 7JFX0CKFDU class PostsController < ApplicationController def index @view_model = PostIndexViewModel.new end end class PostIndexViewModel def posts @posts ||= Post.page(params[:page).per(10) end def recommendation_categories @recommendation_categories ||= Category.recommendations end def recommendations @recommendations ||= recommendation_categories.posts end end

Slide 59

Slide 59 text

7BMJEBUJPO DBMMCBDL ͷ ৔߹෼͚ class Post < ApplicationRecord validates :body, presence: true, length: { maximum: 1000 } validates :published_at, presence: true validate :publish_at_should_after_current, unless: :by_admin attr_reader :by_admin def update_by_admin(attrs) @by_admin = true update(attrs) end def publish_at_should_after_current return unless publish_at return if publish_at > Time.zone.now errors.add(:publish_at, 'は現在時刻より後にしてください') end end

Slide 60

Slide 60 text

ద੾ͳந৅Խ Կͯ͠Δͷ͔ͻͱ໨Ͱ ෼͔ΔΑ͏ʹ͢Δ

Slide 61

Slide 61 text

Կͯ͠Δͷ͔ͻͱ໨Ͱ ෼͔ΔΑ͏ʹ͢Δ ৔߹෼͚

Slide 62

Slide 62 text

ղܾࡦ ϑΥʔϜ͝ͱʹΫϥεΛ࡞Δ

Slide 63

Slide 63 text

'PS̼0CKFDU class PostForm include ActiveModel::Model attr_accessor :post, :body, :publish_at validates :body, presence: true, length: { maximum: 1000 } validates :publish_at, presence: true validate :publish_at_should_after_current def save post.attributes = { body: body, publish_at: publish_at } post.save if valid? end private def publish_at_should_after_current return unless publish_at return if publish_at > Time.zone.now errors.add(:publish_at, 'は現在時刻より後にしてください') end end

Slide 64

Slide 64 text

'PS̼0CKFDU class AdminPostForm include ActiveModel::Model attr_accessor :post, :body, :publish_at validates :body, presence: true, length: { maximum: 1000 } validates :publish_at, presence: true def save post.attributes = { body: body, publish_at: publish_at } post.save if valid? end end

Slide 65

Slide 65 text

IUUQUFDINFEQFFSDPKQFOUSZ

Slide 66

Slide 66 text

ͻͱͭͷΞΫγϣϯͰ ͨ͘͞Μ΍Δ͜ͱ͕͋Δ class PostsController < ApplicationController def create Hoge.prepare_create1 # なんらかの前処理1 @post = current_user.post.build(post_params) Hoge.prepare_create2 # なんらかの前処理2 unless @post.save Fuga.prepare_new # new を表示するためのいろいろな前処理 render :new and return end Foo.after # なんらかの後処理 Bar.after # なんらかの後処理2 end end

Slide 67

Slide 67 text

ϩδοΫΛϞσϧʹ Ҡͯ͠΋TMJNʹͳΓ͖Εͳ͍ έʔε

Slide 68

Slide 68 text

ղܾࡦ ϞσϧͷϩδοΫΛݺͿՕॴΛ ந৅Խ͢Δ

Slide 69

Slide 69 text

4FSWJDF0CKFDU

Slide 70

Slide 70 text

஫ҙਓʹΑ͍ͬͯΖ͍Ζͳ 4FSWJDF͕͋Δ w 1P&""ʮ4FSWJDF-BZFSʯ w %%%ʮ4FSWJDFʯ w ͦΕΒΛ౿·͑ͨ3BJMTͷʮ4FSWJDF0CKFDUʯ w ͜Ε΋ෳ਺छྨ͕͋Δ

Slide 71

Slide 71 text

3BJMTͷ4FSWJDF0CKFDUʹ΋ ਓʹΑ༷ͬͯʑͳ࣮૷͕͋Δ w ͖ͬ͞ͷ1030ͷྫΛ4FSWJDF0CKFDUͱݺΜͰ͍Δ w ίϯτϩʔϥͷϩδοΫͷҰ෦ΛNPEVMFͷΫϥεϝιου ʹҠͯͦ͠ΕΛ4FSWJDFͱݺΜͰ͍Δ w ͦͷΞΫγϣϯͰ࣮ߦ͢Δ͜ͱΛ·ΔͬͱผΫϥεʹஔ͖ ׵͑Δ

Slide 72

Slide 72 text

͜ͷൃදͰͷ4FSWJDF0CKFDU w ΞΫγϣϯͷࡉʑͨ͠෦෼Λ·Δͬͱ୲͏ΦϒδΣΫτ w SFOEFS SFEJSFDU@UPͳͲ͸ίϯτϩʔϥ͕΍Δ w υϝΠϯϩδοΫ͸Ϟσϧ͕΍Δ

Slide 73

Slide 73 text

4FSWJDF0CKFDU class CreatePostService attr_reader :result def self.call(user:, params:) new(user: user, params: params).call end def initialize(user:, params:) @user = user @params = params @result = true end def call before_callback unless post.save fail! prepare_new return end after_callback self end def post @post ||= user.post.build(params) end private attr_reader :user, :paramas def before_callback Hoge.prepare_create1 Hoge.prepare_create2 end def after_callback Foo.after Bar.after end def prepare_new Fuga.prepare_new end def fail! @result = false end def post_params params.require(:post).permit(:body) end end

Slide 74

Slide 74 text

4FSWJDF0CKFDU class PostsController < ApplicationController def create service = CreatePostService.call(user: current_user, params: params) unless service.result @post = service.post render :new and return end redirect_to posts_path, notify: '作成されました' end end

Slide 75

Slide 75 text

͜Ε͚ͩ͋Ε͹͋Δఔ౓େ͖͍ 3BJMTΞϓϦέʔγϣϯͰ΋ઓ͑Δ w7JFX.PEFM w'PSN0CKFDU w4FSWJDF0CKFDU

Slide 76

Slide 76 text

͔͠͠Ͳ͏΍ͬͯݱ৔ ʹಋೖ͢Δʁ

Slide 77

Slide 77 text

νʔϜ։ൃʹ͸۩ମతͳྫ΍ Θ͔Γ΍͍͢υΩϡϝϯτ͕ ෆՄܽ

Slide 78

Slide 78 text

౷Ұͨ͠ॻ͖ํʹ ༠ಋͰ͖Δπʔϧ͕༗Δͱྑ ͍ͷͰ͸

Slide 79

Slide 79 text

IUUQTHJUIVCDPNXJMMOFUZVCB

Slide 80

Slide 80 text

:VCBϙΤϜ w 7JFX.PEFM 'PSN0CKFDU 4FSWJDF0CKFDUͷ࡞੒ิॿ༻HFN w ن໿ʹΑΓ࡞ΓํͰ໎Θͳ͍Α͏ʹ w 1030ͩͱຖճ΍Βͳ͖Ό͍͚ͳ͍໘౗ͳهड़͸؆୯ʹ͔͚ΔΑ͏ʹ w ඞཁͳͱ͜Ζ͚ͩಋೖͰ͖ΔΑ͏ʹ w ಋೖ͠΍ࣺͯ͘͢΍͍͢࡞Γʹ w ബ͘࡞Δ w 3BJMTΤϯδχΞ͕ҧ࿨ײͳ͘࢖͑ΔΠϯλϑΣʔε

Slide 81

Slide 81 text

஫ҙ w WFSͰ͢ w ී௨ʹ࢖͑Δ͸ͣͰ͕͢ɺ͜Ε͔Β࢓༷͕มΘΔՄೳੑ͕ ͋Γ·͢ w ಛʹ:VCB'PSN

Slide 82

Slide 82 text

1030Ͱ7JFX.PEFM class PostViewModel attr_reader :post def initialize(post:, author:, other: nil) @post = post @author = author @other = other end def title post.title end def body post.body end private attr_reader :author, :other end

Slide 83

Slide 83 text

ຖճಉ͡Α͏ͳॲཧΛ ॻ͔ͳ͚Ε͹ͳΒͳ͍ class PostViewModel attr_reader :post def initialize(post:, author:, other: nil) @post = post @author = author @other = other end def title post.title end def body post.body end private attr_reader :author, :other end

Slide 84

Slide 84 text

:VCB7JFX.PEFM class PostViewModel < Yuba::ViewModel property :post property :author, public: true property :other, optional: true def title post.title end def body post.body end end

Slide 85

Slide 85 text

!WJFX@NPEFM໰୊ class PostsController < ApplicationController def show @view_model = PostViewModel.new(post: post, author: author) end end <%= @view_model.post.title %> <%= @view_model.author.name %>

Slide 86

Slide 86 text

ඞͣ!WJFX@NPEFMΛ ܦ༝͠ͳ͚Ε͹ͳΒͳ͍ class PostsController < ApplicationController def show @view_model = PostViewModel.new(post: post, author: author) end end <%= @view_model.post.title %> <%= @view_model.author.name %>

Slide 87

Slide 87 text

7JFX.PEFMΛల։ͯ͠ Πϯελϯεม਺ʹׂ౰ class PostsController < ApplicationController def show view_model = PostViewModel.new(post: post, author: author) render view_model: view_model end end <%= @post.title %> <%= @author.name %>

Slide 88

Slide 88 text

:VCB4FSWJDF class PostController < ApplicationController def new @post = CreatePostService.call(user: current_user).post end def create service = CreatePostService.call(user: current_user, params: params) if service.success? redirect_to root_path else @post = service.post render :new end end end

Slide 89

Slide 89 text

:VCB4FSWJDF class CreatePostService < Yuba::Service property :user, public: true property :params, optional: true def call if post.save notify_to_admin else fail! end end def post user.posts.build(post_params) end private def notify_to_admin AdminMailer.notify_create_post(post).deliver_later end def post_params params.require(:post).permit(:title, :body) end end

Slide 90

Slide 90 text

:VCB4FSWJDFͷಛ௃ w 7JFX.PEFMͱಉ͡࢓༷ͷQSPQFSUZ w ࣮ߦ͸DBMM w DBMMͷҾ਺ΛQSPQFSUZʹׂΓ౰ͯͯDBMMΛݺͼग़͢ w TVDDFTT Ͱ੒ޭ൑ఆ

Slide 91

Slide 91 text

:VCB'PSN w ݱঢ় SFGPSNSBJMTͷϥούʔ w IUUQTHJUIVCDPNUSBJMCMB[FSSFGPSNSBJMT w ࣗલͷίʔυʹॻ͖׵͑தͳͷͰ͠͹Β͓͘଴ͪԼ͍͞ʜ

Slide 92

Slide 92 text

ࢼͯ͠Έ͍ͯͩ͘͞

Slide 93

Slide 93 text

·ͱΊ w .7$ͷਖ਼͍͠࢖͍ํʹ͍ͭͯ w 'BU$POUSPMMFS 'BU.PEFMͷղܾํ๏ w 3BJMT͕ఏڙ͍ͯ͠Δ.7$͚ͩͰ͸ରԠ͖͠Εͳ͍໰୊ͷղܾํ๏ w 7JFX.PEFM w 'PSN0CKFDU w 4FSWJDF0CKFDU w ˢΛແཧͳ͘ಋೖ͢ΔͨΊͷϥΠϒϥϦ:VCBͷ঺հ

Slide 94

Slide 94 text

)BQQZ3BJMT$PEJOH