mongoid_listable 0.2.3 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
  [![Coverage Status](https://coveralls.io/repos/richardcalahan/mongoid_listable/badge.png?branch=master)](https://coveralls.io/r/richardcalahan/mongoid_listable?branch=master)
5
5
  [![Gem Version](https://badge.fury.io/rb/mongoid_listable.png)](http://badge.fury.io/rb/mongoid_listable)
6
6
 
7
- Mongoid Listable is a library for managing listable relations. It works great for isolated collections or for more complex `has_many` / `embeds_many` relationships.
7
+ Mongoid Listable is a library for managing listable relations. It works great for non-relational collections or for more complex `has_many` / `embeds_many` relationships.
8
8
 
9
9
  There are two main macros:
10
10
 
@@ -12,13 +12,14 @@ There are two main macros:
12
12
  * `lists` for `has_many` / `embeds_many` relationships.
13
13
 
14
14
 
15
- ## Basic Usage - Isolated
15
+ ## Basic Usage - Non-Relational
16
16
 
17
17
  class Photo
18
18
  include Mongoid::Document
19
19
  include Mongoid::Listable
20
20
 
21
21
  listed
22
+ ...
22
23
  end
23
24
 
24
25
  The `listed` macro will assign a `position` field and a `list` scope to the Photo class. All Photo instances that are
@@ -28,15 +29,18 @@ Non-relational lists can have as many listed contexts as needed. You'll need to
28
29
  `field` options in these cases.
29
30
 
30
31
  class Photo
31
-
32
32
  include Mongoid::Document
33
33
  include Mongoid::Listable
34
34
 
35
35
  listed :scope :list, field: :position
36
36
  listed :scope :slideshow, field: :slideshow_position
37
-
37
+ ...
38
38
  end
39
39
 
40
+ Photo.list # orders by position field
41
+ Photo.slideshow # orders by slideshow_position
42
+
43
+
40
44
  ## Basic Usage - Has Many / Embeds Many
41
45
 
42
46
 
@@ -78,8 +82,7 @@ will trigger a reordering of all sibling instances. For example:
78
82
 
79
83
 
80
84
  Each photo that belongs to the user will automatically obtain a field called `user_position`. The field name
81
- is derived from the foreign key of the relation, replacing "\_id" with "_position". Having a unique position key
82
- for each 1-n relationship allows for more complex lists.
85
+ is derived from the foreign key of the relation, replacing "\_id" with "_position".
83
86
 
84
87
  The `has_many` / `embeds_many` relationship of a user to their photos will automadtically be ordered by `user_position` unless otherwise specified
85
88
  via the standard `order` option to the `has_many` macro.
@@ -103,7 +106,7 @@ via the standard `order` option to the `has_many` macro.
103
106
  foreign_key: :kodaked_by_user_id
104
107
 
105
108
  lists :featured_photos
106
- lists :kodak_moments
109
+ lists :kodak_moments
107
110
  ...
108
111
  end
109
112
 
@@ -41,8 +41,22 @@ module Mongoid
41
41
  #
42
42
  # @since 0.1.0
43
43
  def siblings field=:position
44
- klass = embedded? ? _parent.send(metadata.key) : self.class
45
- klass.exists(field => true).ne id: id
44
+ klass = if embedded_one?
45
+ _parent.send(metadata.key).class
46
+ elsif embedded_many?
47
+ _parent.send(metadata.key)
48
+ else
49
+ self.class
50
+ end
51
+ klass.where(field.exists => true).ne id: id
52
+ end
53
+
54
+ def embedded_one?
55
+ embedded? && metadata[:relation] == Mongoid::Relations::Embedded::One
56
+ end
57
+
58
+ def embedded_many?
59
+ embedded? && metadata[:relation] == Mongoid::Relations::Embedded::Many
46
60
  end
47
61
 
48
62
  private
@@ -98,7 +98,24 @@ module Mongoid
98
98
  end
99
99
 
100
100
  private
101
-
101
+
102
+ # Registers a callback using name (either the position
103
+ # field, or in the case of a has_many/embeds_many relationship,
104
+ # the relation name) as a unique identifier for the generated
105
+ # method name.
106
+ #
107
+ # If reflect_on_association(name) returns non-nil, we know
108
+ # this is a has_many/embeds_many relationship, and we register
109
+ # the callback on the inverse class of the relation. If the
110
+ # return value is nil, register the callback on the class itself.
111
+ #
112
+ # @param [ Symbol ] name The name of the position field
113
+ # @param [ Symbol|String ] hook The name of the Mongoid relation callback
114
+ # @param [ Proc ] block The body of the callback method
115
+ #
116
+ # @return [ Object ] self
117
+ #
118
+ # @since 0.0.6
102
119
  def register_callback name, hook, &block
103
120
  meta = reflect_on_association name
104
121
  callback = "#{hook}_#{name}"
@@ -68,6 +68,3 @@ module Mongoid
68
68
  end
69
69
 
70
70
  ::Object.__send__ :include, Mongoid::Listable::Extensions::Object
71
-
72
-
73
-
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Listable
3
- VERSION = '0.2.3'
3
+ VERSION = '0.2.5'
4
4
  end
5
5
  end
@@ -1,4 +1 @@
1
1
  require 'mongoid/listable'
2
-
3
-
4
-
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
20
  spec.require_paths = [ 'lib' ]
21
21
 
22
- spec.add_dependency 'mongoid', '>= 3.1.0'
22
+ spec.add_dependency 'mongoid', '>= 3.1.5'
23
23
 
24
24
  spec.add_development_dependency 'bundler', '~> 1.3'
25
25
  spec.add_development_dependency 'rake'
metadata CHANGED
@@ -1,32 +1,36 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_listable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.5
5
+ prerelease:
5
6
  platform: ruby
6
7
  authors:
7
8
  - richardcalahan
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-11-12 00:00:00.000000000 Z
12
+ date: 2013-12-09 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: mongoid
15
16
  requirement: !ruby/object:Gem::Requirement
17
+ none: false
16
18
  requirements:
17
19
  - - ! '>='
18
20
  - !ruby/object:Gem::Version
19
- version: 3.1.0
21
+ version: 3.1.5
20
22
  type: :runtime
21
23
  prerelease: false
22
24
  version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
23
26
  requirements:
24
27
  - - ! '>='
25
28
  - !ruby/object:Gem::Version
26
- version: 3.1.0
29
+ version: 3.1.5
27
30
  - !ruby/object:Gem::Dependency
28
31
  name: bundler
29
32
  requirement: !ruby/object:Gem::Requirement
33
+ none: false
30
34
  requirements:
31
35
  - - ~>
32
36
  - !ruby/object:Gem::Version
@@ -34,6 +38,7 @@ dependencies:
34
38
  type: :development
35
39
  prerelease: false
36
40
  version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
37
42
  requirements:
38
43
  - - ~>
39
44
  - !ruby/object:Gem::Version
@@ -41,6 +46,7 @@ dependencies:
41
46
  - !ruby/object:Gem::Dependency
42
47
  name: rake
43
48
  requirement: !ruby/object:Gem::Requirement
49
+ none: false
44
50
  requirements:
45
51
  - - ! '>='
46
52
  - !ruby/object:Gem::Version
@@ -48,6 +54,7 @@ dependencies:
48
54
  type: :development
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
51
58
  requirements:
52
59
  - - ! '>='
53
60
  - !ruby/object:Gem::Version
@@ -88,26 +95,27 @@ files:
88
95
  homepage: ''
89
96
  licenses:
90
97
  - MIT
91
- metadata: {}
92
98
  post_install_message:
93
99
  rdoc_options: []
94
100
  require_paths:
95
101
  - lib
96
102
  required_ruby_version: !ruby/object:Gem::Requirement
103
+ none: false
97
104
  requirements:
98
105
  - - ! '>='
99
106
  - !ruby/object:Gem::Version
100
107
  version: '0'
101
108
  required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
102
110
  requirements:
103
111
  - - ! '>='
104
112
  - !ruby/object:Gem::Version
105
113
  version: '0'
106
114
  requirements: []
107
115
  rubyforge_project:
108
- rubygems_version: 2.0.3
116
+ rubygems_version: 1.8.24
109
117
  signing_key:
110
- specification_version: 4
118
+ specification_version: 3
111
119
  summary: ''
112
120
  test_files:
113
121
  - spec/models/article.rb
checksums.yaml DELETED
@@ -1,15 +0,0 @@
1
- ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- YWE5MDIwMmJjOWRhNTM2NzU2Yzg2OWYzYTI4YmM4NGQ3ODczYjg0Mw==
5
- data.tar.gz: !binary |-
6
- MDJmNTM5NzAyYjM4MGU2ZjEyZWFiYjY0NmY2YWYzMzEyMDVlYmRlYQ==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- M2JhNWFhMjcwNjUxMTE5OGM1MzU1OTZlN2U3NDNiNzI2N2M5MjY5YmQxNWY1
10
- MzBiMDkzZDliOTJjNTNkZmQyN2MwYjU3NDhiZGE3NmU3YTdhZmFiYWRmOGU2
11
- YzBkNzM3Njg3MTA0ODg2ZTQ5MzY1YTc1YWM4Yzk1Y2NmNWQ5NjQ=
12
- data.tar.gz: !binary |-
13
- MGQwYTNkYzcyNjk2OGU3YWMzODliODEyMWY0MzQ4ZTE0MGIyOGRkOTc5MzUx
14
- ZjU1OTA4ZGRkOWNiNTc0ZDNhNjYxMTBjNDUyZmRhMThkNzQ4NWY1NTc4Y2Ey
15
- NTgwZTU4OTk2NDMwOGQyN2I4MTVmMGFkYzBjNmM5NTUxZjg2ZTg=