mongoid-urls 0.0.7 → 0.0.9

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b5712fe3113cb418853a86467166a467ef85e918
4
- data.tar.gz: 87fd69af5d300bcaa822616e88f64f5f3f4d97af
3
+ metadata.gz: 025093f44d0ac503c479d29b060fbdc6b02bce2d
4
+ data.tar.gz: 047fde3fdbb3ce920d3ecfb0fc10edf7014d25c9
5
5
  SHA512:
6
- metadata.gz: 9a7a34585091f75f746e903fde578f8a23ac97eed52678239b617613c0de1dc5e1042b467a2f62e64cf84b73d8cc7ff78e33d8a544bcbee431026aaad919f075
7
- data.tar.gz: 5a26ba691150d4587738a363f6cb7bfc67f2053218776f43181dd10f15ad6925ecad8c13bf89219fb48d1ced3265c0cce8d7b6af9fc4c9611774a9d2a43169b9
6
+ metadata.gz: 516d37fffdfc84b839498510a5160532aaf34697718cc095f29362ef895b0378d69163b42a9c02830aaac466ed70728455471fbcf988426ec3458ddd26dfd1ed
7
+ data.tar.gz: aca47cb090d946b6436f18abec6bb9947e3a94f576a7045273e2d06bab2ea39171eb536b54989df0f3e84edbe4cf01abece67038657bb93beefd95cab32789b7
data/.travis.yml CHANGED
@@ -24,3 +24,5 @@ services:
24
24
  env: CI="travis"
25
25
 
26
26
  script: "bundle exec rspec"
27
+
28
+ sudo: false
data/Gemfile CHANGED
@@ -1,5 +1,7 @@
1
1
  source 'http://rubygems.org'
2
2
 
3
+ gem 'mongoid', '>=5.0.0.beta'
4
+
3
5
  gemspec
4
6
 
5
7
  group :test do
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(%r{.+\.rb$})
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(/^generators\/(.+)\.rb$/) { |_m| 'spec/schemaless/worker_spec' }
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.png)](http://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.png)](http://travis-ci.org/nofxx/mongoid-urls)
7
- [![Code Climate](https://codeclimate.com/github/nofxx/mongoid-urls.png)](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 mongoid documents.
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 Video
41
+ class Company
40
42
  include Mongoid::Document
41
43
  include Mongoid::Urls
42
44
 
43
- field :title
45
+ field :nickname
46
+ field :fullname
47
+ ...
44
48
 
45
- url :title
49
+ url :nickname, :fullname, ...
46
50
  end
47
51
 
48
52
  ```
@@ -1,3 +1,3 @@
1
- $: << File.expand_path("../../lib", __FILE__)
1
+ $LOAD_PATH << File.expand_path('../../lib', __FILE__)
2
2
 
3
3
  # Maybe benchmark stringex vs babosa just for fun...
@@ -1,6 +1,6 @@
1
1
  module Mongoid
2
2
  # Mongoid::Urls::VERSION
3
3
  module Urls
4
- VERSION = '0.0.7'
4
+ VERSION = '0.0.9'
5
5
  end
6
6
  end
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
- :url_key
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 url_key
28
- self.url_key = args.first.to_s
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
- self[url_key].to_slug.normalize.to_s
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 validate_urls(u)
64
- if self.class.find_by_url(u)
65
- errors.add(:title, :uniqueness)
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 changes.include?(url_key)
75
- validate_urls(new_url)
87
+ return unless (slug = new_url)
88
+ validate_url(slug)
76
89
 
77
- self.url = new_url
90
+ self.url = slug
78
91
  return if url_simple
79
- urls << new_url
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
- $:.push File.expand_path('../lib', __FILE__)
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
@@ -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, :sweet)
69
- expect(document).to have_field(:urls)
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, :sweet, simple: true)
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(:title)
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(:title)
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(:title)
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
@@ -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.7
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-20 00:00:00.000000000 Z
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: