Trailblazer Introduction by Nick Sutterer

84

Transcript of Trailblazer Introduction by Nick Sutterer

Page 1: Trailblazer Introduction by Nick Sutterer
Page 2: Trailblazer Introduction by Nick Sutterer
Page 3: Trailblazer Introduction by Nick Sutterer
Page 4: Trailblazer Introduction by Nick Sutterer
Page 5: Trailblazer Introduction by Nick Sutterer

class CommentsController < AppController def create Comment::Create.(params) endend

Page 6: Trailblazer Introduction by Nick Sutterer

class Comment < ActiveRecord::Base belongs_to :authorend

Page 7: Trailblazer Introduction by Nick Sutterer

.comment .text = body .author = avatar

Page 8: Trailblazer Introduction by Nick Sutterer
Page 9: Trailblazer Introduction by Nick Sutterer

FUNCTIONS

Page 10: Trailblazer Introduction by Nick Sutterer

”Create comment”

”View profile”

”Follow user”

Page 11: Trailblazer Introduction by Nick Sutterer

POST /comments/

”Create comment”

”View profile”

”Follow user”GET /profiles/1

POST /users/1/follow

Page 12: Trailblazer Introduction by Nick Sutterer

POST /comments/

”Create comment”

Page 13: Trailblazer Introduction by Nick Sutterer

POST /comments/

”Create comment”

Page 14: Trailblazer Introduction by Nick Sutterer

POST /comments/

”Create comment”

Page 15: Trailblazer Introduction by Nick Sutterer
Page 16: Trailblazer Introduction by Nick Sutterer
Page 17: Trailblazer Introduction by Nick Sutterer
Page 18: Trailblazer Introduction by Nick Sutterer
Page 19: Trailblazer Introduction by Nick Sutterer
Page 20: Trailblazer Introduction by Nick Sutterer
Page 21: Trailblazer Introduction by Nick Sutterer
Page 22: Trailblazer Introduction by Nick Sutterer
Page 23: Trailblazer Introduction by Nick Sutterer

“Most Rails developers do not write object-oriented Ruby code.

They write MVC-oriented Ruby code by putting models and controllers in the expected locations.

It takes 2-3 years before developers realize: “Rails is just Ruby.”

-- Mike Perham

Page 24: Trailblazer Introduction by Nick Sutterer
Page 25: Trailblazer Introduction by Nick Sutterer
Page 26: Trailblazer Introduction by Nick Sutterer
Page 27: Trailblazer Introduction by Nick Sutterer
Page 28: Trailblazer Introduction by Nick Sutterer
Page 29: Trailblazer Introduction by Nick Sutterer

POST /comments { id : 1 body: “Great!”, author: { “id”: 1 }}

Page 30: Trailblazer Introduction by Nick Sutterer
Page 31: Trailblazer Introduction by Nick Sutterer
Page 32: Trailblazer Introduction by Nick Sutterer
Page 33: Trailblazer Introduction by Nick Sutterer

app concepts comment cell index.rb show.rb contract create.rb update.rb operation create.rb update.rb view index.haml show.haml

Page 34: Trailblazer Introduction by Nick Sutterer

app concepts comment cell index.rb show.rb contract create.rb update.rb operation create.rb update.rb view index.haml show.haml

Page 35: Trailblazer Introduction by Nick Sutterer
Page 36: Trailblazer Introduction by Nick Sutterer

POST /comments { body: “Great!”, author: { “id”: 1 }}

Page 37: Trailblazer Introduction by Nick Sutterer

POST /comments

Page 38: Trailblazer Introduction by Nick Sutterer

class CommentsController < AppController def create Comment::Create.(params) endend

POST /comments

Page 39: Trailblazer Introduction by Nick Sutterer

Comment::Create.(params)

POST /comments

Page 40: Trailblazer Introduction by Nick Sutterer

class Comment < ActiveRecord::Base class Create < Trailblazer::Operation def process(params) validate do end end endend

Page 41: Trailblazer Introduction by Nick Sutterer

class Comment < ActiveRecord::Base class Create < Trailblazer::Operation def process(params) validate do end end endend

app concepts comment cell index.rb show.rb contract create.rb update.rb operation create.rb update.rb view index.haml show.haml

Page 42: Trailblazer Introduction by Nick Sutterer

class Comment < ActiveRecord::Base class Create < Trailblazer::Operation def process(params)

end endend

Page 43: Trailblazer Introduction by Nick Sutterer

class Comment < ActiveRecord::Base class Create < Trailblazer::Operation def process(params)

end endend

Page 44: Trailblazer Introduction by Nick Sutterer

class Comment < ActiveRecord::Base class Create < Trailblazer::Operation def process(params) Comment.create(params)

end endend

Page 45: Trailblazer Introduction by Nick Sutterer

class Comment < ActiveRecord::Base class Create < Trailblazer::Operation def process(params) model = Comment.new validate(params, model) do |f| f.save end end endend

Page 46: Trailblazer Introduction by Nick Sutterer

class Comment < ActiveRecord::Base class Create < Trailblazer::Operation def process(params) model = Comment.new validate(params, model) do |f| f.save end end endend

Page 47: Trailblazer Introduction by Nick Sutterer

class Comment < ActiveRecord::Base class Create < Trailblazer::Operation contract do

end

def process(params) model = Comment.new validate(params, model) do |f| f.save end end endend

Page 48: Trailblazer Introduction by Nick Sutterer

class Comment < ActiveRecord::Base class Create < Trailblazer::Operation contract do property :body validates :body, presence: true end

def process(params) model = Comment.new validate(params, model) do |f| f.save end end endend

Page 49: Trailblazer Introduction by Nick Sutterer

class Comment < ActiveRecord::Base class Create < Trailblazer::Operation contract Contract::Create

def process(params) model = Comment.new validate(params, model) do |f| f.save end end endend

Page 50: Trailblazer Introduction by Nick Sutterer

class Comment < ActiveRecord::Base module Contract class Create < Reform::Form

end endend

Page 51: Trailblazer Introduction by Nick Sutterer

class Comment < ActiveRecord::Base module Contract class Create < Reform::Form

end endend

app concepts comment cell index.rb show.rb contract create.rb update.rb operation create.rb update.rb view index.haml show.haml

Page 52: Trailblazer Introduction by Nick Sutterer

class Comment < ActiveRecord::Base module Contract class Create < Reform::Form

end endend

Page 53: Trailblazer Introduction by Nick Sutterer

class Comment < ActiveRecord::Base module Contract class Create < Reform::Form property :body validates :body, presence: true

end endend

Page 54: Trailblazer Introduction by Nick Sutterer
Page 55: Trailblazer Introduction by Nick Sutterer

class Comment < ActiveRecord::Base class Create < Trailblazer::Operation contract Contract::Create

def process(params) model = Comment.new validate(params, model) do |f| f.save end end endend

Page 56: Trailblazer Introduction by Nick Sutterer

class Comment < ActiveRecord::Base class Create < Trailblazer::Operation contract Contract::Create

def process(params) model = Comment.new validate(params, model) do |f| f.save end end endend

Page 57: Trailblazer Introduction by Nick Sutterer

class Comment < ActiveRecord::Base belongs_to :authorend

Page 58: Trailblazer Introduction by Nick Sutterer
Page 59: Trailblazer Introduction by Nick Sutterer

class CommentsController < AppController def create Comment::Create.(params) endend

Page 60: Trailblazer Introduction by Nick Sutterer

describe Comment::Create do it do Comment::Create.(params)

endend

Page 61: Trailblazer Introduction by Nick Sutterer

describe Comment::Create do it do Comment::Create.(params). model.persisted? must_equal true endend

Page 62: Trailblazer Introduction by Nick Sutterer

describe Comment::Update do let(:comment) { Comment::Create.(..) } end

Page 63: Trailblazer Introduction by Nick Sutterer

describe Comment::Create do it do Comment::Create.(params). model.persisted? must_equal true endend

Page 64: Trailblazer Introduction by Nick Sutterer
Page 65: Trailblazer Introduction by Nick Sutterer

class Comment < ActiveRecord::Base class Create < Trailblazer::Operation contract Contract::Create

def process(params) model = Comment.new validate(params, model) do |f| f.save

end end endend

Page 66: Trailblazer Introduction by Nick Sutterer

class Comment < ActiveRecord::Base class Create < Trailblazer::Operation contract Contract::Create

def process(params) model = Comment.new validate(params, model) do |f| f.save after_save! end end endend

Page 67: Trailblazer Introduction by Nick Sutterer
Page 68: Trailblazer Introduction by Nick Sutterer

= cell("comment/cell/show", comment)

Page 69: Trailblazer Introduction by Nick Sutterer

class Comment::Cell::Show < Cell::ViewModel

end

Page 70: Trailblazer Introduction by Nick Sutterer

class Comment::Cell::Show < Cell::ViewModel

end

app concepts comment cell index.rb show.rb contract create.rb update.rb operation create.rb update.rb view index.haml show.haml

Page 71: Trailblazer Introduction by Nick Sutterer

class Comment::Cell::Show < Cell::ViewModel property :body def show render end

private def avatar image_tag model.image.url endend

.comment .text = body .author = avatar

Page 72: Trailblazer Introduction by Nick Sutterer

class Comment::Cell::Show < Cell::ViewModel property :body def show render end

private def avatar image_tag model.image.url endend

.comment .text = body .author = avatar

app concepts comment cell index.rb show.rb contract create.rb update.rb operation create.rb update.rb view index.haml show.haml

Page 73: Trailblazer Introduction by Nick Sutterer
Page 74: Trailblazer Introduction by Nick Sutterer
Page 75: Trailblazer Introduction by Nick Sutterer
Page 76: Trailblazer Introduction by Nick Sutterer
Page 77: Trailblazer Introduction by Nick Sutterer
Page 78: Trailblazer Introduction by Nick Sutterer
Page 79: Trailblazer Introduction by Nick Sutterer

http://leanpub.com/trailblazer

Page 80: Trailblazer Introduction by Nick Sutterer
Page 81: Trailblazer Introduction by Nick Sutterer

POST_RAILS_BOOK_BUNDLE_159

Page 82: Trailblazer Introduction by Nick Sutterer

trailblazer.to

Page 83: Trailblazer Introduction by Nick Sutterer
Page 84: Trailblazer Introduction by Nick Sutterer

@apotonick