spec/mongoid/urls_spec.rb in mongoid-urls-0.0.3 vs spec/mongoid/urls_spec.rb in mongoid-urls-0.0.7
- old
+ new
@@ -18,31 +18,32 @@
let(:article) do
Article.new(title: "I'm an Article!")
end
- describe '#slug' do
+ describe '#url' do
before(:each) { document_class.send(:url, :title) }
it 'should slugize a few stuff' do
- expect(document.slug).to eq('im-a-document')
+ expect(document.url).to eq('im-a-document')
end
- it 'should update slug' do
+ it 'should update url' do
document.title = 'I "quoted"'
- expect(document.slug).to eq('i-quoted')
+ document.valid?
+ expect(document.url).to eq('i-quoted')
end
- it 'should keep old slugs' do
+ it 'should keep old urls' do
article.save
- article.title = "Hello Ruby!"
+ article.title = 'Hello Ruby!'
article.save
- expect(article.urls).to eq ["im-an-article", "hello-ruby"]
+ expect(article.urls).to eq ['im-an-article', 'hello-ruby']
end
end
describe '#url' do
- describe 'default "_id"' do
+ describe 'default ":title"' do
before(:each) { document_class.send(:url, :title) }
it 'should be created' do
expect(document).to have_field(:urls)
end
@@ -66,10 +67,23 @@
it 'should accept custom field names' do
document_class.send(:url, :sweet)
expect(document).to have_field(:urls)
end
+ it 'should accept simple field names' do
+ document_class.send(:url, :sweet, simple: true)
+ expect(document).to_not have_field(:urls)
+ expect(document).to have_field(:url)
+ end
+
+ 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')
+ end
+
it 'should not create custom finders with default id' do
# A sample model
class UntaintedDocument
include Mongoid::Document
include Mongoid::Urls
@@ -147,10 +161,36 @@
document_class.send(:url, :title)
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)
+ end
+
+ it 'should respect default edit' do
+ article.title = 'edit'
+ expect(article.save).to be_falsey
+ expect(article.errors).to include(:title)
+ end
+
+ it 'should match' do
+ article.title = 'anew'
+ expect(article.save).to be_truthy
+ end
+
+ it 'should match' do
+ article.title = 'newa'
+ expect(article.save).to be_truthy
+ end
+ end
+
describe '(no) collision resolution' do
before(:each) do
document_class.send(:url, :title)
document_class.create_indexes
end
@@ -170,23 +210,20 @@
dup = Article.create(title: '1234')
expect(dup.errors.messages).to_not be_empty
end
end
- context 'with other unique indexes present' do
+ context 'with other url present' do
before(:each) do
document_class.send(:field, :name)
- document_class.send(:url, :name)
document_class.send(:index, { name: 1 }, unique: true)
document_class.create_indexes
end
context 'when violating the other index' do
it 'should raise an operation failure' do
- duplicate_name = 'Got Duped.'
- document_class.create!(name: duplicate_name)
- expect { document_class.create!(name: duplicate_name) }
- .to raise_exception(Mongo::Error::OperationFailure)
+ expect { document_class.send(:url, :name) }
+ .to raise_exception(RuntimeError)
end
end
end
end
end