sa-detector 0.3.0

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.
@@ -0,0 +1,55 @@
1
+ # Stand Alone Detector
2
+
3
+ This gem is created for simple addition of search by a certain parameter. Additionally sorting the records by the position of the search phrase in the parameter.
4
+
5
+ ## Installation
6
+
7
+ Gemfile:
8
+
9
+ gem 'sa-detector'
10
+
11
+ ## How to use
12
+
13
+ Suppose there is a class named 'Document' with the parameter named 'title':
14
+
15
+ create_table :documents do |t|
16
+ t.string :title
17
+ t.string :annotation
18
+ end
19
+
20
+ ### Search only
21
+
22
+ You need to add **searchable_by** inside class.
23
+
24
+ # For search by one parameter
25
+
26
+ class Document < ActiveRecord::Base
27
+ searchable_by :title
28
+ end
29
+
30
+ # For search by multiple parameters
31
+
32
+ class Document < ActiveRecord::Base
33
+ searchable_by :title, :annotation
34
+ end
35
+
36
+ And then you can search using the following command:
37
+
38
+ Document.search_with params[:search_query]
39
+
40
+ ### Search and sort by only one parameter
41
+
42
+ **TODO:** add support for multiple parameters
43
+
44
+ You need to add **searchable_and_sortable_by** inside class:
45
+
46
+ class Document < ActiveRecord::Base
47
+ searchable_and_sortable_by :title
48
+ end
49
+
50
+ And then you can search and sort using the following command:
51
+
52
+ Document.search_and_sort_with params[:search_query]
53
+
54
+ The result is an array of found records, sorted by the positions of search phrase in the parameter.
55
+
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'sa_detector'
@@ -0,0 +1 @@
1
+ require 'sa_detector/active_record'
@@ -0,0 +1,43 @@
1
+ module SaDetector
2
+ module ActiveRecord
3
+ def self.included(base)
4
+ base.extend(ClassMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+ def searchable_by *attributes
9
+ @@attributes = attributes
10
+
11
+ class << self
12
+ define_method "search_with" do |query|
13
+ query = query.to_s.downcase
14
+
15
+ sql = [@@attributes.map{ |attribute| "LOWER(#{self.table_name}.#{attribute}) LIKE ?" }.join(" OR ")]
16
+ sql += Array.new(@@attributes.count, "%#{query}%")
17
+ self.where(sql)
18
+ end
19
+ end
20
+ end
21
+
22
+ def searchable_and_sortable_by attribute
23
+ @@attribute = attribute
24
+
25
+ class << self
26
+ define_method "search_and_sort_with" do |query|
27
+ query = query.to_s.downcase
28
+
29
+ result = self.where("LOWER(#{self.table_name}.#{@@attribute}) LIKE ?", "%#{query}%")
30
+ result.size > 0 ? self.sort_for_search_results(result, query) : []
31
+ end
32
+
33
+ define_method "sort_for_search_results" do |result, query|
34
+ result.sort{ |a,b| a[@@attribute].downcase.index(query) <=> b[@@attribute].downcase.index(query) }
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ ActiveRecord::Base.class_eval { include SaDetector::ActiveRecord }
43
+
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sa-detector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Artem Kornienko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-09 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Simple gem for searching and sorting data.
15
+ email: send.this.to.moff@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/sa_detector/active_record.rb
21
+ - lib/sa_detector.rb
22
+ - README.md
23
+ - init.rb
24
+ homepage: http://rubygems.org/gems/sa-detector
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.21
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Stand Alone Detector
48
+ test_files: []