mongoid_sortable_relation 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0f2adec289d77795fdbfa41d056dd24efee27f48
4
+ data.tar.gz: 72278f8a51ae88d868f3d1dd4beb3e4d77191e46
5
+ SHA512:
6
+ metadata.gz: 07020779650dca997bc3570c27f90ce58d5db2b76e584043d66871fd71869afe3cdfac507444feba10d0f314e1edf47ee68627152cd3af98d3bcca044e59b9b7
7
+ data.tar.gz: 34e8cc158a18c6a4ac546916fc2ba38a9188fac4f02f05bd9c20fe58e1237a2658440982fd42cf44ce44f737fb4dddce55e7b9d36b012ef37665b099d6936c2e
@@ -0,0 +1,16 @@
1
+ require 'rails'
2
+ require 'mongoid'
3
+ require 'mongoid_criteria_filter'
4
+
5
+ require 'mongoid_sortable_relation/referenced/many'
6
+ require 'mongoid_sortable_relation/referenced/many_to_many'
7
+ require 'mongoid_sortable_relation/findable'
8
+ require 'mongoid_sortable_relation/macros'
9
+ require 'mongoid_sortable_relation/many'
10
+ require 'mongoid_sortable_relation/many_to_many'
11
+ require 'mongoid_sortable_relation/metadata'
12
+ require 'mongoid_sortable_relation/origin_optional'
13
+
14
+ module MongoidSortableRelation
15
+
16
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+ module Mongoid
3
+ module Findable
4
+
5
+ def prepend_order_by(*args)
6
+ with_default_scope.prepend_order_by(*args)
7
+ end
8
+
9
+ end
10
+ end
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+ module Mongoid
3
+ module Relations
4
+ module Macros
5
+ module ClassMethods
6
+
7
+ alias_method :original_reference, :reference
8
+
9
+ private
10
+
11
+ def reference(metadata, type = Object)
12
+ original_reference(metadata, type)
13
+ if metadata.sortable?
14
+ to_class = metadata.class_name.classify.constantize
15
+ to_class.field metadata.sortable_field, :type => Integer
16
+ to_class.scope metadata.sortable_scope, -> { prepend_order_by(metadata.sortable_field => :asc) }
17
+ end
18
+ end
19
+
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ module Mongoid
3
+ module Relations
4
+ module Builders
5
+ module Referenced
6
+ class Many
7
+
8
+ def build(type = nil)
9
+ return object unless query?
10
+ return [] if object.is_a?(Array)
11
+ criteria = metadata.criteria(Conversions.flag(object, metadata), base.class)
12
+ criteria = criteria.send(metadata.sortable_scope) if metadata.sortable?
13
+ criteria
14
+ end
15
+
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ module Mongoid
3
+ module Relations
4
+ module Builders
5
+ module Referenced
6
+ class ManyToMany < Builder
7
+
8
+ def build(type = nil)
9
+ return object.try(:dup) unless query?
10
+ ids = object || []
11
+ criteria = metadata.criteria(ids, base.class)
12
+ criteria.add_filter { |entities| sorted_entities(entities, ids) } if metadata.sortable?
13
+ criteria
14
+ end
15
+
16
+ private
17
+
18
+ def sorted_entities(entities, sorted_ids)
19
+ entities_hash = {}
20
+ entities.each do |entity|
21
+ entities_hash[entity.id] = entity
22
+ end
23
+ sorted_ids.map { |id| entities_hash[id] }
24
+ end
25
+
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+ module Mongoid
3
+ module Relations
4
+ class Metadata
5
+
6
+ def sortable
7
+ self[:sortable]
8
+ end
9
+
10
+ def sortable?
11
+ !!sortable
12
+ end
13
+
14
+ def sortable_field
15
+ :"#{prefix}_position"
16
+ end
17
+
18
+ def sortable_scope
19
+ :"sort_for_#{prefix}"
20
+ end
21
+
22
+ private
23
+
24
+ # rm_todo rename
25
+ def prefix
26
+ @prefix ||= prefix_name
27
+ end
28
+
29
+ def prefix_name
30
+ if polymorphic?
31
+ as
32
+ else
33
+ inverse_of || inverse_class_name.underscore.gsub('/', '_')
34
+ end
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ module Origin
3
+ module Optional
4
+
5
+ def prepend_order_by(*spec)
6
+ option(spec) do |options, query|
7
+ spec.compact.each do |criterion|
8
+ criterion.__sort_option__.each_pair do |field, direction|
9
+ add_sort_option(options, field, direction, true)
10
+ end
11
+ query.pipeline.push('$sort' => options[:sort]) if query and aggregating? # todo unknown method aggregating?
12
+ end
13
+ end
14
+ end
15
+ alias :prepend_order :prepend_order_by
16
+
17
+ private
18
+
19
+ def add_sort_option(options, field, direction, prepend = false)
20
+ if driver == :mongo
21
+ sorting = (options[:sort] || []).dup
22
+ sort_params = [field, direction]
23
+ prepend ? sorting.unshift(sort_params) : sorting.push(sort_params)
24
+ else
25
+ sorting = (options[:sort] || {}).dup
26
+ prepend ? sorting = { field => direction }.merge(sorting) : sorting[field] = direction
27
+ end
28
+ options.store(:sort, sorting)
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ module Mongoid
3
+ module Relations
4
+ module Referenced
5
+ class Many
6
+
7
+ class << self
8
+
9
+ alias_method :original_valid_options, :valid_options
10
+
11
+ def valid_options
12
+ original_valid_options << :sortable
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ module Mongoid
3
+ module Relations
4
+ module Referenced
5
+ class ManyToMany
6
+
7
+ class << self
8
+
9
+ alias_method :original_valid_options, :valid_options
10
+
11
+ def valid_options
12
+ original_valid_options << :sortable
13
+ end
14
+
15
+ end
16
+
17
+ end
18
+ end
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_sortable_relation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Vladislav Melanitskiy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mongoid
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mongoid_criteria_filter
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: It adds possibility for sorting relation
56
+ email:
57
+ - co@rademade.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/mongoid_sortable_relation.rb
63
+ - lib/mongoid_sortable_relation/findable.rb
64
+ - lib/mongoid_sortable_relation/macros.rb
65
+ - lib/mongoid_sortable_relation/many.rb
66
+ - lib/mongoid_sortable_relation/many_to_many.rb
67
+ - lib/mongoid_sortable_relation/metadata.rb
68
+ - lib/mongoid_sortable_relation/origin_optional.rb
69
+ - lib/mongoid_sortable_relation/referenced/many.rb
70
+ - lib/mongoid_sortable_relation/referenced/many_to_many.rb
71
+ homepage: https://github.com/Rademade/mongoid_sortable_relation
72
+ licenses:
73
+ - MIT
74
+ metadata: {}
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ requirements: []
90
+ rubyforge_project:
91
+ rubygems_version: 2.2.2
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: Extension for mongoid.
95
+ test_files: []