sequel_sluggable 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.rdoc +61 -2
- data/Rakefile +1 -1
- data/lib/sequel_sluggable.rb +11 -13
- data/lib/version.rb +1 -1
- data/sequel_sluggable.gemspec +6 -9
- data/spec/sequel_sluggable_spec.rb +13 -0
- metadata +3 -6
- data/TODO +0 -3
- data/VERSION +0 -1
data/.gitignore
CHANGED
data/README.rdoc
CHANGED
@@ -1,8 +1,67 @@
|
|
1
1
|
= sequel_sluggable
|
2
2
|
|
3
|
-
|
3
|
+
== Install
|
4
4
|
|
5
|
-
|
5
|
+
Add gemcutter.org source if you don't have it:
|
6
|
+
gem source http://gemcutter.org
|
7
|
+
|
8
|
+
Install:
|
9
|
+
[sudo] gem install sequel_sluggable
|
10
|
+
|
11
|
+
== Usage
|
12
|
+
|
13
|
+
This plug-in provide functionality to allow Sequel::Model to have a slug.
|
14
|
+
Slug is created in the *before_save* hook which is called when you're
|
15
|
+
*creating* or *updating* your model.
|
16
|
+
|
17
|
+
To use plug-in you add plug-in to your model:
|
18
|
+
|
19
|
+
class MyModel < Sequel::Model
|
20
|
+
plugin :sluggable, :source => :name
|
21
|
+
end
|
22
|
+
|
23
|
+
You can use following options:
|
24
|
+
*source*:: Column which value will be used to generate slug.
|
25
|
+
*target*:: Column where slug will be written, defaults to *:slug*.
|
26
|
+
*sluggator*:: Proc or Symbol to call to create slug.
|
27
|
+
|
28
|
+
Options *target* and *sluggator* are optional.
|
29
|
+
|
30
|
+
== Algorithm customization
|
31
|
+
|
32
|
+
You can customize algorithm of the slug creation in several places.
|
33
|
+
If you provide _:sluggator_ Proc or Symbol the sluggator will be called:
|
34
|
+
|
35
|
+
class MyModel < Sequel::Model
|
36
|
+
plugin :sluggable,
|
37
|
+
:source => :name,
|
38
|
+
:sluggator => Proc.new {|value, model| do_something }
|
39
|
+
end
|
40
|
+
|
41
|
+
OR
|
42
|
+
|
43
|
+
class MyModel < Sequel::Model
|
44
|
+
plugin :sluggable,:source => :name, :sluggator => :my_to_slug
|
45
|
+
end
|
46
|
+
|
47
|
+
If you don't provide _:sluggator_ sequel_sluggable will try to use
|
48
|
+
<b>Model#to_slug(value)</b>. So if you have in your model this method
|
49
|
+
it will be used:
|
50
|
+
|
51
|
+
class MyModel < Sequel::Model
|
52
|
+
plugin :sluggable, :source => :name
|
53
|
+
|
54
|
+
def to_slug(value)
|
55
|
+
value.upcase
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
If you don't define <b>Model#to_slug</b> or *:sluggator* sequel_sluggable
|
60
|
+
will use it's own default implementation which does following:
|
61
|
+
|
62
|
+
model.source_column.chomp.downcase.gsub(/[^a-z0-9]+/,'-')
|
63
|
+
|
64
|
+
= Note on Patches/Pull Requests
|
6
65
|
|
7
66
|
* Fork the project.
|
8
67
|
* Make your feature addition or bug fix.
|
data/Rakefile
CHANGED
@@ -13,7 +13,7 @@ begin
|
|
13
13
|
gem.email = "pavel.kunc@gmail.com"
|
14
14
|
gem.homepage = "http://github.com/pk/sequel_sluggable"
|
15
15
|
gem.authors = ["Pavel Kunc"]
|
16
|
-
gem.add_dependency "sequel"
|
16
|
+
gem.add_dependency "sequel", ">= 3.0.0"
|
17
17
|
gem.add_development_dependency "sqlite3-ruby"
|
18
18
|
gem.add_development_dependency "rspec"
|
19
19
|
gem.add_development_dependency "yard"
|
data/lib/sequel_sluggable.rb
CHANGED
@@ -35,14 +35,13 @@ module Sequel
|
|
35
35
|
#
|
36
36
|
# Options:
|
37
37
|
# @param [Hash] plugin options
|
38
|
-
# @option source [Symbol] :Column to get value to be slugged from.
|
38
|
+
# @option source [Symbol] :Column to get value to be slugged from.
|
39
39
|
# @option target [Symbol] :Column to write value of the slug to.
|
40
|
-
# @option sluggator [Proc]
|
40
|
+
# @option sluggator [Proc, Symbol] :Algorithm to convert string to slug.
|
41
41
|
def sluggable_options=(options)
|
42
42
|
raise ArgumentError, "You must provide :source column" unless options[:source]
|
43
|
-
|
44
|
-
|
45
|
-
!options[:sluggator].respond_to?(:call)
|
43
|
+
sluggator = options[:sluggator]
|
44
|
+
if sluggator && !sluggator.is_a?(Symbol) && !sluggator.respond_to?(:call)
|
46
45
|
raise ArgumentError, "If you provide :sluggator it must be Symbol or callable."
|
47
46
|
end
|
48
47
|
options[:source] = options[:source].to_sym
|
@@ -69,12 +68,11 @@ module Sequel
|
|
69
68
|
#
|
70
69
|
# @param [String] String to be slugged
|
71
70
|
# @return [String]
|
72
|
-
def slug=(
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
end
|
71
|
+
def slug=(value)
|
72
|
+
sluggator = self.class.sluggable_options[:sluggator]
|
73
|
+
slug = sluggator.call(value, self) if sluggator.respond_to?(:call)
|
74
|
+
slug ||= self.send(sluggator, value) if sluggator
|
75
|
+
slug ||= to_slug(value)
|
78
76
|
super(slug)
|
79
77
|
end
|
80
78
|
|
@@ -82,8 +80,8 @@ module Sequel
|
|
82
80
|
#
|
83
81
|
# @param [String] String to be slugged
|
84
82
|
# @return [String]
|
85
|
-
def to_slug(
|
86
|
-
|
83
|
+
def to_slug(value)
|
84
|
+
value.chomp.downcase.gsub(/[^a-z0-9]+/,'-')
|
87
85
|
end
|
88
86
|
|
89
87
|
end # InstanceMethods
|
data/lib/version.rb
CHANGED
data/sequel_sluggable.gemspec
CHANGED
@@ -5,17 +5,16 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{sequel_sluggable}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Pavel Kunc"]
|
12
|
-
s.date = %q{2010-02-
|
12
|
+
s.date = %q{2010-02-15}
|
13
13
|
s.description = %q{Sequel plugin which provides Slug functionality for model.}
|
14
14
|
s.email = %q{pavel.kunc@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
17
|
-
"README.rdoc"
|
18
|
-
"TODO"
|
17
|
+
"README.rdoc"
|
19
18
|
]
|
20
19
|
s.files = [
|
21
20
|
".document",
|
@@ -23,8 +22,6 @@ Gem::Specification.new do |s|
|
|
23
22
|
"LICENSE",
|
24
23
|
"README.rdoc",
|
25
24
|
"Rakefile",
|
26
|
-
"TODO",
|
27
|
-
"VERSION",
|
28
25
|
"lib/sequel_sluggable.rb",
|
29
26
|
"lib/sluggable_rspec_helper.rb",
|
30
27
|
"lib/version.rb",
|
@@ -48,18 +45,18 @@ Gem::Specification.new do |s|
|
|
48
45
|
s.specification_version = 3
|
49
46
|
|
50
47
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
51
|
-
s.add_runtime_dependency(%q<sequel>, [">= 0"])
|
48
|
+
s.add_runtime_dependency(%q<sequel>, [">= 3.0.0"])
|
52
49
|
s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
|
53
50
|
s.add_development_dependency(%q<rspec>, [">= 0"])
|
54
51
|
s.add_development_dependency(%q<yard>, [">= 0"])
|
55
52
|
else
|
56
|
-
s.add_dependency(%q<sequel>, [">= 0"])
|
53
|
+
s.add_dependency(%q<sequel>, [">= 3.0.0"])
|
57
54
|
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
|
58
55
|
s.add_dependency(%q<rspec>, [">= 0"])
|
59
56
|
s.add_dependency(%q<yard>, [">= 0"])
|
60
57
|
end
|
61
58
|
else
|
62
|
-
s.add_dependency(%q<sequel>, [">= 0"])
|
59
|
+
s.add_dependency(%q<sequel>, [">= 3.0.0"])
|
63
60
|
s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
|
64
61
|
s.add_dependency(%q<rspec>, [">= 0"])
|
65
62
|
s.add_dependency(%q<yard>, [">= 0"])
|
@@ -122,6 +122,19 @@ describe "SequelSluggable" do
|
|
122
122
|
:sluggator => Proc.new {|value, model| value.chomp.downcase.gsub(/[^a-z0-9]+/,'_')}
|
123
123
|
Item.create(:name => 'Pavel Kunc').slug.should eql 'pavel_kunc'
|
124
124
|
end
|
125
|
+
|
126
|
+
it "should use only :sluggator Symbol if defined" do
|
127
|
+
Item.plugin :sluggable,
|
128
|
+
:source => :name,
|
129
|
+
:target => :slug,
|
130
|
+
:sluggator => :my_custom_sluggator
|
131
|
+
Item.class_eval do
|
132
|
+
def my_custom_sluggator(v)
|
133
|
+
v.chomp.upcase.gsub(/[^a-zA-Z0-9]+/,'-')
|
134
|
+
end
|
135
|
+
end
|
136
|
+
Item.create(:name => 'Pavel Kunc').slug.should eql 'PAVEL-KUNC'
|
137
|
+
end
|
125
138
|
end
|
126
139
|
|
127
140
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sequel_sluggable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pavel Kunc
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-02-
|
12
|
+
date: 2010-02-15 00:00:00 +00:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -20,7 +20,7 @@ dependencies:
|
|
20
20
|
requirements:
|
21
21
|
- - ">="
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version:
|
23
|
+
version: 3.0.0
|
24
24
|
version:
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: sqlite3-ruby
|
@@ -61,15 +61,12 @@ extensions: []
|
|
61
61
|
extra_rdoc_files:
|
62
62
|
- LICENSE
|
63
63
|
- README.rdoc
|
64
|
-
- TODO
|
65
64
|
files:
|
66
65
|
- .document
|
67
66
|
- .gitignore
|
68
67
|
- LICENSE
|
69
68
|
- README.rdoc
|
70
69
|
- Rakefile
|
71
|
-
- TODO
|
72
|
-
- VERSION
|
73
70
|
- lib/sequel_sluggable.rb
|
74
71
|
- lib/sluggable_rspec_helper.rb
|
75
72
|
- lib/version.rb
|
data/TODO
DELETED
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.0.1
|