Skip to content

Instantly share code, notes, and snippets.

@konalegi
Last active October 19, 2016 07:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save konalegi/7213739ebe160ee16e640f899866fbfd to your computer and use it in GitHub Desktop.
Save konalegi/7213739ebe160ee16e640f899866fbfd to your computer and use it in GitHub Desktop.
op = ::Admin::Client::Create.(params)
render_client(op.profile)
class Admin::IncomingContact::Create < BaseOperation
attr_reader :client, :car, :case
CarContract = Class.new(Admin::Car::Create.contract) do
property :client_id, readable: false
validation :validate_client, inherit: true do
optional(:client_id).maybe(:int?)
end
end
contract do
property :client, form: Admin::Client::Create.contract
property :car, form: CarContract
property :case, form: Admin::Case::Create.contract
validation do
required(:client).filled
required(:car).filled
required(:case).filled
end
end
def setup_model!(params = {})
@client = UserFabric.build_client
@car = ::Car.new(client: @client)
@case = ::Case.new(car: @car)
self.model = OpenStruct.new(car: @car, case: @case, client: { user: @client.user, profile: @client })
end
def process_without_exception(params)
validate(params)
contract.save
end
end
class Admin::Client::Create < BaseOperation
attr_reader :profile
contract do
include Reform::Form::Composition
properties :name, :surname, :zc_comment, on: :profile do
validation do
required(:name).filled(:str?)
required(:surname).filled(:str?)
optional(:zc_comment).maybe(:str?)
end
end
properties :phone, :email, on: :user do
required(:phone).filled(:str?)
optional(:email).maybe(:str?)
end
end
def process_without_exception(params)
@profile = Profile::Client.new
@profile.user = User.new
validate(params, user: @profile.user, profile: @profile)
contract.save
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment