urls_for_humans 0.0.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -0
- data/README.md +58 -11
- data/lib/urls_for_humans.rb +3 -3
- data/lib/urls_for_humans/version.rb +1 -1
- data/spec/spec_helper.rb +0 -4
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: adc219f6fec209e61ef474a4371f16d7787be5da
|
4
|
+
data.tar.gz: 30e269e5d75bae8b28829db3cc1facfeaf6a6269
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: feb54b5172dd50f1d673b793dd8e7121afcdab7f32ca459a742a93841083da79c427970197702edcc78f77eca04bcf6d56eb31762175a15e363794b47fa69b28
|
7
|
+
data.tar.gz: 3cd6bb0278871ddcdcad586f5b101254d88bda0764b0b5eb5b9cd3f1e305eb2e211094a800f052e8fef34ce08e9895d12e763ef9b316add8ac4969d9668ef1e6
|
data/README.md
CHANGED
@@ -1,10 +1,31 @@
|
|
1
|
-
# Urls
|
1
|
+
# Urls for Humans
|
2
2
|
|
3
|
-
|
3
|
+
[![Build Status](https://travis-ci.org/johnotander/urls_for_humans.svg?branch=master)](https://travis-ci.org/johnotander/urls_for_humans)
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
Urls for Humans is a gem that allows you to apply meaningful names to your Rails Application's urls
|
6
|
+
by leveraging what happens under the covers with `Model.find(params[:id])`, `to_i`, and `to_param`.
|
7
|
+
This makes it easy to turn `users/1` to `users/1-john-otander`. So long as the url is prefixed with
|
8
|
+
the model's `id` (which Urls for Humans ensures), the lookup will happen exactly how we intend it
|
9
|
+
to with a few key benefits:
|
10
|
+
|
11
|
+
* Simple thanks to ActiveSupport.
|
12
|
+
* Lightweight, weighing in at roughly 20 something lines of added gem code to your Rails app (since
|
13
|
+
ActiveSupport is already a dependency).
|
14
|
+
* Persistent urls because changes in the latter portions of a param won't affect it's lookup.
|
15
|
+
* There are no slugs required.
|
16
|
+
|
17
|
+
### Why use Urls for Humans in place of Friendly ID?
|
18
|
+
|
19
|
+
This is a different approach to friendly URLs than the `friendly_id` gem because it doesn't modify
|
20
|
+
the db queries themselves. The `urls_for_humans` approach essentially allows all urls fitting the
|
21
|
+
form `resource/<id>-<anything else>` to route to `resource/:id` because `to_i` is called on the
|
22
|
+
`id` parameter.
|
23
|
+
|
24
|
+
Personally, I prefer this approach because a link out there in the wild to a user's profile
|
25
|
+
`users/previous_username` isn't broken (404'd) when they change their username to `users/new_username`
|
26
|
+
because the slug has been changed.
|
27
|
+
|
28
|
+
Also, I'm biased.
|
8
29
|
|
9
30
|
## Installation
|
10
31
|
|
@@ -29,11 +50,12 @@ $ gem install urls_for_humans
|
|
29
50
|
## Usage
|
30
51
|
|
31
52
|
|
32
|
-
To use Urls For Humans you need to extend the `UrlsForHumans` module, and call the class method
|
53
|
+
To use Urls For Humans you need to extend the `UrlsForHumans` module, and call the class method
|
54
|
+
`urls_for_humans`:
|
33
55
|
|
34
56
|
```ruby
|
35
57
|
class User < ActiveRecord::Base
|
36
|
-
|
58
|
+
include UrlsForHumans
|
37
59
|
|
38
60
|
# ...
|
39
61
|
|
@@ -43,7 +65,8 @@ class User < ActiveRecord::Base
|
|
43
65
|
end
|
44
66
|
```
|
45
67
|
|
46
|
-
The `urls_for_humans` method can be a collection of any information that you'd like to include in
|
68
|
+
The `urls_for_humans` method can be a collection of any information that you'd like to include in
|
69
|
+
the url. For example, with the above class we'd result in:
|
47
70
|
|
48
71
|
```ruby
|
49
72
|
u = User.create(first_name: 'John', last_name: 'Otander')
|
@@ -56,14 +79,36 @@ u.to_param
|
|
56
79
|
# => '1-otander'
|
57
80
|
```
|
58
81
|
|
59
|
-
With this solution, an ActiveRecord object will always produce the correct url throughout the
|
82
|
+
With this solution, an ActiveRecord object will always produce the correct url throughout the
|
83
|
+
application:
|
60
84
|
|
61
85
|
```ruby
|
62
86
|
link_to user.first_name, user
|
63
|
-
# => <a href="http://localhost:3000/users/1-john-otander"
|
87
|
+
# => <a href="http://localhost:3000/users/1-john-otander"
|
64
88
|
```
|
65
89
|
|
66
|
-
Additionally, any link that hits the internet will persist because `1-random-content`, `1-other-random-content`,
|
90
|
+
Additionally, any link that hits the internet will persist because `1-random-content`, `1-other-random-content`,
|
91
|
+
and `1-john-doe` will all route to the same resource.
|
92
|
+
|
93
|
+
### I don't like it when you leverage executable class bodies
|
94
|
+
|
95
|
+
That's fine. You can add a method to your model, instead.
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
class User < ActiveRecord::Base
|
99
|
+
include UrlsForHumans
|
100
|
+
|
101
|
+
# ...
|
102
|
+
|
103
|
+
def humanly_attrs
|
104
|
+
[:first_name, :last_name, :favorite_food]
|
105
|
+
end
|
106
|
+
|
107
|
+
# ...
|
108
|
+
end
|
109
|
+
```
|
110
|
+
|
111
|
+
This will result in `"#{ id }-#{ first_name }-#{ last_name }-#{ favorite_food }"`. Yay.
|
67
112
|
|
68
113
|
## Resources
|
69
114
|
|
@@ -77,3 +122,5 @@ Additionally, any link that hits the internet will persist because `1-random-con
|
|
77
122
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
78
123
|
4. Push to the branch (`git push origin my-new-feature`)
|
79
124
|
5. Create new Pull Request
|
125
|
+
|
126
|
+
Crafted with <3 by [John Otander](http://johnotander.com) ([@4lpine](https://twitter.com/4lpine)).
|
data/lib/urls_for_humans.rb
CHANGED
@@ -6,15 +6,15 @@ require 'active_support/core_ext/object/blank'
|
|
6
6
|
|
7
7
|
module UrlsForHumans
|
8
8
|
extend ActiveSupport::Concern
|
9
|
-
|
9
|
+
|
10
10
|
module ClassMethods
|
11
11
|
attr_accessor :humanly_attrs
|
12
|
-
|
12
|
+
|
13
13
|
def urls_for_humans(*humanly_attrs)
|
14
14
|
@humanly_attrs = humanly_attrs
|
15
15
|
end
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
def to_param
|
19
19
|
(self.class.humanly_attrs || humanly_attrs).dup.unshift(:id).map do |attrib|
|
20
20
|
send(attrib).to_s.parameterize
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: urls_for_humans
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Otander
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -75,6 +75,7 @@ extensions: []
|
|
75
75
|
extra_rdoc_files: []
|
76
76
|
files:
|
77
77
|
- ".gitignore"
|
78
|
+
- ".travis.yml"
|
78
79
|
- Gemfile
|
79
80
|
- LICENSE.txt
|
80
81
|
- README.md
|
@@ -104,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
104
105
|
version: '0'
|
105
106
|
requirements: []
|
106
107
|
rubyforge_project:
|
107
|
-
rubygems_version: 2.
|
108
|
+
rubygems_version: 2.4.3
|
108
109
|
signing_key:
|
109
110
|
specification_version: 4
|
110
111
|
summary: Urls for humans in Rails apps.
|