sexy_to_param 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +47 -0
- data/LICENSE.txt +20 -0
- data/README.markdown +32 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/lib/sexy_to_param.rb +26 -0
- data/spec/sexy_to_param_spec.rb +38 -0
- data/spec/spec_helper.rb +31 -0
- metadata +135 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.0.9)
|
5
|
+
activesupport (= 3.0.9)
|
6
|
+
builder (~> 2.1.2)
|
7
|
+
i18n (~> 0.5.0)
|
8
|
+
activerecord (3.0.9)
|
9
|
+
activemodel (= 3.0.9)
|
10
|
+
activesupport (= 3.0.9)
|
11
|
+
arel (~> 2.0.10)
|
12
|
+
tzinfo (~> 0.3.23)
|
13
|
+
activesupport (3.0.9)
|
14
|
+
arel (2.0.10)
|
15
|
+
builder (2.1.2)
|
16
|
+
diff-lcs (1.1.2)
|
17
|
+
git (1.2.5)
|
18
|
+
i18n (0.5.0)
|
19
|
+
jeweler (1.6.4)
|
20
|
+
bundler (~> 1.0)
|
21
|
+
git (>= 1.2.5)
|
22
|
+
rake
|
23
|
+
rake (0.9.2)
|
24
|
+
rcov (0.9.9)
|
25
|
+
rspec (2.3.0)
|
26
|
+
rspec-core (~> 2.3.0)
|
27
|
+
rspec-expectations (~> 2.3.0)
|
28
|
+
rspec-mocks (~> 2.3.0)
|
29
|
+
rspec-core (2.3.1)
|
30
|
+
rspec-expectations (2.3.0)
|
31
|
+
diff-lcs (~> 1.1.2)
|
32
|
+
rspec-mocks (2.3.0)
|
33
|
+
sqlite3 (1.3.3)
|
34
|
+
sqlite3-ruby (1.3.3)
|
35
|
+
sqlite3 (>= 1.3.3)
|
36
|
+
tzinfo (0.3.29)
|
37
|
+
|
38
|
+
PLATFORMS
|
39
|
+
ruby
|
40
|
+
|
41
|
+
DEPENDENCIES
|
42
|
+
activerecord
|
43
|
+
bundler (~> 1.0.0)
|
44
|
+
jeweler (~> 1.6.4)
|
45
|
+
rcov
|
46
|
+
rspec (~> 2.3.0)
|
47
|
+
sqlite3-ruby
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 holysugar
|
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.markdown
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
sexy_to_param
|
2
|
+
=============
|
3
|
+
|
4
|
+
to_param may need to escape strings. sexy_to_param escapes it automatically.
|
5
|
+
|
6
|
+
Example
|
7
|
+
-------
|
8
|
+
|
9
|
+
class Article < ActiveRecord::Base
|
10
|
+
sexy_to_param :title
|
11
|
+
end
|
12
|
+
|
13
|
+
a = Article.first #=> #<Article id:1 title:"about #github">
|
14
|
+
a.to_param #=> "1-github"
|
15
|
+
|
16
|
+
Contributing to sexy_to_param
|
17
|
+
-----------------------------
|
18
|
+
|
19
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
20
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
21
|
+
* Fork the project
|
22
|
+
* Start a feature/bugfix branch
|
23
|
+
* Commit and push until you are happy with your contribution
|
24
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
25
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
26
|
+
|
27
|
+
Copyright
|
28
|
+
---------
|
29
|
+
|
30
|
+
Copyright (c) 2011 holysugar. See LICENSE.txt for
|
31
|
+
further details.
|
32
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "sexy_to_param"
|
18
|
+
gem.homepage = "http://github.com/holysugar/sexy_to_param"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{methods when using to_param}
|
21
|
+
gem.description = %Q{methods when using to_param}
|
22
|
+
gem.email = "holysugar@gmail.com"
|
23
|
+
gem.authors = ["holysugar"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rake/rdoctask'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "sexy_to_param #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
module SexyToParam
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def sexy_to_param(column = :slug)
|
8
|
+
define_method(:to_param) {
|
9
|
+
if id
|
10
|
+
param_string = SexyToParam.escape(send(column))
|
11
|
+
"#{id}-#{param_string}"
|
12
|
+
end
|
13
|
+
}
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class << self
|
18
|
+
def escape(slug)
|
19
|
+
URI.escape(slug.gsub(/[:@#\/&=\.\s]+/, '-').sub(/\A-/,''))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
ActiveRecord::Base.extend SexyToParam::ClassMethods
|
26
|
+
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
3
|
+
|
4
|
+
class Article < ActiveRecord::Base
|
5
|
+
sexy_to_param
|
6
|
+
end
|
7
|
+
|
8
|
+
class ArticleUsingTitle < ActiveRecord::Base
|
9
|
+
set_table_name 'articles'
|
10
|
+
sexy_to_param :title
|
11
|
+
end
|
12
|
+
|
13
|
+
describe Article do
|
14
|
+
before(:all) {
|
15
|
+
setup_db
|
16
|
+
Article.create(:slug => "myslug", :title => "/About @twitter and #github")
|
17
|
+
Article.create(:slug => "japanese", :title => "日本語")
|
18
|
+
}
|
19
|
+
after(:all) { teardown_db }
|
20
|
+
|
21
|
+
describe "default options" do
|
22
|
+
let(:article) { Article.first }
|
23
|
+
subject { article }
|
24
|
+
|
25
|
+
its(:to_param) { should == "1-myslug" }
|
26
|
+
end
|
27
|
+
|
28
|
+
context "with column name option" do
|
29
|
+
subject { ArticleUsingTitle.first }
|
30
|
+
its(:to_param) { should == "1-About-twitter-and-github" }
|
31
|
+
end
|
32
|
+
|
33
|
+
context "needs to escape, such as Japanese letters" do
|
34
|
+
subject { ArticleUsingTitle.last }
|
35
|
+
its(:to_param) { should == "2-%E6%97%A5%E6%9C%AC%E8%AA%9E" }
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'sexy_to_param'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
end
|
12
|
+
|
13
|
+
ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
|
14
|
+
|
15
|
+
def setup_db
|
16
|
+
ActiveRecord::Migration.suppress_messages do
|
17
|
+
ActiveRecord::Schema.define(:version => 1) do
|
18
|
+
create_table :articles do |t|
|
19
|
+
t.column :slug, :string
|
20
|
+
t.column :title, :string
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def teardown_db
|
27
|
+
ActiveRecord::Base.connection.tables.each do |table|
|
28
|
+
ActiveRecord::Base.connection.drop_table(table)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
metadata
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sexy_to_param
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- holysugar
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-07-13 00:00:00 +09:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: activerecord
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 2.3.0
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: bundler
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.0.0
|
46
|
+
type: :development
|
47
|
+
prerelease: false
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: jeweler
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ~>
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 1.6.4
|
57
|
+
type: :development
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: *id004
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: rcov
|
62
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: *id005
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: sqlite3-ruby
|
73
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
type: :development
|
80
|
+
prerelease: false
|
81
|
+
version_requirements: *id006
|
82
|
+
description: methods when using to_param
|
83
|
+
email: holysugar@gmail.com
|
84
|
+
executables: []
|
85
|
+
|
86
|
+
extensions: []
|
87
|
+
|
88
|
+
extra_rdoc_files:
|
89
|
+
- LICENSE.txt
|
90
|
+
- README.markdown
|
91
|
+
files:
|
92
|
+
- .document
|
93
|
+
- .rspec
|
94
|
+
- Gemfile
|
95
|
+
- Gemfile.lock
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.markdown
|
98
|
+
- Rakefile
|
99
|
+
- VERSION
|
100
|
+
- lib/sexy_to_param.rb
|
101
|
+
- spec/sexy_to_param_spec.rb
|
102
|
+
- spec/spec_helper.rb
|
103
|
+
has_rdoc: true
|
104
|
+
homepage: http://github.com/holysugar/sexy_to_param
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
hash: 420315773723891091
|
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
|
+
version: "0"
|
127
|
+
requirements: []
|
128
|
+
|
129
|
+
rubyforge_project:
|
130
|
+
rubygems_version: 1.6.2
|
131
|
+
signing_key:
|
132
|
+
specification_version: 3
|
133
|
+
summary: methods when using to_param
|
134
|
+
test_files: []
|
135
|
+
|