mongoid-sleeping_king_studios 0.2.0
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.
- checksums.yaml +7 -0
- data/LICENSE +22 -0
- data/README.md +53 -0
- data/lib/mongoid/sleeping_king_studios.rb +7 -0
- data/lib/mongoid/sleeping_king_studios/sluggable.rb +92 -0
- data/lib/mongoid/sleeping_king_studios/tree.rb +66 -0
- data/lib/mongoid/sleeping_king_studios/version.rb +7 -0
- metadata +166 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 69ea55c009d98431ff13407f84c1b210fc5ac89d
|
|
4
|
+
data.tar.gz: cf358c4f2146e0b3bfcad4b92bb1697ecaa3e565
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 2cf1a558a348b53842e181242b37368f2ee50f2fc3af52f8172a851d46f26841ac68f8801ef1350462ce751fba7c251cbbee65a1d9caf3d503ea6fe7d4c0e9bb
|
|
7
|
+
data.tar.gz: 8a67abe9b88d4ddd7879b3e5317adbc704bd7af0d603e44d53c5aae910c5099bc275b7b77cf5c98a2a8b573c95a8d0106329cedf074c5aa765de9080d270f833
|
data/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
(The MIT License)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013 Rob Smith
|
|
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 NONINFRINGEMENT.
|
|
19
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
20
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
21
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
22
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# Mongoid::SleepingKingStudios
|
|
2
|
+
|
|
3
|
+
A collection of concerns and extensions to add functionality to Mongoid
|
|
4
|
+
documents and collections.
|
|
5
|
+
|
|
6
|
+
## The Mixins
|
|
7
|
+
|
|
8
|
+
### Sluggable
|
|
9
|
+
|
|
10
|
+
require 'mongoid/sleeping_king_studios/sluggable'
|
|
11
|
+
|
|
12
|
+
Adds a slug field that automatically tracks a base attribute and stores a
|
|
13
|
+
short, url-friendly version.
|
|
14
|
+
|
|
15
|
+
**How To Use:**
|
|
16
|
+
|
|
17
|
+
class SluggableDocument
|
|
18
|
+
include Mongoid::Document
|
|
19
|
+
include Mongoid::SleepingKingStudios::Sluggable
|
|
20
|
+
|
|
21
|
+
field :title, :type => String
|
|
22
|
+
|
|
23
|
+
slugify :title
|
|
24
|
+
end # class
|
|
25
|
+
|
|
26
|
+
#### Options
|
|
27
|
+
|
|
28
|
+
##### Lockable
|
|
29
|
+
|
|
30
|
+
slugify :title, :lockable => true
|
|
31
|
+
|
|
32
|
+
Allows the slug to be specified manually. Adds an additional slug_lock field
|
|
33
|
+
that is automatically set to true when #slug= is called. To resume tracking the
|
|
34
|
+
base attribute, set :slug_lock to false.
|
|
35
|
+
|
|
36
|
+
### Tree
|
|
37
|
+
|
|
38
|
+
require 'mongoid/sleeping_king_studios/tree'
|
|
39
|
+
|
|
40
|
+
Sets up a basic tree structure by adding belongs_to :parent and has_many
|
|
41
|
+
:children relations, as well as some helper methods.
|
|
42
|
+
|
|
43
|
+
**How To Use:**
|
|
44
|
+
|
|
45
|
+
class TreeDocument
|
|
46
|
+
include Mongoid::Document
|
|
47
|
+
include Mongoid::SleepingKingStudios::Tree
|
|
48
|
+
end # class
|
|
49
|
+
|
|
50
|
+
## License
|
|
51
|
+
|
|
52
|
+
RSpec::SleepingKingStudios is released under the
|
|
53
|
+
[MIT License](http://www.opensource.org/licenses/MIT).
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
# lib/mongoid/sleeping_king_studios/sluggable.rb
|
|
2
|
+
|
|
3
|
+
require 'mongoid/sleeping_king_studios'
|
|
4
|
+
|
|
5
|
+
module Mongoid::SleepingKingStudios
|
|
6
|
+
# Adds a :slug field that stores a short, url-friendly reference string,
|
|
7
|
+
# useful for human-readable urls. By default, the slug field is
|
|
8
|
+
# automatically overwritten from the specified base attribute before
|
|
9
|
+
# validation. To enable setting the slug manually, use the :lockable
|
|
10
|
+
# option; otherwise, the :slug= writer is set to private.
|
|
11
|
+
#
|
|
12
|
+
# @example Setting up the slug:
|
|
13
|
+
# class SluggableDocument
|
|
14
|
+
# include Mongoid::Document
|
|
15
|
+
# include Mongoid::SleepingKingStudios::Sluggable
|
|
16
|
+
#
|
|
17
|
+
# field :title, :type => String
|
|
18
|
+
#
|
|
19
|
+
# slugify :title
|
|
20
|
+
# end # class
|
|
21
|
+
#
|
|
22
|
+
# @see ClassMethods#slugify
|
|
23
|
+
#
|
|
24
|
+
# @since 0.1.0
|
|
25
|
+
module Sluggable
|
|
26
|
+
extend ActiveSupport::Concern
|
|
27
|
+
|
|
28
|
+
# @!attribute [r] slug
|
|
29
|
+
# A url-friendly short string version of the specified base attribute.
|
|
30
|
+
# Read-only unless the :lockable option is selected.
|
|
31
|
+
#
|
|
32
|
+
# @return [String] the slug value
|
|
33
|
+
|
|
34
|
+
# Class methods added to the base class via #extend.
|
|
35
|
+
module ClassMethods
|
|
36
|
+
# Returns the options passed into ::slugify, as well as an additional
|
|
37
|
+
# :attribute value for the attribute passed into ::slugify.
|
|
38
|
+
attr_reader :sluggable_options
|
|
39
|
+
|
|
40
|
+
# @overload slugify attribute, options = {}
|
|
41
|
+
# Creates the :slug field and sets up the callback and validations.
|
|
42
|
+
#
|
|
43
|
+
# @param [String, Symbol] attribute
|
|
44
|
+
# @option options [Boolean] :lockable
|
|
45
|
+
# The :lockable option allows the manual setting of the :slug field.
|
|
46
|
+
# To do so, it adds an additional :slug_lock field, which defaults to
|
|
47
|
+
# false but is set to true whenever #slug= is called. If the slug is
|
|
48
|
+
# locked, its value is not updated to track the base attribute. To
|
|
49
|
+
# resume tracking the base attribute, set :slug_lock to false.
|
|
50
|
+
def slugify attribute, **options
|
|
51
|
+
@sluggable_options = options.merge :attribute => attribute.to_s.intern
|
|
52
|
+
|
|
53
|
+
field :slug, :type => String
|
|
54
|
+
|
|
55
|
+
validates :slug,
|
|
56
|
+
:presence => true,
|
|
57
|
+
:format => {
|
|
58
|
+
:with => /\A[a-z0-9\-]+\z/,
|
|
59
|
+
:message => 'must be lower-case characters a-z, digits 0-9, and hyphens "-"'
|
|
60
|
+
} # end format
|
|
61
|
+
|
|
62
|
+
before_validation do
|
|
63
|
+
return if self.class.sluggable_options[:lockable] && self.slug_lock
|
|
64
|
+
self[:slug] = to_slug
|
|
65
|
+
end # before validation callback
|
|
66
|
+
|
|
67
|
+
# Lockable
|
|
68
|
+
if options.fetch(:lockable, false)
|
|
69
|
+
field :slug_lock, :type => Boolean, :default => false
|
|
70
|
+
|
|
71
|
+
self.class_eval do
|
|
72
|
+
def slug= value
|
|
73
|
+
super
|
|
74
|
+
self.slug_lock = true
|
|
75
|
+
end # method slug=
|
|
76
|
+
end # class eval
|
|
77
|
+
else
|
|
78
|
+
private :slug=
|
|
79
|
+
end # if
|
|
80
|
+
end # class method slugify
|
|
81
|
+
end # module
|
|
82
|
+
|
|
83
|
+
# Processes the specified base attribute and returns a valid slug value. By
|
|
84
|
+
# default, calls String#parameterize.
|
|
85
|
+
#
|
|
86
|
+
# @return [String] the processed value
|
|
87
|
+
def to_slug
|
|
88
|
+
raw = (self.send self.class.sluggable_options[:attribute]) || ""
|
|
89
|
+
raw.parameterize
|
|
90
|
+
end # method to_slug
|
|
91
|
+
end # module
|
|
92
|
+
end # module
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# lib/mongoid/sleeping_king_studios/tree.rb
|
|
2
|
+
|
|
3
|
+
require 'mongoid/sleeping_king_studios'
|
|
4
|
+
|
|
5
|
+
module Mongoid::SleepingKingStudios
|
|
6
|
+
# Adds a belongs_to parent relation and a has_many children relation to set
|
|
7
|
+
# up a basic tree structure, as well as several helper methods.
|
|
8
|
+
#
|
|
9
|
+
# @example Setting up the tree:
|
|
10
|
+
# class SluggableDocument
|
|
11
|
+
# include Mongoid::Document
|
|
12
|
+
# include Mongoid::SleepingKingStudios::Tree
|
|
13
|
+
# end # class
|
|
14
|
+
#
|
|
15
|
+
# @since 0.2.0
|
|
16
|
+
module Tree
|
|
17
|
+
extend ActiveSupport::Concern
|
|
18
|
+
|
|
19
|
+
# @!method parent
|
|
20
|
+
# Returns the parent object, or nil if the object is a root.
|
|
21
|
+
#
|
|
22
|
+
# @return [Tree, nil]
|
|
23
|
+
|
|
24
|
+
# @!method children
|
|
25
|
+
# Returns the list of child objects.
|
|
26
|
+
#
|
|
27
|
+
# @return [Array<Tree>]
|
|
28
|
+
|
|
29
|
+
included do |base|
|
|
30
|
+
belongs_to :parent, :class_name => base.name, :inverse_of => :children
|
|
31
|
+
has_many :children, :class_name => base.name, :inverse_of => :parent
|
|
32
|
+
end # included
|
|
33
|
+
|
|
34
|
+
# Class methods added to the base class via #extend.
|
|
35
|
+
module ClassMethods
|
|
36
|
+
# Returns a Criteria specifying all root objects, e.g. objects with no
|
|
37
|
+
# parent object.
|
|
38
|
+
#
|
|
39
|
+
# @return [Mongoid::Criteria]
|
|
40
|
+
def roots
|
|
41
|
+
where({ :parent_id => nil })
|
|
42
|
+
end # scope routes
|
|
43
|
+
end # module
|
|
44
|
+
|
|
45
|
+
# Returns the root object of the current object's tree.
|
|
46
|
+
#
|
|
47
|
+
# @return [Tree]
|
|
48
|
+
def root
|
|
49
|
+
parent ? parent.root : self
|
|
50
|
+
end # method root
|
|
51
|
+
|
|
52
|
+
# Returns true if the object is a leaf object, e.g. has no child objects.
|
|
53
|
+
#
|
|
54
|
+
# @return [Boolean] true if the object is a leaf; otherwise false
|
|
55
|
+
def leaf?
|
|
56
|
+
children.empty?
|
|
57
|
+
end # method leaf?
|
|
58
|
+
|
|
59
|
+
# Returns true if the object is a root object, e.g. has no parent object.
|
|
60
|
+
#
|
|
61
|
+
# @return [Boolean] true if the object is a root; otherwise false
|
|
62
|
+
def root?
|
|
63
|
+
parent.nil?
|
|
64
|
+
end # method root?
|
|
65
|
+
end # module
|
|
66
|
+
end # module
|
metadata
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mongoid-sleeping_king_studios
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.2.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Rob "Merlin" Smith
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2013-07-19 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: mongoid
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ~>
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '4'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ~>
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '4'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: bson_ext
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ~>
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 1.9.0
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ~>
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: 1.9.0
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ~>
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '2.14'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ~>
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '2.14'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rspec-sleeping_king_studios
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - '>='
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: 1.0.0.rc.2
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - '>='
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: 1.0.0.rc.2
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: factory_girl
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ~>
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '4.2'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ~>
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '4.2'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: database_cleaner
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ~>
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: 1.0.1
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - ~>
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: 1.0.1
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: fuubar
|
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - '>='
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: '0'
|
|
104
|
+
type: :development
|
|
105
|
+
prerelease: false
|
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - '>='
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '0'
|
|
111
|
+
- !ruby/object:Gem::Dependency
|
|
112
|
+
name: pry
|
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
|
114
|
+
requirements:
|
|
115
|
+
- - '>='
|
|
116
|
+
- !ruby/object:Gem::Version
|
|
117
|
+
version: '0'
|
|
118
|
+
type: :development
|
|
119
|
+
prerelease: false
|
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
121
|
+
requirements:
|
|
122
|
+
- - '>='
|
|
123
|
+
- !ruby/object:Gem::Version
|
|
124
|
+
version: '0'
|
|
125
|
+
description: |2
|
|
126
|
+
A collection of concerns and extensions to add functionality to Mongoid
|
|
127
|
+
documents and collections. The features can be included individually or by
|
|
128
|
+
category. For more information, check out the README.
|
|
129
|
+
email:
|
|
130
|
+
- merlin@sleepingkingstudios.com
|
|
131
|
+
executables: []
|
|
132
|
+
extensions: []
|
|
133
|
+
extra_rdoc_files: []
|
|
134
|
+
files:
|
|
135
|
+
- lib/mongoid/sleeping_king_studios/sluggable.rb
|
|
136
|
+
- lib/mongoid/sleeping_king_studios/tree.rb
|
|
137
|
+
- lib/mongoid/sleeping_king_studios/version.rb
|
|
138
|
+
- lib/mongoid/sleeping_king_studios.rb
|
|
139
|
+
- LICENSE
|
|
140
|
+
- README.md
|
|
141
|
+
homepage: http://sleepingkingstudios.com
|
|
142
|
+
licenses:
|
|
143
|
+
- MIT
|
|
144
|
+
metadata: {}
|
|
145
|
+
post_install_message:
|
|
146
|
+
rdoc_options: []
|
|
147
|
+
require_paths:
|
|
148
|
+
- lib
|
|
149
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
150
|
+
requirements:
|
|
151
|
+
- - '>='
|
|
152
|
+
- !ruby/object:Gem::Version
|
|
153
|
+
version: '0'
|
|
154
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
155
|
+
requirements:
|
|
156
|
+
- - '>='
|
|
157
|
+
- !ruby/object:Gem::Version
|
|
158
|
+
version: '0'
|
|
159
|
+
requirements: []
|
|
160
|
+
rubyforge_project:
|
|
161
|
+
rubygems_version: 2.0.3
|
|
162
|
+
signing_key:
|
|
163
|
+
specification_version: 4
|
|
164
|
+
summary: A collection of Mongoid concerns and extensions.
|
|
165
|
+
test_files: []
|
|
166
|
+
has_rdoc:
|