Skip to content

Instantly share code, notes, and snippets.

@gogogarrett
Created January 11, 2015 22:19
Show Gist options
  • Save gogogarrett/18a7875a642db625dc44 to your computer and use it in GitHub Desktop.
Save gogogarrett/18a7875a642db625dc44 to your computer and use it in GitHub Desktop.
Traillazer Operation
class Message < ActiveRecord::Base
class Create < Trailblazer::Operation
include CRUD
model Message, :create
builds do |params|
System if params[:conversation].messages.empty?
end
contract do
model Message
property :user
property :body
property :conversation
validates :user, presence: true
end
def process(params)
conversation = params[:conversation]
user = params[:user]
validate(params.fetch(:message, {}).merge(conversation: conversation, user: user)) do |f|
f.save
end
end
class System < self
contract do
model Message
property :body
property :conversation
validates :user, presence: true
end
def process(params)
@conversation = params[:conversation]
create_hash = params.fetch(:message, {}).merge(conversation: @conversation, body: body)
validate(create_hash) do |f|
f.save
end
end
def body
# [g] gross.
"""
New conversation between #{@conversation.event.organization} and #{@conversation.user}.\n\n
#{@conversation.tier.event} - #{@conversation.tier}
"""
end
end
end
class Update < Create
action :update
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment