mongoid_listable 0.0.6 → 0.0.8
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 +15 -0
- data/.coveralls.yml +1 -0
- data/.travis.yml +14 -0
- data/Gemfile +4 -2
- data/README.md +9 -5
- data/Rakefile +12 -1
- data/lib/mongoid/listable/accessors.rb +54 -0
- data/lib/mongoid/listable/callbacks.rb +58 -0
- data/lib/mongoid/listable/extensions/array.rb +33 -0
- data/lib/mongoid/listable/extensions/object.rb +73 -0
- data/lib/mongoid/listable/extensions.rb +2 -0
- data/lib/mongoid/listable/macros.rb +19 -41
- data/lib/mongoid/listable/version.rb +5 -0
- data/lib/mongoid/listable.rb +40 -5
- data/mongoid_listable.gemspec +3 -2
- data/spec/models/photo.rb +10 -0
- data/spec/models/user.rb +13 -0
- data/spec/mongoid/listable/extensions/object_spec.rb +41 -0
- data/spec/mongoid_listable_spec.rb +46 -0
- data/spec/spec_helper.rb +24 -0
- metadata +24 -30
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZGRjNjdkNDcwYWI2YjJhNzY1OTY1ZGQ4ODZkMWE3ZTlmYWJlMDkzYQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
NTZkOWZhNTFjYmYxMDQ4MjI3NTczZTdhMGQwYjlhYmMwN2I0Nzc1OA==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NjZlY2E4MGVhMGI1OWFjMTQ5NGU2MTQ3NjQ0Y2U1NDJmMzhkMmVlMTkxZGUy
|
10
|
+
Y2QyMDFlZGJlMWVhNjU4NmI5NWMwMTFhYmYwY2RlYWQ2MjBhM2UyNDZjOTVi
|
11
|
+
ZjAxYTRmOTg2YmFmYjU3MzQwZjRmNDFiY2IzMTUwMjBhNDllMWQ=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
NDc1M2YwYzlkZDFjODhhZDQ0ODVmYWUxOGU2MTA2ZGM3M2NmYjg4ZmIyNzdl
|
14
|
+
N2I5ZGE0MjNjYzJhNWEwY2FhOGQ1YmQ4M2ZlMmQ3MWMxOWNjNzIwMGMyMGIz
|
15
|
+
MmM5NjljODU4ZDBiM2QzODEyMTRiNjk2Y2ZmYmY4OWQ0OTdmODM=
|
data/.coveralls.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
service_name: travis-ci
|
data/.travis.yml
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
language: ruby
|
2
|
+
services: mongodb
|
3
|
+
bundler_args: --without development
|
4
|
+
rvm:
|
5
|
+
- 1.9.3
|
6
|
+
- 2.0.0
|
7
|
+
deploy:
|
8
|
+
provider: rubygems
|
9
|
+
api_key:
|
10
|
+
secure: hWkNBQ4LbxAsErQZ8X46SKvCQ2tRwMU/SX2cWPlE0izXPvlpRie7HQvqlcCdmaYhxljxVEOdThMS9UW8rm4e+ykuWic0SD04iod7c9uvQQIxLra49lawOwaRkARRJJXcU5vIs+DXPEODsnRUita2llCf6WNyKbU+4mPacrC+AH4=
|
11
|
+
gem: mongoid_listable
|
12
|
+
on:
|
13
|
+
tags: true
|
14
|
+
repo: richardcalahan/mongoid_listable
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,8 @@
|
|
1
1
|
# Mongoid Listable
|
2
|
+
[](https://codeclimate.com/repos/527c1e427e00a44bc100c7e3/feed)
|
3
|
+
[](https://travis-ci.org/richardcalahan/mongoid_listable)
|
4
|
+
[](https://coveralls.io/r/richardcalahan/mongoid_listable?branch=master)
|
5
|
+
[](http://badge.fury.io/rb/mongoid_listable)
|
2
6
|
|
3
7
|
Mongoid Listable will eventually be a full replacement library for Mongoid List or Mongoid Orderable. Both
|
4
8
|
libraries fail to accomplish the simple task this library handles: separate position scopes for each
|
@@ -8,7 +12,7 @@ defined `has_many` / `belongs_to` relation.
|
|
8
12
|
|
9
13
|
class User
|
10
14
|
include Mongoid::Document
|
11
|
-
include Mongoid:
|
15
|
+
include Mongoid:Listable
|
12
16
|
|
13
17
|
has_many :photos
|
14
18
|
lists :photos
|
@@ -22,11 +26,11 @@ defined `has_many` / `belongs_to` relation.
|
|
22
26
|
...
|
23
27
|
end
|
24
28
|
|
25
|
-
In this example photos that are assigned to a user
|
29
|
+
In this example photos that are assigned to a user will maintain position based on the index
|
26
30
|
of the id in the array argument.
|
27
31
|
|
28
32
|
Each photo that belongs to the user will automatically obtain a field called `user_position`. The position field
|
29
|
-
is derived from the foreign key of the relation, replacing "_id" with "_position".
|
33
|
+
is derived from the foreign key of the relation, replacing "\_id" with "_position".
|
30
34
|
|
31
35
|
The 1-n relation of a user to their photos will automatically be ordered by `user_position` unless otherwise specified
|
32
36
|
via the standard `order` option to the `has_many` macro.
|
@@ -37,7 +41,7 @@ via the standard `order` option to the `has_many` macro.
|
|
37
41
|
|
38
42
|
class User
|
39
43
|
include Mongoid::Document
|
40
|
-
include Mongoid:
|
44
|
+
include Mongoid:Listable
|
41
45
|
|
42
46
|
has_many :featured_photos,
|
43
47
|
class_name: 'Photo',
|
@@ -79,7 +83,7 @@ You can optionally override the name of the position column:
|
|
79
83
|
|
80
84
|
## Todo
|
81
85
|
|
82
|
-
There's a lot more to add to the library! At this point, it conveniently handles
|
86
|
+
There's a lot more to add to the library! At this point, it conveniently handles has many relationships.
|
83
87
|
|
84
88
|
## Installation
|
85
89
|
|
data/Rakefile
CHANGED
@@ -0,0 +1,54 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Listable
|
3
|
+
|
4
|
+
module Accessors
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
|
9
|
+
# Redefines the default ids setter, ensuring that the order of
|
10
|
+
# objects sent to the relation setter corresponds to the order
|
11
|
+
# of the array of ids
|
12
|
+
#
|
13
|
+
# @param [ Symbol ] name The name of the has_many relation
|
14
|
+
# @param [ MetaData ] meta The MetaData class
|
15
|
+
#
|
16
|
+
# @return [ Object ] self
|
17
|
+
#
|
18
|
+
# @since 0.0.6
|
19
|
+
def _ids_setter name, meta
|
20
|
+
ids_method = "#{name.to_s.singularize}_ids="
|
21
|
+
redefine_method ids_method do |ids|
|
22
|
+
send meta.setter, meta.klass.find(ids).sort_by_attr(:id, ids)
|
23
|
+
end
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
# Prepends to the default setter a block that sets the position
|
28
|
+
# attribute on each object according to its index in the array
|
29
|
+
#
|
30
|
+
# @param [ Symbol ] name The name of the has_many relation
|
31
|
+
# @param [ MetaData ] meta The MetaData class
|
32
|
+
#
|
33
|
+
# @return [ Object ] self
|
34
|
+
#
|
35
|
+
# @since 0.0.6
|
36
|
+
def _setter name, meta
|
37
|
+
before_method "#{name}=" do |objects|
|
38
|
+
objects.each_with_index do |object, index|
|
39
|
+
object.update_attribute field_name(meta), index + 1
|
40
|
+
end
|
41
|
+
|
42
|
+
(send(name).to_a - objects).each do |object|
|
43
|
+
object.unset field_name(meta)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
self
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Listable
|
3
|
+
|
4
|
+
module Callbacks
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
|
9
|
+
# Defines a mongoid relation after_add callback.
|
10
|
+
# Sets the position attribute to current relations length + 1
|
11
|
+
#
|
12
|
+
# @param [ Symbol ] name The name of the has_many relation
|
13
|
+
# @param [ MetaData ] meta The MetaData class
|
14
|
+
#
|
15
|
+
# @return [ Object ] self
|
16
|
+
#
|
17
|
+
# @since 0.0.6
|
18
|
+
def added name, meta
|
19
|
+
callback = "#{name.to_s.singularize}_added"
|
20
|
+
define_method callback do |object|
|
21
|
+
object.update_attribute field_name(meta), has_many_count(name)
|
22
|
+
end
|
23
|
+
meta[:after_add] = callback
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
# Defines a mongoid relation before_remove callback.
|
28
|
+
# Resets the position index on all objects that came after
|
29
|
+
#
|
30
|
+
# @param [ Symbol ] name The name of the has_many relation
|
31
|
+
# @param [ MetaData ] meta The MetaData class
|
32
|
+
#
|
33
|
+
# @return [ Object ] self
|
34
|
+
#
|
35
|
+
# @since 0.0.6
|
36
|
+
def removed name, meta
|
37
|
+
field_name = field_name meta
|
38
|
+
callback = "#{name.to_s.singularize}_removed"
|
39
|
+
|
40
|
+
define_method callback do |object|
|
41
|
+
position = object.send field_name
|
42
|
+
send(name).where(field_name.gt => position)
|
43
|
+
.each_with_index do |object, index|
|
44
|
+
object.update_attribute field_name, position + index
|
45
|
+
end
|
46
|
+
object.unset field_name
|
47
|
+
end
|
48
|
+
|
49
|
+
meta[:before_remove] = callback
|
50
|
+
self
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Listable
|
3
|
+
module Extensions
|
4
|
+
|
5
|
+
module Array
|
6
|
+
|
7
|
+
# Sorts an array of objects on the specified key, sorted
|
8
|
+
# by the order of the key in an array. Example:
|
9
|
+
#
|
10
|
+
# [{name: 'Richard'},
|
11
|
+
# {name: 'Ashley'},
|
12
|
+
# {name: 'Chris'}].sort_by_attr(:name, ['Chris', 'Richard', 'Ashley'])
|
13
|
+
#
|
14
|
+
# => [{name: 'Chris'}, {name: 'Richard'}, {name: 'Ashley'}]
|
15
|
+
#
|
16
|
+
# @param [String|Symbol] key The object attribute to sort by
|
17
|
+
# @param [Array] values An array of object attributes
|
18
|
+
#
|
19
|
+
# @return Array
|
20
|
+
#
|
21
|
+
# @since 0.0.6
|
22
|
+
def sort_by_attr k, v
|
23
|
+
v.reject! &:blank?
|
24
|
+
sort { |a, b| v.index(a.send(k)) <=> v.index(b.send(k)) }
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
::Array.__send__ :include, Mongoid::Listable::Extensions::Array
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Listable
|
3
|
+
module Extensions
|
4
|
+
|
5
|
+
module Object
|
6
|
+
|
7
|
+
# Redefine the method. Will undef the method if it exists or simply
|
8
|
+
# just define it.
|
9
|
+
#
|
10
|
+
# @example Redefine the method.
|
11
|
+
# Object.re_define_method("exists?") do
|
12
|
+
# self
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# @param [ String, Symbol ] name The name of the method.
|
16
|
+
# @param [ Proc ] block The method body.
|
17
|
+
#
|
18
|
+
# @return [ Method ] The new method.
|
19
|
+
#
|
20
|
+
# # @since 0.0.7
|
21
|
+
def _redefine_method name, &block
|
22
|
+
undef_method(name) if method_defined?(name)
|
23
|
+
define_method(name, &block)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Redefines a method on owner to first execute &block, then
|
27
|
+
# continue executing original method.
|
28
|
+
#
|
29
|
+
# @param [ Object ] owner The owner of the method
|
30
|
+
# @param [ String ] method The name of the method to override
|
31
|
+
# @param [ Proc ] &block The block to execute before original method
|
32
|
+
#
|
33
|
+
# @since 0.0.3
|
34
|
+
def before_method name, &block
|
35
|
+
original_method = instance_method name
|
36
|
+
_redefine_method name do |*args|
|
37
|
+
instance_exec *args, &block
|
38
|
+
original_method.bind(self).call *args
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Redefines a method on owner to execute &block,
|
43
|
+
# passing to it as arguments the original method and its arguments.
|
44
|
+
# It's the callers' responsibility at that point to run whatever code
|
45
|
+
# and return the results of the original method by calling it.
|
46
|
+
#
|
47
|
+
# Use as an alternative to before_method if your override depends on
|
48
|
+
# the results of the original method. around_method will avoid the
|
49
|
+
# inevitable stack overflow that occurs in before_method
|
50
|
+
# if you call itself.
|
51
|
+
#
|
52
|
+
# @param [ Object ] owner The owner of the method
|
53
|
+
# @param [ String ] method The name of the method to override
|
54
|
+
# @param [ Proc ] &block The block to execute before original method
|
55
|
+
#
|
56
|
+
# @since 0.0.6
|
57
|
+
def around_method name, &block
|
58
|
+
original_method = instance_method name
|
59
|
+
_redefine_method name do |*args|
|
60
|
+
instance_exec original_method, *args, &block
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
::Object.__send__ :include, Mongoid::Listable::Extensions::Object
|
71
|
+
|
72
|
+
|
73
|
+
|
@@ -1,55 +1,33 @@
|
|
1
1
|
module Mongoid
|
2
2
|
module Listable
|
3
3
|
|
4
|
-
# This module contains the core macro for defining listable
|
4
|
+
# This module contains the core macro for defining listable
|
5
|
+
# has_many relationships
|
5
6
|
module Macros
|
7
|
+
extend ActiveSupport::Concern
|
6
8
|
|
7
|
-
|
8
|
-
#
|
9
|
-
# @param [ Symbol ] relation The name of the has_many relation
|
10
|
-
# @param [ Hash ] options The options hash
|
11
|
-
#
|
12
|
-
# @return [ Mongoid:Relations:Metadata ] Instance of metadata for the relation
|
13
|
-
#
|
14
|
-
# @since 0.0.1
|
15
|
-
def lists name, options={}
|
16
|
-
meta = reflect_on_association name
|
17
|
-
field_name = options[:column] ||
|
18
|
-
(meta.foreign_key.to_s.gsub(/_?id$/, '_position')).to_sym
|
19
|
-
|
9
|
+
module ClassMethods
|
20
10
|
|
21
|
-
#
|
22
|
-
# on each relation based on the index of its id in the given array.
|
11
|
+
# Macro to set relation on which to make a list
|
23
12
|
#
|
24
|
-
# @
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
13
|
+
# @param [ Symbol ] name The name of the has_many relation
|
14
|
+
# @param [ Hash ] options The options hash
|
15
|
+
#
|
16
|
+
# @return [ Mongoid:Relations:Metadata ] Instance of metadata
|
17
|
+
#
|
18
|
+
# @since 0.0.1
|
19
|
+
def lists name, options={}
|
20
|
+
meta = reflect_on_association name
|
21
|
+
meta.klass.send :field, field_name(meta), type: Integer
|
22
|
+
_ids_setter(name, meta)
|
23
|
+
._setter(name, meta)
|
24
|
+
.added(name, meta)
|
25
|
+
.removed(name, meta)
|
26
|
+
meta[:order] ||= "#{field_name(meta)} asc"
|
29
27
|
end
|
30
28
|
|
31
|
-
meta[:order] ||= "#{field_name} asc"
|
32
29
|
end
|
33
30
|
|
34
|
-
private
|
35
|
-
|
36
|
-
# Redefines a method on owner to first execute &block, then
|
37
|
-
# continue executing original method.
|
38
|
-
#
|
39
|
-
# @param [ Object ] owner The owner of the method
|
40
|
-
# @param [ String ] method The name of the method to override
|
41
|
-
# @param [ Proc ] &block The block to execute before original method
|
42
|
-
#
|
43
|
-
# @private
|
44
|
-
# @since 0.0.3
|
45
|
-
def before_method owner, method, &block
|
46
|
-
original_method = instance_method method
|
47
|
-
owner.re_define_method method do |*args|
|
48
|
-
block.call *args
|
49
|
-
original_method.bind(self).call *args
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
31
|
end
|
54
32
|
end
|
55
33
|
end
|
data/lib/mongoid/listable.rb
CHANGED
@@ -1,15 +1,50 @@
|
|
1
|
+
require 'mongoid/listable/accessors'
|
2
|
+
require 'mongoid/listable/callbacks'
|
3
|
+
require 'mongoid/listable/extensions'
|
1
4
|
require 'mongoid/listable/macros'
|
2
5
|
|
3
6
|
module Mongoid
|
4
|
-
|
5
7
|
module Listable
|
6
|
-
|
7
8
|
extend ActiveSupport::Concern
|
8
9
|
|
9
|
-
included do
|
10
|
-
|
10
|
+
included do
|
11
|
+
include Mongoid::Listable::Accessors
|
12
|
+
include Mongoid::Listable::Callbacks
|
13
|
+
include Mongoid::Listable::Macros
|
11
14
|
end
|
12
15
|
|
13
|
-
|
16
|
+
module ClassMethods
|
17
|
+
|
18
|
+
# Generates the position field name using the MetaData class
|
19
|
+
#
|
20
|
+
# @param [ MetaData ] meta The MetaData class
|
21
|
+
#
|
22
|
+
# @return Symbol
|
23
|
+
#
|
24
|
+
# @since 0.0.6
|
25
|
+
def field_name meta
|
26
|
+
(meta.foreign_key.to_s.gsub(/_?id$/, '_position')).to_sym
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
14
30
|
|
31
|
+
# Proxies to the class level version
|
32
|
+
#
|
33
|
+
# @see Class.field_name
|
34
|
+
#
|
35
|
+
# @since 0.0.6
|
36
|
+
def field_name meta
|
37
|
+
self.class.field_name meta
|
38
|
+
end
|
39
|
+
|
40
|
+
# Counts unique children of object
|
41
|
+
# Needed because the mongoid callbacks dont update
|
42
|
+
# has_many relations until after invoked.
|
43
|
+
#
|
44
|
+
# @since 0.0.7
|
45
|
+
def has_many_count name
|
46
|
+
send(name).uniq(&:id).count
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
15
50
|
end
|
data/mongoid_listable.gemspec
CHANGED
@@ -2,9 +2,11 @@
|
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
4
|
|
5
|
+
require "mongoid/listable/version"
|
6
|
+
|
5
7
|
Gem::Specification.new do |spec|
|
6
8
|
spec.name = 'mongoid_listable'
|
7
|
-
spec.version =
|
9
|
+
spec.version = Mongoid::Listable::VERSION
|
8
10
|
spec.authors = [ 'richardcalahan' ]
|
9
11
|
spec.email = [ 'richard@calahan.me' ]
|
10
12
|
spec.description = ''
|
@@ -18,7 +20,6 @@ Gem::Specification.new do |spec|
|
|
18
20
|
spec.require_paths = [ 'lib' ]
|
19
21
|
|
20
22
|
spec.add_dependency 'mongoid', '>= 3.1.0'
|
21
|
-
spec.add_dependency 'mongoid-list'
|
22
23
|
|
23
24
|
spec.add_development_dependency 'bundler', '~> 1.3'
|
24
25
|
spec.add_development_dependency 'rake'
|
data/spec/models/user.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::Listable::Extensions::Object do
|
4
|
+
|
5
|
+
before :each do
|
6
|
+
|
7
|
+
class Object
|
8
|
+
@@foo = 1
|
9
|
+
def foo
|
10
|
+
@@foo
|
11
|
+
end
|
12
|
+
def foo= foo
|
13
|
+
@@foo = foo
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'allows a block to be executed before a method' do
|
20
|
+
Object.before_method "foo" do
|
21
|
+
self.foo = 2
|
22
|
+
end
|
23
|
+
|
24
|
+
expect(Object.new.foo).to eq(2)
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'allows a block to be executed around a method' do
|
28
|
+
original_method = nil
|
29
|
+
original_return_value = nil
|
30
|
+
Object.around_method "foo" do |method, *args|
|
31
|
+
original_method = method
|
32
|
+
original_return_value = method.bind(self).call *args
|
33
|
+
end
|
34
|
+
|
35
|
+
Object.new.foo
|
36
|
+
|
37
|
+
expect(original_method).to be_a_kind_of(UnboundMethod)
|
38
|
+
expect(original_return_value).to eq(1)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongoid::Listable do
|
4
|
+
|
5
|
+
before :all do
|
6
|
+
@user = User.create!
|
7
|
+
@photos = []
|
8
|
+
10.times { @photos << Photo.create }
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'created a user' do
|
12
|
+
expect(@user).to be_true
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'created 10 photos' do
|
16
|
+
expect(@photos.count).to eq(10)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'assigns all photos to a user with the default setter' do
|
20
|
+
@user.photos = @photos
|
21
|
+
expect(@user.photos.count).to eq(@photos.count)
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'assigns all photos to a user with the default ids setter' do
|
25
|
+
ids = @photos.collect(&:id)
|
26
|
+
@user.photo_ids = ids
|
27
|
+
expect(@user.photos.count).to eq(@photos.count)
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'removes a photo from a user using the delete method' do
|
31
|
+
@user.photos.delete @photos[0]
|
32
|
+
expect(@user.photos.count).to eq(@photos.count - 1)
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'removes a photo from a user using the default setter' do
|
36
|
+
@user.photos = @photos[0..4]
|
37
|
+
expect(@user.photos.count).to eq(@photos.count - 5)
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'updated the position field for all the photos' do
|
41
|
+
User.first.photos.each_with_index do |photo, index|
|
42
|
+
expect(photo.user_position).to eq(index + 1)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'mongoid'
|
2
|
+
require 'rspec'
|
3
|
+
|
4
|
+
if ENV['CI']
|
5
|
+
require 'coveralls'
|
6
|
+
Coveralls.wear!
|
7
|
+
end
|
8
|
+
|
9
|
+
require File.expand_path '../../lib/mongoid_listable', __FILE__
|
10
|
+
require File.expand_path '../../spec/models/photo', __FILE__
|
11
|
+
require File.expand_path '../../spec/models/user', __FILE__
|
12
|
+
|
13
|
+
Mongoid.configure do |config|
|
14
|
+
config.connect_to 'mongoid_listable_test'
|
15
|
+
Moped.logger = Logger.new $stdout
|
16
|
+
Moped.logger.level = Logger::INFO
|
17
|
+
end
|
18
|
+
|
19
|
+
RSpec.configure do |config|
|
20
|
+
config.before :suite do
|
21
|
+
Mongoid.purge!
|
22
|
+
Mongoid::IdentityMap.clear
|
23
|
+
end
|
24
|
+
end
|
metadata
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongoid_listable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.8
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- richardcalahan
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2013-11-
|
11
|
+
date: 2013-11-09 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: mongoid
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ! '>='
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,31 +20,13 @@ dependencies:
|
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ! '>='
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: 3.1.0
|
30
|
-
- !ruby/object:Gem::Dependency
|
31
|
-
name: mongoid-list
|
32
|
-
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
|
-
requirements:
|
35
|
-
- - ! '>='
|
36
|
-
- !ruby/object:Gem::Version
|
37
|
-
version: '0'
|
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: '0'
|
46
27
|
- !ruby/object:Gem::Dependency
|
47
28
|
name: bundler
|
48
29
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
30
|
requirements:
|
51
31
|
- - ~>
|
52
32
|
- !ruby/object:Gem::Version
|
@@ -54,7 +34,6 @@ dependencies:
|
|
54
34
|
type: :development
|
55
35
|
prerelease: false
|
56
36
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
37
|
requirements:
|
59
38
|
- - ~>
|
60
39
|
- !ruby/object:Gem::Version
|
@@ -62,7 +41,6 @@ dependencies:
|
|
62
41
|
- !ruby/object:Gem::Dependency
|
63
42
|
name: rake
|
64
43
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
44
|
requirements:
|
67
45
|
- - ! '>='
|
68
46
|
- !ruby/object:Gem::Version
|
@@ -70,7 +48,6 @@ dependencies:
|
|
70
48
|
type: :development
|
71
49
|
prerelease: false
|
72
50
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
51
|
requirements:
|
75
52
|
- - ! '>='
|
76
53
|
- !ruby/object:Gem::Version
|
@@ -82,38 +59,55 @@ executables: []
|
|
82
59
|
extensions: []
|
83
60
|
extra_rdoc_files: []
|
84
61
|
files:
|
62
|
+
- .coveralls.yml
|
85
63
|
- .gitignore
|
64
|
+
- .travis.yml
|
86
65
|
- Gemfile
|
87
66
|
- LICENSE.txt
|
88
67
|
- README.md
|
89
68
|
- Rakefile
|
90
69
|
- lib/mongoid/listable.rb
|
70
|
+
- lib/mongoid/listable/accessors.rb
|
71
|
+
- lib/mongoid/listable/callbacks.rb
|
72
|
+
- lib/mongoid/listable/extensions.rb
|
73
|
+
- lib/mongoid/listable/extensions/array.rb
|
74
|
+
- lib/mongoid/listable/extensions/object.rb
|
91
75
|
- lib/mongoid/listable/macros.rb
|
76
|
+
- lib/mongoid/listable/version.rb
|
92
77
|
- lib/mongoid_listable.rb
|
93
78
|
- mongoid_listable.gemspec
|
79
|
+
- spec/models/photo.rb
|
80
|
+
- spec/models/user.rb
|
81
|
+
- spec/mongoid/listable/extensions/object_spec.rb
|
82
|
+
- spec/mongoid_listable_spec.rb
|
83
|
+
- spec/spec_helper.rb
|
94
84
|
homepage: ''
|
95
85
|
licenses:
|
96
86
|
- MIT
|
87
|
+
metadata: {}
|
97
88
|
post_install_message:
|
98
89
|
rdoc_options: []
|
99
90
|
require_paths:
|
100
91
|
- lib
|
101
92
|
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
-
none: false
|
103
93
|
requirements:
|
104
94
|
- - ! '>='
|
105
95
|
- !ruby/object:Gem::Version
|
106
96
|
version: '0'
|
107
97
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
-
none: false
|
109
98
|
requirements:
|
110
99
|
- - ! '>='
|
111
100
|
- !ruby/object:Gem::Version
|
112
101
|
version: '0'
|
113
102
|
requirements: []
|
114
103
|
rubyforge_project:
|
115
|
-
rubygems_version:
|
104
|
+
rubygems_version: 2.0.3
|
116
105
|
signing_key:
|
117
|
-
specification_version:
|
106
|
+
specification_version: 4
|
118
107
|
summary: ''
|
119
|
-
test_files:
|
108
|
+
test_files:
|
109
|
+
- spec/models/photo.rb
|
110
|
+
- spec/models/user.rb
|
111
|
+
- spec/mongoid/listable/extensions/object_spec.rb
|
112
|
+
- spec/mongoid_listable_spec.rb
|
113
|
+
- spec/spec_helper.rb
|