urls_for_humans 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +57 -7
- data/Rakefile +11 -0
- data/lib/urls_for_humans/version.rb +1 -1
- data/lib/urls_for_humans.rb +15 -22
- data/spec/spec_helper.rb +28 -0
- data/spec/urls_for_humans_spec.rb +56 -0
- data/urls_for_humans.gemspec +3 -0
- metadata +34 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 781ea4d78f5f63c5f05cd5cf429a9e22cd4ad95a
|
4
|
+
data.tar.gz: 5c62c5b45cdb175e6d21bffc34f8d6e93c33bbde
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e450763c2eec2fcb6a3a3f3b2c46e1835275292af89eb2db66ff9b07233741758cc3c8352b354aaa356cb9c24feb749e0af22bf3caa79d2c363ae3634ce05140
|
7
|
+
data.tar.gz: 7ed304d075f09ff68290b7d81971176037a48951bba78309a3f790c7726bae0216995d42f3fc7ce3cfa3e3426583b68b44a0e16405cae9af18ba3823cb401afd
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,28 +1,78 @@
|
|
1
|
-
#
|
1
|
+
# Urls For Humans
|
2
2
|
|
3
|
-
|
3
|
+
Urls For Humans allows you to apply meaningful names to your Rails Application's urls by leveraging what happens under the covers with `Model.find(params[:id])` and `to_param`. So long as the url is prefixed with the model's id, the lookup will happen exactly how we intend it to with a few key benefits:
|
4
|
+
|
5
|
+
* Simplicity
|
6
|
+
* Lightweight
|
7
|
+
* Persistent urls because changes the the latter portions of a param won't affect it's lookup.
|
4
8
|
|
5
9
|
## Installation
|
6
10
|
|
7
11
|
Add this line to your application's Gemfile:
|
8
12
|
|
9
|
-
|
13
|
+
```ruby
|
14
|
+
gem 'urls_for_humans'
|
15
|
+
```
|
10
16
|
|
11
17
|
And then execute:
|
12
18
|
|
13
|
-
|
19
|
+
```bash
|
20
|
+
$ bundle
|
21
|
+
```
|
14
22
|
|
15
23
|
Or install it yourself as:
|
16
24
|
|
17
|
-
|
25
|
+
```bash
|
26
|
+
$ gem install urls_for_humans
|
27
|
+
```
|
18
28
|
|
19
29
|
## Usage
|
20
30
|
|
21
|
-
|
31
|
+
|
32
|
+
To use Urls For Humans you need to extend the `UrlsForHumans` module, and call the class method `urls_for_humans`:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
class User < ActiveRecord::Base
|
36
|
+
extend UrlsForHumans
|
37
|
+
|
38
|
+
# ...
|
39
|
+
|
40
|
+
urls_for_humans :first_name, :last_name
|
41
|
+
|
42
|
+
# ...
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
The `urls_for_humans` method can be a collection of any information that you'd like to include in the url. For example, with the above class we'd result in:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
u = User.create(first_name: 'John', last_name: 'Otander')
|
50
|
+
|
51
|
+
u.to_param
|
52
|
+
# => '1-john-otander'
|
53
|
+
|
54
|
+
u.first_name = nil
|
55
|
+
u.to_param
|
56
|
+
# => '1-otander'
|
57
|
+
```
|
58
|
+
|
59
|
+
With this solution, an ActiveRecord object will always produce the correct url throughout the application:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
link_to user.first_name, user
|
63
|
+
# => <a href="http://localhost:3000/users/1-john-otander"
|
64
|
+
```
|
65
|
+
|
66
|
+
Additionally, any link that hits the internet will persist because `1-random-content`, `1-other-random-content`, and `1-john-doe` will all route to the same resource.
|
67
|
+
|
68
|
+
## Resources
|
69
|
+
|
70
|
+
* <https://github.com/norman/friendly_id>
|
71
|
+
* <https://gist.github.com/cdmwebs/1209732>
|
22
72
|
|
23
73
|
## Contributing
|
24
74
|
|
25
|
-
1. Fork it ( http://github.com
|
75
|
+
1. Fork it ( http://github.com/johnotander/urls_for_humans/fork )
|
26
76
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
77
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
78
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/Rakefile
CHANGED
data/lib/urls_for_humans.rb
CHANGED
@@ -1,30 +1,23 @@
|
|
1
|
-
require
|
1
|
+
require 'urls_for_humans/version'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
model_class.class_eval do
|
8
|
-
class << self
|
9
|
-
attr_accessor :humanly_attrs
|
10
|
-
end
|
11
|
-
|
12
|
-
extend Base
|
13
|
-
include Model
|
14
|
-
end
|
15
|
-
end
|
3
|
+
require 'active_support/concern'
|
4
|
+
require 'active_support/inflector'
|
5
|
+
require 'active_support/core_ext/object/blank'
|
16
6
|
|
17
|
-
|
7
|
+
module UrlsForHumans
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
attr_accessor :humanly_attrs
|
12
|
+
|
18
13
|
def urls_for_humans(*humanly_attrs)
|
19
14
|
@humanly_attrs = humanly_attrs
|
20
15
|
end
|
21
16
|
end
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
end.reject(&:blank?).join('-')
|
28
|
-
end
|
17
|
+
|
18
|
+
def to_param
|
19
|
+
(self.class.humanly_attrs || humanly_attrs).dup.unshift(:id).map do |attrib|
|
20
|
+
send(attrib).to_s.parameterize
|
21
|
+
end.reject(&:blank?).join('-')
|
29
22
|
end
|
30
23
|
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require 'rspec'
|
4
|
+
|
5
|
+
require 'urls_for_humans'
|
6
|
+
|
7
|
+
I18n.enforce_available_locales = false
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.color_enabled = true
|
11
|
+
end
|
12
|
+
|
13
|
+
class User
|
14
|
+
include UrlsForHumans
|
15
|
+
|
16
|
+
urls_for_humans :first_name, :last_name
|
17
|
+
|
18
|
+
attr_accessor :first_name, :last_name
|
19
|
+
|
20
|
+
def initialize(first_name, last_name)
|
21
|
+
self.first_name = first_name
|
22
|
+
self.last_name = last_name
|
23
|
+
end
|
24
|
+
|
25
|
+
def id
|
26
|
+
10
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe UrlsForHumans do
|
4
|
+
|
5
|
+
let(:first) { 'John' }
|
6
|
+
let(:last) { 'Doe' }
|
7
|
+
|
8
|
+
let(:user) { User.new(first, last) }
|
9
|
+
|
10
|
+
subject { user }
|
11
|
+
|
12
|
+
describe '.to_param' do
|
13
|
+
|
14
|
+
it 'should return the correct string' do
|
15
|
+
expect(subject.to_param).to eq("#{ user.id }-#{ user.first_name.parameterize }-#{ user.last_name.parameterize }")
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'with no first name' do
|
19
|
+
|
20
|
+
let(:user) { User.new(nil, last) }
|
21
|
+
|
22
|
+
it 'should return the correct string' do
|
23
|
+
expect(subject.to_param).to eq("#{ user.id }-#{ user.last_name.parameterize }")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'with no last name' do
|
28
|
+
|
29
|
+
let(:user) { User.new(first, nil) }
|
30
|
+
|
31
|
+
it 'should return the correct string' do
|
32
|
+
expect(subject.to_param).to eq("#{ user.id }-#{ user.first_name.parameterize }")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'with no first or last name' do
|
37
|
+
|
38
|
+
let(:user) { User.new(nil, nil) }
|
39
|
+
|
40
|
+
it 'should return the correct string' do
|
41
|
+
expect(subject.to_param).to eq(user.id.to_s)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '.urls_for_humans and @humanly_attrs' do
|
47
|
+
|
48
|
+
let(:attrs) { [:fake, :attrs] }
|
49
|
+
|
50
|
+
before { User.urls_for_humans(*attrs) }
|
51
|
+
|
52
|
+
it 'should return the correct attributes' do
|
53
|
+
expect(User.humanly_attrs).to eq(attrs)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/urls_for_humans.gemspec
CHANGED
@@ -18,6 +18,9 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
+
spec.add_dependency "activesupport"
|
22
|
+
|
21
23
|
spec.add_development_dependency "bundler", "~> 1.5"
|
22
24
|
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec"
|
23
26
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Otander
|
@@ -10,6 +10,20 @@ bindir: bin
|
|
10
10
|
cert_chain: []
|
11
11
|
date: 2014-04-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,6 +52,20 @@ dependencies:
|
|
38
52
|
- - ">="
|
39
53
|
- !ruby/object:Gem::Version
|
40
54
|
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
41
69
|
description: A delightfully simple way to make your urls friendly to end users, without
|
42
70
|
muddying up the backend.
|
43
71
|
email:
|
@@ -53,6 +81,8 @@ files:
|
|
53
81
|
- Rakefile
|
54
82
|
- lib/urls_for_humans.rb
|
55
83
|
- lib/urls_for_humans/version.rb
|
84
|
+
- spec/spec_helper.rb
|
85
|
+
- spec/urls_for_humans_spec.rb
|
56
86
|
- urls_for_humans.gemspec
|
57
87
|
homepage: https://github.com/johnotander/urls_for_humans
|
58
88
|
licenses:
|
@@ -78,4 +108,6 @@ rubygems_version: 2.2.2
|
|
78
108
|
signing_key:
|
79
109
|
specification_version: 4
|
80
110
|
summary: Urls for humans in Rails apps.
|
81
|
-
test_files:
|
111
|
+
test_files:
|
112
|
+
- spec/spec_helper.rb
|
113
|
+
- spec/urls_for_humans_spec.rb
|