Views for unique validations created on loading, not execution
This commit is contained in:
parent
938bf2cf2c
commit
d56179aa6b
|
@ -10,6 +10,7 @@
|
||||||
* Removed railties dependency (DAddYE)
|
* Removed railties dependency (DAddYE)
|
||||||
* DesignDoc cache refreshed if a database is deleted.
|
* DesignDoc cache refreshed if a database is deleted.
|
||||||
* Fixing dirty tracking on collection_of association.
|
* Fixing dirty tracking on collection_of association.
|
||||||
|
* Uniqueness Validation views created on initialization, not on demand!
|
||||||
|
|
||||||
|
|
||||||
## 1.1.0.beta5 - 2011-04-30
|
## 1.1.0.beta5 - 2011-04-30
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
module CouchRest
|
module CouchRest
|
||||||
module Model
|
module Model
|
||||||
module Validations
|
module Validations
|
||||||
|
|
||||||
# Validates if a field is unique
|
# Validates if a field is unique
|
||||||
class UniquenessValidator < ActiveModel::EachValidator
|
class UniquenessValidator < ActiveModel::EachValidator
|
||||||
|
|
||||||
|
@ -11,29 +11,33 @@ module CouchRest
|
||||||
# or add one if necessary.
|
# or add one if necessary.
|
||||||
def setup(model)
|
def setup(model)
|
||||||
@model = model
|
@model = model
|
||||||
|
if options[:view].blank?
|
||||||
|
attributes.each do |attribute|
|
||||||
|
opts = merge_view_options(attribute)
|
||||||
|
|
||||||
|
if model.respond_to?(:has_view?) && !model.has_view?(opts[:view_name])
|
||||||
|
opts[:keys] << {:allow_nil => true}
|
||||||
|
model.view_by(*opts[:keys])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def validate_each(document, attribute, value)
|
def validate_each(document, attribute, value)
|
||||||
keys = [attribute]
|
opts = merge_view_options(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]
|
values = opts[:keys].map{|k| document.send(k)}
|
||||||
|
values = values.first if values.length == 1
|
||||||
|
|
||||||
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
|
||||||
base = options[:proxy].nil? ? model : document.instance_eval(options[:proxy])
|
base = opts[:proxy].nil? ? model : document.instance_eval(opts[:proxy])
|
||||||
|
|
||||||
if base.respond_to?(:has_view?) && !base.has_view?(view_name)
|
if base.respond_to?(:has_view?) && !base.has_view?(opts[:view_name])
|
||||||
raise "View #{document.class.name}.#{options[:view]} does not exist!" unless options[:view].nil?
|
raise "View #{document.class.name}.#{opts[:view_name]} does not exist for validation!"
|
||||||
keys << {:allow_nil => true}
|
|
||||||
model.view_by(*keys)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
rows = base.view(view_name, :key => values, :limit => 2, :include_docs => false)['rows']
|
rows = base.view(opts[:view_name], :key => values, :limit => 2, :include_docs => false)['rows']
|
||||||
return if rows.empty?
|
return if rows.empty?
|
||||||
|
|
||||||
unless document.new?
|
unless document.new?
|
||||||
|
@ -47,6 +51,17 @@ module CouchRest
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def merge_view_options(attr)
|
||||||
|
keys = [attr]
|
||||||
|
keys.unshift(*options[:scope]) unless options[:scope].nil?
|
||||||
|
|
||||||
|
view_name = options[:view].nil? ? "by_#{keys.join('_and_')}" : options[:view]
|
||||||
|
|
||||||
|
options.merge({:keys => keys, :view_name => view_name})
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -16,7 +16,11 @@ describe "Validations" do
|
||||||
before(:all) do
|
before(:all) do
|
||||||
@objs = ['title 1', 'title 2', 'title 3'].map{|t| WithUniqueValidation.create(:title => t)}
|
@objs = ['title 1', 'title 2', 'title 3'].map{|t| WithUniqueValidation.create(:title => t)}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should create a new view if none defined before performing" do
|
||||||
|
WithUniqueValidation.has_view?(:by_title).should be_true
|
||||||
|
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
|
||||||
|
@ -35,6 +39,7 @@ describe "Validations" do
|
||||||
@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')
|
||||||
|
@ -50,6 +55,13 @@ describe "Validations" do
|
||||||
}.should raise_error
|
}.should raise_error
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "should not try to create a defined view" do
|
||||||
|
WithUniqueValidationView.validates_uniqueness_of :title, :view => 'fooobar'
|
||||||
|
WithUniqueValidationView.has_view?('fooobar').should be_false
|
||||||
|
WithUniqueValidationView.has_view?('by_title').should be_false
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
it "should not try to create new view when already defined" do
|
it "should not try to create new view when already defined" do
|
||||||
@obj = @objs[1]
|
@obj = @objs[1]
|
||||||
@obj.class.should_not_receive('view_by')
|
@obj.class.should_not_receive('view_by')
|
||||||
|
@ -60,6 +72,11 @@ describe "Validations" do
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with a proxy parameter" do
|
context "with a proxy parameter" do
|
||||||
|
|
||||||
|
it "should create a new view despite proxy" do
|
||||||
|
WithUniqueValidationProxy.has_view?(:by_title).should be_true
|
||||||
|
end
|
||||||
|
|
||||||
it "should be used" do
|
it "should be used" do
|
||||||
@obj = WithUniqueValidationProxy.new(:title => 'test 6')
|
@obj = WithUniqueValidationProxy.new(:title => 'test 6')
|
||||||
proxy = @obj.should_receive('proxy').and_return(@obj.class)
|
proxy = @obj.should_receive('proxy').and_return(@obj.class)
|
||||||
|
|
Loading…
Reference in a new issue