lib/valhammer/validations.rb in valhammer-0.1.0 vs lib/valhammer/validations.rb in valhammer-0.1.1

- old
+ new

@@ -7,11 +7,11 @@ VALHAMMER_EXCLUDED_FIELDS = %w(created_at updated_at) private_constant :VALHAMMER_DEFAULT_OPTS, :VALHAMMER_EXCLUDED_FIELDS def valhammer(opts = {}) - @valhammer_indexes ||= connection.indexes(table_name) + @valhammer_indexes = connection.indexes(table_name) opts = VALHAMMER_DEFAULT_OPTS.merge(opts) columns_hash.each do |name, column| valhammer_validate(name, column, opts) end end @@ -54,11 +54,11 @@ end def valhammer_inclusion(validations, column, opts) return unless opts[:inclusion] && column.type == :boolean - validations[:inclusion] = { in: [false, true], allow_nil: column.null } + validations[:inclusion] = { in: [false, true], allow_nil: true } end def valhammer_unique(validations, column, opts) return unless opts[:uniqueness] @@ -67,22 +67,24 @@ end return unless unique_keys.one? scope = unique_keys.first.columns[0..-2] - validations[:uniqueness] = scope.empty? ? true : { scope: scope } + + opts = validations[:uniqueness] = { allow_nil: true } + opts[:scope] = scope if scope.any? end def valhammer_numeric(validations, column, opts) return unless opts[:numericality] case column.type when :integer validations[:numericality] = { only_integer: true, - allow_nil: column.null } + allow_nil: true } when :decimal validations[:numericality] = { only_integer: false, - allow_nil: column.null } + allow_nil: true } end end def valhammer_length(validations, column, opts) return unless opts[:length] && column.type == :string && column.limit