activeadmin-mongoid-reorder 0.1.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.
@@ -0,0 +1 @@
1
+ require 'mongoid/reorder'
@@ -0,0 +1,36 @@
1
+ module Mongoid::Reorder
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ field :_position, :type => Float, :default => 0.0
6
+ default_scope order_by(:_position => :desc)
7
+ end
8
+
9
+ module ClassMethods
10
+ def reorder_objects(ids)
11
+ objects = self.unscoped.find(ids)
12
+ max_weight = (objects.max { |a,b| a._position <=> b._position })._position
13
+ min_weight = (objects.min { |a,b| a._position <=> b._position })._position
14
+
15
+ if max_weight.nil? or max_weight == 0
16
+ max_weight = 100
17
+ end
18
+
19
+ if min_weight.nil?
20
+ min_weight = 0
21
+ end
22
+
23
+ weight_delta = (max_weight - min_weight) / objects.size
24
+ objects_dict = {}
25
+ objects.each { |o| objects_dict[o.id.to_s] = o }
26
+ weights = []
27
+
28
+ ids.each_with_index do |id, index|
29
+ position = max_weight - index * weight_delta
30
+ objects_dict[id].update_attributes!(_position: position)
31
+ end
32
+
33
+ "ok"
34
+ end
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activeadmin-mongoid-reorder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alex Kravets
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-07-23 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ''
15
+ email: santyor@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/activeadmin-mongoid-reorder.rb
21
+ - lib/mongoid/reorder.rb
22
+ homepage: https://github.com/alexkravets/activeadmin-mongoid-reorder
23
+ licenses:
24
+ - MIT
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project: nowarning
43
+ rubygems_version: 1.8.24
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: Reordarable rows in activeadmin index table view.
47
+ test_files: []