Module: FormObj::Mappable
- Defined in:
- lib/form_obj/mappable.rb,
lib/form_obj/mappable/array.rb,
lib/form_obj/mappable/attribute.rb,
lib/form_obj/mappable/model_attribute.rb,
lib/form_obj/mappable/model_primary_key.rb,
lib/form_obj/mappable/model_attribute/item.rb
Defined Under Namespace
Modules: ClassMethods
Classes: Array, Attribute, ModelAttribute, ModelPrimaryKey, PrimaryKeyMappingError
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.included(base) ⇒ Object
9
10
11
|
# File 'lib/form_obj/mappable.rb', line 9
def self.included(base)
base.extend(ClassMethods)
end
|
Instance Method Details
#copy_errors_from_model(model) ⇒ Object
84
85
86
|
# File 'lib/form_obj/mappable.rb', line 84
def copy_errors_from_model(model)
copy_errors_from_models(default: model)
end
|
#copy_errors_from_models(models) ⇒ Object
88
89
90
91
92
93
94
95
96
|
# File 'lib/form_obj/mappable.rb', line 88
def copy_errors_from_models(models)
self.class._attributes.each do |attribute|
if attribute.subform?
elsif attribute.model_attribute.write_to_model? @errors[attribute.name].push(*attribute.model_attribute.read_errors_from_models(models))
end
end
self
end
|
#load_from_model(model) ⇒ Object
31
32
33
|
# File 'lib/form_obj/mappable.rb', line 31
def load_from_model(model)
load_from_models(default: model)
end
|
#load_from_models(models) ⇒ Object
35
36
37
38
39
|
# File 'lib/form_obj/mappable.rb', line 35
def load_from_models(models)
self.class._attributes.each { |attribute| load_attribute_from_model(attribute, models) }
self.persisted = true
self
end
|
#primary_key=(val) ⇒ Object
51
52
53
54
|
# File 'lib/form_obj/mappable.rb', line 51
def primary_key=(val)
self.class._attributes.find(self.class.primary_key).validate_primary_key!
super
end
|
#save_to_model(model) ⇒ Object
41
42
43
|
# File 'lib/form_obj/mappable.rb', line 41
def save_to_model(model)
save_to_models(default: model)
end
|
#save_to_models(models) ⇒ Object
45
46
47
48
49
|
# File 'lib/form_obj/mappable.rb', line 45
def save_to_models(models)
self.class._attributes.each { |attribute | save_attribute_to_model(attribute, models) }
self.persisted = true
self
end
|
#to_model_hash(model = :default) ⇒ Object
56
57
58
|
# File 'lib/form_obj/mappable.rb', line 56
def to_model_hash(model = :default)
to_models_hash[model]
end
|
#to_models_hash(models = {}) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/form_obj/mappable.rb', line 60
def to_models_hash(models = {})
self.class._attributes.each do |attribute|
val = if attribute.subform?
if attribute.array?
[]
else
attribute.model_attribute.write_to_model? ? {} : (models[attribute.model_attribute.model] ||= {})
end
else
send(attribute.name)
end
value = if attribute.subform? && !attribute.model_attribute.write_to_model?
attribute.array? ? { self: val } : {}
elsif attribute.subform? || attribute.model_attribute.write_to_model?
attribute.model_attribute.to_model_hash(val)
end
(models[attribute.model_attribute.model] ||= {}).merge!(value) if attribute.subform? || attribute.model_attribute.write_to_model?
send(attribute.name).to_models_hash(models.merge(default: val)) if attribute.subform?
end
models
end
|