Module: Operations::Form::Base::ClassMethods

Included in:
Operations::Form::Base
Defined in:
lib/operations/form/base.rb

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/operations/form/base.rb', line 28

def self.extended(base)
  base.singleton_class.include Operations::Inspect.new(:attributes)

  base.extend Dry::Initializer
  base.include Dry::Equalizer(:attributes, :errors)
  base.include Operations::Inspect.new(:attributes, :errors)

  base.param :data,
    type: Operations::Types::Hash.map(Operations::Types::Symbol, Operations::Types::Any),
    default: proc { {} },
    reader: :private
  base.option :messages,
    type: Operations::Types::Hash.map(
      Operations::Types::Nil | Operations::Types::Coercible::Symbol,
      Operations::Types::Any
    ),
    default: proc { {} },
    reader: :private
  base.option :operation_result, default: proc {}

  base.class_attribute :attributes, instance_accessor: false, default: {}
  base.class_attribute :primary_key, instance_accessor: false, default: :id
  base.class_attribute :persisted, instance_accessor: false, default: nil

  base.define_method :initialize do |*args, **kwargs|
    if args.empty?
      # Initializing Operations::Form::Base instance
      super(kwargs, **{})
    else
      # Initializing Operations::Form instance as form object (deprecated)
      super(*args, **kwargs)
    end
  end
end

Instance Method Details

#attribute(name, **options) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/operations/form/base.rb', line 63

def attribute(name, **options)
  attribute = Operations::Form::Attribute.new(name, **options)

  self.attributes = attributes.merge(
    attribute.name => attribute
  )
end

#human_attribute_name(name, options = {}) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/operations/form/base.rb', line 71

def human_attribute_name(name, options = {})
  if attributes[name.to_sym]
    attributes[name.to_sym].model_human_name(options)
  else
    name.to_s.humanize
  end
end

#model_nameObject



83
84
85
# File 'lib/operations/form/base.rb', line 83

def model_name
  @model_name ||= ActiveModel::Name.new(self)
end

#reflect_on_associationObject



87
# File 'lib/operations/form/base.rb', line 87

def reflect_on_association(...); end

#validators_on(name) ⇒ Object



79
80
81
# File 'lib/operations/form/base.rb', line 79

def validators_on(name)
  attributes[name.to_sym]&.model_validators || []
end