wvanbergen-scoped_search 0.3.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +52 -30
- data/TODO +14 -8
- data/lib/scoped_search/query_conditions_builder.rb +25 -1
- data/lib/scoped_search.rb +42 -3
- data/test/search_for_test.rb +111 -27
- data/test/test_helper.rb +163 -1
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -1,54 +1,76 @@
|
|
1
|
-
|
1
|
+
h1. "scoped_search":http://github.com/wvanbergen/scoped_search/tree/master
|
2
2
|
|
3
|
-
This simple plugin will make it easy to search your ActiveRecord models. Searching is performed using a query string, which should be passed to the named_scope <tt>search_for</tt> that uses SQL %LIKE% conditions for searching. You can specify what fields should be used for searching.
|
3
|
+
This simple plugin will make it easy to search your ActiveRecord models. Searching is performed using a query string, which should be passed to the named_scope <tt>search_for</tt> that uses SQL %LIKE% conditions for searching (ILIKE for Postgres). You can specify what fields should be used for searching.
|
4
4
|
|
5
|
-
|
5
|
+
* "scoped_search installation":#INSTALLATION
|
6
|
+
* "scoped_search usage":#USAGE
|
7
|
+
* "scoped_search license":#LICENSE
|
6
8
|
|
7
|
-
|
8
|
-
|
9
|
-
Rails::Initializer.run do |config|
|
10
|
-
...
|
11
|
-
config.gem 'wvanbergen-scoped_search', :lib => 'scoped_search', :source => 'http://gems.github.com/'
|
12
|
-
end
|
9
|
+
<a id="INSTALLATION"/> </a>
|
10
|
+
h2. Installing scoped_search
|
13
11
|
|
14
|
-
|
12
|
+
<pre><code>
|
13
|
+
gem install wvanbergen-scoped_search
|
15
14
|
|
16
|
-
|
17
|
-
clone scoped_search into your +vendor/plugins+ directory of your project.
|
15
|
+
</code></pre>
|
18
16
|
|
19
|
-
|
17
|
+
<a id="USAGE"/> </a>
|
18
|
+
h2. Usage
|
20
19
|
|
21
20
|
First, you have to specify in what columns should be searched:
|
22
21
|
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
<pre><code>
|
23
|
+
class User < ActiveRecord::Base
|
24
|
+
searchable_on :first_name, :last_name
|
25
|
+
end
|
26
26
|
|
27
|
-
|
27
|
+
</code></pre>
|
28
28
|
|
29
|
-
|
29
|
+
Now, the *search_for* scope is available for queries. You should pass a query string to the scope.
|
30
|
+
This can be empty or nil, in which case all no search conditions are set (and all records will be returned).
|
30
31
|
|
31
|
-
|
32
|
+
<pre><code>
|
33
|
+
User.search_for(params[:q]).each { |project| ... }
|
34
|
+
|
35
|
+
</code></pre>
|
36
|
+
|
37
|
+
You can also search on associate models. This works with *belongs_to*, *has_one*, *has_many*,
|
38
|
+
*has_many :through*, and *HABTM*. For example if a User *has_many* Notes (title, content, created_at, updated_at)
|
39
|
+
|
40
|
+
<pre><code>
|
41
|
+
class User < ActiveRecord::Base
|
42
|
+
has_many: notes
|
43
|
+
searchable_on :first_name, :last_name, :notes_title, :notes_content
|
44
|
+
end
|
32
45
|
|
46
|
+
</code></pre>
|
47
|
+
|
48
|
+
The search query language is simple. It supports these constructs:
|
33
49
|
* words: some search keywords
|
34
50
|
* phrases: "a single search phrase"
|
35
51
|
* negation: "look for this" -"but do not look for this phrase and this" -word
|
52
|
+
* OR words/phrases: word/phrase OR word/phrase. Example: "Hello World" OR "Hello Moon"
|
53
|
+
* dates: mm/dd/yyyy, dd/mm/yyyy, yyyy/mm/dd, yyyy-mm-dd
|
54
|
+
* date ranges: > date, >= date, < date, <= date, date TO date. Examples: > mm/dd/yyyy, < yyyy-mm-dd
|
36
55
|
|
37
56
|
This functionality is build on named_scope. The searchable_on statement creates
|
38
|
-
a named_scope
|
57
|
+
a named_scope *search_for*. Because of this, you can actually chain the call with
|
39
58
|
other scopes. For example, this can be very useful if you only want to search in
|
40
59
|
projects that are accessible by a given user.
|
41
60
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
61
|
+
<pre><code>
|
62
|
+
class Project < ActiveRecord::Base
|
63
|
+
searchable_on :name, :description
|
64
|
+
named_scope :accessible_by, lambda { |user| ... }
|
65
|
+
end
|
46
66
|
|
47
|
-
|
48
|
-
|
67
|
+
# using chained named_scopes and will_paginate
|
68
|
+
Project.accessible_by(current_user).search_for(params[:q]).paginate(:page => params[:page], :include => :tasks)
|
69
|
+
|
70
|
+
</code></pre>
|
71
|
+
|
72
|
+
<a id="LICENSE"/> </a>
|
73
|
+
h2. License
|
49
74
|
|
50
|
-
|
75
|
+
This plugin is released under the MIT license. Please contact (willem AT vanbergen DOT org) if you have any suggestions or remarks.
|
51
76
|
|
52
|
-
This Rails plugin is written by Willem van Bergen for the Floorplanner.com website. It is
|
53
|
-
released under the <b>MIT license</b>. Please contact me (willem AT vanbergen DOT org if
|
54
|
-
you have any suggestions or remarks.
|
data/TODO
CHANGED
@@ -2,16 +2,22 @@ TODO items for named_scope
|
|
2
2
|
==========================
|
3
3
|
Contact willem AT vanbergen DOT org if you want to help out
|
4
4
|
|
5
|
+
0.6.0
|
6
|
+
- Add test for Postgres
|
7
|
+
- Add test for MySQL
|
5
8
|
|
6
|
-
New features:
|
7
|
-
- Search fields of associations as well
|
8
|
-
- Allow smart searches. Example: "90 days ago", "Yesterday", "10 days from now"
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
0.7.0
|
11
|
+
|
12
|
+
|
13
|
+
0.8.0
|
14
|
+
|
15
|
+
|
16
|
+
0.9.0
|
17
|
+
|
18
|
+
|
19
|
+
1.0.0
|
20
|
+
|
14
21
|
|
15
22
|
Documentation & testing:
|
16
|
-
- Put something useful in the wiki
|
17
23
|
- Add rdoc en comments to code
|
@@ -107,7 +107,31 @@ module ScopedSearch
|
|
107
107
|
end
|
108
108
|
|
109
109
|
def as_of_date(keyword_name, value)
|
110
|
-
|
110
|
+
retVal = []
|
111
|
+
begin
|
112
|
+
dt = Date.parse(value) # This will throw an exception if it is not valid
|
113
|
+
@query_params[keyword_name] = dt.to_s
|
114
|
+
@query_fields.each do |field, field_type| #|key,value|
|
115
|
+
if field_type == :date or field_type == :datetime or field_type == :timestamp
|
116
|
+
retVal << "#{field} = :#{keyword_name.to_s}"
|
117
|
+
end
|
118
|
+
end
|
119
|
+
rescue
|
120
|
+
# do not search on any date columns since the date is invalid
|
121
|
+
end
|
122
|
+
|
123
|
+
# Search the text fields for the date as well as it could be in text.
|
124
|
+
# Also still search on the text columns for an invalid date as it could
|
125
|
+
# have a different meaning.
|
126
|
+
keyword_name_b = "#{keyword_name}b".to_sym
|
127
|
+
@query_params[keyword_name_b] = "%#{value}%"
|
128
|
+
@query_fields.each do |field, field_type| #|key,value|
|
129
|
+
if field_type == :string or field_type == :text
|
130
|
+
retVal << "#{field} LIKE :#{keyword_name_b.to_s}"
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
"(#{retVal.join(' OR ')})"
|
111
135
|
end
|
112
136
|
|
113
137
|
def greater_than_date(keyword_name, value)
|
data/lib/scoped_search.rb
CHANGED
@@ -9,9 +9,35 @@ module ScopedSearch
|
|
9
9
|
end
|
10
10
|
|
11
11
|
# Creates a named scope in the class it was called upon
|
12
|
-
def searchable_on(*fields)
|
13
|
-
|
12
|
+
def searchable_on(*fields)
|
13
|
+
if fields.first.class.to_s == 'Hash'
|
14
|
+
if fields.first.has_key?(:only)
|
15
|
+
fields = fields.first[:only]
|
16
|
+
elsif fields.first.has_key?(:except)
|
17
|
+
fields = self.column_names.collect { |column|
|
18
|
+
fields.first[:except].include?(column.to_sym) ? nil : column.to_sym }.compact
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
assoc_models = self.reflections.collect { |m| m[0] }
|
23
|
+
assoc_fields = fields - self.column_names.collect { |column| column.to_sym }
|
24
|
+
fields -= assoc_fields
|
25
|
+
|
26
|
+
assoc_groupings = {}
|
27
|
+
assoc_models.each do |assoc_model|
|
28
|
+
assoc_groupings[assoc_model] = []
|
29
|
+
assoc_fields.each do |assoc_field|
|
30
|
+
unless assoc_field.to_s.match(/^#{assoc_model.to_s}_/).nil?
|
31
|
+
assoc_groupings[assoc_model] << assoc_field.to_s.sub(/^#{assoc_model.to_s}_/, '').to_sym
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
assoc_groupings = assoc_groupings.delete_if {|group, field_group| field_group.empty?}
|
37
|
+
|
38
|
+
self.cattr_accessor :scoped_search_fields, :scoped_search_assoc_groupings
|
14
39
|
self.scoped_search_fields = fields
|
40
|
+
self.scoped_search_assoc_groupings = assoc_groupings
|
15
41
|
self.named_scope :search_for, lambda { |keywords| self.build_scoped_search_conditions(keywords) }
|
16
42
|
end
|
17
43
|
|
@@ -28,9 +54,22 @@ module ScopedSearch
|
|
28
54
|
query_fields[field_name] = self.columns_hash[field.to_s].type
|
29
55
|
end
|
30
56
|
|
57
|
+
assoc_models_to_include = []
|
58
|
+
self.scoped_search_assoc_groupings.each do |group|
|
59
|
+
assoc_models_to_include << group[0]
|
60
|
+
group[1].each do |group_field|
|
61
|
+
field_name = connection.quote_table_name(group[0].to_s.pluralize) + "." + connection.quote_column_name(group_field)
|
62
|
+
query_fields[field_name] = self.reflections[group[0]].klass.columns_hash[group_field.to_s].type
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
31
66
|
search_conditions = QueryLanguageParser.parse(search_string)
|
32
67
|
conditions = QueryConditionsBuilder.build_query(search_conditions, query_fields)
|
33
|
-
|
68
|
+
|
69
|
+
retVal = {:conditions => conditions}
|
70
|
+
retVal[:include] = assoc_models_to_include unless assoc_models_to_include.empty?
|
71
|
+
|
72
|
+
return retVal
|
34
73
|
end
|
35
74
|
end
|
36
75
|
end
|
data/test/search_for_test.rb
CHANGED
@@ -5,62 +5,146 @@ class ScopedSearchTest < Test::Unit::TestCase
|
|
5
5
|
def setup
|
6
6
|
setup_db
|
7
7
|
SearchTestModel.create_corpus!
|
8
|
+
Group.create_corpus!
|
9
|
+
Location.create_corpus!
|
10
|
+
Address.create_corpus!
|
11
|
+
User.create_corpus!
|
12
|
+
Client.create_corpus!
|
13
|
+
Office.create_corpus!
|
14
|
+
Note.create_corpus!
|
8
15
|
end
|
9
16
|
|
10
17
|
def teardown
|
11
18
|
teardown_db
|
12
19
|
end
|
13
20
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
21
|
+
def test_enabling
|
22
|
+
assert !SearchTestModel.respond_to?(:search_for)
|
23
|
+
SearchTestModel.searchable_on :string_field, :text_field, :date_field
|
24
|
+
assert SearchTestModel.respond_to?(:search_for)
|
25
|
+
|
26
|
+
assert_equal ActiveRecord::NamedScope::Scope, SearchTestModel.search_for('test').class
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_search_only_fields
|
30
|
+
SearchTestModel.searchable_on :only => [:string_field, :text_field, :date_field]
|
31
|
+
assert SearchTestModel.respond_to?(:search_for)
|
32
|
+
assert_equal SearchTestModel.scoped_search_fields.size, 3
|
33
|
+
assert SearchTestModel.scoped_search_fields.include?(:string_field)
|
34
|
+
assert SearchTestModel.scoped_search_fields.include?(:text_field)
|
35
|
+
assert SearchTestModel.scoped_search_fields.include?(:date_field)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_search_except_fields
|
39
|
+
SearchTestModel.searchable_on :except => [:id, :ignored_field, :created_at, :updated_at]
|
40
|
+
assert SearchTestModel.respond_to?(:search_for)
|
41
|
+
assert_equal SearchTestModel.scoped_search_fields.size, 3
|
42
|
+
assert SearchTestModel.scoped_search_fields.include?(:string_field)
|
43
|
+
assert SearchTestModel.scoped_search_fields.include?(:text_field)
|
44
|
+
assert SearchTestModel.scoped_search_fields.include?(:date_field)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_search_with_only_and_except
|
48
|
+
# :except should be ignored if :only is specified.
|
49
|
+
SearchTestModel.searchable_on({:only => [:text_field], :except => [:text_field]})
|
50
|
+
assert SearchTestModel.respond_to?(:search_for)
|
51
|
+
assert_equal SearchTestModel.scoped_search_fields.size, 1
|
52
|
+
assert SearchTestModel.scoped_search_fields.include?(:text_field), ':except should be ignored if :only is specified'
|
53
|
+
end
|
54
|
+
|
22
55
|
def test_search
|
23
56
|
SearchTestModel.searchable_on :string_field, :text_field, :date_field
|
24
|
-
|
25
|
-
assert_equal
|
57
|
+
|
58
|
+
assert_equal SearchTestModel.count, SearchTestModel.search_for('').count
|
26
59
|
assert_equal 0, SearchTestModel.search_for('456').count
|
27
60
|
assert_equal 2, SearchTestModel.search_for('hays').count
|
28
61
|
assert_equal 1, SearchTestModel.search_for('hay ob').count
|
29
|
-
assert_equal
|
62
|
+
assert_equal 15, SearchTestModel.search_for('o').count
|
30
63
|
assert_equal 2, SearchTestModel.search_for('-o').count
|
31
|
-
assert_equal
|
64
|
+
assert_equal 15, SearchTestModel.search_for('-Jim').count
|
32
65
|
assert_equal 1, SearchTestModel.search_for('Jim -Bush').count
|
33
66
|
assert_equal 1, SearchTestModel.search_for('"Hello World" -"Goodnight Moon"').count
|
34
67
|
assert_equal 2, SearchTestModel.search_for('Wes OR Bob').count
|
35
68
|
assert_equal 3, SearchTestModel.search_for('"Happy cow" OR "Sad Frog"').count
|
36
69
|
assert_equal 3, SearchTestModel.search_for('"Man made" OR Dogs').count
|
37
70
|
assert_equal 2, SearchTestModel.search_for('Cows OR "Frog Toys"').count
|
38
|
-
|
71
|
+
|
39
72
|
# ** DATES **
|
40
73
|
#
|
41
74
|
# The next two dates are invalid therefore it will be ignored.
|
42
|
-
#
|
43
|
-
|
44
|
-
|
45
|
-
|
75
|
+
# Since it is just a date being searched for it will also
|
76
|
+
# be searched for in text fields regardless of whether or
|
77
|
+
# not it is a valid date.
|
78
|
+
assert_equal 0, SearchTestModel.search_for('2/30/1980').count
|
79
|
+
assert_equal 0, SearchTestModel.search_for('99/99/9999').count
|
80
|
+
|
46
81
|
assert_equal 1, SearchTestModel.search_for('9/27/1980').count
|
47
82
|
assert_equal 1, SearchTestModel.search_for('hays 9/27/1980').count
|
48
|
-
assert_equal
|
49
|
-
|
50
|
-
|
83
|
+
assert_equal 0, SearchTestModel.search_for('hays 2/30/1980').count
|
84
|
+
|
51
85
|
assert_equal 1, SearchTestModel.search_for('< 12/01/1980').count
|
52
|
-
assert_equal
|
53
|
-
|
86
|
+
assert_equal 6, SearchTestModel.search_for('> 2006/1/1').count
|
87
|
+
|
54
88
|
assert_equal 5, SearchTestModel.search_for('< 12/26/2002').count
|
55
89
|
assert_equal 6, SearchTestModel.search_for('<= 12/26/2002').count
|
56
|
-
|
57
|
-
assert_equal
|
58
|
-
assert_equal
|
59
|
-
|
90
|
+
|
91
|
+
assert_equal 6, SearchTestModel.search_for('> 2/5/2005').count
|
92
|
+
assert_equal 7, SearchTestModel.search_for('>= 2/5/2005').count
|
93
|
+
|
60
94
|
assert_equal 3, SearchTestModel.search_for('1/1/2005 TO 1/1/2007').count
|
61
|
-
|
95
|
+
|
62
96
|
assert_equal 2, SearchTestModel.search_for('Happy 1/1/2005 TO 1/1/2007').count
|
97
|
+
|
98
|
+
# This should return one with a date of 7/15/2006 found in the text.
|
99
|
+
assert_equal 2, SearchTestModel.search_for('7/15/2006').count
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_search_belongs_to_association
|
103
|
+
User.searchable_on :first_name, :last_name, :group_name
|
104
|
+
|
105
|
+
assert_equal User.count, User.search_for('').count
|
106
|
+
assert_equal 1, User.search_for('Wes').count
|
107
|
+
assert_equal 2, User.search_for('System Administrator').count
|
108
|
+
assert_equal 2, User.search_for('Managers').count
|
63
109
|
end
|
110
|
+
|
111
|
+
def test_search_has_many_association
|
112
|
+
User.searchable_on :first_name, :last_name, :notes_title, :notes_content
|
113
|
+
|
114
|
+
assert_equal User.count, User.search_for('').count
|
115
|
+
assert_equal 2, User.search_for('Router').count
|
116
|
+
assert_equal 1, User.search_for('milk').count
|
117
|
+
assert_equal 1, User.search_for('"Spec Tests"').count
|
118
|
+
assert_equal 0, User.search_for('Wes "Spec Tests"').count
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_search_has_many_through_association
|
122
|
+
User.searchable_on :first_name, :last_name, :clients_first_name, :clients_last_name
|
123
|
+
|
124
|
+
assert_equal User.count, User.search_for('').count
|
125
|
+
assert_equal 2, User.search_for('Smith').count
|
126
|
+
assert_equal 1, User.search_for('Sam').count
|
127
|
+
assert_equal 1, User.search_for('Johnson').count
|
128
|
+
end
|
129
|
+
|
130
|
+
def test_search_has_one_association
|
131
|
+
User.searchable_on :first_name, :last_name, :address_street, :address_city, :address_state, :address_postal_code
|
132
|
+
|
133
|
+
assert_equal User.count, User.search_for('').count
|
134
|
+
assert_equal 1, User.search_for('Fernley').count
|
135
|
+
assert_equal 4, User.search_for('NV').count
|
136
|
+
assert_equal 1, User.search_for('Haskell').count
|
137
|
+
assert_equal 2, User.search_for('89434').count
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_search_has_and_belongs_to_many_association
|
141
|
+
User.searchable_on :first_name, :last_name, :locations_name
|
142
|
+
|
143
|
+
assert_equal User.count, User.search_for('').count
|
144
|
+
assert_equal 2, User.search_for('Office').count
|
145
|
+
assert_equal 1, User.search_for('Store').count
|
146
|
+
assert_equal 1, User.search_for('John Office').count
|
147
|
+
end
|
64
148
|
|
65
149
|
end
|
66
150
|
|
data/test/test_helper.rb
CHANGED
@@ -15,6 +15,44 @@ def setup_db
|
|
15
15
|
t.date :date_field
|
16
16
|
t.timestamps
|
17
17
|
end
|
18
|
+
|
19
|
+
create_table :users do |t|
|
20
|
+
t.string :first_name, :last_name, :login
|
21
|
+
t.integer :group_id
|
22
|
+
t.integer :address_id
|
23
|
+
end
|
24
|
+
|
25
|
+
create_table :clients do |t|
|
26
|
+
t.string :first_name, :last_name
|
27
|
+
end
|
28
|
+
|
29
|
+
create_table :offices do |t|
|
30
|
+
t.string :name
|
31
|
+
t.integer :user_id, :client_id
|
32
|
+
end
|
33
|
+
|
34
|
+
create_table :groups do |t|
|
35
|
+
t.string :name
|
36
|
+
end
|
37
|
+
|
38
|
+
create_table :locations do |t|
|
39
|
+
t.string :name
|
40
|
+
end
|
41
|
+
|
42
|
+
create_table :locations_users, :id => false, :force => true do |t|
|
43
|
+
t.integer :user_id
|
44
|
+
t.integer :location_id
|
45
|
+
end
|
46
|
+
|
47
|
+
create_table :notes do |t|
|
48
|
+
t.string :title
|
49
|
+
t.text :content
|
50
|
+
t.integer :user_id
|
51
|
+
end
|
52
|
+
|
53
|
+
create_table :addresses do |t|
|
54
|
+
t.string :street, :city, :state, :postal_code
|
55
|
+
end
|
18
56
|
end
|
19
57
|
end
|
20
58
|
|
@@ -39,6 +77,130 @@ class SearchTestModel < ActiveRecord::Base
|
|
39
77
|
create!(:string_field => "Excited Frog", :text_field => "Sad Frog", :ignored_field => "123456l", :date_field => '2006-07-15')
|
40
78
|
create!(:string_field => "Man made", :text_field => "Woman made", :ignored_field => "123456m", :date_field => '2007-06-13')
|
41
79
|
create!(:string_field => "Cat Toys", :text_field => "Frog Toys", :ignored_field => "123456n", :date_field => '2008-03-04')
|
42
|
-
create!(:string_field => "Happy Toys", :text_field => "Sad Toys", :ignored_field => "123456n", :date_field => '2008-05-12')
|
80
|
+
create!(:string_field => "Happy Toys", :text_field => "Sad Toys", :ignored_field => "123456n", :date_field => '2008-05-12')
|
81
|
+
|
82
|
+
create!(:string_field => "My son was born on 7/15/2006 and weighed 5.5 lbs",
|
83
|
+
:text_field => "Sad Toys",
|
84
|
+
:ignored_field => "123456n",
|
85
|
+
:date_field => '2008-09-22')
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
class User < ActiveRecord::Base
|
90
|
+
belongs_to :group
|
91
|
+
belongs_to :address
|
92
|
+
has_many :notes
|
93
|
+
has_and_belongs_to_many :locations
|
94
|
+
|
95
|
+
has_many :offices, :dependent => :destroy
|
96
|
+
has_many :clients, :through => :offices
|
97
|
+
def self.create_corpus!
|
98
|
+
create!(:first_name => 'Willem', :last_name => 'Van Bergen', :login => 'wvanbergen', :group_id => 1, :address_id => 1)
|
99
|
+
create!(:first_name => 'Wes', :last_name => 'Hays', :login => 'weshays', :group_id => 1, :address_id => 2)
|
100
|
+
create!(:first_name => 'John', :last_name => 'Dell', :login => 'jdell', :group_id => 2, :address_id => 3)
|
101
|
+
create!(:first_name => 'Ray', :last_name => 'York', :login => 'ryork', :group_id => 3, :address_id => 4)
|
102
|
+
create!(:first_name => 'Anna', :last_name => 'Landis', :login => 'alandis', :group_id => 4, :address_id => 5)
|
103
|
+
|
104
|
+
user = self.find_by_first_name('Willem')
|
105
|
+
user.locations << Location.find_by_name('Office')
|
106
|
+
|
107
|
+
user = self.find_by_first_name('Wes')
|
108
|
+
user.locations << Location.find_by_name('Store')
|
109
|
+
|
110
|
+
user = self.find_by_first_name('John')
|
111
|
+
user.locations << Location.find_by_name('Office')
|
112
|
+
|
113
|
+
user = self.find_by_first_name('Ray')
|
114
|
+
user.locations << Location.find_by_name('Home')
|
115
|
+
|
116
|
+
user = self.find_by_first_name('Anna')
|
117
|
+
user.locations << Location.find_by_name('Beach')
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
class Client < ActiveRecord::Base
|
122
|
+
has_many :offices, :dependent => :destroy
|
123
|
+
has_many :users, :through => :offices
|
124
|
+
def self.create_corpus!
|
125
|
+
create!(:first_name => 'Bob', :last_name => 'Smith')
|
126
|
+
create!(:first_name => 'Sam', :last_name => 'Lovett')
|
127
|
+
create!(:first_name => 'Sally', :last_name => 'May')
|
128
|
+
create!(:first_name => 'Mary', :last_name => 'Smith')
|
129
|
+
create!(:first_name => 'Darren', :last_name => 'Johnson')
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
class Office < ActiveRecord::Base
|
134
|
+
belongs_to :client
|
135
|
+
belongs_to :user
|
136
|
+
def self.create_corpus!
|
137
|
+
create!(:name => 'California Office', :user_id => 1, :client_id => 1)
|
138
|
+
create!(:name => 'California Office', :user_id => 2, :client_id => 2)
|
139
|
+
create!(:name => 'California Office', :user_id => 3, :client_id => 3)
|
140
|
+
create!(:name => 'Reno Office', :user_id => 4, :client_id => 4)
|
141
|
+
create!(:name => 'Reno Office', :user_id => 5, :client_id => 5)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
class Group < ActiveRecord::Base
|
146
|
+
has_many :users
|
147
|
+
def self.create_corpus!
|
148
|
+
create!(:name => 'System Administrator')
|
149
|
+
create!(:name => 'Software Managers')
|
150
|
+
create!(:name => 'Office Managers')
|
151
|
+
create!(:name => 'Accounting')
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
class Location < ActiveRecord::Base
|
156
|
+
has_and_belongs_to_many :users
|
157
|
+
def self.create_corpus!
|
158
|
+
create!(:name => 'Home')
|
159
|
+
create!(:name => 'Office')
|
160
|
+
create!(:name => 'Store')
|
161
|
+
create!(:name => 'Beach')
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
class Note < ActiveRecord::Base
|
166
|
+
belongs_to :user
|
167
|
+
def self.create_corpus!
|
168
|
+
wes = User.find_by_first_name('Wes')
|
169
|
+
john = User.find_by_first_name('John')
|
170
|
+
|
171
|
+
create!(:user_id => wes.id,
|
172
|
+
:title => 'Purchases',
|
173
|
+
:content => "1) Linksys Router. 2) Network Cable")
|
174
|
+
|
175
|
+
create!(:user_id => wes.id,
|
176
|
+
:title => 'Tasks',
|
177
|
+
:content => 'Clean my car, walk the dog and mow the yard buy milk')
|
178
|
+
|
179
|
+
create!(:user_id => wes.id,
|
180
|
+
:title => 'Grocery List',
|
181
|
+
:content => 'milk, gum, apples')
|
182
|
+
|
183
|
+
create!(:user_id => wes.id,
|
184
|
+
:title => 'Stocks to watch',
|
185
|
+
:content => 'MA, AAPL, V and SSO. Straddle MA at 200 with JAN 09 options')
|
186
|
+
|
187
|
+
create!(:user_id => john.id,
|
188
|
+
:title => 'Spec Tests',
|
189
|
+
:content => 'Spec Tests... Spec Tests... Spec Tests!!')
|
190
|
+
|
191
|
+
create!(:user_id => john.id,
|
192
|
+
:title => 'Things To Do',
|
193
|
+
:content => '1) Did I mention Spec Tests!!!, 2) Buy Linksys Router WRT160N')
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
class Address < ActiveRecord::Base
|
198
|
+
has_one :user
|
199
|
+
def self.create_corpus!
|
200
|
+
create!(:street => '800 Haskell St', :city => 'Reno', :state => 'NV', :postal_code => '89509')
|
201
|
+
create!(:street => '2499 Dorchester Rd', :city => 'Charleston', :state => 'SC', :postal_code => '29414')
|
202
|
+
create!(:street => '474 Mallard Way', :city => 'Fernley', :state => 'NV', :postal_code => '89408')
|
203
|
+
create!(:street => '1600 Montero Ct', :city => 'Sparks', :state => 'NV', :postal_code => '89434')
|
204
|
+
create!(:street => '200 4th St', :city => 'Sparks', :state => 'NV', :postal_code => '89434')
|
43
205
|
end
|
44
206
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wvanbergen-scoped_search
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Willem van Bergen
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2008-09-
|
13
|
+
date: 2008-09-23 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|