has_unique_slug 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
1
  # Has Unique Slug
2
+ [![Build Status](https://secure.travis-ci.org/HuffMoody/has_unique_slug.png)](http://travis-ci.org/HuffMoody/has_unique_slug)
3
+
2
4
  Generates a unique slug for use as a drop-in replacement for ids Ruby on Rails Active Record
3
5
 
4
6
  ## Install
@@ -57,9 +59,27 @@ All the standard url helper methods will still work since `to_param` is overrid
57
59
 
58
60
  If you are adding this to an existing project that already contains records, perform the necessary setup then run something like `Post.all.each {|p| p.save }` in the rails console to populate the slug columns.
59
61
 
62
+ ## Active Admin
63
+
64
+ Due to the way inherited resources work, this gem will not work with Active Admin out of the box. You can easily get around this however by adding the following line to your resource admin file:
65
+
66
+ controller do
67
+ defaults :finder => :find_by_slug
68
+ end
69
+
60
70
  ## TODO:
61
71
 
62
72
  - Add support for scopes
63
73
  - Add support for database versioning
64
74
  - Consider optimizing the method to ensure a unique slug
65
- - Add rake task for rebuildings slugs
75
+ - Add rake task for rebuildings slugs
76
+
77
+ ## License
78
+
79
+ Copyright (c) 2012 Brendan Stennett
80
+
81
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
82
+
83
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
84
+
85
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile CHANGED
@@ -1 +1,6 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -18,10 +18,9 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- # specify any dependencies here; for example:
22
21
  s.add_development_dependency "rspec"
23
22
  s.add_development_dependency "activerecord"
24
23
  s.add_development_dependency "sqlite3"
25
24
  s.add_development_dependency "rake"
26
- # s.add_runtime_dependency "rest-client"
25
+
27
26
  end
@@ -1,3 +1,3 @@
1
1
  module HasUniqueSlug
2
- VERSION = "0.1.6"
2
+ VERSION = "0.1.7"
3
3
  end
@@ -8,7 +8,12 @@ module HasUniqueSlug
8
8
  # Builds a slug from the subject_column unless a block is specified.
9
9
  # If a block is specified, the result of the block is returned.
10
10
  def build_slug(record, subject_column)
11
- ( subject_column.is_a?(Proc) ? subject_column.call(record) : record[subject_column] ).parameterize
11
+ if subject_column.is_a?(Proc)
12
+ slug = subject_column.call(record)
13
+ else
14
+ slug = record[subject_column]
15
+ end
16
+ slug.try(:parameterize) || '-'
12
17
  end
13
18
 
14
19
  module ClassMethods
@@ -58,13 +63,6 @@ module HasUniqueSlug
58
63
  end
59
64
  EOV
60
65
 
61
- # # Add find method to override ActiveRecord::Base.find.
62
- # # Note: find_by_id will still work to search for record by their database id.
63
- # instance_eval do
64
- # def find(*args)
65
- # args.length == 1 ? where(slug_column => args.first).first : where(slug_column => args)
66
- # end
67
- # end
68
66
  end
69
67
 
70
68
  end
@@ -81,4 +79,4 @@ end
81
79
 
82
80
  class ActiveRecord::Base
83
81
  include HasUniqueSlug
84
- end
82
+ end
@@ -59,6 +59,37 @@ describe HasUniqueSlug do
59
59
  r.slug.should == "sample-record-#{i}"
60
60
  end
61
61
  end
62
+
63
+ it "should allow a slug to be changed and updated" do
64
+ r = Standard.create! :title => "Sample Record"
65
+ r.slug = "another-slug"
66
+ r.save
67
+ r.slug.should == "another-slug"
68
+ end
69
+
70
+ it "should be able to manually set a slug and ensure it is unique for new records" do
71
+ r1 = Standard.create! :title => "Sample Record", slug: "another-slug"
72
+ r1.reload
73
+ r1.slug.should == "another-slug"
74
+
75
+ r2 = Standard.create! :title => "Sample Record", slug: "another-slug"
76
+ r2.reload
77
+ r2.slug.should == "another-slug-2"
78
+ end
79
+
80
+ it "should be able to manually set a slug and ensure it is unique for existing records" do
81
+ r1 = Standard.create! :title => "Sample Record"
82
+ r1.slug = "another-slug"
83
+ r1.save
84
+ r1.reload
85
+ r1.slug.should == "another-slug"
86
+
87
+ r2 = Standard.create! :title => "Sample Record"
88
+ r2.slug = "another-slug"
89
+ r2.save
90
+ r2.reload
91
+ r2.slug.should == "another-slug-2"
92
+ end
62
93
 
63
94
  it "should not increment the slug if the duplicate is itself" do
64
95
  r = Standard.create! :title => "Sample Record"
@@ -108,5 +139,10 @@ describe HasUniqueSlug do
108
139
  r = StandardWithScope.create! :title => "Sample Record", :some_scope => 1
109
140
  r.slug.should == "sample-record-2"
110
141
  end
142
+
143
+ it "will not raise an exception if subject column is blank" do
144
+ r = Standard.create! :title => nil
145
+ expect { r.valid? }.to_not raise_error
146
+ end
111
147
 
112
- end
148
+ end
data/spec/spec_helper.rb CHANGED
@@ -5,7 +5,7 @@ require 'active_record'
5
5
  gem 'sqlite3'
6
6
  require 'sqlite3'
7
7
 
8
- require 'has_unique_slug' # and any other gems you need
8
+ require 'has_unique_slug'
9
9
 
10
10
  RSpec.configure do |config|
11
11
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_unique_slug
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-28 00:00:00.000000000Z
12
+ date: 2013-06-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70256493008000 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70256493008000
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: activerecord
27
- requirement: &70256493007320 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '0'
33
38
  type: :development
34
39
  prerelease: false
35
- version_requirements: *70256493007320
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: sqlite3
38
- requirement: &70256493006540 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: '0'
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *70256493006540
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: rake
49
- requirement: &70256493005660 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ! '>='
@@ -54,7 +69,12 @@ dependencies:
54
69
  version: '0'
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *70256493005660
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
58
78
  description: Generates a unique slug for use as a drop-in replacement for ids.
59
79
  email:
60
80
  - brendan@unknowncollective.com
@@ -86,7 +106,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
86
106
  version: '0'
87
107
  segments:
88
108
  - 0
89
- hash: -4480308170059512738
109
+ hash: 2032112713944694544
90
110
  required_rubygems_version: !ruby/object:Gem::Requirement
91
111
  none: false
92
112
  requirements:
@@ -95,10 +115,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
115
  version: '0'
96
116
  segments:
97
117
  - 0
98
- hash: -4480308170059512738
118
+ hash: 2032112713944694544
99
119
  requirements: []
100
120
  rubyforge_project: has_unique_slug
101
- rubygems_version: 1.8.10
121
+ rubygems_version: 1.8.23
102
122
  signing_key:
103
123
  specification_version: 3
104
124
  summary: Generates a unique slug for use as a drop-in replacement for ids.