parameterize 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.md +45 -30
- data/lib/parameterize.rb +5 -1
- data/lib/parameterize/version.rb +1 -1
- data/parameterize.gemspec +4 -4
- data/spec/spec_helper.rb +11 -21
- metadata +17 -15
- data/Gemfile.lock +0 -44
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,51 +1,66 @@
|
|
1
1
|
# Parameterize
|
2
2
|
|
3
|
-
_The simplest permalink/slug solution for ActiveRecord 3.0._
|
3
|
+
_The simplest permalink/slug solution for ActiveRecord >= 3.0._
|
4
4
|
|
5
5
|
It uses ActiveSupport's `String#parameterize` to create the slug. There are no validations. No slug history. No extra tables or models.
|
6
6
|
|
7
7
|
## Getting Started
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
9
|
+
```ruby
|
10
|
+
# Gemfile
|
11
|
+
gem 'parameterize'
|
12
|
+
```
|
13
|
+
|
14
|
+
```
|
15
|
+
bundle install
|
16
|
+
rails generate model User name:string param:string
|
17
|
+
```
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
# models/user.rb
|
21
|
+
class User < ActiveRecord::Base
|
22
|
+
parameterize :name
|
23
|
+
end
|
24
|
+
|
25
|
+
# elsewhere
|
26
|
+
user = User.new :name => 'John Doe'
|
27
|
+
user.valid?
|
28
|
+
user.param = 'john-doe'
|
29
|
+
user.to_param = 'john-doe'
|
30
|
+
```
|
19
31
|
|
20
32
|
## Notes
|
21
33
|
|
22
34
|
Validations **are not** included. I prefer to be able to customize my validations directly in the model. The most common validations you would add are:
|
23
35
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
36
|
+
```ruby
|
37
|
+
class Post < ActiveRecord::Base
|
38
|
+
parameterize
|
39
|
+
validates_presence_of :param
|
40
|
+
validates_uniqueness_of :param
|
41
|
+
end
|
42
|
+
```
|
29
43
|
|
30
44
|
You could easily use [Babosa's](http://github.com/norman/babosa) #to_identifier or [Stringex's](http://github.com/rsl/stringex) #to_url instead of ActiveSupport's #parameterize by just aliasing the method you want to use:
|
31
45
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
46
|
+
```ruby
|
47
|
+
require 'babosa'
|
48
|
+
class String
|
49
|
+
alias :parameterize :to_identifier
|
50
|
+
end
|
51
|
+
```
|
36
52
|
|
37
53
|
There is also a Shoulda-style RSpec matcher available for use in your specs:
|
38
54
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
55
|
+
```ruby
|
56
|
+
# spec_helper.rb
|
57
|
+
require 'parameterize/matcher'
|
58
|
+
|
59
|
+
# post_spec.rb
|
60
|
+
describe Post do
|
61
|
+
it { should parameterize(:title) }
|
62
|
+
end
|
63
|
+
```
|
49
64
|
|
50
65
|
## Copyright
|
51
66
|
|
data/lib/parameterize.rb
CHANGED
@@ -4,6 +4,8 @@ require 'active_support/core_ext/string/inflections'
|
|
4
4
|
module Parameterize
|
5
5
|
# Adds a before validation filter for updating the param field
|
6
6
|
# with the parameterized version of the given source field.
|
7
|
+
#
|
8
|
+
# @param [Symbol, String] source The field to parameterize
|
7
9
|
def parameterize(source = :title)
|
8
10
|
include InstanceMethods
|
9
11
|
|
@@ -14,15 +16,17 @@ module Parameterize
|
|
14
16
|
end
|
15
17
|
|
16
18
|
module InstanceMethods
|
19
|
+
# Overwrite #to_param to return the generated param.
|
17
20
|
def to_param
|
18
21
|
self.param
|
19
22
|
end
|
20
23
|
|
24
|
+
protected
|
25
|
+
|
21
26
|
# Updates the param field with the parameterized version of the source field.
|
22
27
|
def update_param
|
23
28
|
self.param = __send__(self.class.param_source_field).to_s.parameterize
|
24
29
|
end
|
25
|
-
protected :update_param
|
26
30
|
end
|
27
31
|
end
|
28
32
|
|
data/lib/parameterize/version.rb
CHANGED
data/parameterize.gemspec
CHANGED
@@ -9,14 +9,14 @@ Gem::Specification.new do |s|
|
|
9
9
|
s.authors = %w(Pete Browne)
|
10
10
|
s.email = %w(me@petebrowne.com)
|
11
11
|
s.homepage = 'http://github.com/petebrowne/parameterize'
|
12
|
-
s.summary = %{The simplest permalink/slug solution for ActiveRecord 3.0}
|
12
|
+
s.summary = %{The simplest permalink/slug solution for ActiveRecord >= 3.0}
|
13
13
|
s.description = %{It uses ActiveSupport's String#parameterize to create the slug. There are no validations. No slug history. No extra tables or models.}
|
14
14
|
|
15
15
|
s.rubyforge_project = 'parameterize'
|
16
16
|
|
17
|
-
s.add_dependency 'activerecord', '~> 3.0
|
18
|
-
s.add_dependency 'activesupport', '~> 3.0
|
19
|
-
s.add_development_dependency 'rspec', '~> 2.
|
17
|
+
s.add_dependency 'activerecord', '~> 3.0'
|
18
|
+
s.add_dependency 'activesupport', '~> 3.0'
|
19
|
+
s.add_development_dependency 'rspec', '~> 2.6.0'
|
20
20
|
s.add_development_dependency 'sqlite3-ruby', '~> 1.3.3'
|
21
21
|
|
22
22
|
s.files = `git ls-files`.split("\n")
|
data/spec/spec_helper.rb
CHANGED
@@ -1,31 +1,21 @@
|
|
1
|
-
lib = File.expand_path('../../lib', __FILE__)
|
2
|
-
$:.unshift(lib) unless $:.include?(lib)
|
3
|
-
|
4
1
|
require 'rubygems'
|
5
2
|
require 'bundler/setup'
|
6
|
-
require
|
3
|
+
Bundler.require :default, :development
|
7
4
|
require 'active_record'
|
8
|
-
require 'parameterize'
|
9
5
|
require 'parameterize/matcher'
|
10
6
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
7
|
+
ActiveRecord::Base.extend Parameterize
|
8
|
+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
|
9
|
+
ActiveRecord::Migration.verbose = false
|
10
|
+
ActiveRecord::Schema.define :version => 1 do
|
11
|
+
create_table :posts do |t|
|
12
|
+
t.string :title
|
13
|
+
t.string :param
|
15
14
|
end
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
create_table :posts do |t|
|
21
|
-
t.string :title
|
22
|
-
t.string :param
|
23
|
-
end
|
24
|
-
|
25
|
-
create_table :users do |t|
|
26
|
-
t.string :name
|
27
|
-
t.string :param
|
28
|
-
end
|
16
|
+
create_table :users do |t|
|
17
|
+
t.string :name
|
18
|
+
t.string :param
|
29
19
|
end
|
30
20
|
end
|
31
21
|
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: parameterize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 25
|
5
|
+
prerelease:
|
5
6
|
segments:
|
6
7
|
- 0
|
7
8
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Pete
|
@@ -15,8 +16,7 @@ autorequire:
|
|
15
16
|
bindir: bin
|
16
17
|
cert_chain: []
|
17
18
|
|
18
|
-
date: 2011-
|
19
|
-
default_executable:
|
19
|
+
date: 2011-07-19 00:00:00 Z
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
22
|
name: activerecord
|
@@ -26,11 +26,11 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
29
30
|
segments:
|
30
31
|
- 3
|
31
32
|
- 0
|
32
|
-
|
33
|
-
version: 3.0.3
|
33
|
+
version: "3.0"
|
34
34
|
type: :runtime
|
35
35
|
version_requirements: *id001
|
36
36
|
- !ruby/object:Gem::Dependency
|
@@ -41,11 +41,11 @@ dependencies:
|
|
41
41
|
requirements:
|
42
42
|
- - ~>
|
43
43
|
- !ruby/object:Gem::Version
|
44
|
+
hash: 7
|
44
45
|
segments:
|
45
46
|
- 3
|
46
47
|
- 0
|
47
|
-
|
48
|
-
version: 3.0.3
|
48
|
+
version: "3.0"
|
49
49
|
type: :runtime
|
50
50
|
version_requirements: *id002
|
51
51
|
- !ruby/object:Gem::Dependency
|
@@ -56,11 +56,12 @@ dependencies:
|
|
56
56
|
requirements:
|
57
57
|
- - ~>
|
58
58
|
- !ruby/object:Gem::Version
|
59
|
+
hash: 23
|
59
60
|
segments:
|
60
61
|
- 2
|
61
|
-
-
|
62
|
+
- 6
|
62
63
|
- 0
|
63
|
-
version: 2.
|
64
|
+
version: 2.6.0
|
64
65
|
type: :development
|
65
66
|
version_requirements: *id003
|
66
67
|
- !ruby/object:Gem::Dependency
|
@@ -71,6 +72,7 @@ dependencies:
|
|
71
72
|
requirements:
|
72
73
|
- - ~>
|
73
74
|
- !ruby/object:Gem::Version
|
75
|
+
hash: 29
|
74
76
|
segments:
|
75
77
|
- 1
|
76
78
|
- 3
|
@@ -90,7 +92,6 @@ extra_rdoc_files: []
|
|
90
92
|
files:
|
91
93
|
- .gitignore
|
92
94
|
- Gemfile
|
93
|
-
- Gemfile.lock
|
94
95
|
- LICENSE
|
95
96
|
- README.md
|
96
97
|
- Rakefile
|
@@ -101,7 +102,6 @@ files:
|
|
101
102
|
- parameterize.gemspec
|
102
103
|
- spec/parameterize_spec.rb
|
103
104
|
- spec/spec_helper.rb
|
104
|
-
has_rdoc: true
|
105
105
|
homepage: http://github.com/petebrowne/parameterize
|
106
106
|
licenses: []
|
107
107
|
|
@@ -115,6 +115,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
115
115
|
requirements:
|
116
116
|
- - ">="
|
117
117
|
- !ruby/object:Gem::Version
|
118
|
+
hash: 3
|
118
119
|
segments:
|
119
120
|
- 0
|
120
121
|
version: "0"
|
@@ -123,16 +124,17 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
124
|
requirements:
|
124
125
|
- - ">="
|
125
126
|
- !ruby/object:Gem::Version
|
127
|
+
hash: 3
|
126
128
|
segments:
|
127
129
|
- 0
|
128
130
|
version: "0"
|
129
131
|
requirements: []
|
130
132
|
|
131
133
|
rubyforge_project: parameterize
|
132
|
-
rubygems_version: 1.
|
134
|
+
rubygems_version: 1.8.5
|
133
135
|
signing_key:
|
134
136
|
specification_version: 3
|
135
|
-
summary: The simplest permalink/slug solution for ActiveRecord 3.0
|
137
|
+
summary: The simplest permalink/slug solution for ActiveRecord >= 3.0
|
136
138
|
test_files:
|
137
139
|
- spec/parameterize_spec.rb
|
138
140
|
- spec/spec_helper.rb
|
data/Gemfile.lock
DELETED
@@ -1,44 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: .
|
3
|
-
specs:
|
4
|
-
parameterize (0.1.0)
|
5
|
-
activerecord (~> 3.0.3)
|
6
|
-
activesupport (~> 3.0.3)
|
7
|
-
|
8
|
-
GEM
|
9
|
-
remote: http://rubygems.org/
|
10
|
-
specs:
|
11
|
-
activemodel (3.0.3)
|
12
|
-
activesupport (= 3.0.3)
|
13
|
-
builder (~> 2.1.2)
|
14
|
-
i18n (~> 0.4)
|
15
|
-
activerecord (3.0.3)
|
16
|
-
activemodel (= 3.0.3)
|
17
|
-
activesupport (= 3.0.3)
|
18
|
-
arel (~> 2.0.2)
|
19
|
-
tzinfo (~> 0.3.23)
|
20
|
-
activesupport (3.0.3)
|
21
|
-
arel (2.0.7)
|
22
|
-
builder (2.1.2)
|
23
|
-
diff-lcs (1.1.2)
|
24
|
-
i18n (0.5.0)
|
25
|
-
rspec (2.4.0)
|
26
|
-
rspec-core (~> 2.4.0)
|
27
|
-
rspec-expectations (~> 2.4.0)
|
28
|
-
rspec-mocks (~> 2.4.0)
|
29
|
-
rspec-core (2.4.0)
|
30
|
-
rspec-expectations (2.4.0)
|
31
|
-
diff-lcs (~> 1.1.2)
|
32
|
-
rspec-mocks (2.4.0)
|
33
|
-
sqlite3 (1.3.3)
|
34
|
-
sqlite3-ruby (1.3.3)
|
35
|
-
sqlite3 (>= 1.3.3)
|
36
|
-
tzinfo (0.3.24)
|
37
|
-
|
38
|
-
PLATFORMS
|
39
|
-
ruby
|
40
|
-
|
41
|
-
DEPENDENCIES
|
42
|
-
parameterize!
|
43
|
-
rspec (~> 2.4.0)
|
44
|
-
sqlite3-ruby (~> 1.3.3)
|