Module: Operations::Form::Base::InstanceMethods

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

Overview

:nodoc:

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs) ⇒ Object

For now we gracefully return nil for unknown methods



111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/operations/form/base.rb', line 111

def method_missing(name, *args, **kwargs)
  build_attribute_name = build_attribute_name(name)
  build_attribute = self.class.attributes[build_attribute_name]
  plural_build_attribute = self.class.attributes[build_attribute_name.to_s.pluralize.to_sym]

  if has_attribute?(name)
    read_attribute(name)
  elsif build_attribute&.form
    build_attribute.form.new(*args, **kwargs)
  elsif plural_build_attribute&.form
    plural_build_attribute.form.new(*args, **kwargs)
  end
end

Instance Method Details

#_destroyObject Also known as: marked_for_destruction?



143
144
145
# File 'lib/operations/form/base.rb', line 143

def _destroy
  Operations::Types::Params::Bool.call(read_attribute(:_destroy)) { false }
end

#assigned_attributesObject



104
105
106
107
108
# File 'lib/operations/form/base.rb', line 104

def assigned_attributes
  (self.class.attributes.keys & data.keys).to_h do |name|
    [name, read_attribute(name)]
  end
end

#attributesObject



98
99
100
101
102
# File 'lib/operations/form/base.rb', line 98

def attributes
  self.class.attributes.keys.to_h do |name|
    [name, read_attribute(name)]
  end
end

#errorsObject



154
155
156
157
158
159
160
161
162
163
# File 'lib/operations/form/base.rb', line 154

def errors
  @errors ||= ActiveModel::Errors.new(self).tap do |errors|
    self.class.attributes.each do |name, attribute|
      add_messages(errors, name, messages[name])
      add_messages_to_collection(errors, name, messages[name]) if attribute.collection
    end

    add_messages(errors, :base, messages[nil])
  end
end

#has_attribute?(name) ⇒ Boolean

rubocop:disable Naming/PredicateName

Returns:

  • (Boolean)


94
95
96
# File 'lib/operations/form/base.rb', line 94

def has_attribute?(name) # rubocop:disable Naming/PredicateName
  self.class.attributes.key?(name.to_sym)
end

#localized_attr_name_for(name, locale) ⇒ Object



90
91
92
# File 'lib/operations/form/base.rb', line 90

def localized_attr_name_for(name, locale)
  self.class.attributes[name.to_sym].model_localized_attr_name(locale)
end

#model_nameObject



131
132
133
# File 'lib/operations/form/base.rb', line 131

def model_name
  self.class.model_name
end

#new_record?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/operations/form/base.rb', line 139

def new_record?
  !persisted?
end

#persisted?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/operations/form/base.rb', line 135

def persisted?
  self.class.persisted.nil? ? read_attribute(self.class.primary_key).present? : self.class.persisted
end

#read_attribute(name) ⇒ Object Also known as: read_attribute_for_validation



169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/operations/form/base.rb', line 169

def read_attribute(name)
  cached_attribute(name) do |value, attribute|
    if attribute.collection && attribute.form
      wrap_collection([name], value, attribute.form)
    elsif attribute.form
      wrap_object([name], value, attribute.form)
    elsif attribute.collection
      value.nil? ? [] : value
    else
      value
    end
  end
end

#respond_to_missing?(name) ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
128
129
# File 'lib/operations/form/base.rb', line 125

def respond_to_missing?(name, *)
  has_attribute?(name) ||
    build_nested_form?(build_attribute_name(name)) ||
    self.class.attributes[nested_attribute_name(name)]&.form
end

#to_hashObject



184
185
186
187
188
189
# File 'lib/operations/form/base.rb', line 184

def to_hash
  {
    attributes: attributes,
    errors: errors
  }
end

#to_keyObject

Probably can be always nil, it is used in automated URL derival. We can make it work later but it will require additional concepts.



150
151
152
# File 'lib/operations/form/base.rb', line 150

def to_key
  nil
end

#type_for_attribute(name) ⇒ Object



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

def type_for_attribute(name)
  self.class.attributes[name.to_sym].model_type
end

#valid?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/operations/form/base.rb', line 165

def valid?
  errors.empty?
end