inferred_slug 1.0.0
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 +1 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +33 -0
- data/LICENSE +20 -0
- data/README.rdoc +84 -0
- data/inferred_slug.gemspec +16 -0
- data/lib/generators/inferred_slug/install/install_generator.rb +17 -0
- data/lib/generators/inferred_slug/install/templates/slug.rb +12 -0
- data/lib/inferred_slug/finders.rb +23 -0
- data/lib/inferred_slug/slug.rb +19 -0
- data/lib/inferred_slug/version.rb +3 -0
- data/lib/inferred_slug.rb +2 -0
- metadata +89 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
inferred_slug (0.0.7)
|
5
|
+
activerecord
|
6
|
+
stringex
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: http://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activemodel (3.2.3)
|
12
|
+
activesupport (= 3.2.3)
|
13
|
+
builder (~> 3.0.0)
|
14
|
+
activerecord (3.2.3)
|
15
|
+
activemodel (= 3.2.3)
|
16
|
+
activesupport (= 3.2.3)
|
17
|
+
arel (~> 3.0.2)
|
18
|
+
tzinfo (~> 0.3.29)
|
19
|
+
activesupport (3.2.3)
|
20
|
+
i18n (~> 0.6)
|
21
|
+
multi_json (~> 1.0)
|
22
|
+
arel (3.0.2)
|
23
|
+
builder (3.0.0)
|
24
|
+
i18n (0.6.0)
|
25
|
+
multi_json (1.3.5)
|
26
|
+
stringex (1.4.0)
|
27
|
+
tzinfo (0.3.33)
|
28
|
+
|
29
|
+
PLATFORMS
|
30
|
+
ruby
|
31
|
+
|
32
|
+
DEPENDENCIES
|
33
|
+
inferred_slug!
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Rafael Fernández López <ereslibre@ereslibre.es>
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
== What is Inferred Slug ?
|
2
|
+
|
3
|
+
Most slug engines require you to modify your database schema to save slugs in
|
4
|
+
it. The idea behind Inferred Slug is that slugs can be inferred and treated as
|
5
|
+
logic, instead of being treated as data.
|
6
|
+
|
7
|
+
This gem is compatible with the rest of slug engines, as far as I know. If you
|
8
|
+
find any incompatibility, I'd be glad to hear about it, so I can fix it.
|
9
|
+
|
10
|
+
== Why yet another slug engine ?
|
11
|
+
|
12
|
+
I started to search for gems providing slug support. I have to say there are
|
13
|
+
really great slug engines out there. However, I couldn't find any slug engine
|
14
|
+
able to work without a database. So I implemented it. Is dead simple.
|
15
|
+
|
16
|
+
== Advantages
|
17
|
+
|
18
|
+
1. Avoid duplicated data in your database. You need to take care of the slugs
|
19
|
+
in the case they aren't permanent, by installing ActiveRecord callbacks for
|
20
|
+
updating it. Forgetting installing a callback could mean that you have
|
21
|
+
inconsistent and misleading information (e.g. '/users/23-john-doee' could
|
22
|
+
actually show user 'John Doe' because he updated his profile to fix the typo).
|
23
|
+
|
24
|
+
2. Prettify URL's.
|
25
|
+
|
26
|
+
3. Secure your ID's. Avoid the typical random script that will just increase an
|
27
|
+
ID, and will find out random information about your users/clients.
|
28
|
+
|
29
|
+
4. Save space in database. While the database grows, slugs are also taking its
|
30
|
+
part in consuming space.
|
31
|
+
|
32
|
+
5. Compatible with other slug engines. You can still use friendly_id wherever
|
33
|
+
you want (or any other slug engine). You can decide on which models you want
|
34
|
+
to use Inferred Slugs, and avoid saving slugs of that particular model in the
|
35
|
+
database.
|
36
|
+
|
37
|
+
== Disadvantages
|
38
|
+
|
39
|
+
1. You cannot set permalinks with this slugs. Since the slug itself is inferred
|
40
|
+
from the data of the record, you cannot relay on that (unless that field is
|
41
|
+
immutable by design on your application) to provide permalinks.
|
42
|
+
|
43
|
+
2. The ID is shown. Resources on the location bar is of the form '3-test-foo'.
|
44
|
+
Given the design, there is no way of removing it.
|
45
|
+
|
46
|
+
== Installing
|
47
|
+
|
48
|
+
1. Add 'inferred_slug' to your Gemfile.
|
49
|
+
2. Optional: run 'rails g inferred_slug:install'
|
50
|
+
|
51
|
+
The optional step will include slugs on all your models automatically, by
|
52
|
+
installing an initializer that adds slugs to ActiveRecord::Base, superclass of
|
53
|
+
all your models. Thus, all your models magically have slugs.
|
54
|
+
|
55
|
+
If you want to specify slugs on a per-model basis, you can skip step 2 (or
|
56
|
+
delete the config/initializers/slug.rb file if you already ran it), and
|
57
|
+
explicitly include InferredSlug::Slug on the models that you want to have slugs.
|
58
|
+
|
59
|
+
== How it works
|
60
|
+
|
61
|
+
Right now is very simple. It will check if your model contains a 'name' column,
|
62
|
+
and in that case, the resource will automatically be called '#{id}-#{name}'. Now
|
63
|
+
let's suppose you have a Post model that instead of name has a title column, and
|
64
|
+
you want to use that as the slug:
|
65
|
+
|
66
|
+
class Post < ActiveRecord::Base
|
67
|
+
include InferredSlug::Slug # Only necessary if the initializer isn't installed
|
68
|
+
|
69
|
+
def slug
|
70
|
+
"#{id}-#{title}"
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
== Working with it
|
75
|
+
|
76
|
+
You have now two methods that will allow you to search by slugs:
|
77
|
+
|
78
|
+
1. find_by_slug
|
79
|
+
2. find_by_slug!
|
80
|
+
|
81
|
+
They are finder methods, so they are also in collections. Examples:
|
82
|
+
|
83
|
+
1. Client.find_by_slug '1-john-doe'
|
84
|
+
2. Client.first.users.find_by_slug '54-alice-smith'
|
@@ -0,0 +1,16 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
|
2
|
+
require 'inferred_slug/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'inferred_slug'
|
6
|
+
s.version = InferredSlug::VERSION
|
7
|
+
s.summary = 'Library for adding slugs on your Rails application'
|
8
|
+
s.description = 'Library for easily adding slugs on your Rails application without the need of database storage'
|
9
|
+
s.authors = ['Rafael Fernández López']
|
10
|
+
s.email = 'ereslibre@ereslibre.es'
|
11
|
+
s.files = `git ls-files`.split("\n")
|
12
|
+
s.homepage = 'https://github.com/ereslibre/inferred_slug'
|
13
|
+
|
14
|
+
s.add_dependency('stringex')
|
15
|
+
s.add_dependency('activerecord')
|
16
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
module InferredSlug
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < ::Rails::Generators::Base
|
6
|
+
|
7
|
+
desc "This generator installs inferred slugs on all your models"
|
8
|
+
source_root File.expand_path('../templates', __FILE__)
|
9
|
+
|
10
|
+
def copy_slug_hook
|
11
|
+
say_status "copying", "slug global hook", :green
|
12
|
+
copy_file "slug.rb", "config/initializers/slug.rb"
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
#
|
2
|
+
# This generator automatically adds slugs to all your models. If you want to use
|
3
|
+
# inferred slugs on individual models, delete this file and make your desired
|
4
|
+
# models include the 'InferredSlug::Slug' module. This is:
|
5
|
+
#
|
6
|
+
# class MyModel < ActiveRecord::Base
|
7
|
+
# include InferredSlug::Slug
|
8
|
+
# end
|
9
|
+
#
|
10
|
+
class ActiveRecord::Base
|
11
|
+
include InferredSlug::Slug
|
12
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module InferredSlug
|
4
|
+
module Finders
|
5
|
+
|
6
|
+
def find_by_slug(*args)
|
7
|
+
record = find(*args)
|
8
|
+
if record and record.respond_to?(:slug)
|
9
|
+
return nil unless record.slug == args.first
|
10
|
+
end
|
11
|
+
record
|
12
|
+
end
|
13
|
+
|
14
|
+
def find_by_slug!(*args)
|
15
|
+
find_by_slug(*args) or raise ActiveRecord::RecordNotFound
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class ActiveRecord::Base
|
22
|
+
extend InferredSlug::Finders
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: inferred_slug
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Rafael Fernández López
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: stringex
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activerecord
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
description: Library for easily adding slugs on your Rails application without the
|
47
|
+
need of database storage
|
48
|
+
email: ereslibre@ereslibre.es
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files: []
|
52
|
+
files:
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
55
|
+
- Gemfile.lock
|
56
|
+
- LICENSE
|
57
|
+
- README.rdoc
|
58
|
+
- inferred_slug.gemspec
|
59
|
+
- lib/generators/inferred_slug/install/install_generator.rb
|
60
|
+
- lib/generators/inferred_slug/install/templates/slug.rb
|
61
|
+
- lib/inferred_slug.rb
|
62
|
+
- lib/inferred_slug/finders.rb
|
63
|
+
- lib/inferred_slug/slug.rb
|
64
|
+
- lib/inferred_slug/version.rb
|
65
|
+
homepage: https://github.com/ereslibre/inferred_slug
|
66
|
+
licenses: []
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
requirements: []
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 1.8.21
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: Library for adding slugs on your Rails application
|
89
|
+
test_files: []
|