lib/valhammer/validations.rb in valhammer-0.1.3 vs lib/valhammer/validations.rb in valhammer-0.2.0

- old
+ new

@@ -60,20 +60,25 @@ end def valhammer_unique(validations, column, opts) return unless opts[:uniqueness] - unique_keys = @valhammer_indexes.select do |i| - i.unique && i.columns.last == column.name - end - + unique_keys = valhammer_unique_keys(column) return unless unique_keys.one? scope = unique_keys.first.columns[0..-2] - opts = validations[:uniqueness] = { allow_nil: true } + validations[:uniqueness] = valhammer_unique_opts(scope) + end + + def valhammer_unique_opts(scope) + nullable = scope.select { |c| columns_hash[c].null } + + opts = { allow_nil: true } opts[:scope] = scope if scope.any? + opts[:if] = -> { nullable.all? { |c| send(c) } } if nullable.any? + opts end def valhammer_numeric(validations, column, opts) return unless opts[:numericality] @@ -104,9 +109,15 @@ "null=#{column.null || 'false'} limit=#{column.limit || 'nil'})" end def valhammer_exclude?(field) field == primary_key || VALHAMMER_EXCLUDED_FIELDS.include?(field) + end + + def valhammer_unique_keys(column) + @valhammer_indexes.select do |i| + i.unique && !i.where && i.columns.last == column.name + end end def valhammer_assoc_name(field) reflect_on_all_associations(:belongs_to) .find { |a| a.foreign_key == field }.try(:name)