Top Level Namespace
Defined Under Namespace
Modules: DatabaseValidations
Instance Method Summary collapse
-
#validate_db_uniqueness_of ⇒ Object
Matches when model validate database uniqueness of some field.
Instance Method Details
#validate_db_uniqueness_of ⇒ Object
Matches when model validate database uniqueness of some field.
Modifiers:
-
‘with_message(message)` – specifies a message of the error;
-
‘scoped_to(scope)` – specifies a scope for the validator;
-
‘with_where(where)` – specifies a where condition for the validator;
Example:
“‘ruby it { expect(Model).to validate_db_uniqueness_of(:field) } “`
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/database_validations/rspec/uniqueness_validator_matcher.rb', line 15 RSpec::Matchers.define :validate_db_uniqueness_of do |field| chain(:with_message) do || @message = end chain(:scoped_to) do |*scope| @scope = scope.flatten end chain(:with_where) do |where| @where = where end match do |model| @validators = [] DatabaseValidations::Helpers.each_validator(model) do |validator| @validators << { field: validator.field, scope: validator.scope, where: validator.where_clause, message: validator. } end @validators.include?(field: field, scope: Array.wrap(@scope), where: @where, message: @message) end description do desc = "validate database uniqueness of #{field}. " desc += "With options - " if @message || @scope || @where desc += "message: '#{@message}'; " if @message desc += "scope: #{@scope}; " if @scope desc += "where: '#{@where}'. " if @where desc end do <<-TEXT There is no such database uniqueness validator. Available validators are: #{@validators}. TEXT end end |