handler 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.rdoc +3 -0
- data/README.rdoc +2 -0
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/handler.gemspec +6 -2
- data/lib/handler.rb +38 -26
- data/test/handler_test.rb +11 -8
- data/test/test_helper.rb +1 -1
- metadata +23 -35
- data/.gitignore +0 -5
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4e1e6c53663ede3c98f55892c9cc63eb86ec9e78
|
4
|
+
data.tar.gz: 2446f113f9b31c486007e8a033cfe6529ba8f97e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 72c3cc99324f5031b926e606ccc1727d9a35d549077ee824e83418cc708da0a014873e2356dba4cc051883c738831be369aa9f453ec10565401a874b3274262f
|
7
|
+
data.tar.gz: 6a227d82f710b2e4d176a5dcbce4105114642984fbc2597d4891462bf57cc8c08abbb0faa1478f30dbe57fbcdfe72a4faf2fc240c14cb29bcdaa3badf8f0c642
|
data/CHANGELOG.rdoc
CHANGED
data/README.rdoc
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
= Handler
|
2
2
|
|
3
|
+
<b>Handler is currently compatible with Rails 3.x, 4.x, and 5.x.</b>
|
4
|
+
|
3
5
|
Handler is a Rails plugin that generates handles (filesystem- and URL-friendly names) for ActiveRecord models based on a given attribute or method. For example, in your model:
|
4
6
|
|
5
7
|
handle_based_on :title
|
data/Rakefile
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.9.0
|
data/handler.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{handler}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.9.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Alex Reisner"]
|
@@ -36,9 +36,13 @@ Gem::Specification.new do |s|
|
|
36
36
|
s.summary = %q{Handler generates filesystem- and URL-friendly names for ActiveRecord models.}
|
37
37
|
s.test_files = [
|
38
38
|
"test/handler_test.rb",
|
39
|
-
|
39
|
+
"test/test_helper.rb"
|
40
40
|
]
|
41
41
|
|
42
|
+
s.add_dependency 'activesupport', '>= 3.0'
|
43
|
+
s.add_dependency 'activerecord', '>= 3.0'
|
44
|
+
s.add_development_dependency 'minitest'
|
45
|
+
|
42
46
|
if s.respond_to? :specification_version then
|
43
47
|
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
48
|
s.specification_version = 3
|
data/lib/handler.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
module Handler
|
2
|
-
|
2
|
+
|
3
3
|
##
|
4
4
|
# Included hook: extend including class.
|
5
5
|
#
|
6
6
|
def self.included(base)
|
7
7
|
base.extend ClassMethods
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
##
|
11
11
|
# Transliterate a string: change accented Unicode characters to ASCII
|
12
12
|
# approximations. Tries several methods, attempting the best first:
|
13
|
-
#
|
13
|
+
#
|
14
14
|
# 1. unidecode, if installed (http://rubyforge.org/projects/unidecode)
|
15
15
|
# 2. iconv (included with Ruby, doesn't work with all Ruby versions)
|
16
16
|
# 3. normalize, then remove un-normalizable characters
|
@@ -21,7 +21,7 @@ module Handler
|
|
21
21
|
transliterate_with_normalization(string) or
|
22
22
|
string
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
##
|
26
26
|
# Transliterate a string with the unidecode library.
|
27
27
|
# Return nil if unsuccessful (eg, library not available).
|
@@ -34,7 +34,7 @@ module Handler
|
|
34
34
|
nil
|
35
35
|
end
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
##
|
39
39
|
# Transliterate a string with the iconv library.
|
40
40
|
# Return nil if unsuccessful (eg, library not available).
|
@@ -46,7 +46,16 @@ module Handler
|
|
46
46
|
nil
|
47
47
|
end
|
48
48
|
end
|
49
|
-
|
49
|
+
|
50
|
+
##
|
51
|
+
# Transliterate a string using multibyte normalization,
|
52
|
+
# then remove remaining non-ASCII characters. Taken from
|
53
|
+
# <tt>ActiveSupport::Inflector.transliterate</tt>.
|
54
|
+
#
|
55
|
+
def self.transliterate_with_normalization(string)
|
56
|
+
string.mb_chars.normalize.gsub(/[^\x00-\x7F]+/, '').to_s
|
57
|
+
end
|
58
|
+
|
50
59
|
##
|
51
60
|
# Generate a handle from a string.
|
52
61
|
#
|
@@ -59,10 +68,11 @@ module Handler
|
|
59
68
|
str = str.gsub('&', ' and ') # add space for, e.g., "Y&T"
|
60
69
|
str = str.delete('.\'"') # no space
|
61
70
|
str = str.gsub(/\W/, ' ') # space
|
71
|
+
str = str.strip
|
62
72
|
str = str.gsub(/ +/, separator)
|
63
|
-
str
|
73
|
+
str
|
64
74
|
end
|
65
|
-
|
75
|
+
|
66
76
|
##
|
67
77
|
# Get the next handle in the sequence (for avoiding duplicates).
|
68
78
|
#
|
@@ -73,22 +83,13 @@ module Handler
|
|
73
83
|
handle + separator + "2"
|
74
84
|
end
|
75
85
|
end
|
76
|
-
|
77
|
-
##
|
78
|
-
# Transliterate a string using multibyte normalization,
|
79
|
-
# then remove remaining non-ASCII characters. Taken from
|
80
|
-
# <tt>ActiveSupport::Inflector.transliterate</tt>.
|
81
|
-
#
|
82
|
-
def self.transliterate_with_normalization(string)
|
83
|
-
string.mb_chars.normalize.gsub(/[^\x00-\x7F]+/, '').to_s
|
84
|
-
end
|
85
|
-
|
86
|
+
|
86
87
|
module ClassMethods
|
87
|
-
|
88
|
+
|
88
89
|
##
|
89
90
|
# Declare that a model generates a handle based on
|
90
91
|
# a given attribute or method. Options include:
|
91
|
-
#
|
92
|
+
#
|
92
93
|
# <tt>:separator</tt> - character to place between words
|
93
94
|
# <tt>:store</tt> - attribute in which to store handle
|
94
95
|
# <tt>:unique</tt> - generate a handle which is unique among all records
|
@@ -98,22 +99,33 @@ module Handler
|
|
98
99
|
options[:write_to] ||= :handle
|
99
100
|
options[:unique] = true if options[:unique].nil?
|
100
101
|
|
101
|
-
|
102
|
-
|
103
|
-
|
102
|
+
##
|
103
|
+
# Generate a URL-friendly name.
|
104
|
+
#
|
104
105
|
define_method :generate_handle do
|
105
106
|
h = Handler.generate_handle(send(attribute), options[:separator])
|
106
107
|
if options[:unique]
|
107
|
-
|
108
|
+
|
109
|
+
# generate a condition for finding an existing record with a
|
110
|
+
# given handle
|
111
|
+
find_dupe = lambda{ |h|
|
112
|
+
conds = ["#{options[:write_to]} = ?", h]
|
113
|
+
unless new_record?
|
114
|
+
conds[0] << " AND id != ?"
|
115
|
+
conds << id
|
116
|
+
end
|
117
|
+
conds
|
118
|
+
}
|
119
|
+
|
108
120
|
# increase number while *other* records exist with the same handle
|
109
121
|
# (record might be saved and should keep its handle)
|
110
|
-
while self.class.
|
122
|
+
while self.class.where(find_dupe.call(h)).size > 0
|
111
123
|
h = Handler.next_handle(h, options[:separator])
|
112
124
|
end
|
113
125
|
end
|
114
126
|
h
|
115
127
|
end
|
116
|
-
|
128
|
+
|
117
129
|
##
|
118
130
|
# Assign the generated handle to the specified attribute.
|
119
131
|
#
|
data/test/handler_test.rb
CHANGED
@@ -24,7 +24,7 @@ class HandlerTest < ActiveSupport::TestCase
|
|
24
24
|
".38 Special" => "38_special",
|
25
25
|
"Guns N' Roses" => "guns_n_roses",
|
26
26
|
"The Rossington-Collins Band" => "the_rossington_collins_band"
|
27
|
-
|
27
|
+
|
28
28
|
}.each do |s,t|
|
29
29
|
b.name = s
|
30
30
|
assert_equal t, b.generate_handle
|
@@ -37,19 +37,19 @@ class HandlerTest < ActiveSupport::TestCase
|
|
37
37
|
"Y&T" => "y_and_t",
|
38
38
|
"Huey Lewis & the News" => "huey_lewis_and_the_news",
|
39
39
|
"Emerson, Lake & Palmer" => "emerson_lake_and_palmer"
|
40
|
-
|
40
|
+
|
41
41
|
}.each do |s,t|
|
42
42
|
b.name = s
|
43
43
|
assert_equal t, b.generate_handle
|
44
44
|
end
|
45
45
|
end
|
46
|
-
|
46
|
+
|
47
47
|
test "next handle" do
|
48
48
|
assert_equal "banana_boat_2", Handler.next_handle("banana_boat", "_")
|
49
49
|
assert_equal "banana-boat-3", Handler.next_handle("banana-boat-2", "-")
|
50
50
|
assert_equal "banana-boat-12", Handler.next_handle("banana-boat-11", "-")
|
51
51
|
end
|
52
|
-
|
52
|
+
|
53
53
|
test "unique handle generation" do
|
54
54
|
p = Person.new
|
55
55
|
p.name = "Captain Beefheart"
|
@@ -62,14 +62,17 @@ end
|
|
62
62
|
class ActiveRecordSimulator
|
63
63
|
include Handler
|
64
64
|
attr_accessor :name
|
65
|
-
|
65
|
+
|
66
66
|
# simulate id method
|
67
67
|
def id; 123; end
|
68
68
|
|
69
|
-
# simulate
|
69
|
+
# simulate new_record? method
|
70
|
+
def new_record?; true; end
|
71
|
+
|
72
|
+
# simulate ActiveRecord::Base.where method;
|
70
73
|
# needed for testing whether records exist with a given handle
|
71
|
-
def self.
|
72
|
-
@existing_handles.include?(options[
|
74
|
+
def self.where(options)
|
75
|
+
@existing_handles.include?(options[1]) ? [nil, nil] : []
|
73
76
|
end
|
74
77
|
end
|
75
78
|
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,29 +1,24 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: handler
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
|
-
authors:
|
6
|
+
authors:
|
7
7
|
- Alex Reisner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
|
12
|
-
date: 2009-10-13 00:00:00 -04:00
|
13
|
-
default_executable:
|
11
|
+
date: 2016-09-04 00:00:00.000000000 Z
|
14
12
|
dependencies: []
|
15
|
-
|
16
|
-
|
13
|
+
description: Handler is a Rails plugin that generates handles (filesystem- and URL-friendly
|
14
|
+
names) for ActiveRecord models based on a given attribute or method.
|
17
15
|
email: alex@alexreisner.com
|
18
16
|
executables: []
|
19
|
-
|
20
17
|
extensions: []
|
21
|
-
|
22
|
-
extra_rdoc_files:
|
18
|
+
extra_rdoc_files:
|
23
19
|
- LICENSE
|
24
20
|
- README.rdoc
|
25
|
-
files:
|
26
|
-
- .gitignore
|
21
|
+
files:
|
27
22
|
- CHANGELOG.rdoc
|
28
23
|
- LICENSE
|
29
24
|
- README.rdoc
|
@@ -34,34 +29,27 @@ files:
|
|
34
29
|
- lib/handler.rb
|
35
30
|
- test/handler_test.rb
|
36
31
|
- test/test_helper.rb
|
37
|
-
has_rdoc: true
|
38
32
|
homepage: http://github.com/alexreisner/handler
|
39
33
|
licenses: []
|
40
|
-
|
34
|
+
metadata: {}
|
41
35
|
post_install_message:
|
42
|
-
rdoc_options:
|
43
|
-
|
44
|
-
require_paths:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
45
38
|
- lib
|
46
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
-
requirements:
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
48
41
|
- - ">="
|
49
|
-
- !ruby/object:Gem::Version
|
50
|
-
version:
|
51
|
-
|
52
|
-
|
53
|
-
requirements:
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
54
46
|
- - ">="
|
55
|
-
- !ruby/object:Gem::Version
|
56
|
-
version:
|
57
|
-
version:
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
58
49
|
requirements: []
|
59
|
-
|
60
50
|
rubyforge_project:
|
61
|
-
rubygems_version:
|
51
|
+
rubygems_version: 2.5.1
|
62
52
|
signing_key:
|
63
|
-
specification_version:
|
53
|
+
specification_version: 4
|
64
54
|
summary: Handler generates filesystem- and URL-friendly names for ActiveRecord models.
|
65
|
-
test_files:
|
66
|
-
- test/handler_test.rb
|
67
|
-
- test/test_helper.rb
|
55
|
+
test_files: []
|