parameterize 0.1.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 +3 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +44 -0
- data/LICENSE +20 -0
- data/README.md +52 -0
- data/Rakefile +7 -0
- data/lib/parameterize.rb +29 -0
- data/lib/parameterize/matcher.rb +11 -0
- data/lib/parameterize/railtie.rb +9 -0
- data/lib/parameterize/version.rb +3 -0
- data/parameterize.gemspec +26 -0
- data/spec/parameterize_spec.rb +35 -0
- data/spec/spec_helper.rb +38 -0
- metadata +138 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,44 @@
|
|
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)
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Peter Browne
|
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.md
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
# Parameterize
|
2
|
+
|
3
|
+
_The simplest permalink/slug solution for ActiveRecord 3.0._
|
4
|
+
|
5
|
+
It uses ActiveSupport's `String#parameterize` to create the slug. There are no validations. No slug history. No extra tables or models.
|
6
|
+
|
7
|
+
## Getting Started
|
8
|
+
|
9
|
+
rails generate model User name:string param:string
|
10
|
+
|
11
|
+
class User < ActiveRecord::Base
|
12
|
+
parameterize :name
|
13
|
+
end
|
14
|
+
|
15
|
+
user = User.new :name => 'John Doe'
|
16
|
+
user.valid?
|
17
|
+
user.param = 'john-doe'
|
18
|
+
user.to_param = 'john-doe'
|
19
|
+
|
20
|
+
## Notes
|
21
|
+
|
22
|
+
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
|
+
|
24
|
+
class Post < ActiveRecord::Base
|
25
|
+
parameterize
|
26
|
+
validates_presence_of :param
|
27
|
+
validates_uniqueness_of :param
|
28
|
+
end
|
29
|
+
|
30
|
+
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
|
+
|
32
|
+
require 'babosa'
|
33
|
+
class String
|
34
|
+
alias :parameterize :to_identifier
|
35
|
+
end
|
36
|
+
|
37
|
+
There is also a Shoulda-style RSpec matcher available for use in your specs:
|
38
|
+
|
39
|
+
# spec_helper.rb
|
40
|
+
...
|
41
|
+
require 'parameterize/matcher'
|
42
|
+
...
|
43
|
+
|
44
|
+
# post_spec.rb
|
45
|
+
...
|
46
|
+
describe Post do
|
47
|
+
it { should parameterize(:title) }
|
48
|
+
end
|
49
|
+
|
50
|
+
## Copyright
|
51
|
+
|
52
|
+
Copyright (c) 2011 [Peter Browne](http://petebrowne.com). See LICENSE for details.
|
data/Rakefile
ADDED
data/lib/parameterize.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'active_support/core_ext/array/extract_options'
|
2
|
+
require 'active_support/core_ext/string/inflections'
|
3
|
+
|
4
|
+
module Parameterize
|
5
|
+
# Adds a before validation filter for updating the param field
|
6
|
+
# with the parameterized version of the given source field.
|
7
|
+
def parameterize(source = :title)
|
8
|
+
include InstanceMethods
|
9
|
+
|
10
|
+
class_attribute :param_source_field
|
11
|
+
before_validation :update_param
|
12
|
+
|
13
|
+
self.param_source_field = source
|
14
|
+
end
|
15
|
+
|
16
|
+
module InstanceMethods
|
17
|
+
def to_param
|
18
|
+
self.param
|
19
|
+
end
|
20
|
+
|
21
|
+
# Updates the param field with the parameterized version of the source field.
|
22
|
+
def update_param
|
23
|
+
self.param = __send__(self.class.param_source_field).to_s.parameterize
|
24
|
+
end
|
25
|
+
protected :update_param
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
require 'parameterize/railtie' if defined?(Rails)
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'rspec/matchers'
|
2
|
+
|
3
|
+
RSpec::Matchers.define :parameterize do |source|
|
4
|
+
match do |model|
|
5
|
+
model.class.include?(Parameterize::InstanceMethods) && model.class.param_source_field == source
|
6
|
+
end
|
7
|
+
|
8
|
+
failure_message_for_should do |model|
|
9
|
+
"expected #{model.class} to parameterize #{source}"
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'parameterize/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'parameterize'
|
7
|
+
s.version = Parameterize::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = %w(Pete Browne)
|
10
|
+
s.email = %w(me@petebrowne.com)
|
11
|
+
s.homepage = 'http://github.com/petebrowne/parameterize'
|
12
|
+
s.summary = %{The simplest permalink/slug solution for ActiveRecord 3.0}
|
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
|
+
|
15
|
+
s.rubyforge_project = 'parameterize'
|
16
|
+
|
17
|
+
s.add_dependency 'activerecord', '~> 3.0.3'
|
18
|
+
s.add_dependency 'activesupport', '~> 3.0.3'
|
19
|
+
s.add_development_dependency 'rspec', '~> 2.4.0'
|
20
|
+
s.add_development_dependency 'sqlite3-ruby', '~> 1.3.3'
|
21
|
+
|
22
|
+
s.files = `git ls-files`.split("\n")
|
23
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
24
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
25
|
+
s.require_paths = %w(lib)
|
26
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Parameterize do
|
4
|
+
it 'parameterizes the source field' do
|
5
|
+
post = Post.new :title => "Hey Ho, Let's Go!"
|
6
|
+
post.valid?
|
7
|
+
post.param.should == 'hey-ho-let-s-go'
|
8
|
+
end
|
9
|
+
|
10
|
+
context 'with a different source field' do
|
11
|
+
it 'parameterizes the source field' do
|
12
|
+
user = User.new :name => 'Donald E. Knuth'
|
13
|
+
user.valid?
|
14
|
+
user.param.should == 'donald-e-knuth'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#to_param' do
|
19
|
+
it 'returns the param' do
|
20
|
+
post = Post.new :title => "Hey Ho, Let's Go!"
|
21
|
+
post.valid?
|
22
|
+
post.to_param.should == 'hey-ho-let-s-go'
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'matcher' do
|
27
|
+
it 'checks if Post parameterized :title' do
|
28
|
+
Post.new.should parameterize(:title)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'checks if User parameterized :name' do
|
32
|
+
User.new.should parameterize(:name)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
lib = File.expand_path('../../lib', __FILE__)
|
2
|
+
$:.unshift(lib) unless $:.include?(lib)
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'bundler/setup'
|
6
|
+
require 'rspec'
|
7
|
+
require 'active_record'
|
8
|
+
require 'parameterize'
|
9
|
+
require 'parameterize/matcher'
|
10
|
+
|
11
|
+
module ActiveRecord
|
12
|
+
class Base
|
13
|
+
extend Parameterize
|
14
|
+
establish_connection :adapter => 'sqlite3', :database => ':memory:'
|
15
|
+
end
|
16
|
+
|
17
|
+
Migration.verbose = false
|
18
|
+
|
19
|
+
Schema.define(:version => 1) do
|
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
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class Post < ActiveRecord::Base
|
33
|
+
parameterize
|
34
|
+
end
|
35
|
+
|
36
|
+
class User < ActiveRecord::Base
|
37
|
+
parameterize :name
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,138 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: parameterize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Pete
|
13
|
+
- Browne
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-01-29 00:00:00 -06:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: activerecord
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
segments:
|
30
|
+
- 3
|
31
|
+
- 0
|
32
|
+
- 3
|
33
|
+
version: 3.0.3
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: activesupport
|
38
|
+
prerelease: false
|
39
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
segments:
|
45
|
+
- 3
|
46
|
+
- 0
|
47
|
+
- 3
|
48
|
+
version: 3.0.3
|
49
|
+
type: :runtime
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: rspec
|
53
|
+
prerelease: false
|
54
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ~>
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
segments:
|
60
|
+
- 2
|
61
|
+
- 4
|
62
|
+
- 0
|
63
|
+
version: 2.4.0
|
64
|
+
type: :development
|
65
|
+
version_requirements: *id003
|
66
|
+
- !ruby/object:Gem::Dependency
|
67
|
+
name: sqlite3-ruby
|
68
|
+
prerelease: false
|
69
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
70
|
+
none: false
|
71
|
+
requirements:
|
72
|
+
- - ~>
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
segments:
|
75
|
+
- 1
|
76
|
+
- 3
|
77
|
+
- 3
|
78
|
+
version: 1.3.3
|
79
|
+
type: :development
|
80
|
+
version_requirements: *id004
|
81
|
+
description: It uses ActiveSupport's String#parameterize to create the slug. There are no validations. No slug history. No extra tables or models.
|
82
|
+
email:
|
83
|
+
- me@petebrowne.com
|
84
|
+
executables: []
|
85
|
+
|
86
|
+
extensions: []
|
87
|
+
|
88
|
+
extra_rdoc_files: []
|
89
|
+
|
90
|
+
files:
|
91
|
+
- .gitignore
|
92
|
+
- Gemfile
|
93
|
+
- Gemfile.lock
|
94
|
+
- LICENSE
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- lib/parameterize.rb
|
98
|
+
- lib/parameterize/matcher.rb
|
99
|
+
- lib/parameterize/railtie.rb
|
100
|
+
- lib/parameterize/version.rb
|
101
|
+
- parameterize.gemspec
|
102
|
+
- spec/parameterize_spec.rb
|
103
|
+
- spec/spec_helper.rb
|
104
|
+
has_rdoc: true
|
105
|
+
homepage: http://github.com/petebrowne/parameterize
|
106
|
+
licenses: []
|
107
|
+
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
segments:
|
119
|
+
- 0
|
120
|
+
version: "0"
|
121
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
segments:
|
127
|
+
- 0
|
128
|
+
version: "0"
|
129
|
+
requirements: []
|
130
|
+
|
131
|
+
rubyforge_project: parameterize
|
132
|
+
rubygems_version: 1.3.7
|
133
|
+
signing_key:
|
134
|
+
specification_version: 3
|
135
|
+
summary: The simplest permalink/slug solution for ActiveRecord 3.0
|
136
|
+
test_files:
|
137
|
+
- spec/parameterize_spec.rb
|
138
|
+
- spec/spec_helper.rb
|