Module: FormObj::ModelMapper

Defined in:
lib/form_obj/model_mapper.rb,
lib/form_obj/model_mapper/array.rb,
lib/form_obj/model_mapper/attribute.rb,
lib/form_obj/model_mapper/model_attribute.rb,
lib/form_obj/model_mapper/model_primary_key.rb,
lib/form_obj/model_mapper/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



10
11
12
# File 'lib/form_obj/model_mapper.rb', line 10

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#copy_errors_from_model(model) ⇒ Object



76
77
78
# 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) ⇒ Object



80
81
82
83
84
85
86
87
88
# 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) ⇒ Object



40
41
42
# 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) ⇒ Object



44
45
46
47
48
# 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) ⇒ Object



60
61
62
63
# 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

#sync_to_model(model) ⇒ Object



50
51
52
# File 'lib/form_obj/model_mapper.rb', line 50

def sync_to_model(model)
  sync_to_models(default: model)
end

#sync_to_models(models) ⇒ Object



54
55
56
57
58
# 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) ⇒ Object



65
66
67
# File 'lib/form_obj/model_mapper.rb', line 65

def to_model_hash(model = :default)
  to_models_hash[model]
end

#to_models_hash(models = {}) ⇒ Object



69
70
71
72
73
74
# 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