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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b71d8482958f190295f9a8a0d52ca7cfdf8ef63c
4
- data.tar.gz: 22b5ffff3481e0488d74b6b7f4743d7f6bb4fc80
3
+ metadata.gz: b5712fe3113cb418853a86467166a467ef85e918
4
+ data.tar.gz: 87fd69af5d300bcaa822616e88f64f5f3f4d97af
5
5
  SHA512:
6
- metadata.gz: b263576272e13a2c1971355de8c28f5475d8a18aeef4e9a183c3d964a25e4ea43125b36d641b3ea5112eacec5bc088c2a0bb2f090d3c00a7aae171bf412ef9c3
7
- data.tar.gz: fa31fde6011957d031e9b82cc9a3db6495c719c4c701f85c5b224a23e63710e6dd1f1cd56d92921f80a0b05d1b2532fc0ca75d6874fb6f50f1d70765019cec99
6
+ metadata.gz: 9a7a34585091f75f746e903fde578f8a23ac97eed52678239b617613c0de1dc5e1042b467a2f62e64cf84b73d8cc7ff78e33d8a544bcbee431026aaad919f075
7
+ data.tar.gz: 5a26ba691150d4587738a363f6cb7bfc67f2053218776f43181dd10f15ad6925ecad8c13bf89219fb48d1ced3265c0cce8d7b6af9fc4c9611774a9d2a43169b9
data/.gitignore CHANGED
@@ -3,3 +3,5 @@
3
3
  Gemfile.lock
4
4
  pkg/*
5
5
  .DS_Store
6
+ /.coveralls.yml
7
+ coverage/*
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-19mode
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
@@ -7,6 +7,7 @@ group :test do
7
7
  gem 'guard-rspec'
8
8
  gem 'guard-rubocop'
9
9
  gem 'rspec', '>= 3.2.0'
10
+ gem 'coveralls', require: false
10
11
  gem 'codeclimate-test-reporter', require: nil
11
12
  gem 'mongoid-rspec', git: 'https://github.com/nofxx/mongoid-rspec'
12
13
  end
data/README.md CHANGED
@@ -12,7 +12,7 @@ Mongoid::Urls
12
12
  Simple slugs for mongoid models!
13
13
 
14
14
 
15
- ## Short snappy token ids for Mongoid documents
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 more like this:
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` and the
36
- `url` method will take care of all the setup, like so:
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 Article
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
- Default #urls
72
+ Simple: `#url` String only.
70
73
 
71
- Simple #url
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.
@@ -1,6 +1,6 @@
1
1
  module Mongoid
2
2
  # Mongoid::Urls::VERSION
3
3
  module Urls
4
- VERSION = '0.0.3'
4
+ VERSION = '0.0.7'
5
5
  end
6
6
  end
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 find_by_url(u)
35
- url_simple ? find_by(url: u) : find_by(urls: u)
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
- if url_simple
44
- field :url, type: String
45
- index({ url: 1 }, unique: true)
46
- else
47
- field :urls, type: Array, default: []
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
- urls.last
54
+ url
55
55
  end
56
56
 
57
- def slug
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 = slug)
75
+ validate_urls(new_url)
76
76
 
77
- return self.url = new_url if url_simple
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 = ["lib"]
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
22
+ s.require_paths = ['lib']
23
23
  end
@@ -20,27 +20,28 @@ describe Mongoid::Urls do
20
20
  Article.new(title: "I'm an Article!")
21
21
  end
22
22
 
23
- describe '#slug' do
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.slug).to eq('im-a-document')
26
+ expect(document.url).to eq('im-a-document')
27
27
  end
28
28
 
29
- it 'should update slug' do
29
+ it 'should update url' do
30
30
  document.title = 'I "quoted"'
31
- expect(document.slug).to eq('i-quoted')
31
+ document.valid?
32
+ expect(document.url).to eq('i-quoted')
32
33
  end
33
34
 
34
- it 'should keep old slugs' do
35
+ it 'should keep old urls' do
35
36
  article.save
36
- article.title = "Hello Ruby!"
37
+ article.title = 'Hello Ruby!'
37
38
  article.save
38
- expect(article.urls).to eq ["im-an-article", "hello-ruby"]
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 "_id"' do
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 unique indexes present' do
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
- duplicate_name = 'Got Duped.'
186
- document_class.create!(name: duplicate_name)
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
- clients: {
18
- default: {
19
- database: 'mongoid_urls_test',
20
- hosts: ["localhost: #{ENV['BOXEN_MONGODB_PORT'] || 27_017}"],
21
- options: {}
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
@@ -1,3 +1,4 @@
1
+ # A nice model for a blog!
1
2
  class Article
2
3
  include Mongoid::Document
3
4
  include Mongoid::Urls
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.3
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-06-19 00:00:00.000000000 Z
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.7
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.