Last active
August 29, 2015 14:13
-
-
Save gogogarrett/5596e51882c05ed1e6e5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Bid < ActiveRecord::Base | |
class Create < Trailblazer::Operation | |
include CRUD | |
model Bid, :create | |
contract do | |
model Bid | |
property :approved | |
property :creator | |
property :comment | |
property :tier | |
validates :creator, :tier, :comment, presence: true | |
end | |
def process(params) | |
validate(params.fetch(:bid, {}).merge(creator: params[:creator], tier: params[:tier])) do |f| | |
f.save | |
end | |
end | |
end | |
class Update < Create | |
action :update | |
builds do |params| | |
Publish if params[:bid].present? && params[:bid][:published] | |
end | |
def process(params) | |
validate(params.fetch(:bid, {})) do |f| | |
f.save | |
end | |
end | |
end | |
class Publish < Update | |
action :update | |
def process(params) | |
@creator = params[:creator] | |
validate(params.fetch(:bid, {})) do |f| | |
f.save | |
if BidStatus.new(model).approved? | |
deduct_tier_quantity | |
create_conversation | |
create_message | |
end | |
end | |
end | |
def deduct_tier_quantity | |
model.tier.update(quantity: model.tier.quantity - 1) | |
end | |
def create_conversation | |
# [g] should this be an operation? | |
@conversation = ::Service::CreateConversation.new(model).call | |
end | |
def create_message | |
run Message::Create, {}.merge(conversation: @conversation, user: @creator) | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 :body, :conversation, 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