Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Trailblazerの紹介

kbaba1001
September 20, 2015

 Trailblazerの紹介

kbaba1001

September 20, 2015
Tweet

More Decks by kbaba1001

Other Decks in Programming

Transcript

  1. A Concept-Driven OOP Framework app ├── concepts │ ├── comment

    │ │ ├── views │ │ │ ├── show.haml │ │ │ ├── list.haml │ │ ├── assets │ │ │ ├── comment.css.sass │ │ ├── cell.rb │ │ ├── operation.rb │ │ ├── twin.rb
  2. Model class Thing < ActiveRecord::Base has_many :comments, -> { order(created_at:

    :desc) } has_many :users, through: :authorships has_many :authorships scope :latest, lambda { all.limit(9).order("id DESC") } end
  3. Controller class ThingsController < ApplicationController def create run Thing::Create do

    |op| return redirect_to(thing_path op.model) # success. end render :new # re-render form. end
  4. Operation class Thing < ActiveRecord::Base class Create < Trailblazer::Operation include

    CRUD model Thing, :create contract do property :name property :description validates :name, presence: true validates :description, length: {in: 4..160}, allow_blank: true end def process(params) validate(params[:thing]) do |f| f.save end end end
  5. Controller class ThingsController < ApplicationController def create run Thing::Create do

    |op| return redirect_to(thing_path op.model) # success. end render :new # re-render form. end
  6. cell # app/concepts/comment/cell.rb class Comment::Cell < Cell::Concept property :body property

    :author def show render end private def author_link link_to "#{author.email}", author end end
  7. Operation Test it "persists valid" do thing = Thing::Create[ thing:

    { name: "Rails", description: "Kickass web dev" } ].model thing.persisted?.must_equal true thing.name.must_equal "Rails" thing.description.must_equal "Kickass web dev" end
  8. Cell の Test class CommentCellTest < Cell::TestCase controller ThingsController it

    do html = concept("comment/cell") html = Capybara.string(html) comments = html.all(:css, ".comment") first = comments[0] comments[0].find(".header").must_have_content "[email protected]" comments[0].find(".header time")["datetime"].must_match /\d\d-/