Refactoring tests and Validation callbacks
This commit is contained in:
parent
ea4325f5bf
commit
3579e0e334
47 changed files with 142 additions and 142 deletions
|
@ -7,7 +7,6 @@ module CouchRest
|
|||
include CouchRest::Model::Configuration
|
||||
include CouchRest::Model::Connection
|
||||
include CouchRest::Model::Persistence
|
||||
include CouchRest::Model::Callbacks
|
||||
include CouchRest::Model::DocumentQueries
|
||||
include CouchRest::Model::Views
|
||||
include CouchRest::Model::DesignDoc
|
||||
|
@ -18,6 +17,7 @@ module CouchRest
|
|||
include CouchRest::Model::PropertyProtection
|
||||
include CouchRest::Model::Associations
|
||||
include CouchRest::Model::Validations
|
||||
include CouchRest::Model::Callbacks
|
||||
include CouchRest::Model::Designs
|
||||
include CouchRest::Model::CastedBy
|
||||
include CouchRest::Model::Dirty
|
||||
|
|
|
@ -5,21 +5,23 @@ module CouchRest #:nodoc:
|
|||
|
||||
module Callbacks
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
CALLBACKS = [
|
||||
:before_validation, :after_validation,
|
||||
:after_initialize,
|
||||
:before_create, :around_create, :after_create,
|
||||
:before_destroy, :around_destroy, :after_destroy,
|
||||
:before_save, :around_save, :after_save,
|
||||
:before_update, :around_update, :after_update,
|
||||
]
|
||||
|
||||
included do
|
||||
extend ActiveModel::Callbacks
|
||||
include ActiveModel::Validations::Callbacks
|
||||
|
||||
define_model_callbacks \
|
||||
:create,
|
||||
:destroy,
|
||||
:save,
|
||||
:update
|
||||
|
||||
define_model_callbacks :initialize, :only => :after
|
||||
define_model_callbacks :create, :destroy, :save, :update
|
||||
end
|
||||
|
||||
def valid?(*) #nodoc
|
||||
_run_validation_callbacks { super }
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -5,11 +5,11 @@ module CouchRest::Model
|
|||
|
||||
included do
|
||||
include CouchRest::Model::Configuration
|
||||
include CouchRest::Model::Callbacks
|
||||
include CouchRest::Model::Properties
|
||||
include CouchRest::Model::PropertyProtection
|
||||
include CouchRest::Model::Associations
|
||||
include CouchRest::Model::Validations
|
||||
include CouchRest::Model::Callbacks
|
||||
include CouchRest::Model::CastedBy
|
||||
include CouchRest::Model::Dirty
|
||||
class_eval do
|
||||
|
|
|
@ -13,22 +13,33 @@ module CouchRest
|
|||
# Validations may be applied to both Model::Base and Model::CastedModel
|
||||
module Validations
|
||||
extend ActiveSupport::Concern
|
||||
included do
|
||||
include ActiveModel::Validations
|
||||
include ActiveModel::Validations::Callbacks
|
||||
include ActiveModel::Validations
|
||||
|
||||
# Determine if the document is valid.
|
||||
#
|
||||
# @example Is the document valid?
|
||||
# person.valid?
|
||||
#
|
||||
# @example Is the document valid in a context?
|
||||
# person.valid?(:create)
|
||||
#
|
||||
# @param [ Symbol ] context The optional validation context.
|
||||
#
|
||||
# @return [ true, false ] True if valid, false if not.
|
||||
#
|
||||
def valid?(context = nil)
|
||||
super context ? context : (new? ? :create : :update)
|
||||
end
|
||||
|
||||
|
||||
module ClassMethods
|
||||
|
||||
# Validates the associated casted model. This method should not be
|
||||
|
||||
# Validates the associated casted model. This method should not be
|
||||
# used within your code as it is automatically included when a CastedModel
|
||||
# is used inside the model.
|
||||
#
|
||||
def validates_casted_model(*args)
|
||||
validates_with(CastedModelValidator, _merge_attributes(args))
|
||||
end
|
||||
|
||||
|
||||
# Validates if the field is unique for this type of document. Automatically creates
|
||||
# a view if one does not already exist and performs a search for all matching
|
||||
# documents.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue