mongoid-urls 0.0.3 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.travis.yml +5 -2
- data/Gemfile +1 -0
- data/README.md +20 -10
- data/lib/mongoid/urls/version.rb +1 -1
- data/lib/mongoid/urls.rb +14 -13
- data/mongoid-urls.gemspec +3 -3
- data/spec/mongoid/urls_spec.rb +51 -14
- data/spec/spec_helper.rb +19 -8
- data/spec/support/models.rb +1 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b5712fe3113cb418853a86467166a467ef85e918
|
4
|
+
data.tar.gz: 87fd69af5d300bcaa822616e88f64f5f3f4d97af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9a7a34585091f75f746e903fde578f8a23ac97eed52678239b617613c0de1dc5e1042b467a2f62e64cf84b73d8cc7ff78e33d8a544bcbee431026aaad919f075
|
7
|
+
data.tar.gz: 5a26ba691150d4587738a363f6cb7bfc67f2053218776f43181dd10f15ad6925ecad8c13bf89219fb48d1ced3265c0cce8d7b6af9fc4c9611774a9d2a43169b9
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
@@ -1,15 +1,18 @@
|
|
1
1
|
language: ruby
|
2
2
|
|
3
3
|
rvm:
|
4
|
-
- 1.9.3
|
5
4
|
- 2.1.1
|
6
5
|
- 2.2.2
|
7
6
|
- ruby-head
|
8
|
-
- jruby-
|
7
|
+
- jruby-head
|
9
8
|
- rbx-2
|
10
9
|
|
10
|
+
env: CI="travis" JRUBY_OPTS="--2.0 --server -Xcompile.invokedynamic=false -J-XX:+TieredCompilation -J-XX:TieredStopAtLevel=1 -J-noverify -J-Xms512m -J-Xmx1024m"
|
11
|
+
|
11
12
|
matrix:
|
12
13
|
allow_failures:
|
14
|
+
- rvm: ruby-head
|
15
|
+
- rvm: jruby-head
|
13
16
|
- rvm: rbx-2
|
14
17
|
|
15
18
|
gemfile:
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -12,7 +12,7 @@ Mongoid::Urls
|
|
12
12
|
Simple slugs for mongoid models!
|
13
13
|
|
14
14
|
|
15
|
-
##
|
15
|
+
## Nice URLs for Mongoid Documents
|
16
16
|
|
17
17
|
This library is a quick and simple way to generate slugs
|
18
18
|
for your mongoid documents.
|
@@ -21,7 +21,7 @@ Mongoid::Urls can help turn this:
|
|
21
21
|
|
22
22
|
http://bestappever.com/video/4dcfbb3c6a4f1d4c4a000012
|
23
23
|
|
24
|
-
Into something
|
24
|
+
Into something like this:
|
25
25
|
|
26
26
|
http://bestappever.com/video/kittens-playing-with-puppies
|
27
27
|
|
@@ -32,11 +32,11 @@ In your gemfile, add:
|
|
32
32
|
|
33
33
|
gem 'mongoid-urls'
|
34
34
|
|
35
|
-
In your Mongoid documents, just add `include Mongoid::Urls`
|
36
|
-
`url` method
|
35
|
+
In your Mongoid documents, just add `include Mongoid::Urls`
|
36
|
+
and use the `url` method to setup, like so:
|
37
37
|
|
38
38
|
```ruby
|
39
|
-
class
|
39
|
+
class Video
|
40
40
|
include Mongoid::Document
|
41
41
|
include Mongoid::Urls
|
42
42
|
|
@@ -53,25 +53,35 @@ listed [below](#configuration).
|
|
53
53
|
|
54
54
|
## Finders
|
55
55
|
|
56
|
-
`Mongoid::Urls` will **never** override `find
|
57
|
-
There's some helpers for custom fields:
|
56
|
+
`Mongoid::Urls` will **never** override `find`!
|
58
57
|
|
59
58
|
```ruby
|
60
59
|
Video.find_by_url("the-nice-url")
|
61
|
-
Account.find_by_url("acc-123456")
|
62
60
|
```
|
63
61
|
|
62
|
+
Or just:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
Account.find_url("acc-123456")
|
66
|
+
```
|
64
67
|
|
65
68
|
## Configuration
|
66
69
|
|
67
70
|
You may choose between two different systems for how your urls are stored:
|
68
71
|
|
69
|
-
|
72
|
+
Simple: `#url` String only.
|
70
73
|
|
71
|
-
|
74
|
+
Default to simple + `#urls` Array for history (find when page moved).
|
72
75
|
|
73
76
|
|
74
77
|
#### Reserved
|
75
78
|
|
79
|
+
Defaults to `%w(new edit)`.
|
80
|
+
Have in mind: It's an overwrite, not a merge.
|
81
|
+
|
76
82
|
|
77
83
|
# Notes
|
84
|
+
|
85
|
+
If you're looking for conflict resolution, check out `mongoid-slugs`.
|
86
|
+
This gem intended to be used for manual conflict resolution (duplicates):
|
87
|
+
Use your own logic and/or return conflict error to the user.
|
data/lib/mongoid/urls/version.rb
CHANGED
data/lib/mongoid/urls.rb
CHANGED
@@ -24,6 +24,7 @@ module Mongoid
|
|
24
24
|
#
|
25
25
|
def url(*args)
|
26
26
|
options = args.extract_options!
|
27
|
+
fail 'One #url per model!' if url_key
|
27
28
|
self.url_key = args.first.to_s
|
28
29
|
self.url_simple = options[:simple]
|
29
30
|
self.reserved_words = options[:reserve] || Set.new(%w(new edit))
|
@@ -31,30 +32,29 @@ module Mongoid
|
|
31
32
|
before_validation :create_urls
|
32
33
|
end
|
33
34
|
|
34
|
-
def
|
35
|
-
|
35
|
+
def find_url(u)
|
36
|
+
find_by(url: u) || (!url_simple && find_by(urls: u))
|
36
37
|
rescue Mongoid::Errors::DocumentNotFound
|
37
38
|
nil
|
38
39
|
end
|
40
|
+
alias_method :find_by_url, :find_url
|
39
41
|
|
40
42
|
private
|
41
43
|
|
42
44
|
def create_url_fields
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
index(urls: 1)
|
49
|
-
end
|
45
|
+
field :url, type: String
|
46
|
+
index({ url: 1 }, unique: true)
|
47
|
+
return if url_simple
|
48
|
+
field :urls, type: Array, default: []
|
49
|
+
index(urls: 1)
|
50
50
|
end
|
51
51
|
end # ClassMethods
|
52
52
|
|
53
53
|
def to_param
|
54
|
-
|
54
|
+
url
|
55
55
|
end
|
56
56
|
|
57
|
-
def
|
57
|
+
def new_url
|
58
58
|
self[url_key].to_slug.normalize.to_s
|
59
59
|
end
|
60
60
|
|
@@ -72,9 +72,10 @@ module Mongoid
|
|
72
72
|
|
73
73
|
def create_urls
|
74
74
|
return unless changes.include?(url_key)
|
75
|
-
validate_urls(new_url
|
75
|
+
validate_urls(new_url)
|
76
76
|
|
77
|
-
|
77
|
+
self.url = new_url
|
78
|
+
return if url_simple
|
78
79
|
urls << new_url
|
79
80
|
urls.uniq!
|
80
81
|
end
|
data/mongoid-urls.gemspec
CHANGED
@@ -8,9 +8,9 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ['Marcos Piccinini']
|
10
10
|
s.homepage = 'http://github.com/nofxx/mongoid-urls'
|
11
|
-
s.licenses = ['MIT']
|
12
11
|
s.summary = 'A url sanitizer (slugs) for Mongoid documents.'
|
13
12
|
s.description = 'Mongoid Urls creates unique sanitized URLs for Mongoid documents. Simple and great for making URLs look good.'
|
13
|
+
s.license = 'MIT'
|
14
14
|
|
15
15
|
s.rubyforge_project = 'mongoid-urls'
|
16
16
|
s.add_dependency 'mongoid', '> 4.0.0'
|
@@ -18,6 +18,6 @@ Gem::Specification.new do |s|
|
|
18
18
|
|
19
19
|
s.files = `git ls-files`.split("\n")
|
20
20
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
-
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
-
s.require_paths = [
|
21
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
|
22
|
+
s.require_paths = ['lib']
|
23
23
|
end
|
data/spec/mongoid/urls_spec.rb
CHANGED
@@ -20,27 +20,28 @@ describe Mongoid::Urls do
|
|
20
20
|
Article.new(title: "I'm an Article!")
|
21
21
|
end
|
22
22
|
|
23
|
-
describe '#
|
23
|
+
describe '#url' do
|
24
24
|
before(:each) { document_class.send(:url, :title) }
|
25
25
|
it 'should slugize a few stuff' do
|
26
|
-
expect(document.
|
26
|
+
expect(document.url).to eq('im-a-document')
|
27
27
|
end
|
28
28
|
|
29
|
-
it 'should update
|
29
|
+
it 'should update url' do
|
30
30
|
document.title = 'I "quoted"'
|
31
|
-
|
31
|
+
document.valid?
|
32
|
+
expect(document.url).to eq('i-quoted')
|
32
33
|
end
|
33
34
|
|
34
|
-
it 'should keep old
|
35
|
+
it 'should keep old urls' do
|
35
36
|
article.save
|
36
|
-
article.title =
|
37
|
+
article.title = 'Hello Ruby!'
|
37
38
|
article.save
|
38
|
-
expect(article.urls).to eq [
|
39
|
+
expect(article.urls).to eq ['im-an-article', 'hello-ruby']
|
39
40
|
end
|
40
41
|
end
|
41
42
|
|
42
43
|
describe '#url' do
|
43
|
-
describe 'default "
|
44
|
+
describe 'default ":title"' do
|
44
45
|
before(:each) { document_class.send(:url, :title) }
|
45
46
|
|
46
47
|
it 'should be created' do
|
@@ -68,6 +69,19 @@ describe Mongoid::Urls do
|
|
68
69
|
expect(document).to have_field(:urls)
|
69
70
|
end
|
70
71
|
|
72
|
+
it 'should accept simple field names' do
|
73
|
+
document_class.send(:url, :sweet, simple: true)
|
74
|
+
expect(document).to_not have_field(:urls)
|
75
|
+
expect(document).to have_field(:url)
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should create simple field to_param' do
|
79
|
+
document_class.send(:field, :name)
|
80
|
+
document_class.send(:url, :name, simple: true)
|
81
|
+
doc = document_class.create(name: 'nice doc')
|
82
|
+
expect(doc.to_param).to eq('nice-doc')
|
83
|
+
end
|
84
|
+
|
71
85
|
it 'should not create custom finders with default id' do
|
72
86
|
# A sample model
|
73
87
|
class UntaintedDocument
|
@@ -149,6 +163,32 @@ describe Mongoid::Urls do
|
|
149
163
|
end
|
150
164
|
end
|
151
165
|
|
166
|
+
describe 'reserved words' do
|
167
|
+
before(:each) do
|
168
|
+
end
|
169
|
+
it 'should respect default new' do
|
170
|
+
article.title = 'new'
|
171
|
+
expect(article.save).to be_falsey
|
172
|
+
expect(article.errors).to include(:title)
|
173
|
+
end
|
174
|
+
|
175
|
+
it 'should respect default edit' do
|
176
|
+
article.title = 'edit'
|
177
|
+
expect(article.save).to be_falsey
|
178
|
+
expect(article.errors).to include(:title)
|
179
|
+
end
|
180
|
+
|
181
|
+
it 'should match' do
|
182
|
+
article.title = 'anew'
|
183
|
+
expect(article.save).to be_truthy
|
184
|
+
end
|
185
|
+
|
186
|
+
it 'should match' do
|
187
|
+
article.title = 'newa'
|
188
|
+
expect(article.save).to be_truthy
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
152
192
|
describe '(no) collision resolution' do
|
153
193
|
before(:each) do
|
154
194
|
document_class.send(:url, :title)
|
@@ -172,20 +212,17 @@ describe Mongoid::Urls do
|
|
172
212
|
end
|
173
213
|
end
|
174
214
|
|
175
|
-
context 'with other
|
215
|
+
context 'with other url present' do
|
176
216
|
before(:each) do
|
177
217
|
document_class.send(:field, :name)
|
178
|
-
document_class.send(:url, :name)
|
179
218
|
document_class.send(:index, { name: 1 }, unique: true)
|
180
219
|
document_class.create_indexes
|
181
220
|
end
|
182
221
|
|
183
222
|
context 'when violating the other index' do
|
184
223
|
it 'should raise an operation failure' do
|
185
|
-
|
186
|
-
|
187
|
-
expect { document_class.create!(name: duplicate_name) }
|
188
|
-
.to raise_exception(Mongo::Error::OperationFailure)
|
224
|
+
expect { document_class.send(:url, :name) }
|
225
|
+
.to raise_exception(RuntimeError)
|
189
226
|
end
|
190
227
|
end
|
191
228
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
# require 'codeclimate-test-reporter'
|
2
2
|
# CodeClimate::TestReporter.start
|
3
|
+
if ENV['CI']
|
4
|
+
require 'coveralls'
|
5
|
+
Coveralls.wear!
|
6
|
+
end
|
3
7
|
|
4
8
|
$LOAD_PATH << File.expand_path('../../lib', __FILE__)
|
5
9
|
|
@@ -12,20 +16,27 @@ require 'mongoid/urls'
|
|
12
16
|
|
13
17
|
ENV['MONGOID_ENV'] = 'test'
|
14
18
|
|
19
|
+
db_config = {
|
20
|
+
default: {
|
21
|
+
database: 'mongoid_urls_test',
|
22
|
+
hosts: ["localhost: #{ENV['MONGODB_PORT'] || 27_017}"],
|
23
|
+
options: {}
|
24
|
+
}
|
25
|
+
}
|
26
|
+
|
15
27
|
Mongoid.configure do |config|
|
16
28
|
config.load_configuration(
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
})
|
29
|
+
if Mongoid::VERSION >= '5'
|
30
|
+
{ clients: db_config }
|
31
|
+
else
|
32
|
+
{ sessions: db_config }
|
33
|
+
end
|
34
|
+
)
|
24
35
|
end
|
25
36
|
|
26
37
|
require 'support/models'
|
27
38
|
|
28
|
-
Mongo::Logger.logger.level = Logger::INFO
|
39
|
+
Mongo::Logger.logger.level = Logger::INFO if Mongoid::VERSION >= '5'
|
29
40
|
|
30
41
|
RSpec.configure do |config|
|
31
42
|
config.include Mongoid::Matchers
|
data/spec/support/models.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid-urls
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcos Piccinini
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-07-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
@@ -80,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
80
80
|
version: '0'
|
81
81
|
requirements: []
|
82
82
|
rubyforge_project: mongoid-urls
|
83
|
-
rubygems_version: 2.4.
|
83
|
+
rubygems_version: 2.4.8
|
84
84
|
signing_key:
|
85
85
|
specification_version: 4
|
86
86
|
summary: A url sanitizer (slugs) for Mongoid documents.
|