barely_searchable 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +38 -0
- data/Rakefile +2 -0
- data/barely_searchable.gemspec +17 -0
- data/lib/barely_searchable/model_injections.rb +52 -0
- data/lib/barely_searchable/railtie.rb +11 -0
- data/lib/barely_searchable/version.rb +3 -0
- data/lib/barely_searchable.rb +6 -0
- metadata +72 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Robert L. Carpenter
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# BarelySearchable
|
2
|
+
|
3
|
+
This is a bare-bones almost functional "search" ability for your model layer.
|
4
|
+
|
5
|
+
It adds `.search` to your models which builds out a simple `LIKE OR` query to search your models and requires no external service to index the your tables or any index tables.
|
6
|
+
|
7
|
+
It is a terrible and slow search engine....but it'll probably work for a handful of use cases until you quit being lazy and install Sphinx.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile: `gem 'barely_searchable'`
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
In your model:
|
16
|
+
|
17
|
+
```
|
18
|
+
class User < ActiveRecord::Base
|
19
|
+
#define the fields it'll search on
|
20
|
+
searches_on :id, :username, :email, :first_name, :last_name
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
Now elsewhere in your application:
|
25
|
+
|
26
|
+
```
|
27
|
+
def search_users
|
28
|
+
@users = User.search 'user@domain.com'
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
## Contributing
|
33
|
+
|
34
|
+
1. Fork it
|
35
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
36
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
37
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
38
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/barely_searchable/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Robert L. Carpenter"]
|
6
|
+
gem.email = ["codemonkey@robacarp.com"]
|
7
|
+
gem.description = %q{Adds a very rudimentary .search() function to your activerecord models}
|
8
|
+
gem.summary = %q{Add quick and dirty search functionality to your model layer without any indexing service}
|
9
|
+
gem.homepage = "https://github.com/robacarp/barely_searchable"
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "barely_searchable"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = BarelySearchable::VERSION
|
17
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'ruby-debug'
|
2
|
+
|
3
|
+
module BarelySearchable
|
4
|
+
module ModelInjections
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def searches_on *fields
|
8
|
+
if @searches.nil?
|
9
|
+
init_searches_on
|
10
|
+
end
|
11
|
+
|
12
|
+
@searches << fields
|
13
|
+
@searches.flatten!
|
14
|
+
end
|
15
|
+
|
16
|
+
def init_searches_on
|
17
|
+
@searches = []
|
18
|
+
end
|
19
|
+
|
20
|
+
def search query, limit = nil
|
21
|
+
where = '0'
|
22
|
+
like = '%' + query.to_s.gsub(' ','%') + '%'
|
23
|
+
|
24
|
+
@searches.each do |field|
|
25
|
+
field = field.to_s
|
26
|
+
unless self.columns_hash.include? field
|
27
|
+
raise NameError, "#{field} is not a valid column on #{self}"
|
28
|
+
end
|
29
|
+
|
30
|
+
if [:integer, :decimal, :float].include? self.columns_hash[ field.to_s ].type
|
31
|
+
where += ' or ' + self.arel_table[field].eq( query ).to_sql
|
32
|
+
else
|
33
|
+
where += ' or ' + self.arel_table[field].matches( like ).to_sql
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
if limit.nil?
|
38
|
+
self.where( where )
|
39
|
+
else
|
40
|
+
self.where( where ).limit( limit )
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.included to
|
47
|
+
to.extend ClassMethods
|
48
|
+
to.init_searches_on
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'ruby-debug'
|
2
|
+
|
3
|
+
module BarelySearchable
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
config.to_prepare do
|
6
|
+
::ActiveRecord::Base.send(:load, 'barely_searchable/model_injections.rb')
|
7
|
+
::ActiveRecord::Base.send(:include, BarelySearchable::ModelInjections)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: barely_searchable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Robert L. Carpenter
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2012-02-10 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Adds a very rudimentary .search() function to your activerecord models
|
22
|
+
email:
|
23
|
+
- codemonkey@robacarp.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- Gemfile
|
33
|
+
- LICENSE
|
34
|
+
- README.md
|
35
|
+
- Rakefile
|
36
|
+
- barely_searchable.gemspec
|
37
|
+
- lib/barely_searchable.rb
|
38
|
+
- lib/barely_searchable/model_injections.rb
|
39
|
+
- lib/barely_searchable/railtie.rb
|
40
|
+
- lib/barely_searchable/version.rb
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: https://github.com/robacarp/barely_searchable
|
43
|
+
licenses: []
|
44
|
+
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.3.6
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Add quick and dirty search functionality to your model layer without any indexing service
|
71
|
+
test_files: []
|
72
|
+
|