has_unique_slug 0.1.2 → 0.1.3

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.
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ has_unique_slug/*
data/README.md CHANGED
@@ -1,9 +1,11 @@
1
- # HasUniqueSlug
1
+ # Has Unique Slug
2
2
  Generates a unique slug for use as a drop-in replacement for ids Ruby on Rails Active Record
3
3
 
4
4
  ## Install
5
5
 
6
- Add `gem has_unique_slug` to your Gemfile and run `bundle install` to get it installed.
6
+ Add `gem 'has_unique_slug'` to your Gemfile and run `bundle install` to get it installed.
7
+
8
+ Tested and working on Rails 3.1.x
7
9
 
8
10
  ## Usage
9
11
 
@@ -14,18 +16,22 @@ Assume you have a Post model that has a title and slug column, you can use the f
14
16
  end
15
17
 
16
18
 
17
- A unique slug will be generated automatically on creation.
18
- If the generated slug is not unique, a number is added onto the end to endure uniqueness. The series starts at 2 and increments
19
- up by one until a unique slug is found.
20
- If a slug is already specified, this slug will be used however the above rules still apply for uniqueness.
19
+ A unique slug will be generated automatically on creation by calling parameterize on title.
20
+ If the generated slug is not unique, a number is added onto the end to ensure uniqueness. The series starts at 2 and increments up by one until a unique slug is found.
21
+ If a slug is already specified, this slug will be used however the above rules still apply for incrementing the slug until a unique one is found.
22
+ Ex. Post 1 has title "Sample Post" which would then generate slug "sample-post"
23
+ Post 2 has also has title "Sample Post" which then would generate slug "sample-post-2"
21
24
 
22
25
  You can specify which column to use to generate the slug and which column to use to store the slug. Below is the default:
23
26
 
24
27
  class Post < ActiveRecord::Base
28
+ # the column slug will store the slug, title.parameterize will be called to build the slug
25
29
  has_unique_slug :slug, :title
26
30
  end
27
31
 
28
- Or if only 1 argument is given, use that column to store the slug:
32
+ The entire argument list is `has_unique_slug(slug_column, title_column, options, &block)` however there are no options you can pass in at this time.
33
+
34
+ If only 1 argument is given, use that column to store the slug:
29
35
 
30
36
  class Post < ActiveRecord::Base
31
37
  has_unique_slug :permalink # Uses the permalink column to store the slug
@@ -38,6 +44,21 @@ Optionally, a block can be provided to generate the slug:
38
44
  end
39
45
  Note the space: parameterize will be called on the result of the block to ensure the slug is url friendly.
40
46
 
47
+ You do not have to modify your controller to get this to work:
48
+
49
+ class PostsController < ApplicationController
50
+
51
+ # ...
52
+
53
+ def show
54
+ @post = Post.find params[:id]
55
+ # you may still use find_by_id to find a record by the database id if need be
56
+ end
57
+ end
58
+
59
+ Then you may use all your standard url helpers as normal.
60
+ Ex. If a `post` has a title "Sample Post" and a slug "sample-post", the helper `post_path(post)` will create /posts/sample-post
61
+
41
62
  ## TODO:
42
63
 
43
64
  - Would like to write some tests.
@@ -1,3 +1,3 @@
1
1
  module HasUniqueSlug
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -25,7 +25,7 @@ module HasUniqueSlug
25
25
  before_save do |record|
26
26
 
27
27
  # Add a slug if slug doesn't exist
28
- slug_prefix = record[slug_column].blank? ? build_slug(record, subject_column, &block) : record[slug_column]
28
+ slug_prefix = record[slug_column].blank? ? build_slug(record, subject_column, &block) : record[slug_column].parameterize
29
29
 
30
30
  # Ensure the current slug is unique in the database, if not, make it unqiue
31
31
  test_slug, i = slug_prefix, 1
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_unique_slug
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Brendan Stennett