pg_search 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f0dac0584f13909a05901de21df0de7ccee6312b
4
- data.tar.gz: 5eafba8855a040b9bfea3ea7df2a6e9fadb7bb02
3
+ metadata.gz: cfa2efbbbc811da8b5c62690f0084c9ddd60615d
4
+ data.tar.gz: 90e7078224516ca17fdc503b418f309fd72560ae
5
5
  SHA512:
6
- metadata.gz: 4ff3d3978df858242d44590e1dfde3107c943e4496ce251b2d71663beb800b922a742d5c2dea65f017946630d34b63eef2c29b206514ce2debfabdb2a969d922
7
- data.tar.gz: ebaea869e71b1c2239aaa8cb1d613623e6a41ce14628fc5afa96f210a0ee52dca39151ae0b156aca9fc6fe81cfc5dd9b75de561bd8d675bdbce01bae8463c8eb
6
+ metadata.gz: 9c177eb231f004b44affdd5006ab5de2d75b13b4b4da0fbc0c4cb85b9d4394a1d96e26a27f28d23c4200638fc523e1dda1f414067a350c417f160ab0d2a96a2c
7
+ data.tar.gz: 7e98e612c9d089f5bab6c19365485c6190ad0b6338f662bfeb8864516592618f7883508c019bb031c7472ff3b5793b51f35ec27aaafcee748b9b85a5006fffe0
data/CHANGELOG.md CHANGED
@@ -1,8 +1,12 @@
1
1
  # pg_search changelog
2
2
 
3
+ ## 1.0.2
4
+
5
+ * Don’t use SQL to rebuild search documents when models are multisearchable against dynamic methods and not just columns. Iterate over each record with `find_each` instead.
6
+
3
7
  ## 1.0.1
4
8
 
5
- * Call `.unscoped` on relation used to build subquery, to eliminate unnecessary JOINs. (Markus Doits)
9
+ * Call `.unscoped` on relation used to build subquery, to eliminate unnecessary JOINs. (Markus Doits)
6
10
 
7
11
  ## 1.0.0
8
12
 
@@ -13,7 +13,7 @@ module PgSearch
13
13
  def rebuild
14
14
  if model.respond_to?(:rebuild_pg_search_documents)
15
15
  model.rebuild_pg_search_documents
16
- elsif model.pg_search_multisearchable_options.key?(:if) || model.pg_search_multisearchable_options.key?(:unless)
16
+ elsif conditional? || dynamic?
17
17
  model.find_each { |record| record.update_pg_search_document }
18
18
  else
19
19
  model.connection.execute(rebuild_sql)
@@ -24,6 +24,15 @@ module PgSearch
24
24
 
25
25
  attr_reader :model
26
26
 
27
+ def conditional?
28
+ model.pg_search_multisearchable_options.key?(:if) || model.pg_search_multisearchable_options.key?(:unless)
29
+ end
30
+
31
+ def dynamic?
32
+ column_names = model.columns.map(&:name)
33
+ columns.any? { |column| !column_names.include?(column.to_s) }
34
+ end
35
+
27
36
  def connection
28
37
  model.connection
29
38
  end
@@ -1,3 +1,3 @@
1
1
  module PgSearch
2
- VERSION = "1.0.1".freeze
2
+ VERSION = "1.0.2".freeze
3
3
  end
@@ -23,7 +23,7 @@ describe PgSearch::Multisearch::Rebuilder do
23
23
  end
24
24
  end
25
25
 
26
- context "when multisearch is conditional" do
26
+ context "and multisearchable is conditional" do
27
27
  [:if, :unless].each do |conditional_key|
28
28
  context "via :#{conditional_key}" do
29
29
  with_model :Model do
@@ -51,55 +51,56 @@ describe PgSearch::Multisearch::Rebuilder do
51
51
  end
52
52
 
53
53
  context "when the model does not define .rebuild_pg_search_documents" do
54
- context "when multisearchable is not conditional" do
55
- with_model :Model do
56
- table do |t|
57
- t.string :name
58
- end
54
+ context "and multisearchable is not conditional" do
55
+ context "when :against only includes columns" do
56
+ with_model :Model do
57
+ table do |t|
58
+ t.string :name
59
+ end
59
60
 
60
- model do
61
- include PgSearch
62
- multisearchable :against => :name
61
+ model do
62
+ include PgSearch
63
+ multisearchable :against => :name
64
+ end
63
65
  end
64
- end
65
66
 
66
- it "should not call :rebuild_pg_search_documents" do
67
- rebuilder = PgSearch::Multisearch::Rebuilder.new(Model)
67
+ it "should not call :rebuild_pg_search_documents" do
68
+ rebuilder = PgSearch::Multisearch::Rebuilder.new(Model)
68
69
 
69
- # stub respond_to? to return false since should_not_receive defines the method
70
- original_respond_to = Model.method(:respond_to?)
71
- allow(Model).to receive(:respond_to?) do |method_name, *args|
72
- if method_name == :rebuild_pg_search_documents
73
- false
74
- else
75
- original_respond_to.call(method_name, *args)
70
+ # stub respond_to? to return false since should_not_receive defines the method
71
+ original_respond_to = Model.method(:respond_to?)
72
+ allow(Model).to receive(:respond_to?) do |method_name, *args|
73
+ if method_name == :rebuild_pg_search_documents
74
+ false
75
+ else
76
+ original_respond_to.call(method_name, *args)
77
+ end
76
78
  end
77
- end
78
79
 
79
- expect(Model).not_to receive(:rebuild_pg_search_documents)
80
- rebuilder.rebuild
81
- end
80
+ expect(Model).not_to receive(:rebuild_pg_search_documents)
81
+ rebuilder.rebuild
82
+ end
82
83
 
83
- it "should execute the default SQL" do
84
- time = DateTime.parse("2001-01-01")
85
- rebuilder = PgSearch::Multisearch::Rebuilder.new(Model, lambda { time } )
84
+ it "should execute the default SQL" do
85
+ time = DateTime.parse("2001-01-01")
86
+ rebuilder = PgSearch::Multisearch::Rebuilder.new(Model, lambda { time } )
86
87
 
87
- # Handle change in precision of DateTime objects in SQL in Active Record 4.0.1
88
- # https://github.com/rails/rails/commit/17f5d8e062909f1fcae25351834d8e89967b645e
89
- version_4_0_1_or_newer = (
88
+ # Handle change in precision of DateTime objects in SQL in Active Record 4.0.1
89
+ # https://github.com/rails/rails/commit/17f5d8e062909f1fcae25351834d8e89967b645e
90
+ version_4_0_1_or_newer = (
90
91
  (ActiveRecord::VERSION::MAJOR > 4) ||
91
- (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR >= 1) ||
92
- (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 0 && ActiveRecord::VERSION::TINY >= 1)
93
- )
94
-
95
- expected_timestamp =
96
- if version_4_0_1_or_newer
97
- "2001-01-01 00:00:00.000000"
98
- else
99
- "2001-01-01 00:00:00"
100
- end
92
+ (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR >= 1) ||
93
+ (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 0 && ActiveRecord::VERSION::TINY >= 1)
94
+ )
95
+
96
+ expected_timestamp =
97
+ if version_4_0_1_or_newer
98
+ "2001-01-01 00:00:00.000000"
99
+ else
100
+ "2001-01-01 00:00:00"
101
+ end
101
102
 
102
- expected_sql = <<-SQL.strip_heredoc
103
+ expected_sql = <<-SQL.strip_heredoc
103
104
  INSERT INTO "pg_search_documents" (searchable_type, searchable_id, content, created_at, updated_at)
104
105
  SELECT 'Model' AS searchable_type,
105
106
  #{Model.quoted_table_name}.#{Model.primary_key} AS searchable_id,
@@ -109,53 +110,53 @@ describe PgSearch::Multisearch::Rebuilder do
109
110
  '#{expected_timestamp}' AS created_at,
110
111
  '#{expected_timestamp}' AS updated_at
111
112
  FROM #{Model.quoted_table_name}
112
- SQL
113
+ SQL
113
114
 
114
- executed_sql = []
115
+ executed_sql = []
115
116
 
116
- notifier = ActiveSupport::Notifications.subscribe("sql.active_record") do |name, start, finish, id, payload|
117
- executed_sql << payload[:sql] if payload[:sql].include?(%Q{INSERT INTO "pg_search_documents"})
118
- end
117
+ notifier = ActiveSupport::Notifications.subscribe("sql.active_record") do |name, start, finish, id, payload|
118
+ executed_sql << payload[:sql] if payload[:sql].include?(%Q{INSERT INTO "pg_search_documents"})
119
+ end
119
120
 
120
- rebuilder.rebuild
121
- ActiveSupport::Notifications.unsubscribe(notifier)
121
+ rebuilder.rebuild
122
+ ActiveSupport::Notifications.unsubscribe(notifier)
122
123
 
123
- expect(executed_sql.length).to eq(1)
124
- expect(executed_sql.first.strip).to eq(expected_sql.strip)
125
- end
124
+ expect(executed_sql.length).to eq(1)
125
+ expect(executed_sql.first.strip).to eq(expected_sql.strip)
126
+ end
126
127
 
127
- context "for a model with a non-standard primary key" do
128
- with_model :ModelWithNonStandardPrimaryKey do
129
- table primary_key: :non_standard_primary_key do |t|
130
- t.string :name
131
- end
128
+ context "for a model with a non-standard primary key" do
129
+ with_model :ModelWithNonStandardPrimaryKey do
130
+ table primary_key: :non_standard_primary_key do |t|
131
+ t.string :name
132
+ end
132
133
 
133
- model do
134
- include PgSearch
135
- multisearchable :against => :name
134
+ model do
135
+ include PgSearch
136
+ multisearchable :against => :name
137
+ end
136
138
  end
137
- end
138
139
 
139
- it "generates SQL with the correct primary key" do
140
- time = DateTime.parse("2001-01-01")
141
- rebuilder = PgSearch::Multisearch::Rebuilder.new(ModelWithNonStandardPrimaryKey, lambda { time } )
140
+ it "generates SQL with the correct primary key" do
141
+ time = DateTime.parse("2001-01-01")
142
+ rebuilder = PgSearch::Multisearch::Rebuilder.new(ModelWithNonStandardPrimaryKey, lambda { time } )
142
143
 
143
- # Handle change in precision of DateTime objects in SQL in Active Record 4.0.1
144
- # https://github.com/rails/rails/commit/17f5d8e062909f1fcae25351834d8e89967b645e
145
- version_4_0_1_or_newer = (
144
+ # Handle change in precision of DateTime objects in SQL in Active Record 4.0.1
145
+ # https://github.com/rails/rails/commit/17f5d8e062909f1fcae25351834d8e89967b645e
146
+ version_4_0_1_or_newer = (
146
147
  (ActiveRecord::VERSION::MAJOR > 4) ||
147
- (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR >= 1) ||
148
- (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 0 && ActiveRecord::VERSION::TINY >= 1)
149
- )
150
-
151
- expected_timestamp =
152
- if version_4_0_1_or_newer
153
- "2001-01-01 00:00:00.000000"
154
- else
155
- "2001-01-01 00:00:00"
156
- end
148
+ (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR >= 1) ||
149
+ (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 0 && ActiveRecord::VERSION::TINY >= 1)
150
+ )
151
+
152
+ expected_timestamp =
153
+ if version_4_0_1_or_newer
154
+ "2001-01-01 00:00:00.000000"
155
+ else
156
+ "2001-01-01 00:00:00"
157
+ end
157
158
 
158
- expected_sql = <<-SQL.strip_heredoc
159
+ expected_sql = <<-SQL.strip_heredoc
159
160
  INSERT INTO "pg_search_documents" (searchable_type, searchable_id, content, created_at, updated_at)
160
161
  SELECT 'ModelWithNonStandardPrimaryKey' AS searchable_type,
161
162
  #{ModelWithNonStandardPrimaryKey.quoted_table_name}.non_standard_primary_key AS searchable_id,
@@ -165,24 +166,62 @@ describe PgSearch::Multisearch::Rebuilder do
165
166
  '#{expected_timestamp}' AS created_at,
166
167
  '#{expected_timestamp}' AS updated_at
167
168
  FROM #{ModelWithNonStandardPrimaryKey.quoted_table_name}
168
- SQL
169
+ SQL
169
170
 
170
- executed_sql = []
171
+ executed_sql = []
171
172
 
172
- notifier = ActiveSupport::Notifications.subscribe("sql.active_record") do |name, start, finish, id, payload|
173
- executed_sql << payload[:sql] if payload[:sql].include?(%Q{INSERT INTO "pg_search_documents"})
173
+ notifier = ActiveSupport::Notifications.subscribe("sql.active_record") do |name, start, finish, id, payload|
174
+ executed_sql << payload[:sql] if payload[:sql].include?(%Q{INSERT INTO "pg_search_documents"})
175
+ end
176
+
177
+ rebuilder.rebuild
178
+ ActiveSupport::Notifications.unsubscribe(notifier)
179
+
180
+ expect(executed_sql.length).to eq(1)
181
+ expect(executed_sql.first.strip).to eq(expected_sql.strip)
174
182
  end
183
+ end
184
+ end
185
+
186
+ context "when :against includes non-column dynamic methods" do
187
+ with_model :Model do
188
+ table do
189
+ end
190
+
191
+ model do
192
+ include PgSearch
193
+ multisearchable against: [:foo]
194
+
195
+ def foo
196
+ "bar"
197
+ end
198
+ end
199
+ end
200
+
201
+ it "calls update_pg_search_document on each record" do
202
+ record = Model.create!
203
+
204
+ rebuilder = PgSearch::Multisearch::Rebuilder.new(Model)
205
+
206
+ # stub respond_to? to return false since should_not_receive defines the method
207
+ original_respond_to = Model.method(:respond_to?)
208
+ allow(Model).to receive(:respond_to?) do |method_name, *args|
209
+ if method_name == :rebuild_pg_search_documents
210
+ false
211
+ else
212
+ original_respond_to.call(method_name, *args)
213
+ end
214
+ end
215
+ expect(Model).not_to receive(:rebuild_pg_search_documents)
175
216
 
176
217
  rebuilder.rebuild
177
- ActiveSupport::Notifications.unsubscribe(notifier)
178
218
 
179
- expect(executed_sql.length).to eq(1)
180
- expect(executed_sql.first.strip).to eq(expected_sql.strip)
219
+ expect(record.pg_search_document).to be_present
181
220
  end
182
221
  end
183
222
  end
184
223
 
185
- context "when multisearchable is conditional" do
224
+ context "and multisearchable is conditional" do
186
225
  context "via :if" do
187
226
  with_model :Model do
188
227
  table do |t|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Grant Hutchins
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-05-02 00:00:00.000000000 Z
12
+ date: 2015-05-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord