leifcr-mm-sluggable 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dd133368d98c05b9fa8d73ac87002ca30dda856a
4
+ data.tar.gz: 3b1432df20d7cfdc384161e499e75b41df8f5d26
5
+ SHA512:
6
+ metadata.gz: da2472198a8721fd551177c05b0f8abb7c8adfb2ad914dad0475bf03d3deef7d416b2b8f6a74ebdd839a770515c5962a100e9ccf926dd5e9ee098382a1ce41fb
7
+ data.tar.gz: a8f1f65701da020944f23a7eacc7a5155e9c667802a9c522676a2b49644f128bcc6e1277aa108d3850c49addbe4a9b0bb71ae87607567714087f6b66334419e7
data/Gemfile CHANGED
@@ -1,19 +1,19 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
+
2
3
  gem 'rake'
3
4
  gem 'bson_ext', '>= 1.6.4'
4
5
  gem 'multi_json', '>= 1.3.6'
5
6
 
6
7
  group :test do
7
- # gem 'jnunemaker-matchy', '>= 0.4', :require => 'matchy'
8
- # gem 'shoulda', '>= 3.1.1'
9
- # gem 'mocha', '>= 0.12.3'
10
- gem 'rspec'
8
+ gem 'rspec', "~> 2.8"
11
9
  gem 'database_cleaner', '>= 0.8.0'
12
10
  end
13
11
 
14
- group :development do
15
- gem "wirble"
16
- gem "hirb"
17
- gem "awesome_print"
12
+ group :development, :test do
13
+ gem 'coveralls', :require => false
14
+ gem 'simplecov', :require => false
18
15
  end
19
- gemspec
16
+
17
+ gem 'activemodel', "~> 3.2"
18
+
19
+ gem 'mongo_mapper', '0.13.0.beta2'
data/LICENSE CHANGED
@@ -1,5 +1,5 @@
1
1
  Copyright (c) 2010 Richard Livsey
2
- Copyright (c) 2012 Leif Ringstad
2
+ Copyright (c) 2012-2014 Leif Ringstad
3
3
 
4
4
  Permission is hereby granted, free of charge, to any person obtaining
5
5
  a copy of this software and associated documentation files (the
@@ -1,23 +1,30 @@
1
- = MongoMapper::Plugins::Sluggable
1
+ # MongoMapper::Plugins::Sluggable
2
+ [![Build Status](https://travis-ci.org/leifcr/mm-sluggable.svg?branch=master)](https://travis-ci.org/leifcr/mm-sluggable) [![Coverage Status](https://coveralls.io/repos/leifcr/mm-sluggable/badge.png)](https://coveralls.io/r/leifcr/mm-sluggable) [![Dependency Status](https://gemnasium.com/leifcr/mm-sluggable.svg)](https://gemnasium.com/leifcr/mm-sluggable)
2
3
 
3
- Add slugs to your fields. And use it for URLS...
4
+ Add slugs to your fields. And use it for nice urls. Difference from other mm-sluggables: This uses regexp query for checking conflicting slugs, so you get only 2 queries even if your collection has 1000 upon 1000 of documents.
4
5
 
5
- == Usage
6
+ ## Usage
6
7
 
7
8
  Either load it into all models, or individual models:
8
9
 
9
10
  Add to all models
11
+ ```ruby
10
12
  #initializers/mongo.rb
11
13
  MongoMapper::Document.plugin(MongoMapper::Plugins::Sluggable)
14
+ ```
12
15
 
13
16
  Add to a specific model
17
+ ```ruby
14
18
  #app/models/my_model.rb
15
19
  plugin MongoMapper::Plugins::Sluggable
20
+ ```
16
21
 
17
22
  Then call sluggable to configure it
23
+ ```ruby
18
24
  sluggable :title, :scope => :account_id
25
+ ```
19
26
 
20
- NOTE: The slugs will always be updated if you change the field it creates
27
+ NOTE: The slugs will always be updated if you change the field it creates
21
28
  the slugs from. see options to change behaviour
22
29
 
23
30
  Free slugs will be reused
@@ -25,7 +32,7 @@ Example: you have my-title-1 and my-title-3 creating a new slug will generate my
25
32
 
26
33
  Why always change slugs? Feels better to have same slug as title...
27
34
 
28
- == Improve performance on your models
35
+ ## Improve performance on your models
29
36
 
30
37
  This is important to get fast queries! (you will regret if you don't add it...)
31
38
 
@@ -35,8 +42,8 @@ https://github.com/jnunemaker/mongomapper/issues/337
35
42
  Easiest is to add db/indexes.rb to your app, then add this to that file:
36
43
  # db/indexes.rb
37
44
  MyModel.ensure_index(:slug_key)
38
-
39
- == Options
45
+
46
+ ## Options
40
47
 
41
48
  Available options are:
42
49
 
@@ -48,28 +55,30 @@ Available options are:
48
55
 
49
56
  Eg.
50
57
 
51
- sluggable :title, :scope => :account_id, :key => :title_slug, :method => :to_url, :index => false
52
-
58
+ ```ruby
59
+ sluggable :title, scope: :account_id, key: :title_slug, method: :to_url, always_update: true
60
+ ```
61
+
53
62
  This will slug the title to the title_slug key, scoped to the account, will use String#to_url to slug it and won't add an index to the key
54
63
 
55
- == Versioning
64
+ ## Versioning
56
65
 
57
66
  If an item with the same slug exists, it will add a version number to the slug.
58
67
 
59
68
  IE assuming we already have an item with the slug of "monkey", the slug will be generated as "monkey-1"
60
69
 
61
- == Note on Patches/Pull Requests
70
+ ## Note on Patches/Pull Requests
62
71
  * Fork the project.
63
72
  * Make your feature addition or bug fix.
64
73
  * Add tests for it.
65
74
 
66
- == Install
75
+ ## Install
67
76
  Bundler: (You are most likely using bundler in your app...)
68
77
  gem 'mm-sluggable', :git => 'http://github.com/luuf/mm-sluggable.git'
69
78
 
70
- == Thanks
79
+ ## Thanks
71
80
  * Richard Livsey for creating the original mm-sluggable
72
81
  * John Nunemaker, Brandon Keepers & Others for MongoMapper
73
82
 
74
- == Copyright
83
+ ## Copyright
75
84
  See LICENSE for details.
data/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  require "bundler/gem_tasks"
2
2
  require 'rspec/core/rake_task'
3
3
 
4
- require File.join(File.dirname(__FILE__), '/lib/sluggable/version')
4
+ require File.join(File.dirname(__FILE__), 'lib', 'sluggable', 'version')
5
5
 
6
6
  RSpec::Core::RakeTask.new(:spec)
7
7
  task :default => :spec
data/lib/mm-sluggable.rb CHANGED
@@ -10,28 +10,39 @@ module MongoMapper
10
10
  class_attribute :slug_options
11
11
 
12
12
  self.slug_options = {
13
- :to_slug => to_slug,
14
- :key => :slug,
15
- :method => :parameterize,
16
- :scope => nil,
17
- :max_length => 256,
18
- :always_update => true, # allow always updating slug...
19
- :callback => :before_validation,
20
- :callback_on => nil,
13
+ to_slug: to_slug,
14
+ key: :slug,
15
+ method: :parameterize,
16
+ scope: nil,
17
+ max_length: 256,
18
+ start: 1,
19
+ always_update: true, # allow always updating slug...
20
+ callback: :before_validation,
21
+ callback_on: nil,
21
22
  }.merge(options)
22
23
  key slug_options[:key], String
23
24
 
24
25
  #before_validation :set_slug
25
26
  self.send(slug_options[:callback], :set_slug, {:on => slug_options[:callback_on]}) if slug_options[:callback] && slug_options[:callback_on]
26
- self.send(slug_options[:callback], :set_slug) if slug_options[:callback] && slug_options[:callback_on].nil?
27
+ self.send(slug_options[:callback], :set_slug) if slug_options[:callback] && slug_options[:callback_on].nil?
27
28
  end
28
29
  end
29
30
 
30
31
  def set_slug
31
- options = self.class.slug_options
32
-
32
+ klass = self.class
33
+ while klass.respond_to?(:single_collection_parent)
34
+ superclass = klass.single_collection_parent
35
+ if superclass && superclass.respond_to?(:slug_options)
36
+ klass = superclass
37
+ else
38
+ break
39
+ end
40
+ end
41
+
42
+ options = klass.slug_options
43
+
33
44
  if options[:always_update] == false
34
- return unless self.send(options[:key]).blank?
45
+ return unless self.send(options[:key]).blank?
35
46
  end
36
47
 
37
48
  to_slug = self[options[:to_slug]]
@@ -52,32 +63,40 @@ module MongoMapper
52
63
  conds[options[:scope]] = self.send(options[:scope]) if options[:scope]
53
64
 
54
65
  # first see if there is a equal slug
55
- used_slugs = self.class.where(conds)
66
+ used_slugs = klass.where(conds).fields(options[:key])
56
67
  if (used_slugs.count > 0)
57
68
  last_digit = 0 # zero for last one...
58
69
  # if we are updating, check if the current slug is same as the one we want
59
70
  conds[options[:key]] = /(#{the_slug}-\d+)/
60
- used_slugs = self.class.where(conds).sort(options[:key].asc)
61
- new_slug_set = false
71
+ used_slugs = klass.where(conds).fields(options[:key])
72
+ used_slugs_array = Array.new
62
73
  used_slugs.each do |used_slug|
63
- # get the last digit through regex
64
- next_digit = used_slug.send(options[:key])[/(\d+)$/]
65
- if (!next_digit.nil?)
66
- # catch any numbers that are in between and free
67
- if ((next_digit.to_i - last_digit.to_i) > 1)
68
- the_slug = "#{raw_slug}-#{last_digit+1}"
69
- new_slug_set = true
70
- break # set a new slug, so all is good
74
+ used_slugs_array << used_slug.send(options[:key])[/(\d+)$/].to_i
75
+ end
76
+ used_slugs_array.sort!
77
+
78
+ if used_slugs_array.length <= 0
79
+ next_digit = options[:start]
80
+ else
81
+ prev_num = used_slugs_array.shift
82
+ if used_slugs_array.length == 0
83
+ next_digit = prev_num + 1
84
+ else
85
+ used_slugs_array.each do |slug_num|
86
+ if ((slug_num - prev_num) > 1)
87
+ next_digit = prev_num + 1
88
+ break
89
+ end
90
+ next_digit = slug_num + 1
91
+ prev_num = slug_num
71
92
  end
72
- last_digit = next_digit.to_i
73
- # puts last_digit.inspect
74
93
  end
75
94
  end
76
- the_slug = "#{raw_slug}-#{last_digit+1}" if new_slug_set == false
95
+ the_slug = "#{raw_slug}-#{next_digit}"
77
96
  end
78
97
 
79
98
  self.send(:"#{options[:key]}=", the_slug)
80
99
  end
81
100
  end
82
101
  end
83
- end
102
+ end
@@ -1,3 +1,3 @@
1
1
  module Sluggable
2
- VERSION = '0.2.5'
2
+ VERSION = '0.2.6'
3
3
  end
metadata CHANGED
@@ -1,46 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: leifcr-mm-sluggable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
5
- prerelease:
4
+ version: 0.2.6
6
5
  platform: ruby
7
6
  authors:
8
7
  - Leif Ringstad
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-09-17 00:00:00.000000000 Z
11
+ date: 2014-03-26 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: mongo_mapper
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
- version: 0.12.0
19
+ version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
- version: 0.12.0
26
+ version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rspec
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  description:
@@ -55,29 +50,28 @@ files:
55
50
  - Gemfile
56
51
  - Rakefile
57
52
  - LICENSE
58
- - README.rdoc
53
+ - README.md
59
54
  homepage: http://github.com/leifcr/mm-sluggable
60
55
  licenses: []
56
+ metadata: {}
61
57
  post_install_message:
62
58
  rdoc_options: []
63
59
  require_paths:
64
60
  - lib
65
61
  required_ruby_version: !ruby/object:Gem::Requirement
66
- none: false
67
62
  requirements:
68
- - - ! '>='
63
+ - - '>='
69
64
  - !ruby/object:Gem::Version
70
65
  version: '0'
71
66
  required_rubygems_version: !ruby/object:Gem::Requirement
72
- none: false
73
67
  requirements:
74
- - - ! '>='
68
+ - - '>='
75
69
  - !ruby/object:Gem::Version
76
70
  version: '0'
77
71
  requirements: []
78
72
  rubyforge_project:
79
- rubygems_version: 1.8.24
73
+ rubygems_version: 2.1.4
80
74
  signing_key:
81
- specification_version: 3
75
+ specification_version: 4
82
76
  summary: MongoMapper Plugin to store a slugged version of of a field.
83
77
  test_files: []