mongoid-urls 0.0.7 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -0
- data/Gemfile +2 -0
- data/Guardfile +2 -2
- data/README.md +12 -8
- data/benchmarks/benchmark.rb +1 -1
- data/lib/mongoid/urls/version.rb +1 -1
- data/lib/mongoid/urls.rb +32 -19
- data/mongoid-urls.gemspec +1 -2
- data/spec/mongoid/urls_spec.rb +99 -9
- data/spec/support/models.rb +15 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 025093f44d0ac503c479d29b060fbdc6b02bce2d
|
4
|
+
data.tar.gz: 047fde3fdbb3ce920d3ecfb0fc10edf7014d25c9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 516d37fffdfc84b839498510a5160532aaf34697718cc095f29362ef895b0378d69163b42a9c02830aaac466ed70728455471fbcf988426ec3458ddd26dfd1ed
|
7
|
+
data.tar.gz: aca47cb090d946b6436f18abec6bb9947e3a94f576a7045273e2d06bab2ea39171eb536b54989df0f3e84edbe4cf01abece67038657bb93beefd95cab32789b7
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/Guardfile
CHANGED
@@ -9,14 +9,14 @@
|
|
9
9
|
|
10
10
|
# guard :rubocop do
|
11
11
|
guard :rubocop, all_on_start: false, keep_failed: false, cli: ['-D'] do
|
12
|
-
watch(
|
12
|
+
watch(/.+\.rb$/)
|
13
13
|
watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
|
14
14
|
end
|
15
15
|
|
16
16
|
guard :rspec, cmd: 'bundle exec rspec' do
|
17
17
|
watch(%r{^spec/.+_spec\.rb$})
|
18
18
|
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
|
19
|
-
watch(
|
19
|
+
watch(%r{^generators/(.+)\.rb$}) { |_m| 'spec/schemaless/worker_spec' }
|
20
20
|
|
21
21
|
watch('spec/spec_helper.rb') { 'spec' }
|
22
22
|
end
|
data/README.md
CHANGED
@@ -1,21 +1,23 @@
|
|
1
1
|
Mongoid::Urls
|
2
2
|
=============
|
3
3
|
|
4
|
-
[![Gem Version](https://badge.fury.io/rb/mongoid-urls.
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/mongoid-urls.svg)](http://badge.fury.io/rb/mongoid-urls)
|
5
5
|
[![Dependency Status](https://gemnasium.com/nofxx/mongoid-urls.svg)](https://gemnasium.com/nofxx/mongoid-urls)
|
6
|
-
[![Build Status](https://secure.travis-ci.org/nofxx/mongoid-urls.
|
7
|
-
[![Code Climate](https://codeclimate.com/github/nofxx/mongoid-urls.
|
6
|
+
[![Build Status](https://secure.travis-ci.org/nofxx/mongoid-urls.svg)](http://travis-ci.org/nofxx/mongoid-urls)
|
7
|
+
[![Code Climate](https://codeclimate.com/github/nofxx/mongoid-urls.svg)](https://codeclimate.com/github/nofxx/mongoid-urls)
|
8
8
|
[![Coverage Status](https://coveralls.io/repos/nofxx/mongoid-urls/badge.svg)](https://coveralls.io/r/nofxx/mongoid-urls)
|
9
9
|
|
10
10
|
## Mongoid::Urls
|
11
11
|
|
12
12
|
Simple slugs for mongoid models!
|
13
|
+
Set the fields you want to try to make an URL out of,
|
14
|
+
when impossible, use your logic or set the url directly.
|
13
15
|
|
14
16
|
|
15
17
|
## Nice URLs for Mongoid Documents
|
16
18
|
|
17
|
-
This library is a quick and simple way to generate slugs
|
18
|
-
for your
|
19
|
+
This library is a quick and simple way to generate URLs (slugs)
|
20
|
+
for your Mongoid documents.
|
19
21
|
|
20
22
|
Mongoid::Urls can help turn this:
|
21
23
|
|
@@ -36,13 +38,15 @@ In your Mongoid documents, just add `include Mongoid::Urls`
|
|
36
38
|
and use the `url` method to setup, like so:
|
37
39
|
|
38
40
|
```ruby
|
39
|
-
class
|
41
|
+
class Company
|
40
42
|
include Mongoid::Document
|
41
43
|
include Mongoid::Urls
|
42
44
|
|
43
|
-
field :
|
45
|
+
field :nickname
|
46
|
+
field :fullname
|
47
|
+
...
|
44
48
|
|
45
|
-
url :
|
49
|
+
url :nickname, :fullname, ...
|
46
50
|
end
|
47
51
|
|
48
52
|
```
|
data/benchmarks/benchmark.rb
CHANGED
data/lib/mongoid/urls/version.rb
CHANGED
data/lib/mongoid/urls.rb
CHANGED
@@ -8,8 +8,8 @@ module Mongoid
|
|
8
8
|
included do
|
9
9
|
cattr_accessor :reserved_words,
|
10
10
|
:url_simple,
|
11
|
-
:url_scope,
|
12
|
-
:
|
11
|
+
# :url_scope,
|
12
|
+
:url_keys
|
13
13
|
end
|
14
14
|
|
15
15
|
# Methods avaiable at the model
|
@@ -20,16 +20,24 @@ module Mongoid
|
|
20
20
|
# url :title
|
21
21
|
#
|
22
22
|
# :simple -> Only one url per instance
|
23
|
-
# :reserve -> Defaults to %w( new edit )
|
23
|
+
# :reserve -> Defaults to %w( new edit ) + I18n.locales
|
24
24
|
#
|
25
25
|
def url(*args)
|
26
26
|
options = args.extract_options!
|
27
|
-
fail 'One #url per model!' if
|
28
|
-
self.
|
27
|
+
fail 'One #url per model!' if url_keys
|
28
|
+
self.url_keys = args # .first.to_s
|
29
29
|
self.url_simple = options[:simple]
|
30
|
-
self.reserved_words = options[:reserve] || Set.new(%w(new edit))
|
31
30
|
create_url_fields
|
31
|
+
create_url_validations(options)
|
32
|
+
end
|
33
|
+
|
34
|
+
def create_url_validations(options)
|
32
35
|
before_validation :create_urls
|
36
|
+
reserve = Set.new(%w(new edit)) + (options[:reserved] || [])
|
37
|
+
reserve << I18n.available_locales if Object.const_defined?('I18n')
|
38
|
+
self.reserved_words = reserve.flatten
|
39
|
+
validates :url, uniqueness: true, presence: true,
|
40
|
+
format: { with: /[a-z\d-]+/ }
|
33
41
|
end
|
34
42
|
|
35
43
|
def find_url(u)
|
@@ -44,6 +52,9 @@ module Mongoid
|
|
44
52
|
def create_url_fields
|
45
53
|
field :url, type: String
|
46
54
|
index({ url: 1 }, unique: true)
|
55
|
+
define_method('url=') do |val|
|
56
|
+
self[:url] = val.to_slug.normalize.to_s
|
57
|
+
end
|
47
58
|
return if url_simple
|
48
59
|
field :urls, type: Array, default: []
|
49
60
|
index(urls: 1)
|
@@ -55,28 +66,30 @@ module Mongoid
|
|
55
66
|
end
|
56
67
|
|
57
68
|
def new_url
|
58
|
-
|
69
|
+
url_keys.each do |key|
|
70
|
+
val = send(key)
|
71
|
+
next if val.blank?
|
72
|
+
url = val.to_slug.normalize.to_s
|
73
|
+
next if self.class.find_url(url)
|
74
|
+
return url
|
75
|
+
end
|
76
|
+
nil
|
59
77
|
end
|
60
78
|
|
61
79
|
protected
|
62
80
|
|
63
|
-
def
|
64
|
-
|
65
|
-
|
66
|
-
elsif reserved_words.include?(u)
|
67
|
-
errors.add(:title, :reserved)
|
68
|
-
else
|
69
|
-
true
|
70
|
-
end
|
81
|
+
def validate_url(slug)
|
82
|
+
return unless reserved_words.include?(slug)
|
83
|
+
errors.add(:url, :reserved)
|
71
84
|
end
|
72
85
|
|
73
86
|
def create_urls
|
74
|
-
return unless
|
75
|
-
|
87
|
+
return unless (slug = new_url)
|
88
|
+
validate_url(slug)
|
76
89
|
|
77
|
-
self.url =
|
90
|
+
self.url = slug
|
78
91
|
return if url_simple
|
79
|
-
urls <<
|
92
|
+
urls << slug
|
80
93
|
urls.uniq!
|
81
94
|
end
|
82
95
|
end # Urls
|
data/mongoid-urls.gemspec
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
-
|
2
|
+
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
|
3
3
|
require 'mongoid/urls/version'
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
@@ -18,6 +18,5 @@ 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
21
|
s.require_paths = ['lib']
|
23
22
|
end
|
data/spec/mongoid/urls_spec.rb
CHANGED
@@ -8,12 +8,13 @@ describe Mongoid::Urls do
|
|
8
8
|
include Mongoid::Document
|
9
9
|
include Mongoid::Urls
|
10
10
|
field :title
|
11
|
+
field :doc
|
11
12
|
end
|
12
13
|
Class.new(Document)
|
13
14
|
end
|
14
15
|
|
15
16
|
let(:document) do
|
16
|
-
document_class.create(title: "I'm a Document!")
|
17
|
+
document_class.create(title: "I'm a Document!", doc: '123')
|
17
18
|
end
|
18
19
|
|
19
20
|
let(:article) do
|
@@ -41,6 +42,18 @@ describe Mongoid::Urls do
|
|
41
42
|
end
|
42
43
|
|
43
44
|
describe '#url' do
|
45
|
+
it 'should accept custom field names' do
|
46
|
+
document_class.send(:url, :doc)
|
47
|
+
expect(document).to have_field(:url)
|
48
|
+
expect(document).to have_field(:urls)
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should accept simple field names' do
|
52
|
+
document_class.send(:url, :title, simple: true)
|
53
|
+
expect(document).to_not have_field(:urls)
|
54
|
+
expect(document).to have_field(:url)
|
55
|
+
end
|
56
|
+
|
44
57
|
describe 'default ":title"' do
|
45
58
|
before(:each) { document_class.send(:url, :title) }
|
46
59
|
|
@@ -65,15 +78,18 @@ describe Mongoid::Urls do
|
|
65
78
|
|
66
79
|
describe 'options' do
|
67
80
|
it 'should accept custom field names' do
|
68
|
-
document_class.send(:url, :
|
69
|
-
expect(document).to have_field(:
|
81
|
+
document_class.send(:url, :title)
|
82
|
+
expect(document).to have_field(:url)
|
70
83
|
end
|
71
84
|
|
72
85
|
it 'should accept simple field names' do
|
73
|
-
document_class.send(:url, :
|
86
|
+
document_class.send(:url, :title, simple: true)
|
74
87
|
expect(document).to_not have_field(:urls)
|
75
88
|
expect(document).to have_field(:url)
|
76
89
|
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe 'index field' do
|
77
93
|
|
78
94
|
it 'should create simple field to_param' do
|
79
95
|
document_class.send(:field, :name)
|
@@ -164,18 +180,16 @@ describe Mongoid::Urls do
|
|
164
180
|
end
|
165
181
|
|
166
182
|
describe 'reserved words' do
|
167
|
-
before(:each) do
|
168
|
-
end
|
169
183
|
it 'should respect default new' do
|
170
184
|
article.title = 'new'
|
171
185
|
expect(article.save).to be_falsey
|
172
|
-
expect(article.errors).to include(:
|
186
|
+
expect(article.errors).to include(:url)
|
173
187
|
end
|
174
188
|
|
175
189
|
it 'should respect default edit' do
|
176
190
|
article.title = 'edit'
|
177
191
|
expect(article.save).to be_falsey
|
178
|
-
expect(article.errors).to include(:
|
192
|
+
expect(article.errors).to include(:url)
|
179
193
|
end
|
180
194
|
|
181
195
|
it 'should match' do
|
@@ -201,15 +215,41 @@ describe Mongoid::Urls do
|
|
201
215
|
d2 = article.clone
|
202
216
|
expect(d2).to_not be_valid
|
203
217
|
expect(d2.save).to be_falsey
|
204
|
-
expect(d2.errors.messages).to include(:
|
218
|
+
expect(d2.errors.messages).to include(:url)
|
205
219
|
end
|
206
220
|
|
207
221
|
it 'should raise when collisions can\'t be resolved on create!' do
|
208
222
|
article.title = '1234'
|
209
223
|
article.save
|
224
|
+
expect(article.reload.url).to eq '1234'
|
210
225
|
dup = Article.create(title: '1234')
|
226
|
+
expect(dup.url).to be nil
|
211
227
|
expect(dup.errors.messages).to_not be_empty
|
212
228
|
end
|
229
|
+
|
230
|
+
it 'should be possible to edit the url directly' do
|
231
|
+
article.title = '1234'
|
232
|
+
article.save
|
233
|
+
expect(article.reload.url).to eq '1234'
|
234
|
+
dup = Article.create(title: '1234')
|
235
|
+
expect(dup.url).to be nil
|
236
|
+
dup.url = 'onetwo'
|
237
|
+
dup.save
|
238
|
+
expect(dup.errors.messages).to be_empty
|
239
|
+
expect(dup.reload.url).to eq('onetwo')
|
240
|
+
end
|
241
|
+
|
242
|
+
it 'should be safe to edit the url directly' do
|
243
|
+
article.title = '1234'
|
244
|
+
article.save
|
245
|
+
expect(article.reload.url).to eq '1234'
|
246
|
+
dup = Article.create(title: '1234')
|
247
|
+
expect(dup.url).to be nil
|
248
|
+
dup.url = 'One Two'
|
249
|
+
dup.save
|
250
|
+
expect(dup.errors.messages).to be_empty
|
251
|
+
expect(dup.reload.url).to eq('one-two')
|
252
|
+
end
|
213
253
|
end
|
214
254
|
|
215
255
|
context 'with other url present' do
|
@@ -227,4 +267,54 @@ describe Mongoid::Urls do
|
|
227
267
|
end
|
228
268
|
end
|
229
269
|
end
|
270
|
+
|
271
|
+
describe 'Dynamic' do
|
272
|
+
it 'should create' do
|
273
|
+
com = Company.create(name: 'ACME Corp LLC', nick: 'ACME')
|
274
|
+
expect(com.url).to eq 'acme'
|
275
|
+
end
|
276
|
+
|
277
|
+
it 'should create w/o one' do
|
278
|
+
com = Company.create(name: 'ACME Corp LLC')
|
279
|
+
expect(com.url).to eq 'acme-corp-llc'
|
280
|
+
end
|
281
|
+
|
282
|
+
it 'should assign attr' do
|
283
|
+
com = Company.new
|
284
|
+
com.assign_attributes(name: 'ACME Corp LLC', nick: 'ACME')
|
285
|
+
com.save
|
286
|
+
expect(com.url).to eq 'acme'
|
287
|
+
expect(Company.count).to eq 1
|
288
|
+
end
|
289
|
+
|
290
|
+
it 'should assign second attr when first is taken' do
|
291
|
+
com1 = Company.create!(name: 'ACME One', nick: 'ACME')
|
292
|
+
com2 = Company.create!(name: 'ACME Two', nick: 'ACME')
|
293
|
+
expect(com1.url).to eq 'acme'
|
294
|
+
expect(com2.url).to eq 'acme-two'
|
295
|
+
end
|
296
|
+
|
297
|
+
it 'should assign third attr when second is taken' do
|
298
|
+
com1 = Company.create!(name: 'Common Name', nick: 'ACME')
|
299
|
+
com2 = Company.create!(name: 'Common Name', nick: 'ACME')
|
300
|
+
com3 = Company.create!(name: 'Common Name', nick: 'ACME')
|
301
|
+
expect(com1.url).to eq 'acme'
|
302
|
+
expect(com2.url).to eq 'common-name'
|
303
|
+
expect(com3.url).to eq 'acme-common-name'
|
304
|
+
end
|
305
|
+
|
306
|
+
it 'should assign attr' do
|
307
|
+
com = Company.new
|
308
|
+
com.assign_attributes(name: 'ACME Corp LLC', nick: nil)
|
309
|
+
com.save
|
310
|
+
expect(com.url).to eq 'acme-corp-llc'
|
311
|
+
end
|
312
|
+
|
313
|
+
it 'should build up' do
|
314
|
+
com = Company.new
|
315
|
+
com.nick = 'ACME'
|
316
|
+
com.valid?
|
317
|
+
expect(com.url).to eq 'acme'
|
318
|
+
end
|
319
|
+
end
|
230
320
|
end
|
data/spec/support/models.rb
CHANGED
@@ -5,3 +5,18 @@ class Article
|
|
5
5
|
field :title
|
6
6
|
url :title
|
7
7
|
end
|
8
|
+
|
9
|
+
# A model with dynamic key
|
10
|
+
class Company
|
11
|
+
include Mongoid::Document
|
12
|
+
include Mongoid::Urls
|
13
|
+
field :name
|
14
|
+
field :nick
|
15
|
+
|
16
|
+
url :nick, :name, :fullname
|
17
|
+
|
18
|
+
def fullname
|
19
|
+
return nick + ' - ' + name if nick && name
|
20
|
+
nick || name
|
21
|
+
end
|
22
|
+
end
|
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.9
|
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-07-
|
11
|
+
date: 2015-07-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongoid
|
@@ -88,3 +88,4 @@ test_files:
|
|
88
88
|
- spec/mongoid/urls_spec.rb
|
89
89
|
- spec/spec_helper.rb
|
90
90
|
- spec/support/models.rb
|
91
|
+
has_rdoc:
|