lib/rom/rails/model/form.rb in rom-rails-0.7.0 vs lib/rom/rails/model/form.rb in rom-rails-0.8.0
- old
+ new
@@ -90,12 +90,13 @@
delegate :model_name, to: :attributes
end
# @api private
def initialize(params = {}, options = {})
- @params = params
- @model = self.class.model.new(params.merge(options.slice(*self.class.key)))
+ @params = params.respond_to?(:to_unsafe_hash) ?
+ params.to_unsafe_hash : params
+ @model = self.class.model.new(params.to_h.merge(options.slice(*self.class.key)))
@result = nil
@errors = ErrorProxy.new
options.each { |key, value| instance_variable_set("@#{key}", value) }
end
@@ -112,13 +113,13 @@
#
# @return [self]
#
# @api public
def save(*args)
- @errors.clear
- @result = commit!(*args)
+ validate!
+ @result = commit!(*args) unless @errors.present?
@errors.set @result.error if result.respond_to? :error
self
end
@@ -134,9 +135,10 @@
# Trigger validation and store errors (if any)
#
# @api public
def validate!
@errors.clear
+ return unless defined? self.class::Validator
validator = self.class::Validator.new(attributes)
validator.validate
@errors.set validator.errors
end