# File lib/form_obj/model_mapper.rb, line 60 def primary_key=(val) self.class._attributes.find(self.class.primary_key).validate_primary_key! super end
module FormObj::ModelMapper
Public Class Methods
included(base)
click to toggle source
# File lib/form_obj/model_mapper.rb, line 10 def self.included(base) base.extend(ClassMethods) end
Public Instance Methods
copy_errors_from_model(model)
click to toggle source
# File lib/form_obj/model_mapper.rb, line 76 def copy_errors_from_model(model) copy_errors_from_models(default: model) end
copy_errors_from_models(models)
click to toggle source
# File lib/form_obj/model_mapper.rb, line 80 def copy_errors_from_models(models) self.class._attributes.each do |attribute| if attribute.subform? elsif attribute.model_attribute.write_to_model? # Use :write_to_model? instead of :read_to_model? because validation errors appears after writing to model @errors[attribute.name].push(*attribute.model_attribute.read_errors_from_models(models)) end end self end
load_from_model(model, *args)
click to toggle source
# File lib/form_obj/model_mapper.rb, line 40 def load_from_model(model, *args) load_from_models({ default: model }, *args) end
load_from_models(models, *args)
click to toggle source
# File lib/form_obj/model_mapper.rb, line 44 def load_from_models(models, *args) self.class._attributes.each { |attribute| load_attribute_from_model(attribute, models, *args) } self.persisted = true self end
primary_key=(val)
click to toggle source
Calls superclass method
sync_to_model(model)
click to toggle source
# File lib/form_obj/model_mapper.rb, line 50 def sync_to_model(model) sync_to_models(default: model) end
sync_to_models(models)
click to toggle source
# File lib/form_obj/model_mapper.rb, line 54 def sync_to_models(models) self.class._attributes.each { |attribute | sync_attribute_to_model(attribute, models) } self.persisted = true self end
to_model_hash(model = :default)
click to toggle source
# File lib/form_obj/model_mapper.rb, line 65 def to_model_hash(model = :default) to_models_hash[model] end
to_models_hash(models = {})
click to toggle source
# File lib/form_obj/model_mapper.rb, line 69 def to_models_hash(models = {}) self.class._attributes.each do |attribute| attribute_to_models_hash(attribute, models) end models end
Private Instance Methods
attribute_to_models_hash(attribute, models)
click to toggle source
# File lib/form_obj/model_mapper.rb, line 120 def attribute_to_models_hash(attribute, models) return unless attribute.model_attribute.write_to_model? val = if attribute.subform? if attribute.array? [] else attribute.model_attribute.nesting? ? {} : (models[attribute.model_attribute.model] ||= {}) end else read_attribute(attribute) end value = if attribute.subform? && !attribute.model_attribute.nesting? attribute.array? ? { self: val } : {} else attribute.model_attribute.to_model_hash(val) end (models[attribute.model_attribute.model] ||= {}).merge!(value) read_attribute(attribute).to_models_hash(models.merge(default: val)) if attribute.subform? end
load_attribute_from_model(attribute, models, *args)
click to toggle source
# File lib/form_obj/model_mapper.rb, line 92 def load_attribute_from_model(attribute, models, *args) return unless attribute.model_attribute.read_from_model? if attribute.subform? if attribute.model_attribute.nesting? read_attribute(attribute).load_from_models(models.merge(default: attribute.model_attribute.read_from_models(models)), *args) else read_attribute(attribute).load_from_models(models.merge(default: models[attribute.model_attribute.model]), *args) end else write_attribute(attribute, attribute.model_attribute.read_from_models(models)) end end
sync_attribute_to_model(attribute, models)
click to toggle source
# File lib/form_obj/model_mapper.rb, line 106 def sync_attribute_to_model(attribute, models) return unless attribute.model_attribute.write_to_model? if attribute.subform? if attribute.model_attribute.nesting? read_attribute(attribute).sync_to_models(models.merge(default: attribute.model_attribute.read_from_models(models, create_nested_model_if_nil: true))) else read_attribute(attribute).sync_to_models(models.merge(default: models[attribute.model_attribute.model])) end else attribute.model_attribute.write_to_models(models, read_attribute(attribute)) end end