mongoid_listable 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +35 -6
- data/lib/mongoid/lists.rb +18 -5
- data/mongoid_listable.gemspec +1 -1
- metadata +1 -1
data/README.md
CHANGED
@@ -1,6 +1,39 @@
|
|
1
|
-
#
|
1
|
+
# Mongoid Listable
|
2
2
|
|
3
|
-
|
3
|
+
Mongoid Listable will eventually be a full replacement library for Mongoid List or Mongoid Orderable. Both
|
4
|
+
libraries fail to accomplish the simple task this library handles: lists that need to be specific for a given `has_many`
|
5
|
+
relation.
|
6
|
+
|
7
|
+
## Usage
|
8
|
+
|
9
|
+
class User
|
10
|
+
include Mongoid::Document
|
11
|
+
include Mongoid:Listable
|
12
|
+
|
13
|
+
has_many :photos
|
14
|
+
|
15
|
+
lists :photos
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
class Photo
|
20
|
+
include Mongoid::Document
|
21
|
+
|
22
|
+
belongs_to :user
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
Now, photos that are assigned to a user via by the method `user.photo_ids=[ids]` will maintain position based on the index
|
27
|
+
of the id in the array argument.
|
28
|
+
|
29
|
+
In this example, each photo object that belongs to the user will obtain a field called `user_position`. The 1-n relation of
|
30
|
+
a user to their photos will automatically be ordered by `user_position` unless otherwise specified.
|
31
|
+
|
32
|
+
You can also override the name of the position column:
|
33
|
+
|
34
|
+
lists :photos, column: :users_photos_order
|
35
|
+
|
36
|
+
There's a lot more to add to the library. At this point, it will conveniently handle rails based multiselect form fields.
|
4
37
|
|
5
38
|
## Installation
|
6
39
|
|
@@ -16,10 +49,6 @@ Or install it yourself as:
|
|
16
49
|
|
17
50
|
$ gem install mongoid_listable
|
18
51
|
|
19
|
-
## Usage
|
20
|
-
|
21
|
-
TODO: Write usage instructions here
|
22
|
-
|
23
52
|
## Contributing
|
24
53
|
|
25
54
|
1. Fork it
|
data/lib/mongoid/lists.rb
CHANGED
@@ -6,7 +6,11 @@ module Mongoid
|
|
6
6
|
|
7
7
|
extend ActiveSupport::Concern
|
8
8
|
|
9
|
-
FIELD_SUFFIX = '_position'
|
9
|
+
FIELD_SUFFIX = '_position'
|
10
|
+
|
11
|
+
include do
|
12
|
+
after_add
|
13
|
+
end
|
10
14
|
|
11
15
|
module ClassMethods
|
12
16
|
|
@@ -20,15 +24,24 @@ module Mongoid
|
|
20
24
|
# @since 0.0.1
|
21
25
|
def lists relation, options={}
|
22
26
|
meta = reflect_on_association relation
|
23
|
-
field_name = options[:column] || (meta
|
24
|
-
klass = meta.klass
|
27
|
+
field_name = options[:column] || (meta.foreign_key.to_s.gsub(/_?id$/, FIELD_SUFFIX)).to_sym
|
28
|
+
klass = meta.klass
|
25
29
|
|
26
|
-
|
30
|
+
ids_setter_name = "#{relation.to_s.singularize}_ids="
|
31
|
+
ids_setter = instance_method ids_setter_name
|
32
|
+
re_define_method ids_setter_name do |ids|
|
33
|
+
# assign new position int
|
27
34
|
ids.each_with_index do |id, index|
|
28
35
|
klass.find(id).update_attribute field_name, index + 1
|
29
36
|
end
|
30
37
|
|
31
|
-
|
38
|
+
# unassign old position ints
|
39
|
+
klass.where(meta.foreign_key => id).not_in(id: ids).each do |obj|
|
40
|
+
obj.update_attribute field_name, nil
|
41
|
+
end
|
42
|
+
|
43
|
+
# invoke original method
|
44
|
+
ids_setter.bind(self).call(ids)
|
32
45
|
end
|
33
46
|
|
34
47
|
meta[:order] = "#{field_name} asc"
|
data/mongoid_listable.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = 'mongoid_listable'
|
7
|
-
spec.version = '0.0.
|
7
|
+
spec.version = '0.0.2'
|
8
8
|
spec.authors = [ 'richardcalahan' ]
|
9
9
|
spec.email = [ 'richard@calahan.me' ]
|
10
10
|
spec.description = ''
|