Adding support for scopes on unique validation

This commit is contained in:
Sam Lown 2011-04-08 14:36:29 +02:00
parent 760d855845
commit 1d37f12982
8 changed files with 123 additions and 48 deletions

View file

@ -1,9 +1,9 @@
PATH PATH
remote: . remote: .
specs: specs:
couchrest_model (1.1.0.beta) couchrest_model (1.1.0.beta2)
activemodel (~> 3.0.0) activemodel (~> 3.0.0)
couchrest (~> 1.0.2) couchrest (~> 1.1.0.pre)
mime-types (~> 1.15) mime-types (~> 1.15)
railties (~> 3.0.0) railties (~> 3.0.0)
tzinfo (~> 0.3.22) tzinfo (~> 0.3.22)
@ -28,7 +28,7 @@ GEM
i18n (~> 0.4) i18n (~> 0.4)
activesupport (3.0.5) activesupport (3.0.5)
builder (2.1.2) builder (2.1.2)
couchrest (1.0.2) couchrest (1.1.0.pre)
json (~> 1.5.1) json (~> 1.5.1)
mime-types (~> 1.15) mime-types (~> 1.15)
rest-client (~> 1.6.1) rest-client (~> 1.6.1)
@ -66,6 +66,11 @@ PLATFORMS
ruby ruby
DEPENDENCIES DEPENDENCIES
activemodel (~> 3.0.0)
couchrest (~> 1.1.0.pre)
couchrest_model! couchrest_model!
mime-types (~> 1.15)
rack-test (>= 0.5.7) rack-test (>= 0.5.7)
railties (~> 3.0.0)
rspec (>= 2.0.0) rspec (>= 2.0.0)
tzinfo (~> 0.3.22)

View file

@ -23,7 +23,7 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"] s.require_paths = ["lib"]
s.add_dependency(%q<couchrest>, "~> 1.0.2") s.add_dependency(%q<couchrest>, "~> 1.1.0.pre")
s.add_dependency(%q<mime-types>, "~> 1.15") s.add_dependency(%q<mime-types>, "~> 1.15")
s.add_dependency(%q<activemodel>, "~> 3.0.0") s.add_dependency(%q<activemodel>, "~> 3.0.0")
s.add_dependency(%q<tzinfo>, "~> 0.3.22") s.add_dependency(%q<tzinfo>, "~> 0.3.22")

View file

@ -3,6 +3,8 @@
* Minor enhancements: * Minor enhancements:
* Time handling improved in accordance with CouchRest 1.0.3. Always set to UTC. * Time handling improved in accordance with CouchRest 1.0.3. Always set to UTC.
* Refinements to associations and uniqueness validation for proxy (based on issue found by Gleb Kanterov) * Refinements to associations and uniqueness validation for proxy (based on issue found by Gleb Kanterov)
* Added :allow_nil and :allow_blank options when creating a new view
* Unique Validation now supports scopes!
== 1.1.0.beta == 1.1.0.beta

View file

@ -412,6 +412,16 @@ module CouchRest
# The view name is the same, but three keys would be used in the # The view name is the same, but three keys would be used in the
# subsecuent index. # subsecuent index.
# #
# By default, a check is made on each of the view's keys to ensure they
# do not contain a nil value ('null' in javascript). This is probably what
# you want in most cases but sometimes in can be useful to create an
# index where nil is permited. Set the <tt>:allow_nil</tt> option to true to
# remove this check.
#
# Conversely, keys are not checked to see if they are empty or blank. If you'd
# like to enable this, set the <tt>:allow_blank</tt> option to false. The default
# is true, empty strings are permited in the indexes.
#
def create(model, name, opts = {}) def create(model, name, opts = {})
unless opts[:map] unless opts[:map]
@ -421,12 +431,14 @@ module CouchRest
raise "View cannot be created without recognised name, :map or :by options" if opts[:by].nil? raise "View cannot be created without recognised name, :map or :by options" if opts[:by].nil?
opts[:allow_blank] = opts[:allow_blank].nil? ? true : opts[:allow_blank]
opts[:guards] ||= [] opts[:guards] ||= []
opts[:guards].push "(doc['#{model.model_type_key}'] == '#{model.to_s}')" opts[:guards].push "(doc['#{model.model_type_key}'] == '#{model.to_s}')"
keys = opts[:by].map{|o| "doc['#{o}']"} keys = opts[:by].map{|o| "doc['#{o}']"}
emit = keys.length == 1 ? keys.first : "[#{keys.join(', ')}]" emit = keys.length == 1 ? keys.first : "[#{keys.join(', ')}]"
opts[:guards] += keys.map{|k| "(#{k} != null)"} opts[:guards] += keys.map{|k| "(#{k} != null)"} unless opts[:allow_nil]
opts[:guards] += keys.map{|k| "(#{k} != '')"} unless opts[:allow_blank]
opts[:map] = <<-EOF opts[:map] = <<-EOF
function(doc) { function(doc) {
if (#{opts[:guards].join(' && ')}) { if (#{opts[:guards].join(' && ')}) {

View file

@ -14,7 +14,14 @@ module CouchRest
end end
def validate_each(document, attribute, value) def validate_each(document, attribute, value)
view_name = options[:view].nil? ? "by_#{attribute}" : options[:view] keys = [attribute]
unless options[:scope].nil?
keys = (options[:scope].is_a?(Array) ? options[:scope] : [options[:scope]]) + keys
end
values = keys.map{|k| document.send(k)}
values = values.first if values.length == 1
view_name = options[:view].nil? ? "by_#{keys.join('_and_')}" : options[:view]
model = (document.respond_to?(:model_proxy) && document.model_proxy ? document.model_proxy : @model) model = (document.respond_to?(:model_proxy) && document.model_proxy ? document.model_proxy : @model)
# Determine the base of the search # Determine the base of the search
@ -22,18 +29,20 @@ module CouchRest
if base.respond_to?(:has_view?) && !base.has_view?(view_name) if base.respond_to?(:has_view?) && !base.has_view?(view_name)
raise "View #{document.class.name}.#{options[:view]} does not exist!" unless options[:view].nil? raise "View #{document.class.name}.#{options[:view]} does not exist!" unless options[:view].nil?
model.view_by attribute model.view_by *keys, :allow_nil => true
end end
docs = base.view(view_name, :key => value, :limit => 2, :include_docs => false)['rows'] rows = base.view(view_name, :key => values, :limit => 2, :include_docs => false)['rows']
return if docs.empty? return if rows.empty?
unless document.new? unless document.new?
return if docs.find{|doc| doc['id'] == document.id} return if rows.find{|row| row['id'] == document.id}
end end
if docs.length > 0 if rows.length > 0
document.errors.add(attribute, :taken, options.merge(:value => value)) opts = options.merge(:value => value)
opts.delete(:scope) # Has meaning with I18n!
document.errors.add(attribute, :taken, opts)
end end
end end

View file

@ -12,45 +12,45 @@ describe "Validations" do
describe "Uniqueness" do describe "Uniqueness" do
before(:all) do context "basic" do
@objs = ['title 1', 'title 2', 'title 3'].map{|t| WithUniqueValidation.create(:title => t)} before(:all) do
end @objs = ['title 1', 'title 2', 'title 3'].map{|t| WithUniqueValidation.create(:title => t)}
end
it "should validate a new unique document" do it "should validate a new unique document" do
@obj = WithUniqueValidation.create(:title => 'title 4') @obj = WithUniqueValidation.create(:title => 'title 4')
@obj.new?.should_not be_true @obj.new?.should_not be_true
@obj.should be_valid @obj.should be_valid
end end
it "should not validate a non-unique document" do it "should not validate a non-unique document" do
@obj = WithUniqueValidation.create(:title => 'title 1') @obj = WithUniqueValidation.create(:title => 'title 1')
@obj.should_not be_valid @obj.should_not be_valid
@obj.errors[:title].should == ["has already been taken"] @obj.errors[:title].should == ["has already been taken"]
end end
it "should save already created document" do it "should save already created document" do
@obj = @objs.first @obj = @objs.first
@obj.save.should_not be_false @obj.save.should_not be_false
@obj.should be_valid @obj.should be_valid
end end
it "should allow own view to be specified" do it "should allow own view to be specified" do
# validates_uniqueness_of :code, :view => 'all' # validates_uniqueness_of :code, :view => 'all'
WithUniqueValidationView.create(:title => 'title 1', :code => '1234') WithUniqueValidationView.create(:title => 'title 1', :code => '1234')
@obj = WithUniqueValidationView.new(:title => 'title 5', :code => '1234') @obj = WithUniqueValidationView.new(:title => 'title 5', :code => '1234')
@obj.should_not be_valid @obj.should_not be_valid
end end
it "should raise an error if specified view does not exist" do it "should raise an error if specified view does not exist" do
WithUniqueValidationView.validates_uniqueness_of :title, :view => 'fooobar' WithUniqueValidationView.validates_uniqueness_of :title, :view => 'fooobar'
@obj = WithUniqueValidationView.new(:title => 'title 2', :code => '12345') @obj = WithUniqueValidationView.new(:title => 'title 2', :code => '12345')
lambda { lambda {
@obj.valid? @obj.valid?
}.should raise_error }.should raise_error
end end
context "with a pre-defined view" do it "should not try to create new view when already defined" do
it "should not try to create new view" do
@obj = @objs[1] @obj = @objs[1]
@obj.class.should_not_receive('view_by') @obj.class.should_not_receive('view_by')
@obj.class.should_receive('has_view?').and_return(true) @obj.class.should_receive('has_view?').and_return(true)
@ -85,6 +85,44 @@ describe "Validations" do
@obj.stub!(:model_proxy).twice.and_return(mp) @obj.stub!(:model_proxy).twice.and_return(mp)
@obj.valid? @obj.valid?
end end
end
context "with a scope" do
before(:all) do
@objs = [['title 1', 1], ['title 2', 1], ['title 3', 1]].map{|t| WithScopedUniqueValidation.create(:title => t[0], :parent_id => t[1])}
@objs_nil = [['title 1', nil], ['title 2', nil], ['title 3', nil]].map{|t| WithScopedUniqueValidation.create(:title => t[0], :parent_id => t[1])}
end
it "should create the view" do
@objs.first.class.has_view?('by_parent_id_and_title')
end
it "should validate unique document" do
@obj = WithScopedUniqueValidation.create(:title => 'title 4', :parent_id => 1)
@obj.should be_valid
end
it "should validate unique document outside of scope" do
@obj = WithScopedUniqueValidation.create(:title => 'title 1', :parent_id => 2)
@obj.should be_valid
end
it "should validate non-unique document" do
@obj = WithScopedUniqueValidation.create(:title => 'title 1', :parent_id => 1)
@obj.should_not be_valid
@obj.errors[:title].should == ["has already been taken"]
end
it "should validate unique document will nil scope" do
@obj = WithScopedUniqueValidation.create(:title => 'title 4', :parent_id => nil)
@obj.should be_valid
end
it "should validate non-unique document with nil scope" do
@obj = WithScopedUniqueValidation.create(:title => 'title 1', :parent_id => nil)
@obj.should_not be_valid
@obj.errors[:title].should == ["has already been taken"]
end
end end

View file

@ -138,4 +138,13 @@ class WithUniqueValidationView < CouchRest::Model::Base
validates_uniqueness_of :code, :view => 'all' validates_uniqueness_of :code, :view => 'all'
end end
class WithScopedUniqueValidation < CouchRest::Model::Base
use_database DB
property :parent_id
property :title
validates_uniqueness_of :title, :scope => :parent_id
end

View file

@ -32,7 +32,7 @@ RSpec.configure do |config|
cr = TEST_SERVER cr = TEST_SERVER
test_dbs = cr.databases.select { |db| db =~ /^#{TESTDB}/ } test_dbs = cr.databases.select { |db| db =~ /^#{TESTDB}/ }
test_dbs.each do |db| test_dbs.each do |db|
cr.database(db).delete! rescue nil #cr.database(db).delete! rescue nil
end end
end end
end end