Sha256: 3b027ca83f404fa27f84d5bcdb5befe7f8db1cd6fac9fc98c9e85ae80ce9f4a3
Contents?: true
Size: 1.67 KB
Versions: 1
Compression:
Stored size: 1.67 KB
Contents
# frozen_string_literal: true require 'readymade/response' require 'readymade/action' module Readymade class Operation < Readymade::Action attr_reader :form, :record, :record_params private def build_form @form = form_class.new(record_params, record: record) end def form_valid? build_form if @form.nil? && defined?(:build_form) form.validate end def assign_attributes record.assign_attributes(record_params) end def record_valid? return true if record.errors.none? && record.valid? sync_errors_to_form && false end def save_record record.save end def response(status, *args) Response.new(status, *args) end def success(*args) response(:success, *args) end def validation_fail(status = :validation_fail, args = {}) sync_errors_to_form response(status, args.merge!(record: record, record_params: record_params, form: form, errors: form.errors.messages)) end def fail(status = :fail, args = {}) sync_errors_to_form response(status, args.merge!(record: record, record_params: record_params, form: form, errors: form&.errors&.messages.presence || record&.errors&.messages)) end def form_class raise 'Define your own form object class in your operation' end def sync_errors_to_form form.sync_errors(from: record, to: form) end def sync_errors_to_record form.sync_errors end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
readymade-0.1.4 | lib/readymade/operation.rb |