Module: FormModel

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Validations, Virtus
Defined in:
lib/form_model/model.rb,
lib/form_model/errors.rb,
lib/form_model/mapper.rb,
lib/form_model/version.rb,
lib/form_model/associated_validation.rb

Defined Under Namespace

Modules: ClassMethods, Mapper Classes: AssociatedValidator, Error, MapperAssertionError, ModelMisMatchError

Constant Summary collapse

VERSION =
"0.0.1"

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



167
168
169
# File 'lib/form_model/model.rb', line 167

def method_missing(method, *args, &block)
  data_model.send(method, *args, &block)
end

Instance Method Details

#bound_classObject



56
57
58
# File 'lib/form_model/model.rb', line 56

def bound_class
  self.class.bound_class
end

#form_valid?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/form_model/model.rb', line 64

def form_valid?
  valid?(:form)
end

#merge_errors_with!(object) ⇒ Object



50
51
52
53
54
# File 'lib/form_model/model.rb', line 50

def merge_errors_with!(object)
  object.errors.to_hash.each do |key, value|
    self.errors.add(key, value)
  end
end

#persisted?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/form_model/model.rb', line 73

def persisted?
  data_model.persisted?
end

#respond_to?(method_sym, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/form_model/model.rb', line 104

def respond_to?(method_sym, include_private = false)
  super || data_model.respond_to?(method_sym, include_private)
end

#saveObject



60
61
62
# File 'lib/form_model/model.rb', line 60

def save
  valid? ? data_model.save : false
end

#to_keyObject



81
82
83
# File 'lib/form_model/model.rb', line 81

def to_key
  data_model.to_key
end

#to_modelObject



77
78
79
# File 'lib/form_model/model.rb', line 77

def to_model
  data_model.to_model
end

#to_paramObject



85
86
87
# File 'lib/form_model/model.rb', line 85

def to_param
  data_model.to_param
end

#to_pathObject



89
90
91
# File 'lib/form_model/model.rb', line 89

def to_path
  data_model.to_path
end

#update(attrs = {}) ⇒ Object



68
69
70
71
# File 'lib/form_model/model.rb', line 68

def update(attrs = {})
  self.attributes = attrs || {}
  self
end

#update_data_model!Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/form_model/model.rb', line 93

def update_data_model!
  unless data_model
    @data_model = bound_class.new
  end
  attrs = attributes.slice(*data_model_attribute_names).stringify_keys
  apply_mappers_to_model!(attrs)
  self.instance_exec(&before_write_block) unless self.class.before_write_block.nil?
  data_model.write_attributes(attrs)
  data_model
end

#valid?(type = :model_and_form) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
47
48
# File 'lib/form_model/model.rb', line 42

def valid?(type = :model_and_form)
  return super if type == :form
  update_data_model!
  ok = (super and data_model.valid?)
  merge_data_model_errors! unless ok
  ok
end