activerecord-search 0.1.1

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: c8ef7cc040cb55d4441f14102a4b54f59c442381
4
+ data.tar.gz: d7b779810f9712b735e3b6521955f7f743e20c64
5
+ SHA512:
6
+ metadata.gz: d4eca6e16ac94069b4baf73d9c026455c1d875b36fcce734b8d2542edc31e7b6bd50f7bb41df4faa30ee57ada7c6c5a5dbc21ece153152be763e20cbb8b175d0
7
+ data.tar.gz: eaa616a114c777fc99954fefdc0e8bdcbc7abe82d150cf9cbd43a257ed591c8050d7b3869e208656b994cc29495188e68c2168b4231dc78faea5fcb144e7f63d
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-search.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Mehdi FARSI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,113 @@
1
+ # ActiveRecord::Search
2
+
3
+ a lightweight search-engine using ActiveRecord
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'activerecord-search'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install activerecord-search
20
+
21
+ ## Usage
22
+
23
+ This plugin permits, with few lines of code, to have a lightweight and steady ActiveRecord search engine.
24
+
25
+ Below is an example of the features offered by this plugin:
26
+
27
+ Use case:
28
+
29
+ A model named Post that contains 2 fields: `title:string` and `content:text`.
30
+ We want to search any posts that include `Ruby` in their content:
31
+
32
+ ##### In `app/models/posts.rb`:
33
+
34
+ ```ruby
35
+ class Post < ActiveRecord::Base
36
+ search_field :content
37
+ search_option :anywhere
38
+ end
39
+ ```
40
+ ##### In `app/controllers/posts_controller.rb`:
41
+
42
+ ```ruby
43
+ class PostsController < ApplicationController
44
+
45
+ def index
46
+ Post.search_post("Ruby")
47
+ end
48
+
49
+ ...
50
+ end
51
+ ```
52
+
53
+ ## More about the API
54
+
55
+ ##### search_option method
56
+
57
+ `search_option` method specifies where to search in the sentence. Available options:
58
+
59
+ - `:start_with` : the sentence starts with the pattern.
60
+ - `:end_with` : the sentence ends with the pattern.
61
+ - `:anywhere` : search anywhere in the sentence. [DEFAULT OPTION]
62
+
63
+ ##### search_MODEL method
64
+
65
+ A method :search_MODEL is auto-generated. It's the method used in controller in our above example. some examples:
66
+
67
+ - for a model Post a method `search_post` is generated.
68
+ - for a model Book a method `search_book` is generated.
69
+
70
+ The method can accept a second argument which is a `search_option`. Example:
71
+
72
+ ```ruby
73
+ class PostsController < ApplicationController
74
+
75
+ def index
76
+ Post.search_post("Ruby", :start_with) # See search_option method section for more information
77
+ end
78
+
79
+ ...
80
+ end
81
+ ```
82
+
83
+ Append the model name to this method results from the following question: What is the probability that a `search` method already exists in the model?
84
+
85
+ In this example, the search-engine will search any posts that the content starts with `"Ruby"`.
86
+
87
+ ##### search_field method
88
+
89
+ `search_field` method specifies which field is used for the search. It's possible to search in multiple fields by using the method `search_fields([])`:
90
+
91
+ ```ruby
92
+ class Post < ActiveRecord::Base
93
+ search_fields [:title, :content]
94
+ end
95
+ ```
96
+
97
+ In this example, the search-engine will search any posts that include `"Ruby"` in their content OR in their title.
98
+
99
+ ## Development
100
+
101
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
102
+
103
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
104
+
105
+ ## Contributing
106
+
107
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mehdi-farsi/activerecord-search.
108
+
109
+
110
+ ## License
111
+
112
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
113
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,45 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'activerecord/search/version'
5
+ require 'activerecord/base'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "activerecord-search"
9
+ spec.version = Activerecord::Search::VERSION
10
+ spec.date = '2015-10-05'
11
+ spec.authors = ["Mehdi FARSI"]
12
+ spec.email = ["mehdifarsi.pro@gmail.com"]
13
+
14
+ spec.summary = %q{ActiveRecord::Search}
15
+ spec.description = %q{a lightweight search-engine using ActiveRecord}
16
+ spec.homepage = "https://github.com/mehdi-farsi/activerecord-search"
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ spec.bindir = "bin"
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.required_ruby_version = '>= 1.9.3'
25
+ spec.post_install_message = <<-EOF
26
+ Thanks for installing!
27
+ You can follow me on:
28
+ - Twitter: @farsi_mehdi
29
+ - Github: mehdi-farsi
30
+
31
+ This project permits to help to solve a common problem. My reward is to see you using it.
32
+
33
+ So please, feel free to 'star' the project on GitHub:
34
+
35
+ https://github.com/mehdi-farsi/activerecord-search
36
+
37
+ Many Thanks !
38
+ EOF
39
+
40
+ spec.add_development_dependency "bundler", "~> 1.10"
41
+ spec.add_development_dependency "rake", "~> 10.0"
42
+ spec.add_development_dependency "activerecord", '~> 0'
43
+ spec.add_development_dependency "sqlite3", '~> 0'
44
+ spec.add_development_dependency "minitest", '~> 0'
45
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "activerecord/search"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,70 @@
1
+ require "activerecord/search/version"
2
+ require "active_record"
3
+
4
+ module ActiveRecord
5
+ class SearchError < ::StandardError
6
+ def message
7
+ "Please set only one the following options: :start_with, :end_with, :anywhere"
8
+ end
9
+ end
10
+
11
+ class Base
12
+ class << self
13
+ def inherited(subclass)
14
+ super
15
+ return if subclass.to_s.underscore == "active_record/schema_migration"
16
+ class_eval <<-EOF
17
+ class << #{subclass}
18
+ attr_reader :search_option, :search_fields
19
+
20
+ def search_#{subclass.to_s.underscore}(words, option = nil)
21
+ formatted_words = format_search(words, option)
22
+
23
+ search_scope = []
24
+ search_params = {}
25
+ @search_fields.each do |field|
26
+ search_params[field] = formatted_words
27
+ search_scope << field.to_s + " LIKE :" + field.to_s
28
+ end
29
+
30
+ where([search_scope.join(" OR "), search_params])
31
+ end
32
+
33
+ def search_fields(fields)
34
+ @search_fields = [fields].flatten
35
+ end
36
+ alias_method :search_field, :search_fields
37
+
38
+
39
+ # If options are given, they override the default options behavior
40
+ def format_search(words, option)
41
+ if !option.nil?
42
+ format_with_option(words, option)
43
+ else
44
+ o = @search_option.nil? ? :anywhere : @search_option
45
+ format_with_option(words, o)
46
+ end
47
+ end
48
+
49
+ def format_with_option(words, option)
50
+ case option
51
+ when :start_with then words + '%'
52
+ when :end_with then '%' + words
53
+ when :anywhere then '%' + words + '%'
54
+ else
55
+ raise SearchError
56
+ end
57
+ end
58
+
59
+ def search_option(option = :anywhere)
60
+ @search_option = option
61
+ end
62
+ end
63
+ EOF
64
+ end # self.inherited
65
+ end
66
+
67
+ end # Base
68
+ end # ActiveRecord
69
+
70
+
@@ -0,0 +1,5 @@
1
+ module Activerecord
2
+ module Search
3
+ VERSION = "0.1.1"
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ require "activerecord/search/version"
2
+
3
+ module Activerecord
4
+ module Search
5
+ # Your code goes here...
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-search
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Mehdi FARSI
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-10-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: a lightweight search-engine using ActiveRecord
84
+ email:
85
+ - mehdifarsi.pro@gmail.com
86
+ executables:
87
+ - console
88
+ - setup
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - activerecord-search.gemspec
99
+ - bin/console
100
+ - bin/setup
101
+ - lib/activerecord/base.rb
102
+ - lib/activerecord/search.rb
103
+ - lib/activerecord/search/version.rb
104
+ homepage: https://github.com/mehdi-farsi/activerecord-search
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message: " Thanks for installing!\n You can follow me on:\n - Twitter:
109
+ @farsi_mehdi\n - Github: mehdi-farsi\n\n This project permits to help to solve
110
+ a common problem. My reward is to see you using it.\n\n So please, feel free to
111
+ 'star' the project on GitHub:\n\n https://github.com/mehdi-farsi/activerecord-search\n
112
+ \ \n Many Thanks !\n"
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: 1.9.3
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.4.6
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: ActiveRecord::Search
132
+ test_files: []