ruby_simple_search 0.0.1
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.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +60 -0
- data/Rakefile +1 -0
- data/lib/ruby_simple_search.rb +71 -0
- data/lib/ruby_simple_search/version.rb +3 -0
- data/ruby_simple_search.gemspec +22 -0
- metadata +86 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Santosh Wadghule
|
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,60 @@
|
|
1
|
+
# RubySimpleSearch
|
2
|
+
|
3
|
+
RubySimpleSearch allows to search on the table fields.
|
4
|
+
e.g. string and text fields.
|
5
|
+
|
6
|
+
Sometimes we want to do search on the post's title and content
|
7
|
+
or user's email, username and description or on other models but in same way.
|
8
|
+
For those searches we use MySql's or Postgresql's LIKE operator to get the
|
9
|
+
results. While doing same thing on the differet models you actually add lots of
|
10
|
+
duplications in your code.
|
11
|
+
|
12
|
+
To avoid duplicating the same code, use RubySimpleSearch :)
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
gem 'ruby_simple_search'
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install ruby_simple_search
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
Define attributes that you want to search through RubySimpleSearch
|
31
|
+
|
32
|
+
```Ruby
|
33
|
+
class Post < ActiveActiveRecord::Base
|
34
|
+
include RubySimpleSearch
|
35
|
+
|
36
|
+
simple_search_attributes :title, :description
|
37
|
+
end
|
38
|
+
|
39
|
+
class User < < ActiveActiveRecord::Base
|
40
|
+
include RubySimpleSearch
|
41
|
+
|
42
|
+
simple_search_attributes :email, :username, :address
|
43
|
+
end
|
44
|
+
|
45
|
+
Post.simple_serach('tutorial')
|
46
|
+
# => posts which have 'tutorial' text in title or in description fields
|
47
|
+
|
48
|
+
User.simple_search('Mechanciles')
|
49
|
+
# => users which have 'Mechanicles' text in the email, username and in address
|
50
|
+
|
51
|
+
Model.simple_search('string')
|
52
|
+
# => will return ActiveRecord::Relation object
|
53
|
+
```
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
1. Fork it
|
57
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
58
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
59
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
60
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# RubySimpleSearch allows to search on the table fields.
|
2
|
+
# e.g. string and text fields.
|
3
|
+
#
|
4
|
+
# Sometimes we want to do search on the post's title and content
|
5
|
+
# or user's email, username and description or on other models but in same way.
|
6
|
+
# For those searches we use MySql's or Postgresql's LIKE operator to get the
|
7
|
+
# results. While doing same thing on the differet models you actually add lots of
|
8
|
+
# duplications in your code.
|
9
|
+
#
|
10
|
+
# To avoid duplicating the same code, use RubySimpleSearch :)
|
11
|
+
#
|
12
|
+
# Define attributes that you want to search through RubySimpleSearch
|
13
|
+
#
|
14
|
+
# class Post < ActiveActiveRecord::Base
|
15
|
+
# include RubySimpleSearch
|
16
|
+
#
|
17
|
+
# simple_search_attributes :title, :description
|
18
|
+
# end
|
19
|
+
#
|
20
|
+
# class User < < ActiveActiveRecord::Base
|
21
|
+
# include RubySimpleSearch
|
22
|
+
#
|
23
|
+
# simple_search_attributes :email, :username, :address
|
24
|
+
# end
|
25
|
+
#
|
26
|
+
# Post.simple_serach('tutorial')
|
27
|
+
# # => posts which have tutorial text in title or in description fields
|
28
|
+
#
|
29
|
+
# User.simple_search('Mechanciles')
|
30
|
+
# # => users which have mechanicles text in the email, username and in address
|
31
|
+
#
|
32
|
+
# Model.simple_search('string') will return ActiveRecord::Relation object
|
33
|
+
|
34
|
+
|
35
|
+
require "ruby_simple_search/version"
|
36
|
+
require 'active_support/concern'
|
37
|
+
|
38
|
+
module RubySimpleSearch
|
39
|
+
extend ActiveSupport::Concern
|
40
|
+
|
41
|
+
included do
|
42
|
+
class_eval do
|
43
|
+
def self.simple_search_attributes(*args)
|
44
|
+
@simple_search_attributes = []
|
45
|
+
args.each do |arg|
|
46
|
+
@simple_search_attributes << arg
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
module ClassMethods
|
53
|
+
def simple_search(q)
|
54
|
+
raise ArgumentError, "Argument is not string" unless q.is_a? String
|
55
|
+
|
56
|
+
query = ""
|
57
|
+
patterned_text = "%#{q.downcase}%"
|
58
|
+
|
59
|
+
@simple_search_attributes.each do |attr|
|
60
|
+
query += if query == ""
|
61
|
+
"LOWER(#{attr.to_s}) LIKE ?"
|
62
|
+
else
|
63
|
+
" OR LOWER(#{attr.to_s}) LIKE ?"
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
where([query] + Array.new(@simple_search_attributes.size, patterned_text))
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ruby_simple_search/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "ruby_simple_search"
|
8
|
+
gem.version = RubySimpleSearch::VERSION
|
9
|
+
gem.authors = ["Santosh Wadghule"]
|
10
|
+
gem.email = ["santosh.wadghule@gmail.com"]
|
11
|
+
gem.description = %q{It will search on the attributes that you provided to simple_search_attributes method}
|
12
|
+
gem.summary = %q{Ruby simple search for ActiveRecord}
|
13
|
+
gem.homepage = "https://github.com/mechanicles/ruby_simple_search"
|
14
|
+
|
15
|
+
gem.add_dependency "activesupport", ">= 3.0.0"
|
16
|
+
gem.add_dependency "activerecord", ">= 3.0.0"
|
17
|
+
|
18
|
+
gem.files = `git ls-files`.split($/)
|
19
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
20
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
21
|
+
gem.require_paths = ["lib"]
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby_simple_search
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Santosh Wadghule
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-18 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: activerecord
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 3.0.0
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 3.0.0
|
46
|
+
description: It will search on the attributes that you provided to simple_search_attributes
|
47
|
+
method
|
48
|
+
email:
|
49
|
+
- santosh.wadghule@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- Gemfile
|
56
|
+
- LICENSE.txt
|
57
|
+
- README.md
|
58
|
+
- Rakefile
|
59
|
+
- lib/ruby_simple_search.rb
|
60
|
+
- lib/ruby_simple_search/version.rb
|
61
|
+
- ruby_simple_search.gemspec
|
62
|
+
homepage: https://github.com/mechanicles/ruby_simple_search
|
63
|
+
licenses: []
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>='
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.8.24
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Ruby simple search for ActiveRecord
|
86
|
+
test_files: []
|