slugoid4 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +7 -0
- data/.idea/scopes/scope_settings.xml +5 -0
- data/Gemfile +4 -0
- data/README.markdown +101 -0
- data/Rakefile +11 -0
- data/lib/slugoid/acts/slugoid.rb +63 -0
- data/lib/slugoid/mongoid/criterion/optional.rb +31 -0
- data/lib/slugoid/version.rb +5 -0
- data/lib/slugoid.rb +6 -0
- data/slugoid.gemspec +24 -0
- data/test/test_helper.rb +46 -0
- data/test/unit/test_as_slugoid.rb +71 -0
- data/test/unit/test_mongoid_finder.rb +37 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9c0b633f3004ec696b74052700f8d1ad282e4512
|
4
|
+
data.tar.gz: aabef7318ab0948457e3bcc249635e5d90a3a3cc
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b98ff05ceccf59f071570ba8b85a2a7f46258cbeab86326651a5f145922bc3b034df335862364e0659c199848291c5784affa412693e961c08349290498fda30
|
7
|
+
data.tar.gz: bfa470593641690523b6d364a8cb4bfd0d2b4649202ede5fd4e7659144b84dc827b8926eae03efcfff395afae1f4c97012067810801e23e3fd63a055110bbaaf
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.markdown
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
# slugoid
|
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
|
+
### Attention
|
13
|
+
|
14
|
+
If you're using mongoid 2.0.0.rc.7 or less, you need to use version
|
15
|
+
*0.0.5*
|
16
|
+
|
17
|
+
When mongoid 2.0.0.rc.8 was released there was a change that prevented
|
18
|
+
backwards compatibility.
|
19
|
+
|
20
|
+
# Installation
|
21
|
+
|
22
|
+
## Rails 3
|
23
|
+
|
24
|
+
Include it in your Gemfile:
|
25
|
+
|
26
|
+
gem 'slugoid'
|
27
|
+
|
28
|
+
And run bundler
|
29
|
+
|
30
|
+
bundle install
|
31
|
+
|
32
|
+
# Usage
|
33
|
+
|
34
|
+
To use it, all you have to do is call *acts_as_slugoid* in your Mongoid::Document
|
35
|
+
|
36
|
+
class Project
|
37
|
+
include Mongoid::Document
|
38
|
+
|
39
|
+
acts_as_slugoid
|
40
|
+
end
|
41
|
+
|
42
|
+
By default, this will declare a field on the Document called :slug and will try to generate the slug from
|
43
|
+
a field called :name.
|
44
|
+
|
45
|
+
## Options
|
46
|
+
|
47
|
+
### :generate_from
|
48
|
+
|
49
|
+
If you want to change the field to generate the slug you can use the :generate_from option:
|
50
|
+
|
51
|
+
class Organization
|
52
|
+
include Mongoid::Document
|
53
|
+
|
54
|
+
field :alternative_name
|
55
|
+
acts_as_slugoid :generate_from => :alternative_name
|
56
|
+
end
|
57
|
+
|
58
|
+
This will generate the slug form a field called :alternative_name.
|
59
|
+
|
60
|
+
### :store_as
|
61
|
+
|
62
|
+
If you want to change the field where the slug is stored you can use the :store_as option:
|
63
|
+
|
64
|
+
class Organization
|
65
|
+
include Mongoid::Document
|
66
|
+
|
67
|
+
field :name
|
68
|
+
acts_as_slugoid :store_as => :alternative_slug
|
69
|
+
end
|
70
|
+
|
71
|
+
Now it will store the slug in a field called :alternative_slug. If the specified field is not defined
|
72
|
+
on the Document it will be automatically declared, so adding:
|
73
|
+
|
74
|
+
field :alternative_slug
|
75
|
+
|
76
|
+
is optional.
|
77
|
+
|
78
|
+
## find
|
79
|
+
|
80
|
+
To make things transparent and easy, by default, the find method of the acts_as_slugoid Documents will look for the object by the slug, not the id.
|
81
|
+
|
82
|
+
So, if you had the Project class configured as the example above:
|
83
|
+
|
84
|
+
@project = Project.create(:name => "Name")
|
85
|
+
|
86
|
+
Project.find(@project.to_param) #=> @project
|
87
|
+
|
88
|
+
If, for any reason, you have to look for the object using its id, you have to be explicit:
|
89
|
+
|
90
|
+
@project = Project.create(:name => "Name")
|
91
|
+
|
92
|
+
Project.find(:first, :conditions => {:_id => @project.id}) #=> @project
|
93
|
+
Project.where(:_id => @project.id) #=> @project
|
94
|
+
|
95
|
+
The *find* behavior for the other Mongoid::Documents remains the same.
|
96
|
+
|
97
|
+
# About the Author
|
98
|
+
|
99
|
+
[Crowd Interactive](http://www.crowdint.com) is an American web design and development company that happens to work in Colima, Mexico.
|
100
|
+
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.
|
101
|
+
Find more info [here](http://www.crowdint.com)!
|
data/Rakefile
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
module Acts
|
2
|
+
module Slugoid
|
3
|
+
module ClassMethods
|
4
|
+
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
#
|
8
|
+
# Adds the logic to generate the slug
|
9
|
+
#
|
10
|
+
# Options:
|
11
|
+
#
|
12
|
+
# :generate_from
|
13
|
+
# The name of the field used to generate the slug
|
14
|
+
#
|
15
|
+
# :store_as
|
16
|
+
# The name of the field where the slug will be stored
|
17
|
+
#
|
18
|
+
def acts_as_slugoid(options = {})
|
19
|
+
@acts_as_slugoid_options = {
|
20
|
+
:generate_from => :name,
|
21
|
+
:store_as => :slug
|
22
|
+
}.merge(options)
|
23
|
+
|
24
|
+
generate_from = @acts_as_slugoid_options[:generate_from]
|
25
|
+
store_as = @acts_as_slugoid_options[:store_as]
|
26
|
+
|
27
|
+
class_eval do
|
28
|
+
before_save do
|
29
|
+
generate_slug(generate_from, store_as)
|
30
|
+
end
|
31
|
+
|
32
|
+
field(store_as, :type => String) unless self.respond_to?(store_as)
|
33
|
+
index(store_as)
|
34
|
+
alias_method :to_param!, :to_param
|
35
|
+
|
36
|
+
include InstanceMethods
|
37
|
+
|
38
|
+
def self.acts_as_slugoid_options
|
39
|
+
@acts_as_slugoid_options
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.find_by_slug(slug)
|
43
|
+
where(@acts_as_slugoid_options[:store_as] => slug).first
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
define_method("to_param") do
|
48
|
+
self.send(store_as)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
module InstanceMethods
|
54
|
+
def generate_slug(method, slug_field_name)
|
55
|
+
self.send("#{slug_field_name.to_s}=", self.send(method).parameterize)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def self.included(receiver)
|
60
|
+
receiver::ClassMethods.send :include, ClassMethods
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Mongoid::Criterion
|
2
|
+
module Optional
|
3
|
+
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
# Overwrite the id method to find objects
|
7
|
+
# by the specified slug rather than the id.
|
8
|
+
# If you want to find via id you'll have to use
|
9
|
+
# An explicit finder like:
|
10
|
+
#
|
11
|
+
# where(:_id => some_id)
|
12
|
+
#
|
13
|
+
alias :for_ids! :for_ids
|
14
|
+
|
15
|
+
def for_ids(*ids)
|
16
|
+
ids.flatten!
|
17
|
+
if @klass.respond_to?(:acts_as_slugoid_options) && !ids.first.is_a?(BSON::ObjectId)
|
18
|
+
if ids.size > 1
|
19
|
+
self.in(
|
20
|
+
@klass.acts_as_slugoid_options[:store_as] => ::BSON::ObjectId.cast!(@klass, ids, @klass.primary_key.nil?)
|
21
|
+
)
|
22
|
+
else
|
23
|
+
@selector[@klass.acts_as_slugoid_options[:store_as]] = ids.first
|
24
|
+
end
|
25
|
+
self
|
26
|
+
else
|
27
|
+
for_ids!(*ids)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/slugoid.rb
ADDED
data/slugoid.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "slugoid/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "slugoid4"
|
7
|
+
s.version = Acts::Slugoid::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Irfan Ahmed","David Padilla", "Emmanuel Delgado"]
|
10
|
+
s.email = ["irfan@e-solutionpark.com","david@crowdint.com", "emmanuel@crowdint.com"]
|
11
|
+
s.homepage = "http://rubygems.org/gems/slugoid4"
|
12
|
+
s.summary = %q{Drop-in solution to pretty urls when using Mongoid for rails4}
|
13
|
+
s.description = %q{Drop-in solution to pretty urls when using Mongoid}
|
14
|
+
|
15
|
+
s.add_dependency('mongoid', '~>4.0.0')
|
16
|
+
s.add_development_dependency('bson_ext')
|
17
|
+
s.add_development_dependency('shoulda')
|
18
|
+
|
19
|
+
s.files = ['lib/slugoid/acts/slugoid.rb', 'lib/slugoid/mongoid/criterion/optional.rb' ]+ `git ls-files`.split("\n")
|
20
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
+
s.require_paths = ["lib", "lib/slugoid", "lib/slugoid/acts", "lib/slugoid/mongoid"]
|
23
|
+
s.license = 'MIT'
|
24
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'shoulda'
|
5
|
+
require 'mongoid'
|
6
|
+
require 'slugoid'
|
7
|
+
|
8
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
9
|
+
|
10
|
+
module Acts::Slugoid::Test
|
11
|
+
module Config
|
12
|
+
def setup
|
13
|
+
::Mongoid.configure do |config|
|
14
|
+
name = "slugoid_test"
|
15
|
+
host = "localhost"
|
16
|
+
config.master = Mongo::Connection.new.db(name)
|
17
|
+
config.logger = nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def teardown
|
22
|
+
::Mongoid.master.collections.select {|c| c.name !~ /system/ }.each(&:drop)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Project
|
28
|
+
include Mongoid::Document
|
29
|
+
field :name, :type => String
|
30
|
+
end
|
31
|
+
|
32
|
+
class SlugoidProject
|
33
|
+
include Mongoid::Document
|
34
|
+
field :name, :type => String
|
35
|
+
acts_as_slugoid
|
36
|
+
end
|
37
|
+
|
38
|
+
class Organization
|
39
|
+
include Mongoid::Document
|
40
|
+
|
41
|
+
acts_as_slugoid :generate_from => :alternative_name, :store_as => :alternative_slug
|
42
|
+
|
43
|
+
field :alternative_name, :type => String
|
44
|
+
field :alternative_slug, :type => String
|
45
|
+
end
|
46
|
+
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Acts::Slugoid::Test
|
4
|
+
|
5
|
+
class TestSlugoid < Test::Unit::TestCase
|
6
|
+
|
7
|
+
include Config
|
8
|
+
|
9
|
+
def methods_per_model_assert(expected, model)
|
10
|
+
[:find_by_slug, :find].each do |method|
|
11
|
+
assert_equal(expected, model.send(method, expected.to_param))
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "default parameters" do
|
16
|
+
setup do
|
17
|
+
@slugoid_project = SlugoidProject.create(:name => 'Some name')
|
18
|
+
end
|
19
|
+
|
20
|
+
should "create a slug" do
|
21
|
+
assert_equal('some-name', @slugoid_project.slug)
|
22
|
+
end
|
23
|
+
|
24
|
+
context :to_param do
|
25
|
+
should "return the slug too" do
|
26
|
+
assert_equal('some-name', @slugoid_project.to_param)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context :using_finders do
|
31
|
+
should "find the object" do
|
32
|
+
methods_per_model_assert @slugoid_project, SlugoidProject
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "custom parameters" do
|
38
|
+
setup do
|
39
|
+
@organization = Organization.create(:alternative_name => 'Some Other Name')
|
40
|
+
end
|
41
|
+
|
42
|
+
should "create a slug using the custom field and store it on the custom slug field" do
|
43
|
+
assert_equal('some-other-name', @organization.alternative_slug)
|
44
|
+
end
|
45
|
+
|
46
|
+
context :to_param do
|
47
|
+
should "return the slug too" do
|
48
|
+
assert_equal('some-other-name', @organization.to_param)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context :using_finders do
|
53
|
+
should "find the object" do
|
54
|
+
methods_per_model_assert @organization, Organization
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context :acts_as_slugoid_options do
|
60
|
+
should "respond to acts_as_slugoid_options" do
|
61
|
+
assert_equal(true, SlugoidProject.respond_to?(:acts_as_slugoid_options))
|
62
|
+
end
|
63
|
+
|
64
|
+
should "return the options for acts_as_slugoid" do
|
65
|
+
assert_equal(:name, SlugoidProject.acts_as_slugoid_options[:generate_from])
|
66
|
+
assert_equal(:slug, SlugoidProject.acts_as_slugoid_options[:store_as])
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Acts::Slugoid::Test
|
4
|
+
|
5
|
+
class TestMongoidFinder < Test::Unit::TestCase
|
6
|
+
|
7
|
+
include Config
|
8
|
+
alias :setup! :setup
|
9
|
+
|
10
|
+
def setup
|
11
|
+
setup!
|
12
|
+
@slugoid_project = SlugoidProject.create!(:name => 'Scott')
|
13
|
+
@project = Project.create!(:name => 'Bauer')
|
14
|
+
end
|
15
|
+
|
16
|
+
context "find" do
|
17
|
+
should "return the objects when you pass the id" do
|
18
|
+
assert_equal(@slugoid_project, SlugoidProject.find(@slugoid_project.to_param))
|
19
|
+
assert_equal(@slugoid_project, SlugoidProject.find(@slugoid_project.id))
|
20
|
+
end
|
21
|
+
|
22
|
+
should "work as usual for other models" do
|
23
|
+
assert_equal @project, Project.find(@project.id)
|
24
|
+
assert_equal @project, Project.find(:first, :conditions => {:name => @project.name})
|
25
|
+
assert_equal @project, Project.first(:conditions => {:name => @project.name})
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context "explicit find by id" do
|
30
|
+
should "return the object" do
|
31
|
+
assert_equal(@slugoid_project, SlugoidProject.where(:_id => @slugoid_project.id).first)
|
32
|
+
assert_equal(@slugoid_project, SlugoidProject.find(:first, :conditions => {:_id => @slugoid_project.id}))
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: slugoid4
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Irfan Ahmed
|
8
|
+
- David Padilla
|
9
|
+
- Emmanuel Delgado
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2013-10-16 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: mongoid
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 4.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: 4.0.0
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: bson_ext
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
type: :development
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: shoulda
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
type: :development
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
description: Drop-in solution to pretty urls when using Mongoid
|
58
|
+
email:
|
59
|
+
- irfan@e-solutionpark.com
|
60
|
+
- david@crowdint.com
|
61
|
+
- emmanuel@crowdint.com
|
62
|
+
executables: []
|
63
|
+
extensions: []
|
64
|
+
extra_rdoc_files: []
|
65
|
+
files:
|
66
|
+
- lib/slugoid/acts/slugoid.rb
|
67
|
+
- lib/slugoid/mongoid/criterion/optional.rb
|
68
|
+
- .gitignore
|
69
|
+
- .idea/scopes/scope_settings.xml
|
70
|
+
- Gemfile
|
71
|
+
- README.markdown
|
72
|
+
- Rakefile
|
73
|
+
- lib/slugoid.rb
|
74
|
+
- lib/slugoid/version.rb
|
75
|
+
- slugoid.gemspec
|
76
|
+
- test/test_helper.rb
|
77
|
+
- test/unit/test_as_slugoid.rb
|
78
|
+
- test/unit/test_mongoid_finder.rb
|
79
|
+
homepage: http://rubygems.org/gems/slugoid4
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata: {}
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
- lib/slugoid
|
88
|
+
- lib/slugoid/acts
|
89
|
+
- lib/slugoid/mongoid
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
requirements: []
|
101
|
+
rubyforge_project:
|
102
|
+
rubygems_version: 2.0.3
|
103
|
+
signing_key:
|
104
|
+
specification_version: 4
|
105
|
+
summary: Drop-in solution to pretty urls when using Mongoid for rails4
|
106
|
+
test_files: []
|