mongoid_slug 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Hakan Ensari
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.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = Mongoid Slug
2
+
3
+ This gem generates a URL slug/permalink based on a field in a Mongoid model.
4
+
5
+ class Book
6
+ include Mongoid::Document
7
+ include Mongoid::Slug
8
+ field :title
9
+ slug :title
10
+ end
11
+
12
+ >> book = Book.create(:title => "A Thousand Plateaus")
13
+ >> book.to_param
14
+ "a-thousand-plateaus"
15
+ >> book.update_attributes(:title => "Anti Oedipus")
16
+ >> book.to_param
17
+ "anti-oedipus"
@@ -0,0 +1,44 @@
1
+ # Generates a URL slug/permalink based on a field in a Mongoid model.
2
+ module Mongoid::Slug
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ base.class_eval { field :slug; before_save :slugify }
7
+ end
8
+
9
+ module ClassMethods #:nodoc:
10
+
11
+ # Set a field as source of slug
12
+ def slug(field)
13
+ class_variable_set(:@@slugged_field, field.to_sym)
14
+ end
15
+
16
+ def find_by_slug(slug)
17
+ where(:slug => slug).first
18
+ end
19
+ end
20
+
21
+ def to_param
22
+ slug
23
+ end
24
+
25
+ private
26
+
27
+ def slugify
28
+ self.slug = find_unique_slug if new_record? || self.send((self.class.class_eval('@@slugged_field').to_s + '_changed?').to_sym)
29
+ end
30
+
31
+ def find_unique_slug(suffix='')
32
+ slug = ("#{slugged_field} #{suffix}").parameterize
33
+ if collection.find(:slug => slug).count == 0
34
+ slug
35
+ else
36
+ new_suffix = suffix.blank? ? '1' : "#{suffix.to_i + 1}"
37
+ find_unique_slug(new_suffix)
38
+ end
39
+ end
40
+
41
+ def slugged_field
42
+ self.send(self.class.class_eval('@@slugged_field'))
43
+ end
44
+ end
@@ -0,0 +1,7 @@
1
+ class Book
2
+ include Mongoid::Document
3
+ include Mongoid::Slug
4
+ field :title
5
+ field :authors
6
+ slug :title
7
+ end
@@ -0,0 +1,27 @@
1
+ require "rubygems"
2
+ require "bundler"
3
+ Bundler.require(:default)
4
+
5
+ require File.expand_path("../../lib/mongoid_slug", __FILE__)
6
+ Dir["#{File.dirname(__FILE__)}/models/*.rb"].each { |f| require f }
7
+
8
+ Mongoid.configure do |config|
9
+ name = "mongoid_slug_test"
10
+ host = "localhost"
11
+ config.master = Mongo::Connection.new.db(name)
12
+ end
13
+
14
+ DatabaseCleaner.orm = "mongoid"
15
+ Rspec.configure do |config|
16
+ config.before(:all) do
17
+ DatabaseCleaner.strategy = :truncation
18
+ end
19
+
20
+ config.before(:each) do
21
+ DatabaseCleaner.start
22
+ end
23
+
24
+ config.after(:each) do
25
+ DatabaseCleaner.clean
26
+ end
27
+ end
@@ -0,0 +1,40 @@
1
+ require "spec_helper"
2
+
3
+ describe Mongoid::Slug do
4
+ before(:each) do
5
+ @book = ::Book.create(:title => "A Thousand Plateaus", :authors => "Gilles Deleuze, Félix Guattari")
6
+ end
7
+
8
+ it "generates slug" do
9
+ @book.to_param.should eql @book.title.parameterize
10
+ end
11
+
12
+ it "updates slug" do
13
+ @book.update_attributes(:title => "Anti Oedipus")
14
+ @book.reload.to_param.should eql "Anti Oedipus".parameterize
15
+ end
16
+
17
+ it "generates a unique slug" do
18
+ similar_book = Book.create(:title => @book.title)
19
+ similar_book.to_param.should_not eql @book.to_param
20
+ end
21
+
22
+ it "appends a counter when slug is not unique" do
23
+ similar_book = Book.create(:title => @book.title)
24
+ similar_book.slug.should match /\d$/
25
+ end
26
+
27
+ it "does not append a counter when slug is unique" do
28
+ @book.slug.should_not match /\d$/
29
+ end
30
+
31
+ it "does not update slug if slugged field has not changed" do
32
+ existing_slug = @book.slug
33
+ @book.update_attributes('authors' => "Gilles Deleuze")
34
+ @book.slug.should eql existing_slug
35
+ end
36
+
37
+ it "finds by slug" do
38
+ Book.find_by_slug(@book.slug).should eql @book
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_slug
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Hakan Ensari
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-06-22 00:00:00 +01: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: -1848230041
30
+ segments:
31
+ - 2
32
+ - 0
33
+ - 0
34
+ - beta7
35
+ version: 2.0.0.beta7
36
+ type: :runtime
37
+ version_requirements: *id001
38
+ description: Mongoid Slug generates a URL slug/permalink based on a field in a Mongoid model.
39
+ email: code@papercavalier.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files:
45
+ - LICENSE
46
+ - README.rdoc
47
+ files:
48
+ - LICENSE
49
+ - README.rdoc
50
+ - lib/mongoid_slug.rb
51
+ - spec/models/book.rb
52
+ - spec/spec_helper.rb
53
+ - spec/unit/mongoid_slug_spec.rb
54
+ has_rdoc: true
55
+ homepage: http://github.com/papercavalier/mongoid-slug
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options:
60
+ - --charset=UTF-8
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 3
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.3.7
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Generates a URL slug in a Mongoid model
88
+ test_files:
89
+ - spec/models/book.rb
90
+ - spec/spec_helper.rb
91
+ - spec/unit/mongoid_slug_spec.rb