posterboy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Durran Jordan
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
File without changes
@@ -0,0 +1,103 @@
1
+ # encoding: utf-8
2
+
3
+ require 'active_record'
4
+ require 'active_support/concern'
5
+ require 'posterboy/document'
6
+ require 'posterboy/version'
7
+
8
+ module Posterboy #:nodoc:
9
+ extend ActiveSupport::Concern
10
+
11
+ # This is the base query used to perform a full-text search in Postgre.
12
+ QUERY = "to_tsvector('english', documents.search_text) @@ plainto_tsquery('english', ?)"
13
+
14
+ # Operators for performing different types of searches.
15
+ OPERATORS = { and: '&', or: '|' }
16
+
17
+ included do
18
+
19
+ # Each searchable model has a reference to a single {#Posterboy::Document},
20
+ # which is where the searchable attributes will get stored.
21
+ has_one \
22
+ :document,
23
+ as: :searchable,
24
+ inverse_of: :searchable,
25
+ class_name: "Posterboy::Document"
26
+
27
+ # The search scope takes a space separated list of terms to perform an
28
+ # AND query on. This should be configurable in the future.
29
+ #
30
+ # @example
31
+ # User.search('Syd London')
32
+ #
33
+ # @param [ Symbol ] operator The type of search to perform.
34
+ # @param [ String ] term The words to search on.
35
+ #
36
+ # @return [ ActiveRecord::Relation ] The matching records.
37
+ scope :search, lambda { | operator, term |
38
+ if term.present?
39
+ {
40
+ include: [:document],
41
+ conditions: [ QUERY, term.gsub(' ', " #{OPERATORS[:operator]} ") ]
42
+ }
43
+ end
44
+ }
45
+
46
+ # Update the searchable {#Document} after each save.
47
+ set_callback :save, :after, :update_document
48
+
49
+ # Searchable fields are stored here.
50
+ class_inheritable_accessor :searchables
51
+ end
52
+
53
+ private
54
+
55
+ # Get the actual values for the attributes that have been defined as
56
+ # searchable.
57
+ #
58
+ # @example
59
+ # user.searchable_values
60
+ #
61
+ # @return [ Array ] The actual values for this record.
62
+ def searchable_values
63
+ [].tap do |values|
64
+ searchables.each do |searchable|
65
+ searchable.is_a?(Hash) ?
66
+ values << send(searchable.to_a[0][0]).map(&searchable.to_a[0][1]) :
67
+ values << send(searchable)
68
+ end
69
+ end
70
+ end
71
+
72
+ # Updates or creates the document with the supplied attribute values with
73
+ # each save.
74
+ #
75
+ # @example
76
+ # user.update_document
77
+ def update_document
78
+ if searchables
79
+ text = searchable_values.flatten.join(' ')
80
+ return document.update_attribute(:search_text, text) if document
81
+ create_document(search_text: text)
82
+ end
83
+ end
84
+
85
+ module ClassMethods
86
+
87
+ # Macro to define which attributes should be searched on.
88
+ #
89
+ # @example
90
+ # class User < ActiveRecord::Base
91
+ # has_many :tags, through: :user_tags
92
+ #
93
+ # search_on :first_name, :last_name, tags: :name
94
+ # end
95
+ #
96
+ # @param [ Array<Symbol> ] args The attributes to search on.
97
+ def search_on(*args)
98
+ self.searchables = args
99
+ end
100
+ end
101
+ end
102
+
103
+ ActiveRecord::Base.send(:include, Posterboy)
@@ -0,0 +1,7 @@
1
+ # encoding: utf-8
2
+ module Posterboy
3
+ class Document < ActiveRecord::Base
4
+ belongs_to :searchable, polymorphic: true, inverse_of: :document
5
+ set_table_name :documents
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ # encoding: utf-8
2
+ module Posterboy #:nodoc:
3
+ VERSION = "0.0.1"
4
+ end
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: posterboy
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Durran Jordan
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-11-09 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: pg
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 9
30
+ version: "0.9"
31
+ type: :runtime
32
+ prerelease: false
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: activerecord
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 3
43
+ version: "3"
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *id002
47
+ - !ruby/object:Gem::Dependency
48
+ name: activesupport
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 3
56
+ version: "3"
57
+ type: :runtime
58
+ prerelease: false
59
+ version_requirements: *id003
60
+ - !ruby/object:Gem::Dependency
61
+ name: mocha
62
+ requirement: &id004 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ~>
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ - 9
70
+ - 8
71
+ version: 0.9.8
72
+ type: :development
73
+ prerelease: false
74
+ version_requirements: *id004
75
+ - !ruby/object:Gem::Dependency
76
+ name: rspec
77
+ requirement: &id005 !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 2
84
+ - 0
85
+ version: "2.0"
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: *id005
89
+ - !ruby/object:Gem::Dependency
90
+ name: watchr
91
+ requirement: &id006 !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ segments:
97
+ - 0
98
+ - 7
99
+ version: "0.7"
100
+ type: :development
101
+ prerelease: false
102
+ version_requirements: *id006
103
+ - !ruby/object:Gem::Dependency
104
+ name: ruby-debug-wrapper
105
+ requirement: &id007 !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - "="
109
+ - !ruby/object:Gem::Version
110
+ segments:
111
+ - 0
112
+ - 0
113
+ - 1
114
+ version: 0.0.1
115
+ type: :development
116
+ prerelease: false
117
+ version_requirements: *id007
118
+ description: Adds full-text search to Active Record models on PostgreSQL
119
+ email:
120
+ - durran@gmail.com
121
+ executables: []
122
+
123
+ extensions: []
124
+
125
+ extra_rdoc_files: []
126
+
127
+ files:
128
+ - lib/posterboy/document.rb
129
+ - lib/posterboy/version.rb
130
+ - lib/posterboy.rb
131
+ - MIT_LICENSE
132
+ - README
133
+ has_rdoc: true
134
+ homepage: http://github.com/durran/posterboy
135
+ licenses: []
136
+
137
+ post_install_message:
138
+ rdoc_options: []
139
+
140
+ require_paths:
141
+ - lib
142
+ required_ruby_version: !ruby/object:Gem::Requirement
143
+ none: false
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ hash: 398691525175066671
148
+ segments:
149
+ - 0
150
+ version: "0"
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ none: false
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ segments:
157
+ - 1
158
+ - 3
159
+ - 6
160
+ version: 1.3.6
161
+ requirements: []
162
+
163
+ rubyforge_project: posterboy
164
+ rubygems_version: 1.3.7
165
+ signing_key:
166
+ specification_version: 3
167
+ summary: PostgreSQL Search Done Easy
168
+ test_files: []
169
+