has_reindexable_associations 0.1.2

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
+ SHA1:
3
+ metadata.gz: fb3caa27d7fd9f6de7c61270f35e47b75024076e
4
+ data.tar.gz: 7ba1991147e8fb13fb4b943662c55fb67b3a10dd
5
+ SHA512:
6
+ metadata.gz: 72784bfc8bbd82402e196601ae45a40fba8834b03e19f2b6279d13ac8d9607748cf091dfce8ca690d9414b466408bfff90a7486d63114b168133575fcd7af3b0
7
+ data.tar.gz: b02de0a9d55aaab9b65f34ea35956b1c834d00638df428676c4a29572d746646d18c95cfd3f0dc63a42d5897f98406018b64e66da73a7ba3bee5ece25e27391f
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /vendor/bundle
11
+ /db/*.sqlite3
12
+ /*.gem
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.4
4
+ before_install: gem install bundler -v 1.11.2
5
+ services:
6
+ - elasticsearch
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at mkalita@artegence.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in has_reindexable_associations.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Efigence
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,44 @@
1
+ # HasReindexableAssociations [![Build Status](https://travis-ci.org/efigence/has_reindexable_associations.svg?branch=master)](https://travis-ci.org/efigence/has_reindexable_associations)
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/has_reindexable_associations`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'has_reindexable_associations'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install has_reindexable_associations
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/has_reindexable_associations. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## TODO
38
+
39
+ write README.md
40
+
41
+ ## License
42
+
43
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
44
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "has_reindexable_associations"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,49 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'has_reindexable_associations/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "has_reindexable_associations"
8
+ spec.version = HasReindexableAssociations::VERSION
9
+ spec.authors = ["Marcin Kalita"]
10
+ spec.email = ["mkalita@efigence.com"]
11
+
12
+ spec.summary = %q{Automatic Elasticsearch Reindexing of Active Record Associations}
13
+ spec.description = %q{Keep specified associations in sync with ease using async reindexing (searchkick gem).}
14
+ spec.homepage = "https://github.com/efigence/has_reindexable_associations"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ # spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.files = [".gitignore",
27
+ ".travis.yml",
28
+ "CODE_OF_CONDUCT.md",
29
+ "Gemfile",
30
+ "LICENSE.txt",
31
+ "README.md",
32
+ "Rakefile",
33
+ "bin/console",
34
+ "bin/setup",
35
+ "has_reindexable_associations.gemspec",
36
+ "lib/has_reindexable_associations.rb",
37
+ "lib/has_reindexable_associations/version.rb"]
38
+
39
+ spec.bindir = "exe"
40
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
41
+ spec.require_paths = ["lib"]
42
+
43
+ spec.add_dependency "rails", ">= 4.1"
44
+ spec.add_dependency "searchkick", ">= 0.9.1"
45
+ spec.add_development_dependency "sqlite3", ">= 1.3.10"
46
+ spec.add_development_dependency "bundler", "~> 1.11"
47
+ spec.add_development_dependency "rake", "~> 10.0"
48
+ spec.add_development_dependency "minitest", "~> 5.0"
49
+ end
@@ -0,0 +1,3 @@
1
+ module HasReindexableAssociations
2
+ VERSION = "0.1.2"
3
+ end
@@ -0,0 +1,113 @@
1
+ require "has_reindexable_associations/version"
2
+ require 'rails/all'
3
+ require 'searchkick'
4
+
5
+ # To avoid full model reindex, one can't just call reindex on a scope.
6
+ # One must call reindex_async on each associated record.
7
+ # Usage:
8
+ # MyClass < ActiveRecord::Base
9
+ # include HasReindexableAssociations
10
+ # belongs_to :some_association
11
+ # has_many :some_associations
12
+ # has_reindexable_associations :some_association, :some_associations
13
+ module HasReindexableAssociations
14
+ extend ActiveSupport::Concern
15
+
16
+ class_methods do
17
+ cattr_writer :reindexable_associations
18
+
19
+ # Set this to `true` before any seeds or imports to reindex associations later on your own like this:
20
+ # MyClass.reindexable_associations_skip = true
21
+ # MyClass.import(...)
22
+ # MyClass.reindex
23
+ # MyClass.reindexable_associations_skip = false
24
+ # MyClass.reindexable_associations.each { |association| MyClass.send(association).model.reindex }
25
+ cattr_accessor :reindexable_associations_skip
26
+
27
+ def has_reindexable_associations(*args)
28
+ # @@ is shared between classes via concern, not what we want
29
+ # @@reindexable_associations = args
30
+ self.class_variable_set :@@reindexable_associations, args
31
+ end
32
+
33
+ def reindexable_associations
34
+ # @@reindexable_associations || []
35
+ self.class_variable_get :@@reindexable_associations
36
+ rescue NameError
37
+ []
38
+ end
39
+ end
40
+
41
+ included do
42
+ after_commit :reindex_associations
43
+
44
+ private
45
+
46
+ def reindex_associations
47
+ self.class.reindexable_associations.each do |reindexable_association|
48
+ reindex_associations_process(reindexable_association)
49
+ end
50
+ end
51
+
52
+ def reindex_associations_process(reindexable_association)
53
+ record_or_records = reindex_associations_scope(reindexable_association)
54
+ if record_or_records.respond_to?(:to_a)
55
+ reindex_associations_process_records(record_or_records, reindexable_association)
56
+ else
57
+ reindex_associations_process_record(record_or_records, reindexable_association)
58
+ end
59
+ end
60
+
61
+ def reindex_associations_scope(reindexable_association)
62
+ begin
63
+ record_or_records = self.send(reindexable_association)
64
+ rescue NoMethodError
65
+ fail NoMethodError.new("foreign key '#{reindexable_association}' is not defined, verify has_reindexable_associations options")
66
+ end
67
+ end
68
+
69
+ def reindex_associations_process_records(records, reindexable_association)
70
+ unless self.class.reindexable_associations_skip
71
+ records.to_a.each do |record|
72
+ reindex_associations_process_record(record, reindexable_association)
73
+ end
74
+ end
75
+ end
76
+
77
+ def reindex_associations_process_record(record, reindexable_association)
78
+ reindex_association(record)
79
+ reindex_associations_process_old_record(reindexable_association)
80
+ end
81
+
82
+ def reindex_association(record)
83
+ record.reindex_async
84
+ end
85
+
86
+ def reindex_associations_find_old_polymorphic_association(reindexable_association)
87
+ key, type = reindex_associations_describe_polymorphic_association(reindexable_association)
88
+ old_klass = reindex_associations_describe_old_polymorphic_association(key, type)
89
+ return unless old_klass && previous_changes[key] && previous_changes[key].first
90
+ old_record = old_klass.where(id: previous_changes[key].first).first
91
+ end
92
+
93
+ def reindex_associations_process_old_record(reindexable_association)
94
+ old_record = reindex_associations_find_old_polymorphic_association(reindexable_association)
95
+ reindex_association(old_record) if old_record
96
+ end
97
+
98
+ def reindex_associations_describe_polymorphic_association(reindexable_association)
99
+ key = self.class.reflect_on_association(reindexable_association).try(:foreign_key)
100
+ type = self.class.reflect_on_association(reindexable_association).try(:foreign_type)
101
+ [key, type]
102
+ end
103
+
104
+ def reindex_associations_describe_old_polymorphic_association(key, type)
105
+ if (previous_changes[key] && previous_changes[key].first) || (previous_changes[type] && previous_changes[type].first)
106
+ old_klass = previous_changes[type].first rescue NameError
107
+ old_klass = self.send(type).constantize if old_klass == NameError
108
+ end
109
+ rescue NameError
110
+ end
111
+ end
112
+ end
113
+
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_reindexable_associations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Marcin Kalita
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-22 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.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: searchkick
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 1.3.10
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 1.3.10
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.11'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: minitest
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '5.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '5.0'
97
+ description: Keep specified associations in sync with ease using async reindexing
98
+ (searchkick gem).
99
+ email:
100
+ - mkalita@efigence.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".travis.yml"
107
+ - CODE_OF_CONDUCT.md
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - has_reindexable_associations.gemspec
115
+ - lib/has_reindexable_associations.rb
116
+ - lib/has_reindexable_associations/version.rb
117
+ homepage: https://github.com/efigence/has_reindexable_associations
118
+ licenses:
119
+ - MIT
120
+ metadata:
121
+ allowed_push_host: https://rubygems.org
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 2.5.1
139
+ signing_key:
140
+ specification_version: 4
141
+ summary: Automatic Elasticsearch Reindexing of Active Record Associations
142
+ test_files: []