combi_search 0.1.0

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: 855e3f92f4b8b51c83b7640408358a8631cd721c
4
+ data.tar.gz: b453298e6535615e504e29a57478a93168699a82
5
+ SHA512:
6
+ metadata.gz: db99389c0fd91731a12bfcc2b6dc390236ae1becca5a3121a826ac925856fc80b4214a4556b2d30faf282939aa41d603ce2c2cee8e3b223700ce978c710ba596
7
+ data.tar.gz: bdcd43fc941e8fcea31f1f8601da9676a40eb9b92552edc97bb1f88746ab0fec0e7381d12540fae37b47d12a235ba73556ee58295b372dc137777d906a3d9c77
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in combi_search.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Douwe Homans
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,146 @@
1
+ # CombiSearch
2
+
3
+ Use CombiSearch to add a 'global' search to your app. For example; if you have `Book`s and `Movie`s and you want a combined search where you search in all of your titles.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'combi_search'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install combi_search
20
+
21
+ ## Usage
22
+ #### 1. Add the _internal_ CombiSearch-Entry model to your database-schema
23
+ This model is used to store your 'combined' search data. You can create it by running the following command in your rails console
24
+
25
+ CombiSearch.create_table
26
+
27
+ The best way to add it to a rails project however, is to create a migration for it...
28
+ That migration could look like this.
29
+
30
+ ```ruby
31
+ class CreateCombiSearchEntries < ActiveRecord::Migration
32
+ def self.up
33
+ say_with_time("Creating table for combi_search_entries") do
34
+ CombiSearch.create_table
35
+ end
36
+ end
37
+
38
+ def self.down
39
+ say_with_time("Dropping table for combi_search_entries") do
40
+ CombiSearch.drop_table
41
+ end
42
+ end
43
+ end
44
+ ```
45
+
46
+ #### 2. Add `combi_search_scope`'s to your models
47
+ For every model you want to include in your global search you should call `combi_search_scope` with the name of the scope, the model's `attributes` you want to include for that scope, and an optional `if` condition.
48
+
49
+ Say you have a `Book` and a `Movie` model, and you want to search either on their titles, or all their text-contents; then your setup should look like this.
50
+
51
+ ```ruby
52
+ class Book < ActiveRecord::Base
53
+ include CombiSearch
54
+ combi_search_scope :titles, on: [:title]
55
+ combi_search_scope :full_text, on: [:title, :author, :content]
56
+ end
57
+
58
+ class Movie < ActiveRecord::Base
59
+ include CombiSearch
60
+ combi_search_scope :titles, on: [:title]
61
+ combi_search_scope :full_text, on: [:title, :director, :script_content]
62
+ end
63
+ ```
64
+
65
+ If you only want to include `published` books, when you search in the titles scope, then you should change your code like this.
66
+
67
+ ```ruby
68
+ combi_search_scope :titles, on: => [:title], if: lambda { |book| book.published }
69
+ ```
70
+
71
+ #### 3. Create your search index
72
+ The search-index gets updated once your model is updated. To make sure your search-index is up-to-date when you add it for pre-existing models, you can run the following in your console:
73
+
74
+ # delete existing index
75
+ CombiSearch.remove_index
76
+
77
+ # Create index for Book's
78
+ Book.update_combi_search
79
+
80
+ # Create index for Movie's
81
+ Movie.update_combi_search
82
+
83
+ The best way _again_ to do this for your rails project, is to create a migration for it...
84
+ That migration could look like this.
85
+
86
+ ```ruby
87
+ class CreateCombiSearchEntries < ActiveRecord::Migration
88
+ def self.up
89
+ say_with_time("Updating search index") do
90
+ CombiSearch.remove_index
91
+ Book.update_combi_search
92
+ Movie.update_combi_search
93
+ end
94
+ end
95
+
96
+ def self.down
97
+ # there isn't a down migration for this....
98
+ end
99
+ end
100
+ ```
101
+
102
+ #### 4. That's it: search for it!
103
+ To find all Book's and Movie's where the `:titles` includes Harry Potter:
104
+
105
+ library_items = CombiSearch.scoped(:titles).search("Harry Potter")
106
+
107
+ To find all Book's and Movie's where the `:full_text` includes "Abacadabra":
108
+
109
+ library_items = CombiSearch.scoped(:full_text).search("Abacadabra")
110
+
111
+ This returns an `ActiveRecord::Relation` of `CombiSearch::Entry` models. They are not that interesting.... however they have a relation to an included `searchable` model which is the original (`Book` or `Movie`) model.
112
+
113
+ The following code:
114
+
115
+ ```ruby
116
+ library_items = CombiSearch.scoped(:titles).search("Harry Potter")
117
+
118
+ library_items.each { |item|
119
+ original = item.searchable
120
+ if original.is_a?(Book)
121
+ puts "We found a Book; and the title is: #{original.title}"
122
+ end
123
+ if original.is_a?(Movie)
124
+ puts "We found a Movie; and the director is: #{original.director}"
125
+ end
126
+ }
127
+ ```
128
+
129
+ Could output something like:
130
+
131
+ We found a Movie; and the director is: Chris Columbus
132
+ We found a Book; and the title is: Harry Potter and the Sorcerer's Stone
133
+
134
+ ## Development
135
+
136
+ After checking out the repo, run `bundle exec rspec spec` to run the tests.
137
+
138
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
139
+
140
+ ## Contributing
141
+
142
+ 1. Fork it ( https://github.com/douweh/combi_search/fork )
143
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
144
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
145
+ 4. Push to the branch (`git push origin my-new-feature`)
146
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "combi_search"
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'combi_search/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "combi_search"
8
+ spec.version = CombiSearch::VERSION
9
+ spec.authors = ["Douwe Homans"]
10
+ spec.email = ["douwe@avocado.nl"]
11
+
12
+ spec.summary = "Search in multiple models with one combined query."
13
+ spec.description = "Use CombiSearch to add a 'global' search to your app. For example; if you have `Book`s and `Movie`s and you want a combined search where you search in all of your titles."
14
+ spec.homepage = "https://github.com/douweh/combi_search"
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.8"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.2"
25
+
26
+ spec.add_dependency "rails", "~> 4.0"
27
+ spec.add_dependency "sqlite3"
28
+ spec.add_dependency "search_cop"
29
+ end
@@ -0,0 +1,124 @@
1
+ require "active_support"
2
+
3
+ require "combi_search/version"
4
+ require "combi_search/entry"
5
+ require "combi_search/migration"
6
+
7
+ module CombiSearch
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+
12
+ has_many :search_entries,
13
+ :as => :searchable,
14
+ :class_name => "CombiSearch::Entry",
15
+ :dependent => :delete_all
16
+
17
+ # register after_save handler to update search entry
18
+ after_save :update_search_entries
19
+
20
+ # register class_attribute combi_search_scopes to store search scopes
21
+ class_attribute :combi_search_scopes
22
+ self.combi_search_scopes = {};
23
+ end
24
+
25
+ def search_string_for_attrs(attrs)
26
+ if attrs.class == Array
27
+ return attrs.map { |attr| search_string_for_attrs(attr)}.join("\n")
28
+ end
29
+ if attrs.class == Symbol
30
+ return send(attrs)
31
+ end
32
+ end
33
+
34
+ def update_search_entries
35
+ # Retrieve all combi_search_scopes defined for our class
36
+ search_scopes = self.class.combi_search_scopes
37
+ existing_entries_hash = {}
38
+ search_entries.pluck(:id, :scope).each { |result|
39
+ existing_entries_hash[result[1].to_sym] = result[0]
40
+ }
41
+ search_scopes.each { |scope, array_with_configs|
42
+
43
+ # loop over all configs, to determine our searchable_text, if any...
44
+ search_entry_should_exist = false
45
+ searchable_text = ""
46
+
47
+ array_with_configs.each { |option|
48
+
49
+ # if an :if condition was defined, test it
50
+ if option[:if] && option[:if].is_a?(Proc)
51
+ if_condition_matched = option[:if].call(self)
52
+
53
+ # if this condition matched, we should use the text defined in this configuration
54
+ if if_condition_matched
55
+ search_entry_should_exist = true
56
+ searchable_text = search_string_for_attrs(option[:on])
57
+ end
58
+
59
+ # if there was no 'if' condition, then we shouldn't test for it,
60
+ # we should use the text defined in this configuration
61
+ else
62
+ search_entry_should_exist = true
63
+ searchable_text = search_string_for_attrs(option[:on])
64
+ end
65
+ }
66
+
67
+ # if we found a matching config (either with if-condition or not), create or update the entry
68
+ if search_entry_should_exist
69
+ # pre existing search_entry
70
+ id = existing_entries_hash[scope]
71
+ if !id.nil?
72
+ search_entries.update(id, :content=>searchable_text)
73
+ existing_entries_hash.delete(scope)
74
+ else
75
+ search_entries.create(:scope=>scope, :content=>searchable_text)
76
+ end
77
+ end
78
+ }
79
+
80
+ # add this point all pre_existing_entries should be updated and removed from the hash
81
+ # when there are still entries in the hash it means that 'scope' got removed in code
82
+ # therefore we should remove it's search_entry from the database
83
+ remove_ids = existing_entries_hash.each { |scope, id|
84
+ search_entries.delete(id)
85
+ }
86
+ end
87
+
88
+ module ClassMethods
89
+
90
+ # Method to add a combi_search_scope for a specific model
91
+ # Usage:
92
+ # combi_search_scope :public, on: [:name, :title, :something_else]
93
+ def combi_search_scope(name, options = {})
94
+ if options.nil? || options[:on].nil? || !(options[:on].class == Symbol || options[:on].class == Array)
95
+ raise "No attributes defined for combi_search_scope: #{:name}, in: #{self}"
96
+ end
97
+
98
+ if !self.combi_search_scopes[name].is_a?(Array)
99
+ self.combi_search_scopes[name]=[]
100
+ end
101
+ self.combi_search_scopes[name].push(options)
102
+ end
103
+
104
+ def update_combi_search
105
+ self.all.each { |model|
106
+ model.update_search_entries
107
+ }
108
+ end
109
+
110
+ end
111
+
112
+
113
+ ## MODULE METHODS
114
+
115
+ def self.scoped(scope)
116
+ CombiSearch::Entry.where(:scope=>scope).includes(:searchable)
117
+ end
118
+
119
+ def self.remove_index
120
+ CombiSearch::Entry.destroy_all
121
+ end
122
+
123
+
124
+ end
@@ -0,0 +1,15 @@
1
+ require "active_record"
2
+ require "search_cop"
3
+
4
+ module CombiSearch
5
+ class Entry < ActiveRecord::Base
6
+ include SearchCop
7
+
8
+ self.table_name = 'combi_search_entries'
9
+ belongs_to :searchable, :polymorphic => true
10
+
11
+ search_scope :search do
12
+ attributes :content
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,14 @@
1
+ module CombiSearch
2
+ def self.create_table
3
+ ActiveRecord::Base.connection.create_table :combi_search_entries do |t|
4
+ t.text :content
5
+ t.text :scope
6
+ t.belongs_to :searchable, :polymorphic => true
7
+ t.timestamps null: true
8
+ end
9
+ end
10
+
11
+ def self.drop_table
12
+ ActiveRecord::Base.connection.drop_table :combi_search_entries
13
+ end
14
+ end
@@ -0,0 +1,3 @@
1
+ module CombiSearch
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: combi_search
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Douwe Homans
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-06 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.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: search_cop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Use CombiSearch to add a 'global' search to your app. For example; if
98
+ you have `Book`s and `Movie`s and you want a combined search where you search in
99
+ all of your titles.
100
+ email:
101
+ - douwe@avocado.nl
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".gitignore"
107
+ - ".rspec"
108
+ - ".travis.yml"
109
+ - CODE_OF_CONDUCT.md
110
+ - Gemfile
111
+ - LICENSE.txt
112
+ - README.md
113
+ - Rakefile
114
+ - bin/console
115
+ - bin/setup
116
+ - combi_search.gemspec
117
+ - lib/combi_search.rb
118
+ - lib/combi_search/entry.rb
119
+ - lib/combi_search/migration.rb
120
+ - lib/combi_search/version.rb
121
+ homepage: https://github.com/douweh/combi_search
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.4.6
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Search in multiple models with one combined query.
145
+ test_files: []