ruby_simple_search 0.0.2 → 0.0.3

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: 59d86037e8eb49bbae2c3be8a4b0f55176ba87d4
4
+ data.tar.gz: 408a98859b92881a23bd375d118006929a905adf
5
+ SHA512:
6
+ metadata.gz: bd03396c4c5b6cbd0f90ad3e0edd9ec9c5c99f34cce9653e8396c3efc03631ab3a000c82e4746f03c9245b0428502c268a7fe5fabc747ad52172addfcb69f408
7
+ data.tar.gz: 7fbc374a919f6141f3186da99d1083faa3c4749365bcf65ce94a487537719d3c20dd6a256bbc33811a4a883b1dde39fda020dc925c0664f921b724f94b2f7a45
data/.gitignore CHANGED
@@ -15,3 +15,5 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ tags
19
+
data/README.md CHANGED
@@ -3,8 +3,8 @@
3
3
  RubySimpleSearch allows you to search on the table fields (string and text fields)
4
4
  very easily.
5
5
 
6
- Mostly on the admin side, we do have a common text field to search the table
7
- column's data.
6
+ Mostly on the admin side, we do have a common text field to search the data on the
7
+ table.
8
8
 
9
9
  Sometimes we want to do a search on the title, content and ratings on the post model or
10
10
  email, username and description on the user model. For those searches we use MySQL's
@@ -13,27 +13,33 @@ on the different models you actually add lots of duplication in your code.
13
13
 
14
14
  To avoid duplicating the same code, use RubySimpleSearch :)
15
15
 
16
+ #### Version 0.0.3 changes:
17
+ - 'LIKE' pattern is more flexible now. Now you can pass pattern on ```simple_search```
18
+ method directly. Pattern support on the ```simple_search_attributes``` method has been removed
19
+ - Fixed column ambiguous error when used with the joins
20
+
21
+
16
22
  #### RubySimpleSearch Features:
17
- - Added like pattern support ('beginning', 'ending', 'containing', 'underscore', 'plain').
23
+ - Added 'LIKE' pattern support ('beginning', 'ending', 'containing', 'underscore', 'plain').
18
24
  By default pattern is 'containing'
19
25
 
20
26
  ```Ruby
21
- simple_search_attributes :name, :address, :pattern => :ending
27
+ Post.simple_search('york', :pattern => :ending)
22
28
  # It will search like '%york'
23
29
 
24
- simple_search_attributes :name, :address, :pattern => :begining
30
+ Post.simple_search('york', :pattern => :begining)
25
31
  # It will search like 'york%'
26
32
 
27
- simple_search_attributes :name, :address, :pattern => :containing
33
+ Post.simple_search('york', :pattern => :containing)
28
34
  # It will search like '%york%'
29
35
 
30
- simple_search_attributes :name, :address, :pattern => :underscore
36
+ Post.simple_search('o', :pattern => :underscore)
31
37
  # It will search like '_o_'
32
38
 
33
- simple_search_attributes :name, :address, :pattern => :plain
39
+ Post.simple_search('yourk', :pattern => :plain)
34
40
  # It will search like 'york'
35
41
  ```
36
- - Added **block** support to simple_search method, so user can extend the query as per
42
+ - Added **block** support to ```simple_search``` method, so user can extend the query as per
37
43
  his/her requirements (Now you can operate on the integer/decimal values also)
38
44
 
39
45
  - Added specs
@@ -62,7 +68,7 @@ Define attributes that you want to search through RubySimpleSearch
62
68
  class Post < ActiveActiveRecord::Base
63
69
  include RubySimpleSearch
64
70
 
65
- simple_search_attributes :title, :description, :pattern => :begining
71
+ simple_search_attributes :title, :description
66
72
  end
67
73
  ```
68
74
  ```Ruby
@@ -72,11 +78,11 @@ class User < ActiveActiveRecord::Base
72
78
  simple_search_attributes :email, :username, :address
73
79
  end
74
80
  ```
75
- While defining simple_search_attributes, don't add integer/decimal data
81
+ While defining ```simple_search_attributes```, don't add integer/decimal data
76
82
  attributes to it, instead of this you can do integer/decimal operation
77
- by passing block to simple search method
83
+ by passing block to ```simple_search``` method
78
84
  ```Ruby
79
- Post.simple_search('tuto')
85
+ Post.simple_search('tuto', :pattern => :begining)
80
86
  # => posts which have 'tuto%' text in the title or in the description fields
81
87
  ```
82
88
  ```Ruby
@@ -1,10 +1,11 @@
1
1
  require "ruby_simple_search/errors"
2
2
 
3
3
  module RubySimpleSearch
4
- module Error
4
+ module Errors
5
5
  ATTRIBUTES_MISSING = "Simple search attributes are missing"
6
6
  INVALID_TYPE = "Extended query is not an array type"
7
7
  INVALID_CONDITION = "Extended query's array conditions are wrong"
8
8
  INVALID_PATTERN = "Pattern is wrong. it should be in the list #{RubySimpleSearch::LIKE_PATTERNS.keys}"
9
+ WROG_ATTRIBUTES = "simple_search_arguments method's arguments should be in symbol format"
9
10
  end
10
11
  end
@@ -1,3 +1,3 @@
1
1
  module RubySimpleSearch
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -10,23 +10,19 @@ module RubySimpleSearch
10
10
  class_eval do
11
11
  def self.simple_search_attributes(*args)
12
12
  @simple_search_attributes = []
13
- # default pattern is '%q%'
14
- @simple_search_pattern = RubySimpleSearch::LIKE_PATTERNS[:containing]
15
13
  args.each do |arg|
16
- if !arg.is_a?(Hash)
17
- @simple_search_attributes << arg
18
- else
19
- set_pattern(arg[:pattern]) if arg[:pattern].present?
20
- end
14
+ raise ArgumentError, RubySimpleSearch::Errors::WROG_ATTRIBUTES unless arg.is_a? Symbol
15
+ @simple_search_attributes << arg
21
16
  end
22
17
  end
23
18
  end
24
19
  end
25
20
 
26
21
  module ClassMethods
27
- def simple_search(search_term, &block)
28
- raise RubySimpleSearch::Error::ATTRIBUTES_MISSING if @simple_search_attributes.blank?
22
+ def simple_search(search_term, options={}, &block)
23
+ raise RubySimpleSearch::Errors::ATTRIBUTES_MISSING if @simple_search_attributes.blank?
29
24
  raise ArgumentError, "Argument is not string" unless search_term.is_a? String
25
+ set_pattern(options[:pattern])
30
26
 
31
27
  sql_query = nil
32
28
  extended_query = nil
@@ -52,24 +48,29 @@ module RubySimpleSearch
52
48
  end
53
49
  sql_query = [sql_query_condition, sql_query_values]
54
50
 
55
- where(sql_query.try(:flatten))
51
+ where(sql_query.flatten)
56
52
  end
57
53
 
58
54
  private
59
55
 
60
56
  def set_pattern(pattern)
61
- pattern = RubySimpleSearch::LIKE_PATTERNS[pattern.to_sym] rescue nil
62
- raise RubySimpleSearch::Error::INVALID_PATTERN if pattern.nil?
63
- @simple_search_pattern = pattern
57
+ if pattern.nil?
58
+ # default pattern is '%q%'
59
+ @simple_search_pattern = RubySimpleSearch::LIKE_PATTERNS[:containing]
60
+ else
61
+ pattern = RubySimpleSearch::LIKE_PATTERNS[pattern.to_sym]
62
+ raise RubySimpleSearch::Errors::INVALID_PATTERN if pattern.nil?
63
+ @simple_search_pattern = pattern
64
+ end
64
65
  end
65
66
 
66
67
  def extend_simple_search(extended_query, sql_query_condition, sql_query_values)
67
- raise RubySimpleSearch::Error::INVALID_TYPE unless extended_query.is_a?(Array)
68
+ raise RubySimpleSearch::Errors::INVALID_TYPE unless extended_query.is_a?(Array)
68
69
  extended_query_condition = extended_query[0]
69
70
  extended_query_values = extended_query - [extended_query[0]]
70
71
 
71
72
  if extended_query_condition.count('?') != (extended_query_values.size)
72
- raise RubySimpleSearch::Error::INVALID_CONDITION
73
+ raise RubySimpleSearch::Errors::INVALID_CONDITION
73
74
  end
74
75
 
75
76
  sql_query_condition = [sql_query_condition, extended_query_condition].join(' ')
@@ -79,8 +80,8 @@ module RubySimpleSearch
79
80
  end
80
81
 
81
82
  def set_sql_query_condition(attr, sql_query_condition)
82
- return "LOWER(#{attr.to_s}) LIKE ?" if sql_query_condition.blank?
83
- " OR LOWER(#{attr.to_s}) LIKE ?"
83
+ return "LOWER(#{self.table_name}.#{attr.to_s}) LIKE ?" if sql_query_condition.blank?
84
+ " OR LOWER(#{self.table_name}.#{attr.to_s}) LIKE ?"
84
85
  end
85
86
 
86
87
  end
@@ -16,6 +16,7 @@ Gem::Specification.new do |gem|
16
16
  gem.add_dependency "activerecord", ">= 3.0.0"
17
17
  gem.add_dependency "sqlite3"
18
18
  gem.add_development_dependency "rspec"
19
+ gem.required_ruby_version = ">= 1.9.2"
19
20
 
20
21
  gem.files = `git ls-files`.split($/)
21
22
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
data/spec/user_spec.rb CHANGED
@@ -2,56 +2,59 @@ require 'spec_helper'
2
2
  require_relative "../spec/lib/user.rb"
3
3
  require_relative "../spec/lib/user2.rb"
4
4
 
5
- describe User do
6
-
5
+ describe 'User' do
7
6
  describe ".simple_search_attributes" do
8
-
9
- it "returns an exception if simple_search_attributes is not called while loading the model" do
10
- expect { User2.simple_search('usa') }.to raise_error(RubySimpleSearch::Error::ATTRIBUTES_MISSING)
11
- end
12
-
13
- it "sets simple_search_attributes" do
14
- User2.simple_search_attributes :name, :contact
15
- expect(User2.instance_variable_get("@simple_search_attributes")).to eq([:name, :contact])
16
- end
17
-
18
- it "has default pattern" do
19
- User2.simple_search_attributes :name, :contact
20
- expect(User2.instance_variable_get("@simple_search_pattern")).to eq('%q%')
21
- end
22
-
23
- it "can have patterns like plain, beginning, ending, containing and underscore" do
24
- User2.simple_search_attributes :name, :contact, :pattern => :plain
25
- expect(User2.instance_variable_get("@simple_search_pattern")).to eq('q')
26
- User2.simple_search_attributes :name, :contact, :pattern => :beginning
27
- expect(User2.instance_variable_get("@simple_search_pattern")).to eq('q%')
28
- User2.simple_search_attributes :name, :contact, :pattern => :ending
29
- expect(User2.instance_variable_get("@simple_search_pattern")).to eq('%q')
30
- User2.simple_search_attributes :name, :contact, :pattern => :containing
31
- expect(User2.instance_variable_get("@simple_search_pattern")).to eq('%q%')
32
- User2.simple_search_attributes :name, :contact, :pattern => :underscore
33
- expect(User2.instance_variable_get("@simple_search_pattern")).to eq('_q_')
7
+ it "sets attributes" do
8
+ expect(User.instance_variable_get("@simple_search_attributes")).to eq([:name, :email, :contact, :address])
34
9
  end
35
10
 
36
- it "should raise an exception if pattern is wrong" do
37
- expect{ User2.simple_search_attributes :name, :pattern => 'wrong' }.to raise_error(RubySimpleSearch::Error::INVALID_PATTERN)
11
+ it "does not return an exception if simple_search_attributes is not called while loading the model" do
12
+ expect { User.simple_search('usa') }.to_not raise_error(RubySimpleSearch::Errors::ATTRIBUTES_MISSING)
38
13
  end
39
14
  end
40
15
 
41
16
  describe ".simple_search" do
42
- context "Simple" do
43
- it "searches users whose name are 'alice'" do
17
+ context "without block" do
18
+ it "searches users whose names are 'alice'" do
44
19
  user = User.find_by_name('alice')
45
20
  users = User.simple_search('alice')
46
21
  users.should include(user)
47
22
  end
48
23
 
24
+ it "has default 'LIKE' pattern" do
25
+ User.find_by_name('alice')
26
+ expect(User.instance_variable_get("@simple_search_pattern")).to eq('%q%')
27
+ end
28
+
29
+ it "can have 'LIKE' patterns like plain, beginning, ending, containing and underscore" do
30
+ User.simple_search('alice', pattern: :plain)
31
+ expect(User.instance_variable_get("@simple_search_pattern")).to eq('q')
32
+ User.simple_search('al', pattern: :beginning)
33
+ expect(User.instance_variable_get("@simple_search_pattern")).to eq('q%')
34
+ User.simple_search('alice', pattern: :ending)
35
+ expect(User.instance_variable_get("@simple_search_pattern")).to eq('%q')
36
+ User.simple_search('alice', pattern: :containing)
37
+ expect(User.instance_variable_get("@simple_search_pattern")).to eq('%q%')
38
+ User.simple_search('alice', pattern: :underscore)
39
+ expect(User.instance_variable_get("@simple_search_pattern")).to eq('_q_')
40
+ end
41
+
42
+ it "raises an exception if pattern is wrong" do
43
+ expect{ User.simple_search('alice', pattern: 'wrong') }.to raise_error(RubySimpleSearch::Errors::INVALID_PATTERN)
44
+ end
45
+
46
+ it "searches users whose names are 'alice' with beginning pattern" do
47
+ user = User.find_by_name('alice')
48
+ users = User.simple_search('al', { pattern: :beginning })
49
+ users.should include(user)
50
+ end
51
+
49
52
  it "returns empty records if contact number does not exist" do
50
53
  users = User.simple_search('343434')
51
54
  users.should be_empty
52
55
  end
53
56
 
54
- it "searches user records if user belongs to 'USA'" do
57
+ it "searches user records if users belong to 'USA'" do
55
58
  users = User.where(:address => 'usa')
56
59
  searched_users = User.simple_search('usa')
57
60
  expect(users.to_a).to eq(searched_users.to_a)
@@ -59,54 +62,71 @@ describe User do
59
62
 
60
63
  it "searches the records with beginning pattern" do
61
64
  users = User.where("name like ?", 'bo%')
62
- User.simple_search_attributes :name, :contact, :address, :pattern => :beginning
63
- searched_users = User.simple_search('bo')
65
+ User.simple_search_attributes :name, :contact, :address
66
+ searched_users = User.simple_search('bo', pattern: :beginning)
64
67
  expect(users.count).to eq(searched_users.count)
65
68
  end
66
69
 
67
70
  it "searches the records with ending pattern" do
68
71
  users = User.where("name like ?", '%ce')
69
- User.simple_search_attributes :name, :contact, :address, :pattern => :ending
70
- searched_users = User.simple_search('ce')
72
+ User.simple_search_attributes :name, :contact, :address
73
+ searched_users = User.simple_search('ce', pattern: :ending)
71
74
  expect(users.count).to eq(searched_users.count)
72
75
  end
73
76
 
74
77
  it "searches the records with underscore pattern" do
75
78
  users = User.where("name like ?", 'ce')
76
- User.simple_search_attributes :name, :contact, :address, :pattern => :underscore
77
- searched_users = User.simple_search('ce')
79
+ User.simple_search_attributes :name, :contact, :address
80
+ searched_users = User.simple_search('ce', pattern: :underscore)
78
81
  expect(users.count).to eq(searched_users.count)
79
82
  end
80
83
 
81
84
  it "searches the records with plain pattern" do
82
85
  users = User.where("name like ?", 'bob')
83
- User.simple_search_attributes :name, :contact, :address, :pattern => :plain
84
- searched_users = User.simple_search('bob')
86
+ User.simple_search_attributes :name, :contact, :address
87
+ searched_users = User.simple_search('bob', pattern: :plain)
85
88
  expect(users.count).to eq(searched_users.count)
86
89
  end
87
90
  end
88
91
 
89
- context "Extendable" do
90
- it "returns users who live in usa and their age shoule be greater than 50" do
91
- User.simple_search_attributes :name, :contact, :address, :pattern => :plain
92
+ context "with block" do
93
+ it "returns users who live in usa and their age should be greater than 50" do
94
+ User.simple_search_attributes :name, :contact, :address
92
95
  users = User.where(:age => 60)
93
- searched_users = User.simple_search('usa') do |search_term|
96
+ searched_users = User.simple_search('usa', pattern: :plain) do |search_term|
94
97
  ['AND age > ?', 50]
95
98
  end
96
99
  expect(users.to_a).to eq(searched_users.to_a)
97
100
  end
98
101
 
99
- it "returns exception if array condition is wrong in simple_search block" do
102
+ it "returns an exception if array condition is wrong in simple_search block" do
100
103
  expect{ User.simple_search('usa') do |search_term|
101
104
  ['AND age > ?', 50, 60]
102
- end }.to raise_error(RubySimpleSearch::Error::INVALID_CONDITION)
105
+ end }.to raise_error(RubySimpleSearch::Errors::INVALID_CONDITION)
103
106
  end
104
107
 
105
- it "returns exception if return value is not an array type" do
108
+ it "returns an exception if return value is not an array type" do
106
109
  expect{ User.simple_search('usa') do |search_term|
107
110
  "Wrong return"
108
- end }.to raise_error(RubySimpleSearch::Error::INVALID_TYPE)
111
+ end }.to raise_error(RubySimpleSearch::Errors::INVALID_TYPE)
109
112
  end
110
113
  end
111
114
  end
112
115
  end
116
+
117
+ describe User2 do
118
+ describe ".simple_search_attributes" do
119
+ it "returns an exception if simple_search_attributes method is not called while loading the model" do
120
+ expect { User2.simple_search('usa') }.to raise_error(RubySimpleSearch::Errors::ATTRIBUTES_MISSING)
121
+ end
122
+
123
+ it "returns an exception if simple_search_attributes method has wrong attribute type" do
124
+ expect { User2.simple_search_attributes :name, '24' }.to raise_error(RubySimpleSearch::Errors::WROG_ATTRIBUTES)
125
+ end
126
+
127
+ it "sets attributes if simple_search_attributes method is called on the model" do
128
+ User2.simple_search_attributes :name, :contact
129
+ expect(User2.instance_variable_get("@simple_search_attributes")).to eq([:name, :contact])
130
+ end
131
+ end
132
+ end
metadata CHANGED
@@ -1,78 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_simple_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
4
+ version: 0.0.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - Santosh Wadghule
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-12-11 00:00:00.000000000 Z
11
+ date: 2014-03-30 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: activesupport
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: 3.0.0
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: 3.0.0
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: activerecord
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: 3.0.0
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: 3.0.0
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: sqlite3
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - ">="
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rspec
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - ">="
68
60
  - !ruby/object:Gem::Version
69
61
  version: '0'
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - ">="
76
67
  - !ruby/object:Gem::Version
77
68
  version: '0'
78
69
  description: It will search on the attributes that you provided to simple_search_attributes
@@ -83,8 +74,8 @@ executables: []
83
74
  extensions: []
84
75
  extra_rdoc_files: []
85
76
  files:
86
- - .gitignore
87
- - .rspec
77
+ - ".gitignore"
78
+ - ".rspec"
88
79
  - Gemfile
89
80
  - LICENSE.txt
90
81
  - README.md
@@ -100,27 +91,26 @@ files:
100
91
  - spec/user_spec.rb
101
92
  homepage: https://github.com/mechanicles/ruby_simple_search
102
93
  licenses: []
94
+ metadata: {}
103
95
  post_install_message:
104
96
  rdoc_options: []
105
97
  require_paths:
106
98
  - lib
107
99
  required_ruby_version: !ruby/object:Gem::Requirement
108
- none: false
109
100
  requirements:
110
- - - ! '>='
101
+ - - ">="
111
102
  - !ruby/object:Gem::Version
112
- version: '0'
103
+ version: 1.9.2
113
104
  required_rubygems_version: !ruby/object:Gem::Requirement
114
- none: false
115
105
  requirements:
116
- - - ! '>='
106
+ - - ">="
117
107
  - !ruby/object:Gem::Version
118
108
  version: '0'
119
109
  requirements: []
120
110
  rubyforge_project:
121
- rubygems_version: 1.8.25
111
+ rubygems_version: 2.2.0
122
112
  signing_key:
123
- specification_version: 3
113
+ specification_version: 4
124
114
  summary: Ruby simple search for ActiveRecord
125
115
  test_files:
126
116
  - spec/lib/user.rb