acts_as_sluggable 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +37 -0
- data/README.markdown +74 -0
- data/Rakefile +9 -0
- data/acts_as_sluggable.gemspec +25 -0
- data/lib/acts_as_sluggable.rb +6 -0
- data/lib/acts_as_sluggable/acts/sluggable.rb +47 -0
- data/lib/acts_as_sluggable/version.rb +5 -0
- data/test/test_helper.rb +10 -0
- data/test/unit/test_as_sluggable.rb +65 -0
- metadata +128 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
acts_as_sluggable (0.0.1)
|
5
|
+
mongoid (>= 2.0.0.beta.17)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: http://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activemodel (3.0.1)
|
11
|
+
activesupport (= 3.0.1)
|
12
|
+
builder (~> 2.1.2)
|
13
|
+
i18n (~> 0.4.1)
|
14
|
+
activesupport (3.0.1)
|
15
|
+
bson (1.1.1)
|
16
|
+
bson_ext (1.1.1)
|
17
|
+
builder (2.1.2)
|
18
|
+
i18n (0.4.2)
|
19
|
+
mongo (1.0.9)
|
20
|
+
bson (>= 1.0.5)
|
21
|
+
mongoid (2.0.0.beta.19)
|
22
|
+
activemodel (~> 3.0)
|
23
|
+
mongo (= 1.0.9)
|
24
|
+
tzinfo (~> 0.3.22)
|
25
|
+
will_paginate (~> 3.0.pre)
|
26
|
+
shoulda (2.11.3)
|
27
|
+
tzinfo (0.3.23)
|
28
|
+
will_paginate (3.0.pre2)
|
29
|
+
|
30
|
+
PLATFORMS
|
31
|
+
ruby
|
32
|
+
|
33
|
+
DEPENDENCIES
|
34
|
+
acts_as_sluggable!
|
35
|
+
bson_ext (~> 1.1.1)
|
36
|
+
mongoid (>= 2.0.0.beta.17)
|
37
|
+
shoulda (~> 2.11.3)
|
data/README.markdown
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# acts_as_sluggable
|
2
|
+
|
3
|
+
Whenever you use MongoDB and Mongoid, there's a high chance you will end up preparing your urls
|
4
|
+
with a slug instead of the traditional ids because MongoDB ids are ugly.
|
5
|
+
|
6
|
+
This gem will help you generate slugs in an easy way
|
7
|
+
|
8
|
+
## Compatibility
|
9
|
+
|
10
|
+
So far it works with the Rails 3 version of Mongoid.
|
11
|
+
|
12
|
+
# Installation
|
13
|
+
|
14
|
+
## Rails 3
|
15
|
+
|
16
|
+
Include it in your Gemfile:
|
17
|
+
|
18
|
+
gem 'acts_as_sluggable'
|
19
|
+
|
20
|
+
And run bundler
|
21
|
+
|
22
|
+
bundle install
|
23
|
+
|
24
|
+
# Usage
|
25
|
+
|
26
|
+
To use it, all you have to do is call *acts_as_sluggable* in your Mongoid::Document
|
27
|
+
|
28
|
+
class Project
|
29
|
+
include Mongoid::Document
|
30
|
+
|
31
|
+
acts_as_sluggable
|
32
|
+
end
|
33
|
+
|
34
|
+
By default, this will declare a field on the Document called :slug and will try to generate the slug from
|
35
|
+
a field called :name.
|
36
|
+
|
37
|
+
## Options
|
38
|
+
|
39
|
+
### :generate_from
|
40
|
+
|
41
|
+
If you want to change the field to generate the slug you can use the :generate_from option:
|
42
|
+
|
43
|
+
class Organization
|
44
|
+
include Mongoid::Document
|
45
|
+
|
46
|
+
field :alternative_name
|
47
|
+
acts_as_sluggable :generate_from => :alternative_name
|
48
|
+
end
|
49
|
+
|
50
|
+
This will generate the slug form a field called :alternative_name.
|
51
|
+
|
52
|
+
### :store_as
|
53
|
+
|
54
|
+
If you want to change the field where the slug is stored you can use the :store_as option:
|
55
|
+
|
56
|
+
class Organization
|
57
|
+
include Mongoid::Document
|
58
|
+
|
59
|
+
field :name
|
60
|
+
acts_as_sluggable :store_as => :alternative_slug
|
61
|
+
end
|
62
|
+
|
63
|
+
Now it will store the slug in a field called :alternative_slug. If the specified field is not defined
|
64
|
+
on the Document it will be automatically declared, so adding:
|
65
|
+
|
66
|
+
field :alternative_slug
|
67
|
+
|
68
|
+
is optional.
|
69
|
+
|
70
|
+
# About the Author
|
71
|
+
|
72
|
+
[Crowd Interactive](http://www.crowdint.com) is an American web design and development company that happens to work in Colima, Mexico.
|
73
|
+
We specialize in building and growing online retail stores. We don’t work with everyone – just companies we believe in. Call us today to see if there’s a fit.
|
74
|
+
Find more info [here](http://www.crowdint.com)!
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "acts_as_sluggable/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "acts_as_sluggable"
|
7
|
+
s.version = Acts::Sluggable::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["David Padilla"]
|
10
|
+
s.email = ["david@crowdint.com"]
|
11
|
+
s.homepage = "http://rubygems.org/gems/acts_as_sluggable"
|
12
|
+
s.summary = %q{Create a slug for mongoid documents}
|
13
|
+
s.description = %q{Create a slug for mongoid documents}
|
14
|
+
|
15
|
+
s.add_dependency('mongoid', '>= 2.0.0.beta.17')
|
16
|
+
s.add_development_dependency('shoulda', '~>2.11.3')
|
17
|
+
s.add_development_dependency('bson_ext', '~>1.1.1')
|
18
|
+
|
19
|
+
s.rubyforge_project = "acts_as_sluggable"
|
20
|
+
|
21
|
+
s.files = `git ls-files`.split("\n")
|
22
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
23
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
24
|
+
s.require_paths = ["lib"]
|
25
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Acts
|
2
|
+
module Sluggable
|
3
|
+
module ClassMethods
|
4
|
+
#
|
5
|
+
# Adds the logic to generate the slug
|
6
|
+
#
|
7
|
+
# Options:
|
8
|
+
#
|
9
|
+
# :generate_from
|
10
|
+
# The name of the field used to generate the slug
|
11
|
+
#
|
12
|
+
# :store_as
|
13
|
+
# The name of the field where the slug will be stored
|
14
|
+
#
|
15
|
+
def acts_as_sluggable(options = {})
|
16
|
+
options = {
|
17
|
+
:generate_from => :name,
|
18
|
+
:store_as => :slug
|
19
|
+
}.merge(options)
|
20
|
+
|
21
|
+
class_eval do
|
22
|
+
before_save do
|
23
|
+
generate_slug(options[:generate_from], options[:store_as])
|
24
|
+
end
|
25
|
+
field(options[:store_as], :type => String) unless self.respond_to?(options[:store_as])
|
26
|
+
index(options[:store_as])
|
27
|
+
alias_method :to_param!, :to_param
|
28
|
+
end
|
29
|
+
|
30
|
+
define_method("to_param") do
|
31
|
+
self.send(options[:store_as])
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
module InstanceMethods
|
37
|
+
def generate_slug(method, slug_field_name)
|
38
|
+
self.send("#{slug_field_name.to_s}=", self.send(method).parameterize)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.included(receiver)
|
43
|
+
receiver::ClassMethods.send :include, ClassMethods
|
44
|
+
receiver.send :include, InstanceMethods
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class Project
|
4
|
+
include Mongoid::Document
|
5
|
+
|
6
|
+
acts_as_sluggable
|
7
|
+
|
8
|
+
field :name, :type => String
|
9
|
+
end
|
10
|
+
|
11
|
+
class Organization
|
12
|
+
include Mongoid::Document
|
13
|
+
|
14
|
+
acts_as_sluggable :generate_from => :alternative_name, :store_as => :alternative_slug
|
15
|
+
|
16
|
+
field :alternative_name, :type => String
|
17
|
+
field :alternative_slug, :type => String
|
18
|
+
end
|
19
|
+
|
20
|
+
class TestActsAsSluggable < Test::Unit::TestCase
|
21
|
+
def setup
|
22
|
+
::Mongoid.configure do |config|
|
23
|
+
name = "acts_as_sluggable_test"
|
24
|
+
host = "localhost"
|
25
|
+
config.master = Mongo::Connection.new.db(name)
|
26
|
+
config.logger = nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context "default parameters" do
|
31
|
+
setup do
|
32
|
+
@project = Project.create(:name => 'Some name')
|
33
|
+
end
|
34
|
+
|
35
|
+
should "create a slug" do
|
36
|
+
assert_equal('some-name', @project.slug)
|
37
|
+
end
|
38
|
+
|
39
|
+
context :to_param do
|
40
|
+
should "return the slug too" do
|
41
|
+
assert_equal('some-name', @project.to_param)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "custom parameters" do
|
47
|
+
setup do
|
48
|
+
@organization = Organization.create(:alternative_name => 'Some Other Name')
|
49
|
+
end
|
50
|
+
|
51
|
+
should "create a slug using the custom field and store it on the custom slug field" do
|
52
|
+
assert_equal('some-other-name', @organization.alternative_slug)
|
53
|
+
end
|
54
|
+
|
55
|
+
context :to_param do
|
56
|
+
should "return the slug too" do
|
57
|
+
assert_equal('some-other-name', @organization.to_param)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def teardown
|
63
|
+
::Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop)
|
64
|
+
end
|
65
|
+
end
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_sluggable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- David Padilla
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-10-27 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: mongoid
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 62196417
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 0
|
33
|
+
- 0
|
34
|
+
- beta
|
35
|
+
- 17
|
36
|
+
version: 2.0.0.beta.17
|
37
|
+
type: :runtime
|
38
|
+
version_requirements: *id001
|
39
|
+
- !ruby/object:Gem::Dependency
|
40
|
+
name: shoulda
|
41
|
+
prerelease: false
|
42
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
hash: 37
|
48
|
+
segments:
|
49
|
+
- 2
|
50
|
+
- 11
|
51
|
+
- 3
|
52
|
+
version: 2.11.3
|
53
|
+
type: :development
|
54
|
+
version_requirements: *id002
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bson_ext
|
57
|
+
prerelease: false
|
58
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ~>
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
hash: 17
|
64
|
+
segments:
|
65
|
+
- 1
|
66
|
+
- 1
|
67
|
+
- 1
|
68
|
+
version: 1.1.1
|
69
|
+
type: :development
|
70
|
+
version_requirements: *id003
|
71
|
+
description: Create a slug for mongoid documents
|
72
|
+
email:
|
73
|
+
- david@crowdint.com
|
74
|
+
executables: []
|
75
|
+
|
76
|
+
extensions: []
|
77
|
+
|
78
|
+
extra_rdoc_files: []
|
79
|
+
|
80
|
+
files:
|
81
|
+
- .gitignore
|
82
|
+
- Gemfile
|
83
|
+
- Gemfile.lock
|
84
|
+
- README.markdown
|
85
|
+
- Rakefile
|
86
|
+
- acts_as_sluggable.gemspec
|
87
|
+
- lib/acts_as_sluggable.rb
|
88
|
+
- lib/acts_as_sluggable/acts/sluggable.rb
|
89
|
+
- lib/acts_as_sluggable/version.rb
|
90
|
+
- test/test_helper.rb
|
91
|
+
- test/unit/test_as_sluggable.rb
|
92
|
+
has_rdoc: true
|
93
|
+
homepage: http://rubygems.org/gems/acts_as_sluggable
|
94
|
+
licenses: []
|
95
|
+
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
none: false
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
hash: 3
|
107
|
+
segments:
|
108
|
+
- 0
|
109
|
+
version: "0"
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
hash: 3
|
116
|
+
segments:
|
117
|
+
- 0
|
118
|
+
version: "0"
|
119
|
+
requirements: []
|
120
|
+
|
121
|
+
rubyforge_project: acts_as_sluggable
|
122
|
+
rubygems_version: 1.3.7
|
123
|
+
signing_key:
|
124
|
+
specification_version: 3
|
125
|
+
summary: Create a slug for mongoid documents
|
126
|
+
test_files:
|
127
|
+
- test/test_helper.rb
|
128
|
+
- test/unit/test_as_sluggable.rb
|