spec/mongoid/urls_spec.rb in mongoid-urls-0.0.7 vs spec/mongoid/urls_spec.rb in mongoid-urls-0.0.9
- old
+ new
@@ -6,16 +6,17 @@
# A sample model
class Document
include Mongoid::Document
include Mongoid::Urls
field :title
+ field :doc
end
Class.new(Document)
end
let(:document) do
- document_class.create(title: "I'm a Document!")
+ document_class.create(title: "I'm a Document!", doc: '123')
end
let(:article) do
Article.new(title: "I'm an Article!")
end
@@ -39,10 +40,22 @@
expect(article.urls).to eq ['im-an-article', 'hello-ruby']
end
end
describe '#url' do
+ it 'should accept custom field names' do
+ document_class.send(:url, :doc)
+ expect(document).to have_field(:url)
+ expect(document).to have_field(:urls)
+ end
+
+ it 'should accept simple field names' do
+ document_class.send(:url, :title, simple: true)
+ expect(document).to_not have_field(:urls)
+ expect(document).to have_field(:url)
+ end
+
describe 'default ":title"' do
before(:each) { document_class.send(:url, :title) }
it 'should be created' do
expect(document).to have_field(:urls)
@@ -63,20 +76,23 @@
end
end
describe 'options' do
it 'should accept custom field names' do
- document_class.send(:url, :sweet)
- expect(document).to have_field(:urls)
+ document_class.send(:url, :title)
+ expect(document).to have_field(:url)
end
it 'should accept simple field names' do
- document_class.send(:url, :sweet, simple: true)
+ document_class.send(:url, :title, simple: true)
expect(document).to_not have_field(:urls)
expect(document).to have_field(:url)
end
+ end
+ describe 'index field' do
+
it 'should create simple field to_param' do
document_class.send(:field, :name)
document_class.send(:url, :name, simple: true)
doc = document_class.create(name: 'nice doc')
expect(doc.to_param).to eq('nice-doc')
@@ -162,22 +178,20 @@
expect(document.to_param).to eq 'im-a-document'
end
end
describe 'reserved words' do
- before(:each) do
- end
it 'should respect default new' do
article.title = 'new'
expect(article.save).to be_falsey
- expect(article.errors).to include(:title)
+ expect(article.errors).to include(:url)
end
it 'should respect default edit' do
article.title = 'edit'
expect(article.save).to be_falsey
- expect(article.errors).to include(:title)
+ expect(article.errors).to include(:url)
end
it 'should match' do
article.title = 'anew'
expect(article.save).to be_truthy
@@ -199,19 +213,45 @@
it 'should raise when collisions can\'t be resolved on save' do
article.save
d2 = article.clone
expect(d2).to_not be_valid
expect(d2.save).to be_falsey
- expect(d2.errors.messages).to include(:title)
+ expect(d2.errors.messages).to include(:url)
end
it 'should raise when collisions can\'t be resolved on create!' do
article.title = '1234'
article.save
+ expect(article.reload.url).to eq '1234'
dup = Article.create(title: '1234')
+ expect(dup.url).to be nil
expect(dup.errors.messages).to_not be_empty
end
+
+ it 'should be possible to edit the url directly' do
+ article.title = '1234'
+ article.save
+ expect(article.reload.url).to eq '1234'
+ dup = Article.create(title: '1234')
+ expect(dup.url).to be nil
+ dup.url = 'onetwo'
+ dup.save
+ expect(dup.errors.messages).to be_empty
+ expect(dup.reload.url).to eq('onetwo')
+ end
+
+ it 'should be safe to edit the url directly' do
+ article.title = '1234'
+ article.save
+ expect(article.reload.url).to eq '1234'
+ dup = Article.create(title: '1234')
+ expect(dup.url).to be nil
+ dup.url = 'One Two'
+ dup.save
+ expect(dup.errors.messages).to be_empty
+ expect(dup.reload.url).to eq('one-two')
+ end
end
context 'with other url present' do
before(:each) do
document_class.send(:field, :name)
@@ -223,8 +263,58 @@
it 'should raise an operation failure' do
expect { document_class.send(:url, :name) }
.to raise_exception(RuntimeError)
end
end
+ end
+ end
+
+ describe 'Dynamic' do
+ it 'should create' do
+ com = Company.create(name: 'ACME Corp LLC', nick: 'ACME')
+ expect(com.url).to eq 'acme'
+ end
+
+ it 'should create w/o one' do
+ com = Company.create(name: 'ACME Corp LLC')
+ expect(com.url).to eq 'acme-corp-llc'
+ end
+
+ it 'should assign attr' do
+ com = Company.new
+ com.assign_attributes(name: 'ACME Corp LLC', nick: 'ACME')
+ com.save
+ expect(com.url).to eq 'acme'
+ expect(Company.count).to eq 1
+ end
+
+ it 'should assign second attr when first is taken' do
+ com1 = Company.create!(name: 'ACME One', nick: 'ACME')
+ com2 = Company.create!(name: 'ACME Two', nick: 'ACME')
+ expect(com1.url).to eq 'acme'
+ expect(com2.url).to eq 'acme-two'
+ end
+
+ it 'should assign third attr when second is taken' do
+ com1 = Company.create!(name: 'Common Name', nick: 'ACME')
+ com2 = Company.create!(name: 'Common Name', nick: 'ACME')
+ com3 = Company.create!(name: 'Common Name', nick: 'ACME')
+ expect(com1.url).to eq 'acme'
+ expect(com2.url).to eq 'common-name'
+ expect(com3.url).to eq 'acme-common-name'
+ end
+
+ it 'should assign attr' do
+ com = Company.new
+ com.assign_attributes(name: 'ACME Corp LLC', nick: nil)
+ com.save
+ expect(com.url).to eq 'acme-corp-llc'
+ end
+
+ it 'should build up' do
+ com = Company.new
+ com.nick = 'ACME'
+ com.valid?
+ expect(com.url).to eq 'acme'
end
end
end