module Formify::Form

Public Class Methods

t_error_key(attribute, *keys) click to toggle source
# File lib/formify/form.rb, line 63
def self.t_error_key(attribute, *keys)
  [
    translation_attributes_errors_key_base,
    attribute,
    *keys
  ].map(&:to_s).join('.')
end
translation_attributes_errors_key_base() click to toggle source
# File lib/formify/form.rb, line 51
def self.translation_attributes_errors_key_base
  "activemodel.errors.models.#{name.underscore}.attributes"
end

Public Instance Methods

attr_accessor(*attrs) click to toggle source
Calls superclass method
# File lib/formify/form.rb, line 136
def attr_accessor(*attrs)
  @class_params ||= []
  @class_params.concat(attrs)
  super(*attrs)
end
delegate_accessor(*args, **keywords) click to toggle source
# File lib/formify/form.rb, line 142
def delegate_accessor(*args, **keywords)
  delegate(
    *args.map { |arg| [arg, "#{arg}="] }.flatten,
    **keywords
  )
end
failure(*args) click to toggle source
# File lib/formify/form.rb, line 31
def failure(*args)
  Resonad.Failure(*args)
end
params() click to toggle source
# File lib/formify/form.rb, line 35
def params
  self.class.params
end
return_self() click to toggle source
# File lib/formify/form.rb, line 39
def return_self
  Resonad.Success(self)
end
success(*args) click to toggle source
# File lib/formify/form.rb, line 43
def success(*args)
  Resonad.Success(*args)
end
t(*args, **keywords) click to toggle source
# File lib/formify/form.rb, line 55
def t(*args, **keywords)
  I18n.t(*args, **keywords)
end
t_error(*args) click to toggle source
# File lib/formify/form.rb, line 59
def t_error(*args)
  I18n.t(self.class.t_error_key(*args))
end
translation_data() click to toggle source
# File lib/formify/form.rb, line 47
def translation_data
  {}
end
translation_success() click to toggle source
# File lib/formify/form.rb, line 71
def translation_success
  split_name = self.class.name.split('::')
  split_name.shift
  action = split_name.pop.underscore
  key = split_name.map(&:underscore).push(action).push(:success).join('.')
  return t(key) if I18n.exists?(key, translation_data)
end
validate_or_fail(*instances) click to toggle source
# File lib/formify/form.rb, line 79
def validate_or_fail(*instances)
  unless valid?
    return Resonad.Failure(
      Formify::Errors::ValidationError.new(form: self)
    )
  end

  if instances.present?
    instances.each do |instance|
      next if instance.valid?

      return Resonad.Failure(
        Formify::Errors::ValidationError.new(form: self, message: instance.full_messages.first)
      )
    end
  end

  Resonad.Success
end
with_advisory_lock(*keys, **args) { || ... } click to toggle source
# File lib/formify/form.rb, line 99
def with_advisory_lock(*keys, **args)
  key = if keys.present?
          keys.map do |k|
            if k.is_a?(String)
              k
            else
              k.try(:id)
            end
          end.join('/')
        else
          self.class.name.underscore
        end
  ActiveRecord::Base.with_advisory_lock(key, **args) { yield }
end
with_advisory_lock_transaction(*keys) { || ... } click to toggle source
# File lib/formify/form.rb, line 114
def with_advisory_lock_transaction(*keys)
  with_advisory_lock(*keys, transaction: true) do
    with_transaction { yield }
  end
end
with_transaction() { || ... } click to toggle source
# File lib/formify/form.rb, line 120
def with_transaction
  with_transaction_result = Resonad.Failure

  ActiveRecord::Base.transaction do
    with_transaction_result = begin
      yield
    end

    raise ActiveRecord::Rollback if with_transaction_result.failure?
  end

  with_transaction_result
end