chewy-translation 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b2e335e8f628227d76c63fe7dfabd1618435907a
4
+ data.tar.gz: 612041bbe1a308470135dd54d21b84c28698f69a
5
+ SHA512:
6
+ metadata.gz: ba4169bc1edb94a67ba5fa8a210bac89d109e4b4942bdf5c8e7f00f1029063c806e979bc3a70b9129334c4480cf5bb2d8f66c8ace509c2a4b48e473c608b76b6
7
+ data.tar.gz: dad938f4dbecd50400366018255e3b20d37ba7663da5cf93cd43ebd53c395c6339bcc34e803b31587cc6354e542c8aa99cc72e9a9235ac12ffe38d972e7393bf
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.3
5
+ before_install: gem install bundler -v 1.12.5
@@ -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 dev@jobteaser.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,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ ruby '2.3.1'
4
+
5
+ # Specify your gem's dependencies in chewy-translation.gemspec
6
+
7
+ gem 'chewy'
8
+ gem 'activesupport'
9
+
10
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 JobTeaser
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.
@@ -0,0 +1,102 @@
1
+ # Chewy::Translation
2
+
3
+ Chewy::Translation is an easy integration of Translation within Chewy.
4
+
5
+ Chewy::Translation handles translated content in elasticsearch.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'chewy-translation'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install chewy-translation
22
+
23
+ ## Usage
24
+
25
+ ### Indexing translated content
26
+
27
+ Add a new field in `/app/chewy/user_index.rb`
28
+
29
+ ```ruby
30
+ class UserIndex < Chewy::Index
31
+
32
+ define_type User, name: :global do
33
+ field :translations, type: 'nested' do
34
+ field :locale, type: 'string'
35
+ field :name, type: 'string' # add analysers if needed
36
+ end
37
+ end
38
+
39
+ end
40
+ ```
41
+
42
+ If you need to handle an activation status for your translations, add an `active` column in the translation table and index it as follow:
43
+
44
+ ```ruby
45
+ class UserIndex < Chewy::Index
46
+
47
+ define_type User, name: :global do
48
+ field :active_locales, mode: 'arrayed', value:
49
+ -> (c) { c.translations.select { |t| t.active == true }.map(&:locale) }
50
+ end
51
+
52
+ end
53
+ ```
54
+
55
+ ### Querying and filtering translated content
56
+
57
+ #### Autocomplete:
58
+ The autocomplete method searches only in the wanted locale.
59
+ For example the following query will return all the users with a name starting with 'Idefix' in french:
60
+
61
+ ```ruby
62
+ UserIndex::Global.autocomplete(fields: ['name'], query: 'Idefix', locale: :fr)
63
+ ```
64
+ It's possible to perform an autocomplete on more than one simple field, for example you can imagine for Users an autocomplete on first_name and last_name: `fields: ['first_name', 'last_name']`.
65
+
66
+ ## Search:
67
+ If the field is translated, the search_by will search in all the available locales in the context.
68
+ For example the following query will return all the users with 'Getafix' in their names (in any available locale).
69
+ ```ruby
70
+ UserIndex::Global.search_by(name: 'Getafix')
71
+ ```
72
+
73
+ Let's assume we have an object named 'Getafix' in english and 'Panoramix' in french, if a user with a french interface perform the query above, it will return 'Panoramix'.
74
+
75
+ It's also possible to search on different fiels on the same query:
76
+ ```ruby
77
+ UserIndex::Global.search_by(name: 'Getafix', job: 'druid')
78
+ ```
79
+
80
+ The search_by method can also be used on numeric fields, and it is possible to give an array of numbers to those fields (and it will perform a 'or' between those numbers).
81
+ ```ruby
82
+ UserIndex::Global.search_by(name: 'Getafix', some_id: 123, another_id: [123, 456, 789])
83
+ ```
84
+ It will display the object with:
85
+ - 'Getafix' in their name
86
+ - 'sickle_id' = 123
87
+ - 'mistletoe_id' in [123, 456, 789]
88
+
89
+ An other way to perform a search is tu use `search_by_fields`, it allows to search a single string in different fields. For example to search a object containing 'Getafix' in the name or in the character_description:
90
+ ```ruby
91
+ UserIndex::Global.search_by_fields(fields: [:name, :character_description], query: 'Getafix')
92
+ ```
93
+
94
+ ## Contributing
95
+
96
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jobteaser/chewy-translation. 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.
97
+
98
+
99
+ ## License
100
+
101
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
102
+
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "chewy/translation"
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
@@ -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,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'chewy/translation/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'chewy-translation'
8
+ spec.version = Chewy::Translation::VERSION
9
+ spec.authors = ['JobTeaser']
10
+ spec.email = ['dev@jobteaser.com']
11
+
12
+ spec.summary = %q{Index and search on translated content with chewy.}
13
+ spec.description = %q{Index and search on translated content with chewy.}
14
+ spec.homepage = 'https://github.com/jobteaser/chewy-translation'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.12'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'minitest', '~> 5.0'
25
+ end
@@ -0,0 +1,11 @@
1
+ require 'chewy/translation/query'
2
+ require 'chewy/translation/index'
3
+ require 'chewy/translation/version'
4
+
5
+ module Chewy
6
+ module Translation
7
+ Chewy::Query.send(:include, Chewy::Translation::Query)
8
+ Chewy::Index.send(:include, Chewy::Translation::Index)
9
+ Chewy::Type.send(:include, Chewy::Translation::Index)
10
+ end
11
+ end
@@ -0,0 +1,16 @@
1
+ module Chewy
2
+ module Translation
3
+ module Index
4
+ extend ActiveSupport::Concern
5
+ included do
6
+ singleton_class.delegate :autocomplete, to: :all
7
+ singleton_class.delegate :permitted, to: :all
8
+ singleton_class.delegate :search_by, to: :all
9
+ singleton_class.delegate :search_by_fields, to: :all
10
+ singleton_class.delegate :nested_search_by, to: :all
11
+ singleton_class.delegate :active_filter, to: :all
12
+ singleton_class.delegate :inactive_filter, to: :all
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,196 @@
1
+ module Chewy
2
+ module Translation
3
+ module Query
4
+ # Called on an Index, will autocomplete on the fields regarding or not the locale
5
+ # Usage :
6
+ # UserIndex::Global.autocomplete(fields: ['name'], query: 'Idefix', locale: 'fr')
7
+ # => Users with 'Idefix' in their name in french
8
+ # UserIndex.autocomplete(fields:['first_name', 'last_name'], query: 'Ordralfabetix', locale: 'fr')
9
+ # => Users with 'Ordralfabetix' in their first_name or in their last_name
10
+ #
11
+ # /!\ it will fail if you try to autocomplete a non translated field with a locale
12
+ #
13
+ # @param fields [array] fields autocompleted
14
+ # @param query [string] query of the autocomplete
15
+ # @param locale [string] context locale if the field is translated
16
+ # @return [chewy::query]
17
+ def autocomplete(fields:, query:, locale: nil)
18
+ return search_by_fields(fields: fields, query: query) unless locale
19
+ search_by_fields_with_locale(fields: fields, query: query, locale: locale)
20
+ end
21
+
22
+ # Principal search function that should be used as often as possible
23
+ # Usage:
24
+ # UserIndex.search_by(name: 'Assurancetour')
25
+ # => users including Assurancetour in their name
26
+ # UserIndex.search_by(name: 'Assurancetour', harp_id: 1, house_in_tree_id: [1, 2, 3])
27
+ # => users including oran in their name, with harp_id 1 and house_in_tree_id 1, 2 or 3
28
+ #
29
+ # @param opts [Hash] fields and their associated query
30
+ # @return [Chewy::Query]
31
+ def search_by(opts = {})
32
+ return self if opts.blank?
33
+ results = self
34
+ opts.delete_if { |_, v| v.blank? }.each do |k, v|
35
+ results = results.unit_search_by(k, v)
36
+ end
37
+ results
38
+ end
39
+
40
+ # Another way to search the indexes
41
+ # Usage:
42
+ # UserIndex.search_by_fields(fields: ['name', 'character_description'], query: 'Falbala')
43
+ # => all objects containing 'Falbala' in name or in character_description
44
+ #
45
+ # @param fields [Array]
46
+ # @param query [String]
47
+ # @param query_mode [Symbol] :should or :must
48
+ # (:should by default,
49
+ # this is most of all for the use of this method from the inside of this module)
50
+ # @return [Chewy::Query]
51
+ def search_by_fields(fields:, query:, query_mode: :should)
52
+ nested_fields = translated_fields & fields unless translated_fields.blank?
53
+ fields -= translated_fields unless translated_fields.blank?
54
+ result = []
55
+ result << { multi_match: { fields: fields, query: query } } unless fields.blank?
56
+ result << nested_search_by_fields(
57
+ path: 'translations', fields: nested_fields, query: query
58
+ ) unless nested_fields.blank?
59
+ result = { bool: { query_mode => result } }
60
+ query(result)
61
+ end
62
+
63
+ # Usage:
64
+ # FortifiedCampIndex::Global.active_filter(['fr', 'en'])
65
+ # => FortifiedCamps active either in french OR in english OR both
66
+ #
67
+ # @param locales [Array] List of locales on the context
68
+ # @return [Chewy::Query]
69
+ def active_filter(locales:)
70
+ activation_status_filter(active: true, locales: locales)
71
+ end
72
+
73
+ # Usage:
74
+ # FortifiedCampsIndex::Global.active_filter(['fr', 'en'])
75
+ # => FortifiedCamps inactive in french AND in english
76
+ #
77
+ # @param locales [Array] List of locales on the context
78
+ # @return [Chewy::Query]
79
+ def inactive_filter(locales:)
80
+ activation_status_filter(active: false, locales: locales)
81
+ end
82
+
83
+ # Usage:
84
+ # FortifiedCampsIndex.permitted(policy, :enter?)
85
+ # FortifiedCampsIndex.filter { !centurion? }.permitted(policy, :enter?).filter(active:true)
86
+ #
87
+ # To call query.permitted(policy, :enter?) is similar to call policy.elasticsearch_show?(enter)
88
+ #
89
+ # The permission are defined in each policy,
90
+ # in the filter module and should be build as follow:
91
+ # def elasticsearch_action?(query)
92
+ # # returns a chewy query
93
+ # end
94
+ # It needs to be prefixed with elasticsearch
95
+ # and needs to take a query or an index and return a query
96
+ #
97
+ # @param policy [Policy] could be any type of policy linked to the Index.
98
+ # ie: when the method is called on CompanyIndex,
99
+ # it should be CompanyPolicy or Backend::CompanyPolicy
100
+ # @param action [Symbol]
101
+ # @return [ChewyIndex::Query]
102
+ def permitted(policy, action)
103
+ policy.send("elasticsearch_#{action}", self)
104
+ end
105
+
106
+ # Build the hash needed to perform search in nested fields
107
+ # Usage:
108
+ # UserIndex.query(nested_search_by_fields(
109
+ # path: 'nested_path',
110
+ # fields: ['name'],
111
+ # query: 'Fulliautomatix')
112
+ # )
113
+ # => all objects containing 'Fulliautomatix' in nested_path.name
114
+ #
115
+ # CAUTION: this will fail if the field is not of nested type
116
+ #
117
+ # @param path [String]
118
+ # @param fields [Array]
119
+ # @param query [String]
120
+ # @return [Hash]
121
+ def nested_search_by_fields(path:, fields:, query:)
122
+ { nested: {
123
+ path: path, query:
124
+ { multi_match: { fields: fields.map { |f| "#{path}.#{f}" }, query: query }
125
+ }
126
+ } }
127
+ end
128
+
129
+ # Atomic function for search by, should not be called from the outside of this module
130
+ #
131
+ # @param field [String]
132
+ # @param value [String|Integer|Array]
133
+ # @return [Chewy::Query]
134
+ def unit_search_by(field, value)
135
+ if value.is_a?(String)
136
+ search_by_fields(fields: [field.to_s], query: value, query_mode: :must)
137
+ elsif value.is_a?(Fixnum)
138
+ query(match: { field => value }).query_mode(:must)
139
+ elsif value.is_a?(DateTime)
140
+ # TODO: handle dates
141
+ elsif value.is_a?(Array)
142
+ query(bool: { should: value.compact.map { |e| { match: { field => e } } } })
143
+ end
144
+ end
145
+
146
+ # This should not be called from the outside of this module,
147
+ # use active_filter or inactive_filter instead.
148
+ #
149
+ # @param locales [Array] list of locales on the context
150
+ # @param active [Boolean] true to have the active records, false to have the inactive records
151
+ # @return [ChewyIndex::Query]
152
+ def activation_status_filter(locales:, active:)
153
+ filter_mode = active ? :or : :and
154
+ results = []
155
+ locales.each do |locale|
156
+ filter_hash = { term: { active_locales: locale } }
157
+ filter_hash = { not: filter_hash } unless active
158
+ results << filter_hash
159
+ end
160
+ filter(filter_mode => results)
161
+ end
162
+
163
+ # This should not be called from the outside of this module,
164
+ # call autcomplete or search_by instead.
165
+ #
166
+ # @oaram fields [Array]
167
+ # @param query [String]
168
+ # @param locale [String]
169
+ # @return [Chewy::Query]
170
+ def search_by_fields_with_locale(fields:, query:, locale:)
171
+ query(
172
+ nested: { path: 'translations', query: { bool: {
173
+ must: [
174
+ { multi_match: { fields: fields.map { |f| "translations.#{f}" }, query: query } },
175
+ { match: { 'translations.locale' => locale } }
176
+ ]
177
+ } } }
178
+ )
179
+ end
180
+
181
+ # Generate the array of all the translated fields of the current index
182
+ # @return [Array]
183
+ def translated_fields
184
+ @_translated_fields ||= current_index.mappings_hash[:mappings].
185
+ values.first.values.first[:translations].
186
+ try(:[], :properties).try(:keys).try(:map, &:to_s)
187
+ end
188
+
189
+ # Name of the current index
190
+ # @return [Class]
191
+ def current_index
192
+ self.class.to_s.split(':').first.constantize
193
+ end
194
+ end
195
+ end
196
+ end
@@ -0,0 +1,5 @@
1
+ module Chewy
2
+ module Translation
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chewy-translation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - JobTeaser
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: Index and search on translated content with chewy.
56
+ email:
57
+ - dev@jobteaser.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".travis.yml"
64
+ - CODE_OF_CONDUCT.md
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - chewy-translation.gemspec
72
+ - lib/chewy/translation.rb
73
+ - lib/chewy/translation/index.rb
74
+ - lib/chewy/translation/query.rb
75
+ - lib/chewy/translation/version.rb
76
+ homepage: https://github.com/jobteaser/chewy-translation
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.5.1
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Index and search on translated content with chewy.
100
+ test_files: []