Skip to content

Instantly share code, notes, and snippets.

@macedo
Created June 11, 2015 02:44
Show Gist options
  • Save macedo/01c2a54560d022401554 to your computer and use it in GitHub Desktop.
Save macedo/01c2a54560d022401554 to your computer and use it in GitHub Desktop.
class UsersController < ApplicationController
def create
respond User::Create do |user, format|
format.json { render status: :created, json: user.to_json }
end
end
end
class User < ActiveRecord::Base
has_secure_password
class Create < Trailblazer::Operation
include CRUD, Responder, Representer
model User, :create
contract do
property :email
property :password
property :password_confirmation
validates :email, presence: true, uniqueness: true
validates :password, length: { minimum: 20 }
end
def process(params)
validate(params) { contract.save }
end
class JSON < self
contract do
property :id
property :email
end
representer do
property :id
property :email
end
end
builds do |params|
JSON if params[:format] == :json
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment