Skip to content
Browse files

WIP: start to move to using an options hash.

  • Loading branch information...
1 parent 4fdb827 commit a718c468144832d8cbac8b96bd214a352b580717 @gabebw gabebw committed Apr 23, 2012
View
4 NEWS.md
@@ -8,6 +8,10 @@ HEAD
* Fix `ensure_length_of` to use all possible I18n error messages.
+* `have_db_index.unique(nil)` used to function exactly the same as
+ `have_db_index` with no unique option. It now functions the same as
+ `have_db_index.unique(false)`.
+
# v1.1.0
* Added `only_integer` option to `validate_numericality_of`:
View
54 lib/shoulda/matchers/active_record/accept_nested_attributes_for_matcher.rb
@@ -24,21 +24,21 @@ def accept_nested_attributes_for(name)
class AcceptNestedAttributesForMatcher
def initialize(name)
@name = name
- self
+ @options = {}
end
def allow_destroy(allow_destroy)
- @allow_destroy = allow_destroy
+ @options[:allow_destroy] = allow_destroy
self
end
def limit(limit)
- @limit = limit
+ @options[:limit] = limit
self
end
def update_only(update_only)
- @update_only = update_only
+ @options[:update_only] = update_only
self
end
@@ -60,9 +60,9 @@ def negative_failure_message
def description
description = "accepts_nested_attributes_for :#{@name}"
- description += " allow_destroy => #{@allow_destroy}" if @allow_destroy
- description += " limit => #{@limit}" if @limit
- description += " update_only => #{@update_only}" if @update_only
+ description += " allow_destroy => #{@options[:allow_destroy]}" if @options[:allow_destroy]
+ description += " limit => #{@options[:limit]}" if @options[:limit]
+ description += " update_only => #{@options[:update_only]}" if @options[:update_only]
description
end
@@ -78,31 +78,43 @@ def exists?
end
def allow_destroy_correct?
- if @allow_destroy.nil? || @allow_destroy == config[:allow_destroy]
- true
+ if @options.key?(:allow_destroy)
+ if @options[:allow_destroy] == config[:allow_destroy]
+ true
+ else
+ @problem = (@options[:allow_destroy] ? "should" : "should not") +
+ " allow destroy"
+ false
+ end
else
- @problem = (@allow_destroy ? "should" : "should not") +
- " allow destroy"
- false
+ true
end
end
def limit_correct?
- if @limit.nil? || @limit == config[:limit]
- true
+ if @options.key?(:limit)
+ if @options[:limit] == config[:limit]
+ true
+ else
+ @problem = "limit should be #{@options[:limit]}, got #{config[:limit]}"
+ false
+ end
else
- @problem = "limit should be #@limit, got #{config[:limit]}"
- false
+ true
end
end
def update_only_correct?
- if @update_only.nil? || @update_only == config[:update_only]
- true
+ if @options.key?(:update_only)
+ if @options[:update_only] == config[:update_only]
+ true
+ else
+ @problem = (@options[:update_only] ? "should" : "should not") +
+ " be update only"
+ false
+ end
else
- @problem = (@update_only ? "should" : "should not") +
- " be update only"
- false
+ true
end
end
View
71 lib/shoulda/matchers/active_record/association_matcher.rb
@@ -56,31 +56,32 @@ def have_and_belong_to_many(name)
class AssociationMatcher # :nodoc:
def initialize(macro, name)
@macro = macro
- @name = name
+ @name = name
+ @options = {}
end
def through(through)
- @through = through
+ @options[:through] = through
self
end
def dependent(dependent)
- @dependent = dependent
+ @options[:dependent] = dependent
self
end
def order(order)
- @order = order
+ @options[:order] = order
self
end
def conditions(conditions)
- @conditions = conditions
+ @options[:conditions] = conditions
self
end
def class_name(class_name)
- @class_name = class_name
+ @options[:class_name] = class_name
self
end
@@ -107,10 +108,10 @@ def negative_failure_message
def description
description = "#{macro_description} #{@name}"
- description += " through #{@through}" if @through
- description += " dependent => #{@dependent}" if @dependent
- description += " class_name => #{@class_name}" if @class_name
- description += " order => #{@order}" if @order
+ description += " through #{@options[:through]}" if @options.key?(:through)
+ description += " dependent => #{@options[:dependent]}" if @options.key?(:dependent)
+ description += " class_name => #{@options[:class_name]}" if @options.key?(:class_name)
+ description += " order => #{@options[:order]}" if @options.key?(:order)
description
end
@@ -149,61 +150,73 @@ def has_foreign_key_missing?
end
def through_association_valid?
- @through.nil? || (through_association_exists? && through_association_correct?)
+ @options[:through].nil? || (through_association_exists? && through_association_correct?)
end
def through_association_exists?
if through_reflection.nil?
- @missing = "#{model_class.name} does not have any relationship to #{@through}"
+ @missing = "#{model_class.name} does not have any relationship to #{@options[:through]}"
false
else
true
end
end
def through_association_correct?
- if @through == reflection.options[:through]
+ if @options[:through] == reflection.options[:through]
true
else
- @missing = "Expected #{model_class.name} to have #{@name} through #{@through}, " +
+ @missing = "Expected #{model_class.name} to have #{@name} through #{@options[:through]}, " +
"but got it through #{reflection.options[:through]}"
false
end
end
def dependent_correct?
- if @dependent.nil? || @dependent.to_s == reflection.options[:dependent].to_s
+ if @options[:dependent].nil? || @options[:dependent].to_s == reflection.options[:dependent].to_s
true
else
- @missing = "#{@name} should have #{@dependent} dependency"
+ @missing = "#{@name} should have #{@options[:dependent]} dependency"
false
end
end
def class_name_correct?
- if @class_name.nil? || @class_name.to_s == reflection.options[:class_name].to_s
- true
+ if @options.key?(:class_name)
+ if @options[:class_name].to_s == reflection.options[:class_name].to_s
+ true
+ else
+ @missing = "#{@name} should have #{@options[:class_name]} as class_name"
+ false
+ end
else
- @missing = "#{@name} should have #{@class_name} as class_name"
- false
+ true
end
end
def order_correct?
- if @order.nil? || @order.to_s == reflection.options[:order].to_s
- true
+ if @options.key?(:order)
+ if @options[:order].to_s == reflection.options[:order].to_s
+ true
+ else
+ @missing = "#{@name} should be ordered by #{@options[:order]}"
+ false
+ end
else
- @missing = "#{@name} should be ordered by #{@order}"
- false
+ true
end
end
def conditions_correct?
- if @conditions.nil? || @conditions.to_s == reflection.options[:conditions].to_s
- true
+ if @options.key?(:conditions)
+ if @options[:conditions].to_s == reflection.options[:conditions].to_s
+ true
+ else
+ @missing = "#{@name} should have the following conditions: #{@options[:conditions]}"
+ false
+ end
else
- @missing = "#{@name} should have the following conditions: #{@conditions}"
- false
+ true
end
end
@@ -265,7 +278,7 @@ def foreign_key_reflection
end
def through_reflection
- @through_reflection ||= model_class.reflect_on_association(@through)
+ @through_reflection ||= model_class.reflect_on_association(@options[:through])
end
def expectation
View
77 lib/shoulda/matchers/active_record/have_db_column_matcher.rb
@@ -22,20 +22,20 @@ def have_db_column(column)
class HaveDbColumnMatcher # :nodoc:
def initialize(column)
@column = column
+ @options = {}
end
def of_type(column_type)
- @column_type = column_type
+ @options[:column_type] = column_type
self
end
def with_options(opts = {})
- @precision = opts[:precision]
- @limit = opts[:limit]
- @default = opts[:default]
- @null = opts[:null]
- @scale = opts[:scale]
- @primary = opts[:primary]
+ %w(precision limit default null scale primary).each do |attribute|
+ if opts.key?(attribute.to_sym)
+ @options[attribute.to_sym] = opts[attribute.to_sym]
+ end
+ end
self
end
@@ -61,13 +61,13 @@ def negative_failure_message
def description
desc = "have db column named #{@column}"
- desc << " of type #{@column_type}" unless @column_type.nil?
- desc << " of precision #{@precision}" unless @precision.nil?
- desc << " of limit #{@limit}" unless @limit.nil?
- desc << " of default #{@default}" unless @default.nil?
- desc << " of null #{@null}" unless @null.nil?
- desc << " of primary #{@primary}" unless @primary.nil?
- desc << " of scale #{@scale}" unless @scale.nil?
+ desc << " of type #{@options[:column_type]}" if @options.key?(:column_type)
+ desc << " of precision #{@options[:precision]}" if @options.key?(:precision)
+ desc << " of limit #{@options[:limit]}" if @options.key?(:limit)
+ desc << " of default #{@options[:default]}" if @options.key?(:default)
+ desc << " of null #{@options[:null]}" if @options.key?(:null)
+ desc << " of primary #{@options[:primary]}" if @options.key?(:primary)
+ desc << " of scale #{@options[:scale]}" if @options.key?(:scale)
desc
end
@@ -83,82 +83,89 @@ def column_exists?
end
def correct_column_type?
- return true if @column_type.nil?
- if matched_column.type.to_s == @column_type.to_s
+ return true unless @options.key?(:column_type)
+
+ if matched_column.type.to_s == @options[:column_type].to_s
true
else
@missing = "#{model_class} has a db column named #{@column} " <<
- "of type #{matched_column.type}, not #{@column_type}."
+ "of type #{matched_column.type}, not #{@options[:column_type]}."
false
end
end
def correct_precision?
- return true if @precision.nil?
- if matched_column.precision.to_s == @precision.to_s
+ return true unless @options.key?(:precision)
+
+ if matched_column.precision.to_s == @options[:precision].to_s
true
else
@missing = "#{model_class} has a db column named #{@column} " <<
"of precision #{matched_column.precision}, " <<
- "not #{@precision}."
+ "not #{@options[:precision]}."
false
end
end
def correct_limit?
- return true if @limit.nil?
- if matched_column.limit.to_s == @limit.to_s
+ return true unless @options.key?(:limit)
+
+ if matched_column.limit.to_s == @options[:limit].to_s
true
else
@missing = "#{model_class} has a db column named #{@column} " <<
"of limit #{matched_column.limit}, " <<
- "not #{@limit}."
+ "not #{@options[:limit]}."
false
end
end
def correct_default?
- return true if @default.nil?
- if matched_column.default.to_s == @default.to_s
+ return true unless @options.key?(:default)
+
+ if matched_column.default.to_s == @options[:default].to_s
true
else
@missing = "#{model_class} has a db column named #{@column} " <<
"of default #{matched_column.default}, " <<
- "not #{@default}."
+ "not #{@options[:default]}."
false
end
end
def correct_null?
- return true if @null.nil?
- if matched_column.null.to_s == @null.to_s
+ return true unless @options.key?(:null)
+
+ if matched_column.null.to_s == @options[:null].to_s
true
else
@missing = "#{model_class} has a db column named #{@column} " <<
"of null #{matched_column.null}, " <<
- "not #{@null}."
+ "not #{@options[:null]}."
false
end
end
def correct_scale?
- return true if @scale.nil?
- if matched_column.scale.to_s == @scale.to_s
+ return true unless @options.key?(:scale)
+
+ if matched_column.scale.to_s == @options[:scale].to_s
true
else
@missing = "#{model_class} has a db column named #{@column} " <<
- "of scale #{matched_column.scale}, not #{@scale}."
+ "of scale #{matched_column.scale}, not #{@options[:scale]}."
false
end
end
def correct_primary?
- return true if @primary.nil?
- if matched_column.primary == @primary
+ return true unless @options.key?(:primary)
+
+ if matched_column.primary == @options[:primary]
true
else
@missing = "#{model_class} has a db column named #{@column} "
- if @primary
+ if @options[:primary]
@missing << "that is not primary, but should be"
else
@missing << "that is primary, but should not be"
View
28 lib/shoulda/matchers/active_record/have_db_index_matcher.rb
@@ -9,8 +9,7 @@ module ActiveRecord # :nodoc:
# * <tt>unique</tt> - whether or not the index has a unique
# constraint. Use <tt>true</tt> to explicitly test for a unique
# constraint. Use <tt>false</tt> to explicitly test for a non-unique
- # constraint. Use <tt>nil</tt> if you don't care whether the index is
- # unique or not. Default = <tt>nil</tt>
+ # constraint.
#
# Examples:
#
@@ -25,10 +24,11 @@ def have_db_index(columns)
class HaveDbIndexMatcher # :nodoc:
def initialize(columns)
@columns = normalize_columns_to_array(columns)
+ @options = {}
end
def unique(unique)
- @unique = unique
+ @options[:unique] = unique
self
end
@@ -46,7 +46,11 @@ def negative_failure_message
end
def description
- "have a #{index_type} index on columns #{@columns.join(' and ')}"
+ if @options.key?(:unique)
+ "have a #{index_type} index on columns #{@columns.join(' and ')}"
+ else
+ "have an index on columns #{@columns.join(' and ')}"
+ end
end
protected
@@ -56,12 +60,13 @@ def index_exists?
end
def correct_unique?
- return true if @unique.nil?
- if !!matched_index.unique == @unique
+ return true unless @options.key?(:unique)
+
+ if matched_index.unique == @options[:unique]
true
else
@missing = "#{table_name} has an index named #{matched_index.name} " <<
- "of unique #{!!matched_index.unique}, not #{@unique}."
+ "of unique #{matched_index.unique}, not #{@options[:unique]}."
false
end
end
@@ -87,13 +92,10 @@ def expectation
end
def index_type
- case @unique
- when nil
- ''
- when false
- 'non-unique'
- else
+ if @options[:unique]
'unique'
+ else
+ 'non-unique'
end
end

0 comments on commit a718c46

Please sign in to comment.
Something went wrong with that request. Please try again.