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
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/operations/form/base.rb', line 118
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?
150
151
152
|
# File 'lib/operations/form/base.rb', line 150
def _destroy
Operations::Types::Params::Bool.call(read_attribute(:_destroy)) { false }
end
|
#assigned_attributes ⇒ Object
111
112
113
114
115
|
# File 'lib/operations/form/base.rb', line 111
def assigned_attributes
(self.class.attributes.keys & data.keys).to_h do |name|
[name, read_attribute(name)]
end
end
|
#attributes ⇒ Object
105
106
107
108
109
|
# File 'lib/operations/form/base.rb', line 105
def attributes
self.class.attributes.keys.to_h do |name|
[name, read_attribute(name)]
end
end
|
#errors ⇒ Object
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/operations/form/base.rb', line 161
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
101
102
103
|
# File 'lib/operations/form/base.rb', line 101
def has_attribute?(name) self.class.attributes.key?(name.to_sym)
end
|
#localized_attr_name_for(attr_name, locale) ⇒ Object
Copied from globalize-accessors, should be deprecated and removed as it is not a core method
93
94
95
|
# File 'lib/operations/form/base.rb', line 93
def localized_attr_name_for(attr_name, locale)
"#{attr_name}_#{locale.to_s.underscore}"
end
|
#model_name ⇒ Object
138
139
140
|
# File 'lib/operations/form/base.rb', line 138
def model_name
self.class.model_name
end
|
#new_record? ⇒ Boolean
146
147
148
|
# File 'lib/operations/form/base.rb', line 146
def new_record?
!persisted?
end
|
#persisted? ⇒ Boolean
142
143
144
|
# File 'lib/operations/form/base.rb', line 142
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
176
177
178
179
180
181
182
183
184
185
186
187
188
|
# File 'lib/operations/form/base.rb', line 176
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
132
133
134
135
136
|
# File 'lib/operations/form/base.rb', line 132
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
191
192
193
194
195
196
|
# File 'lib/operations/form/base.rb', line 191
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.
157
158
159
|
# File 'lib/operations/form/base.rb', line 157
def to_key
nil
end
|
#type_for_attribute(name) ⇒ Object
97
98
99
|
# File 'lib/operations/form/base.rb', line 97
def type_for_attribute(name)
self.class.attributes[name.to_sym].model_type
end
|
#valid? ⇒ Boolean
172
173
174
|
# File 'lib/operations/form/base.rb', line 172
def valid?
errors.empty?
end
|