has_unique_slug 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in has_unique_slug.gemspec
4
4
  gemspec
5
+
6
+ gem 'activesupport', '> 3.0.0'
data/README.md CHANGED
@@ -1,7 +1,14 @@
1
1
  # Has Unique Slug
2
2
  [![Build Status](https://secure.travis-ci.org/HuffMoody/has_unique_slug.png)](http://travis-ci.org/HuffMoody/has_unique_slug)
3
3
 
4
- Generates a unique slug for use as a drop-in replacement for ids Ruby on Rails Active Record
4
+ Generates a unique slug for use as a drop-in replacement for ids Ruby on Rails Active Record. It allows for easy search
5
+ engine friendly URLs.
6
+
7
+ http://example.com/articles/1
8
+
9
+ Becomes
10
+
11
+ http://example.com/articles/my-article
5
12
 
6
13
  ## Install
7
14
 
@@ -16,9 +23,11 @@ Tested and working on Rails 3.1.x
16
23
  end
17
24
 
18
25
  - by default, the column `title` is assumed as the identifier, and `slug` is used to store the unique slug
19
- - `title.paramterize` is called to generate the slug unless a slug has already been assigned, in which case parameterize is called on the provided slug
26
+ - `title.paramterize` is called to generate the slug unless a slug has already been manually assigned, in which case #parameterize is called on the provided slug to ensure it is url friendly
20
27
  - if a slug is not unique in the database, a suffix is appended on the end in the format "-n" where n starts at 2 and progresses ad infinitum until a unique slug is found.
21
28
 
29
+ ## Options
30
+
22
31
  You can specify which column to use to generate the slug and which column to use to store the slug. For example:
23
32
 
24
33
  class Post < ActiveRecord::Base
@@ -29,12 +38,22 @@ You can specify which column to use to generate the slug and which column to use
29
38
  Optionally, a Proc can be used instead of a column name to create the slug:
30
39
 
31
40
  class Car < ActiveRecord::Base
32
- has_unique_slug :subject => Proc.new {|car| "#{car.year}-#{car.name}"}
41
+ has_unique_slug :subject => Proc.new {|car| "#{car.year} #{car.name}"}
33
42
  end
34
- You do not have to call parameterize on name, this will be done automatically. (You don't even need to add dash, a space will work fine)
43
+ Note: You do not have to call parameterize on the output of the proc, this will be done automatically.
44
+
45
+ ### Usage with scopes
35
46
 
47
+ Scopes are supported when generating the slug column where each slug will be unique for a given scope. To use with scopes,
48
+ simply specify the option on setup
36
49
 
37
- Use the rails built-in convention for finding by an attribute.
50
+ class Article < ActiveRecord::Base
51
+ has_unique_slug scope: :category_id
52
+ end
53
+
54
+ ### Finders
55
+
56
+ Use rails' built-in conventions for finding by an attribute.
38
57
 
39
58
  Note: you will have to modify this if your slug column is set to something else.
40
59
 
@@ -43,8 +62,7 @@ Note: you will have to modify this if your slug column is set to something else.
43
62
  # ...
44
63
 
45
64
  def show
46
- @post = Post.find_by_slug params[:id]
47
- # you may still use find_by_id to find a record by the database id if need be
65
+ @post = Post.find_by_slug(params[:id])
48
66
  end
49
67
  end
50
68
 
@@ -61,15 +79,20 @@ If you are adding this to an existing project that already contains records, per
61
79
 
62
80
  ## Active Admin
63
81
 
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:
82
+ Due to the way inherited resources work (a dependancy of activeadmin), 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's admin file:
65
83
 
66
- controller do
67
- defaults :finder => :find_by_slug
84
+ # articles.rb
85
+
86
+ ActiveAdmin.register Article do
87
+ controller do
88
+ defaults :finder => :find_by_slug
89
+ end
90
+
91
+ # ..
68
92
  end
69
93
 
70
94
  ## TODO:
71
95
 
72
- - Add support for scopes
73
96
  - Add support for database versioning
74
97
  - Consider optimizing the method to ensure a unique slug
75
98
  - Add rake task for rebuildings slugs
@@ -19,7 +19,7 @@ Gem::Specification.new do |s|
19
19
  s.require_paths = ["lib"]
20
20
 
21
21
  s.add_development_dependency "rspec"
22
- s.add_development_dependency "activerecord"
22
+ s.add_development_dependency "activerecord", '>= 4.0.0'
23
23
  s.add_development_dependency "sqlite3"
24
24
  s.add_development_dependency "rake"
25
25
 
@@ -1,3 +1,3 @@
1
1
  module HasUniqueSlug
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  end
@@ -35,7 +35,7 @@ module HasUniqueSlug
35
35
 
36
36
  # Ensure the current slug is unique in the database, if not, make it unqiue
37
37
  test_slug, i = slug_prefix, 1
38
- record_scope = record.new_record? ? record.class.scoped : record.class.where("id != ?", record.id)
38
+ record_scope = record.new_record? ? record.class.all : record.class.where("id != ?", record.id)
39
39
 
40
40
  # Add scope to uniqueness call
41
41
  unless scope_column.nil?
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.7
4
+ version: 0.1.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-18 00:00:00.000000000 Z
12
+ date: 2013-07-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -34,7 +34,7 @@ dependencies:
34
34
  requirements:
35
35
  - - ! '>='
36
36
  - !ruby/object:Gem::Version
37
- version: '0'
37
+ version: 4.0.0
38
38
  type: :development
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,7 +42,7 @@ dependencies:
42
42
  requirements:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
- version: '0'
45
+ version: 4.0.0
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: sqlite3
48
48
  requirement: !ruby/object:Gem::Requirement
@@ -106,7 +106,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
106
106
  version: '0'
107
107
  segments:
108
108
  - 0
109
- hash: 2032112713944694544
109
+ hash: 3450348919392366997
110
110
  required_rubygems_version: !ruby/object:Gem::Requirement
111
111
  none: false
112
112
  requirements:
@@ -115,7 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
115
  version: '0'
116
116
  segments:
117
117
  - 0
118
- hash: 2032112713944694544
118
+ hash: 3450348919392366997
119
119
  requirements: []
120
120
  rubyforge_project: has_unique_slug
121
121
  rubygems_version: 1.8.23