like_search 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in like_search.gemspec
4
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,13 @@
1
+ LikeSearch
2
+ ===========
3
+
4
+ Introduction goes here.
5
+
6
+
7
+ Example
8
+ =======
9
+
10
+ Example goes here.
11
+
12
+
13
+ Copyright (c) 2011 [name of plugin creator], released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+ require 'rdoc/task'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ desc 'Test the like_search plugin.'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.libs << 'test'
13
+ t.pattern = 'test/**/*_test.rb'
14
+ t.verbose = true
15
+ end
16
+
17
+ desc 'Generate documentation for the like_search plugin.'
18
+ RDoc::Task.new(:rdoc) do |rdoc|
19
+ rdoc.rdoc_dir = 'rdoc'
20
+ rdoc.title = 'LikeSearch'
21
+ rdoc.options << '--line-numbers' << '--inline-source'
22
+ rdoc.rdoc_files.include('README')
23
+ rdoc.rdoc_files.include('lib/**/*.rb')
24
+ end
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Include hook code here
2
+ require 'like_search'
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,3 @@
1
+ module LikeSearch
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,54 @@
1
+ # Current db connection through config
2
+ # ActiveRecord::Base.connection_config[:adapter] => "mysql2"
3
+
4
+ module LikeSearch
5
+ DB = {
6
+ :postgres => "{field} ilike ?",
7
+ :sqlite => 'LOWER({field}) like LOWER(?)'
8
+ }
9
+ DB.default = "{field} like ?"
10
+
11
+ extend ActiveSupport::Concern
12
+
13
+ module ClassMethods
14
+ def has_like_search(attribute)
15
+ class_attribute :like_search_attribute
16
+ self.like_search_attribute = attribute
17
+
18
+ extend LikeSearch::SearchMethods
19
+ end
20
+ end
21
+
22
+ module SearchMethods
23
+ def search(query)
24
+ if query.present?
25
+ where(format_search_sql, "%#{query}%")
26
+ else
27
+ scoped # TODO: test if only self works here
28
+ end
29
+ end
30
+
31
+ def token_search(query)
32
+ query.to_s.split.inject(scoped) do |current_scope, term|
33
+ term.size > 3 ? current_scope.search(term) : current_scope
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def format_search_sql
40
+ if LikeSearch::DB[current_db_adapter].nil?
41
+ query_string = DB.default
42
+ else
43
+ query_string = LikeSearch::DB[current_db_adapter]
44
+ end
45
+ query_string.gsub(/\{field\}/, like_search_attribute.to_s)
46
+ end
47
+
48
+ def current_db_adapter
49
+ ActiveRecord::Base.connection_config[:adapter].to_sym
50
+ end
51
+ end
52
+ end
53
+
54
+ ActiveRecord::Base.send :include, LikeSearch
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "like_search/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "like_search"
7
+ s.version = LikeSearch::VERSION
8
+ s.authors = ["Alexandre da Silva"]
9
+ s.email = ["lexrupy@gmail.com"]
10
+ s.homepage = "https://github.com/lexrupy/like_search"
11
+ s.summary = %q{Search ActiveRecord models with Like}
12
+ s.description = %q{Search ActiveRecord models with like}
13
+
14
+ s.rubyforge_project = "like_search"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ # s.add_runtime_dependency "rest-client"
24
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class LikeSearchTest < ActiveSupport::TestCase
4
+ # test "the truth" do
5
+ # assert true
6
+ # end
7
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: like_search
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alexandre da Silva
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-14 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Search ActiveRecord models with like
15
+ email:
16
+ - lexrupy@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - MIT-LICENSE
24
+ - README
25
+ - Rakefile
26
+ - init.rb
27
+ - install.rb
28
+ - lib/like_search.rb
29
+ - lib/like_search/version.rb
30
+ - like_search.gemspec
31
+ - test/like_search_test.rb
32
+ - test/test_helper.rb
33
+ - uninstall.rb
34
+ homepage: https://github.com/lexrupy/like_search
35
+ licenses: []
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project: like_search
54
+ rubygems_version: 1.8.10
55
+ signing_key:
56
+ specification_version: 3
57
+ summary: Search ActiveRecord models with Like
58
+ test_files:
59
+ - test/like_search_test.rb
60
+ - test/test_helper.rb