mongoid_listable 0.0.2 → 0.0.3

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/README.md CHANGED
@@ -8,7 +8,7 @@ relation.
8
8
 
9
9
  class User
10
10
  include Mongoid::Document
11
- include Mongoid:Listable
11
+ include Mongoid:Lists
12
12
 
13
13
  has_many :photos
14
14
 
data/lib/mongoid/lists.rb CHANGED
@@ -6,12 +6,6 @@ module Mongoid
6
6
 
7
7
  extend ActiveSupport::Concern
8
8
 
9
- FIELD_SUFFIX = '_position'
10
-
11
- include do
12
- after_add
13
- end
14
-
15
9
  module ClassMethods
16
10
 
17
11
  # Macro to set relation on which to make a list
@@ -24,29 +18,34 @@ module Mongoid
24
18
  # @since 0.0.1
25
19
  def lists relation, options={}
26
20
  meta = reflect_on_association relation
27
- field_name = options[:column] || (meta.foreign_key.to_s.gsub(/_?id$/, FIELD_SUFFIX)).to_sym
28
- klass = meta.klass
21
+ field_name = options[:column] ||
22
+ (meta.foreign_key.to_s.gsub(/_?id$/, '_position')).to_sym
23
+
29
24
 
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
25
+ # Override model#{name}_ids=
26
+ before_method self, "#{relation.to_s.singularize}_ids=" do |ids|
34
27
  ids.each_with_index do |id, index|
35
- klass.find(id).update_attribute field_name, index + 1
28
+ meta.klass.find(id).update_attribute field_name, index + 1
36
29
  end
37
30
 
38
- # unassign old position ints
39
- klass.where(meta.foreign_key => id).not_in(id: ids).each do |obj|
31
+ meta.klass.where(meta.foreign_key => id).not_in(id: ids).each do |obj|
40
32
  obj.update_attribute field_name, nil
41
33
  end
42
-
43
- # invoke original method
44
- ids_setter.bind(self).call(ids)
45
34
  end
46
35
 
47
36
  meta[:order] = "#{field_name} asc"
48
37
  end
49
38
 
39
+ private
40
+
41
+ def before_method owner, method, &block
42
+ original_method = instance_method method
43
+ owner.re_define_method method do |*args|
44
+ block.call *args
45
+ original_method.bind(self).call *args
46
+ end
47
+ end
48
+
50
49
  end
51
50
 
52
51
  end
@@ -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.2'
7
+ spec.version = '0.0.3'
8
8
  spec.authors = [ 'richardcalahan' ]
9
9
  spec.email = [ 'richard@calahan.me' ]
10
10
  spec.description = ''
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_listable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-11-03 00:00:00.000000000 Z
12
+ date: 2013-11-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mongoid