mongoid-collection-separatable 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c8cb98452f75ab68a4ffe0ac3998ca81b78af4daa8dc586251e764b9baa2575a
4
+ data.tar.gz: 0f7934c424fd0a6f9a517c19ae4608fb6cc3874e7cd17ca46ab50ca10e4b42ac
5
+ SHA512:
6
+ metadata.gz: 23d2e72379ddf98d8a9545ae644a61657fed118178f26dc98f9bee2762d08d06d082a11e40873d02014d1764f8727314bf48836998ea458c7cfb984418f6caff
7
+ data.tar.gz: 4ace2b88c02563c469ad57b3fe807a3c4c613f0ece20d2a588c01692646f343f2300693ba8cdd1d82014645bc5f92af959fe09b2b715d25b8efa592ceae2b508
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .idea
13
+ tags
14
+ .byebug_history
15
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ before_install: gem install bundler -v 1.17.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in mongoid_collection_separatable.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,66 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mongoid-collection-separatable (0.1.0)
5
+ mongoid (~> 6.4.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.2.1)
11
+ activesupport (= 5.2.1)
12
+ activesupport (5.2.1)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 0.7, < 2)
15
+ minitest (~> 5.1)
16
+ tzinfo (~> 1.1)
17
+ bson (4.3.0)
18
+ byebug (10.0.0)
19
+ coderay (1.1.2)
20
+ concurrent-ruby (1.1.3)
21
+ diff-lcs (1.3)
22
+ i18n (1.1.1)
23
+ concurrent-ruby (~> 1.0)
24
+ method_source (0.9.0)
25
+ minitest (5.11.3)
26
+ mongo (2.6.2)
27
+ bson (>= 4.3.0, < 5.0.0)
28
+ mongoid (6.4.2)
29
+ activemodel (>= 5.1, < 6.0.0)
30
+ mongo (>= 2.5.1, < 3.0.0)
31
+ pry (0.11.3)
32
+ coderay (~> 1.1.0)
33
+ method_source (~> 0.9.0)
34
+ pry-byebug (3.6.0)
35
+ byebug (~> 10.0)
36
+ pry (~> 0.10)
37
+ rake (10.5.0)
38
+ rspec (3.8.0)
39
+ rspec-core (~> 3.8.0)
40
+ rspec-expectations (~> 3.8.0)
41
+ rspec-mocks (~> 3.8.0)
42
+ rspec-core (3.8.0)
43
+ rspec-support (~> 3.8.0)
44
+ rspec-expectations (3.8.2)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.8.0)
47
+ rspec-mocks (3.8.0)
48
+ diff-lcs (>= 1.2.0, < 2.0)
49
+ rspec-support (~> 3.8.0)
50
+ rspec-support (3.8.0)
51
+ thread_safe (0.3.6)
52
+ tzinfo (1.2.5)
53
+ thread_safe (~> 0.1)
54
+
55
+ PLATFORMS
56
+ ruby
57
+
58
+ DEPENDENCIES
59
+ bundler (~> 1.17)
60
+ mongoid-collection-separatable!
61
+ pry-byebug (~> 3.4)
62
+ rake (~> 10.0)
63
+ rspec (~> 3.0)
64
+
65
+ BUNDLED WITH
66
+ 1.17.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Oscar Jiang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # MongoidCollectionSeparatable
2
+
3
+ Support mongoid collections to be saved into and queried from separated collections with condition
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mongoid_collection_separatable'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mongoid_collection_separatable
20
+
21
+ ## Usage
22
+ Add the following line into the model class that you want to split:
23
+ ```ruby
24
+ separated_by :form_id, parent_class: 'Form', on_condition: :entries_separated
25
+ ```
26
+
27
+ When `on_condition` field in `parent_class` is set to true, current records referenced to `separated_by` field will be saved into separated collections. Default collections name will be `#{current_collection}_#{form_id_value}`
28
+
29
+ Migration task to separate collections:
30
+ ```bash
31
+ rake db:mongoid:collection:separate[origin_class,condition_key,condition_value]
32
+ ```
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/mongoid_collection_separatable.
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,2 @@
1
+ require "mongoid/collection_separatable"
2
+
@@ -0,0 +1,52 @@
1
+ require 'mongoid'
2
+ require 'mongoid/collection_separatable/monkey_patches'
3
+ require 'mongoid/collection_separatable/tasks/separate'
4
+ require 'active_support'
5
+ require 'mongoid/collection_separatable/version'
6
+ require 'mongoid/collection_separatable/railtie' if defined?(Rails)
7
+
8
+
9
+ module Mongoid
10
+ module CollectionSeparatable
11
+ extend ActiveSupport::Concern
12
+
13
+ included do
14
+ action_method_module = [Mongoid::Persistable.constants.map {|c| Mongoid::Persistable.const_get(c)} + [Mongoid::Reloadable]].flatten.select {|c| c.is_a?(Module)}
15
+ action_method_module.each {|action_module| alias_method_with_collection action_module}
16
+ end
17
+
18
+ class_methods do
19
+ attr_accessor :separated_field, :separated_condition_field, :separated_parent_class, :separated_parent_field, :separated_collection_prefix
20
+
21
+ def separated_by separated_field, opts={}
22
+ @separated_parent_class = opts[:parent_class].constantize
23
+ @separated_parent_field = opts[:parent_field] || :id
24
+ @separated_collection_prefix = opts[:prefix].to_s.presence || self.to_s.downcase.pluralize + '_'
25
+ @separated_condition_field = opts[:on_condition]
26
+ @separated_field = separated_field
27
+ unless @separated_parent_class.fields.key?(@separated_condition_field)
28
+ @separated_parent_class.class_eval <<-END
29
+ field :#{@separated_condition_field}, type: Boolean
30
+ END
31
+ end
32
+ end
33
+
34
+ def alias_method_with_collection action_module
35
+ action_module.instance_methods(false).each do |action_method|
36
+ define_method "#{action_method}_with_context" do |*args|
37
+ klass = self.class
38
+ self.send "#{action_method}_without_context", *args unless klass.respond_to?(:separated_field)
39
+
40
+ collection_name = klass.where(klass.separated_field => self.send(klass.separated_field)).ensured_collection.name
41
+ self.with(collection: collection_name) {self.send "#{action_method}_without_context", *args}
42
+ end
43
+
44
+ alias_method "#{action_method}_without_context".to_sym, action_method
45
+ alias_method action_method.to_sym, "#{action_method}_with_context".to_sym
46
+ end
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+ end
@@ -0,0 +1,126 @@
1
+ module Mongoid
2
+ module Contextual
3
+ private
4
+
5
+ # Changes:
6
+ # 1. Get form_id from selector
7
+ # 2. If collection is entries, not matter from context or current collection object, and form has entries_separated flag set, set collection name instance variable to entries plus form_id as new collection name
8
+ # 3. It's not good because it will query form to check entries_separated flag exists or not each time. Hope to find a better way later
9
+
10
+ def create_context_with_separated_entries
11
+ context = create_context_without_separated_entries
12
+ query_class = instance_variable_get :@klass
13
+ if should_query_from_separated_collection? query_class
14
+ new_collection_name = query_class.separated_collection_prefix + separated_value(query_class)
15
+ # self.selector.except!('form_id')
16
+ # filter = context.instance_variable_get(:@view).filter.except('form_id')
17
+ # context.instance_variable_get(:@view).instance_variable_set :@filter, filter
18
+ context.collection.instance_variable_set :@name, new_collection_name
19
+ collection.instance_variable_set :@name, new_collection_name
20
+ end
21
+ instance_variable_set :@context, context
22
+ context
23
+ end
24
+
25
+ def should_query_from_separated_collection? query_class
26
+ return if !query_class.respond_to?(:separated_field) || !query_class.send(:separated_field)
27
+ query_class.separated_parent_class.where(query_class.separated_parent_field => separated_value(query_class), query_class.separated_condition_field => true).exists?
28
+ end
29
+
30
+ def separated_value query_class
31
+ value = separated_selector(query_class)
32
+ value = separated_value_from_query_in_with_only_one_value(query_class) if value.is_a?(Hash)
33
+ value
34
+ end
35
+
36
+ def separated_value_from_query_in_with_only_one_value query_class
37
+ separated_selector(query_class)['$in']&.first
38
+ end
39
+
40
+ def separated_selector query_class
41
+ selector[query_class.separated_field.to_s]
42
+ end
43
+
44
+ alias_method :create_context_without_separated_entries, :create_context
45
+ alias_method :create_context, :create_context_with_separated_entries
46
+ end
47
+ end
48
+
49
+ module Mongoid
50
+ module Relations
51
+ module Referenced
52
+ class Many < Relations::Many
53
+ private
54
+
55
+ # Changes:
56
+ # 1. 'base' should be an instance of Form
57
+ # 2. If form has entries_separated flat and collection name is entries, clone a new context because it is build each time when called and set to context. Then remove form_id from selector because all the entries inside the new collection has the same form_id
58
+
59
+ def criteria_with_separated_entries
60
+ cri = criteria_without_separated_entries
61
+ query_class = cri.instance_variable_get :@klass
62
+ if should_query_from_separated_collection? query_class
63
+ context = cri.context.clone
64
+ context.collection.instance_variable_set :@name, "#{query_class.separated_collection_prefix}#{base.id}"
65
+ cri.instance_variable_set :'@collection', @collection
66
+ # cri.selector.except!('form_id')
67
+ end
68
+ cri
69
+ end
70
+
71
+ def should_query_from_separated_collection?(query_class)
72
+ query_class.respond_to?(:separated_field) && query_class.send(:separated_field) && base.is_a?(query_class.separated_parent_class) && base.send(query_class.separated_condition_field)
73
+ end
74
+
75
+ alias_method :criteria_without_separated_entries, :criteria
76
+ alias_method :criteria, :criteria_with_separated_entries
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+ module Mongoid
83
+ # The +Criteria+ class is the core object needed in Mongoid to retrieve
84
+ # objects from the database. It is a DSL that essentially sets up the
85
+ # selector and options arguments that get passed on to a Mongo::Collection
86
+ # in the Ruby driver. Each method on the +Criteria+ returns self to they
87
+ # can be chained in order to create a readable criterion to be executed
88
+ # against the database.
89
+ class Criteria
90
+ def ensured_collection
91
+ context.collection
92
+ end
93
+ end
94
+ end
95
+
96
+ module Mongoid
97
+ module Clients
98
+ module Options
99
+ def collection_with_separated(parent = nil)
100
+ klass = self.class
101
+ origin_collection = collection_without_separated(parent)
102
+ return origin_collection unless self.class.respond_to?(:separated_field)
103
+ origin_collection.instance_variable_set :@name, separated_collection_name(klass)
104
+ origin_collection
105
+ end
106
+
107
+ alias_method :collection_without_separated, :collection
108
+ alias_method :collection, :collection_with_separated
109
+
110
+ def collection_name_with_separated
111
+ klass = self.class
112
+ origin_collection_name = collection_name_without_separated
113
+ return origin_collection_name unless self.class.respond_to?(:separated_field)
114
+ separated_collection_name klass
115
+ end
116
+
117
+ alias_method :collection_name_without_separated, :collection_name
118
+ alias_method :collection_name, :collection_name_with_separated
119
+
120
+ def separated_collection_name(klass)
121
+ klass.where(klass.separated_field => self.send(klass.separated_field)).ensured_collection.name
122
+ end
123
+ end
124
+ end
125
+ end
126
+
@@ -0,0 +1,9 @@
1
+ module Mongoid
2
+ module CollectionSeparatable
3
+ class Railtie < ::Rails::Railtie
4
+ rake_tasks do
5
+ load 'mongoid/collection_separatable/tasks/separate.rake'
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,10 @@
1
+ namespace :db do
2
+ namespace :mongoid do
3
+ namespace :collection do
4
+ desc 'Separate records from one collection into another given a condition and new collection name. e.g. rake db:mongoid:collection:separate[Entry,form_id,5bf626b5c2a6f78321dafc50]'
5
+ task :separate, [:origin_class, :condition_key, :condition_value] => :environment do |_t, args|
6
+ Mongoid::CollectionSeparatable::Tasks::Separate.new(args).run
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,25 @@
1
+ module Mongoid
2
+ module CollectionSeparatable
3
+ module Tasks
4
+ class Separate
5
+ attr_accessor :origin_class, :condition_key, :condition_value
6
+
7
+ def initialize opts={}
8
+ @origin_class = opts[:origin_class].constantize
9
+ @condition_key = opts[:condition_key]
10
+ @condition_value = opts[:condition_value] = BSON::ObjectId.legal?(opts[:condition_value]) ? BSON::ObjectId.from_string(opts[:condition_value]) : opts[:condition_value]
11
+ end
12
+
13
+ def run
14
+ target_collection = "#{origin_class.collection_name}_#{condition_value}"
15
+
16
+ origin_class.collection.aggregate([
17
+ {'$match': {condition_key => condition_value}},
18
+ {'$out': target_collection}
19
+ ]).first
20
+
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,6 @@
1
+ module Mongoid
2
+ module CollectionSeparatable
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
6
+
@@ -0,0 +1,2 @@
1
+ require "mongoid/collection_separatable"
2
+
@@ -0,0 +1,30 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "mongoid/collection_separatable/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "mongoid-collection-separatable"
7
+ spec.version = Mongoid::CollectionSeparatable::VERSION
8
+ spec.authors = ["Oscar Jiang"]
9
+ spec.email = ["pengj0520@gmail.com"]
10
+
11
+ spec.summary = %q{Save the mongoid model into different collections by condition}
12
+ spec.description = %q{Mongoid models are saved in one collection by default. However, when collections after too large , it could be extracted into a separated one and query form that, to make it query faster}
13
+ spec.homepage = "https://github.com/warmwind/mongoid_collection_separatable"
14
+ spec.license = "MIT"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency 'mongoid', ' ~> 6.4.0'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.17"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ spec.add_development_dependency "pry-byebug", "~> 3.4"
29
+
30
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid-collection-separatable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Oscar Jiang
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mongoid
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 6.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: 6.4.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.4'
83
+ description: Mongoid models are saved in one collection by default. However, when
84
+ collections after too large , it could be extracted into a separated one and query
85
+ form that, to make it query faster
86
+ email:
87
+ - pengj0520@gmail.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - ".travis.yml"
95
+ - Gemfile
96
+ - Gemfile.lock
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - lib/mongoid-collection-separatable.rb
101
+ - lib/mongoid/collection_separatable.rb
102
+ - lib/mongoid/collection_separatable/monkey_patches.rb
103
+ - lib/mongoid/collection_separatable/railtie.rb
104
+ - lib/mongoid/collection_separatable/tasks/separate.rake
105
+ - lib/mongoid/collection_separatable/tasks/separate.rb
106
+ - lib/mongoid/collection_separatable/version.rb
107
+ - lib/mongoid_collection_separatable.rb
108
+ - mongoid_collection_separatable.gemspec
109
+ homepage: https://github.com/warmwind/mongoid_collection_separatable
110
+ licenses:
111
+ - MIT
112
+ metadata: {}
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubyforge_project:
129
+ rubygems_version: 2.7.8
130
+ signing_key:
131
+ specification_version: 4
132
+ summary: Save the mongoid model into different collections by condition
133
+ test_files: []