Module: Operations::Form::Base::InstanceMethods
Overview
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
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/operations/form/base.rb', line 110
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
#_destroy ⇒ Object
Also known as:
marked_for_destruction?
142
143
144
|
# File 'lib/operations/form/base.rb', line 142
def _destroy
Operations::Types::Params::Bool.call(read_attribute(:_destroy)) { false }
end
|
#assigned_attributes ⇒ Object
103
104
105
106
107
|
# File 'lib/operations/form/base.rb', line 103
def assigned_attributes
(self.class.attributes.keys & data.keys).to_h do |name|
[name, read_attribute(name)]
end
end
|
#attributes ⇒ Object
97
98
99
100
101
|
# File 'lib/operations/form/base.rb', line 97
def attributes
self.class.attributes.keys.to_h do |name|
[name, read_attribute(name)]
end
end
|
#errors ⇒ Object
153
154
155
156
157
158
159
160
161
162
|
# File 'lib/operations/form/base.rb', line 153
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
93
94
95
|
# File 'lib/operations/form/base.rb', line 93
def has_attribute?(name)
self.class.attributes.key?(name.to_sym)
end
|
#localized_attr_name_for(name, locale) ⇒ Object
89
90
91
|
# File 'lib/operations/form/base.rb', line 89
def localized_attr_name_for(name, locale)
self.class.attributes[name.to_sym].model_localized_attr_name(locale)
end
|
#model_name ⇒ Object
130
131
132
|
# File 'lib/operations/form/base.rb', line 130
def model_name
self.class.model_name
end
|
#new_record? ⇒ Boolean
138
139
140
|
# File 'lib/operations/form/base.rb', line 138
def new_record?
!persisted?
end
|
#persisted? ⇒ Boolean
134
135
136
|
# File 'lib/operations/form/base.rb', line 134
def persisted?
!has_attribute?(self.class.primary_key) || read_attribute(self.class.primary_key).present?
end
|
#read_attribute(name) ⇒ Object
Also known as:
read_attribute_for_validation
168
169
170
171
172
173
174
175
176
177
178
179
180
|
# File 'lib/operations/form/base.rb', line 168
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
124
125
126
127
128
|
# File 'lib/operations/form/base.rb', line 124
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_hash ⇒ Object
183
184
185
186
187
188
|
# File 'lib/operations/form/base.rb', line 183
def to_hash
{
attributes: attributes,
errors: errors
}
end
|
#to_key ⇒ Object
Probably can be always nil, it is used in automated URL derival. We can make it work later but it will require additional concepts.
149
150
151
|
# File 'lib/operations/form/base.rb', line 149
def to_key
nil
end
|
#type_for_attribute(name) ⇒ Object
85
86
87
|
# File 'lib/operations/form/base.rb', line 85
def type_for_attribute(name)
self.class.attributes[name.to_sym].model_type
end
|
#valid? ⇒ Boolean
164
165
166
|
# File 'lib/operations/form/base.rb', line 164
def valid?
errors.empty?
end
|