mongoid-slugs 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid-slugs.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Evan Sagge
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Mongoid::Slugs
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'mongoid-slugs'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mongoid-slugs
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ task :default => :spec
6
+
7
+ RSpec::Core::RakeTask.new(:spec) do |spec|
8
+ spec.pattern = "./spec/**/*_spec.rb"
9
+ end
10
+
11
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
12
+ spec.pattern = "./spec/**/*_spec.rb"
13
+ spec.rcov = true
14
+ end
@@ -0,0 +1,2 @@
1
+ require "mongoid/slugs"
2
+ require "mongoid/slugs/version"
@@ -0,0 +1,64 @@
1
+ module Mongoid::Slugs
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ field :slug, type: String
6
+
7
+ index({ slug: -1 }, { unique: true })
8
+
9
+ before_save :generate_slug
10
+ end
11
+
12
+ module ClassMethods
13
+ def slug_on(field)
14
+ @@slug_field ||= {}
15
+ @@slug_field[self.name] = field.to_sym
16
+ end
17
+
18
+ def slug_field
19
+ @@slug_field ||= {}
20
+ @@slug_field[self.name]
21
+ end
22
+
23
+ def find_by_slug(slug)
24
+ find_by(slug: slug)
25
+ end
26
+ end
27
+
28
+ def to_param
29
+ slug
30
+ end
31
+
32
+ protected
33
+
34
+ def generate_slug
35
+ raise Mongoid::Sluggable::Errors::UndefinedField.new(self.class) if self.class.slug_field.blank?
36
+ raise Mongoid::Sluggable::Errors::UnrecognizedField.new(self.class, self.class.slug_field) unless self.class.fields.has_key?(self.class.slug_field.to_s)
37
+
38
+ base_slug = send(self.class.slug_field).parameterize
39
+
40
+ pattern = /^#{Regexp.escape(base_slug)}(?:-(\d{1,2}))?$/
41
+ existing_slugs = self.class.where(slug: pattern).ne(_id: _id).only(:slug).map(&:slug)
42
+ if existing_slugs.any?
43
+ max_counter = existing_slugs.map { |s| (pattern.match(s)[1] || 0 ).to_i }.max
44
+ base_slug << "-#{max_counter + 1}"
45
+ end
46
+
47
+ self.slug = base_slug
48
+ end
49
+
50
+ module Errors
51
+ class UndefinedField < Mongoid::Errors::MongoidError
52
+ def initialize(klass)
53
+ super "Sluggable field needs to be defined on #{klass.name} using :slug_on"
54
+ end
55
+ end
56
+
57
+ class UnrecognizedField < Mongoid::Errors::MongoidError
58
+ def initialize(klass, field)
59
+ super "#{klass.name} needs to have #{field.to_sym} defined as a field to create a slug from it"
60
+ end
61
+ end
62
+ end
63
+
64
+ end
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module Slugs
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mongoid/slugs/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "mongoid-slugs"
8
+ gem.version = Mongoid::Slugs::VERSION
9
+ gem.authors = ["Evan Sagge"]
10
+ gem.email = ["evansagge@gmail.com"]
11
+ gem.homepage = %q{http://github.com/evansagge/mongoid-slugs}
12
+ gem.description = %q{Simple slugging for Mongoid models}
13
+ gem.summary = %q{Simple slugging for Mongoid models}
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency 'rake'
21
+ gem.add_dependency 'mongoid', '>= 3.0.1'
22
+ gem.add_dependency 'rspec', '>= 2.9'
23
+ end
@@ -0,0 +1,29 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "lib"))
3
+ MODELS = File.join(File.dirname(__FILE__), "models")
4
+ $LOAD_PATH.unshift(MODELS)
5
+
6
+ require "rubygems"
7
+ require "bundler"
8
+ Bundler.setup
9
+
10
+ require 'mongoid'
11
+ require 'mongoid/slugs'
12
+ require 'rspec'
13
+ require 'rspec/core'
14
+ require 'rspec/expectations'
15
+
16
+ Mongoid.configure do |config|
17
+ config.connect_to("mongoid-slugs-test")
18
+ end
19
+
20
+ Dir[ File.join(MODELS, "*.rb") ].sort.each { |file| require File.basename(file) }
21
+
22
+
23
+ RSpec.configure do |config|
24
+ config.include RSpec::Matchers
25
+ config.mock_with :rspec
26
+ config.after :all do
27
+ Mongoid::Config.purge!
28
+ end
29
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Slugs do
4
+ before do
5
+ class TestClass
6
+ include Mongoid::Document
7
+ include Mongoid::Slugs
8
+
9
+ field :title
10
+ end
11
+ end
12
+
13
+ describe "sluggable model" do
14
+ it "has a :slug field" do
15
+ expect(TestClass.fields).to have_key("slug")
16
+ expect(TestClass.fields["slug"].options[:type]).to eq String
17
+ end
18
+
19
+ describe ".slug_on" do
20
+ before do
21
+ TestClass.slug_on :title
22
+ end
23
+
24
+ it "sets the .slug_field" do
25
+ expect(TestClass.slug_field).to eq :title
26
+ end
27
+ end
28
+ end
29
+
30
+ describe "sluggable object" do
31
+ before do
32
+ TestClass.slug_on :title
33
+ end
34
+
35
+ let!(:sluggable_object) do
36
+ TestClass.new(title: "This is a test slug")
37
+ end
38
+
39
+ describe "#save" do
40
+ before do
41
+ sluggable_object.save
42
+ end
43
+
44
+ it "sets the slug" do
45
+ expect(sluggable_object.slug).to eq "this-is-a-test-slug"
46
+ end
47
+ end
48
+ end
49
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-slugs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Evan Sagge
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: mongoid
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 3.0.1
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 3.0.1
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '2.9'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '2.9'
62
+ description: Simple slugging for Mongoid models
63
+ email:
64
+ - evansagge@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - lib/mongoid-slugs.rb
75
+ - lib/mongoid/slugs.rb
76
+ - lib/mongoid/slugs/version.rb
77
+ - mongoid-slugs.gemspec
78
+ - spec/spec_helper.rb
79
+ - spec/unit/mongoid/slugs_spec.rb
80
+ homepage: http://github.com/evansagge/mongoid-slugs
81
+ licenses: []
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.23
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Simple slugging for Mongoid models
104
+ test_files:
105
+ - spec/spec_helper.rb
106
+ - spec/unit/mongoid/slugs_spec.rb
107
+ has_rdoc: