search_and_resque 0.3.3 → 0.3.4

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: 14e1917d878751695a81ce42fc5f4a7c84e93cbb
4
- data.tar.gz: 43c77852df71b3b78b8321e27292df2a869e1cfd
3
+ metadata.gz: 8575aca9094955ecb328163636e194afe5c44aab
4
+ data.tar.gz: d5fff4dda4b734eb3c520b8d7cdfc591546e11f5
5
5
  SHA512:
6
- metadata.gz: 52a576f22fa66340ee197449fa30a7652c38e750399821da9e20ea59f37ed4c8750c56eff57e7331c35f94bc2cd6a72dae5b12f4181d1a869928430407f91605
7
- data.tar.gz: 678a8f74ea6d075f340ff94f089314c9688caf201fb33db51e1d767a165176cdf752707eb7150afe149e7a04295f7caa8b4b6f484be582d131e2698f35dd16a5
6
+ metadata.gz: dc66289fcd27861e15e201efbbfb879f79435cdf909578e3baac2a78961de84249cbd0ef9578d9ed25968961863e185282f0538de2d61b2cd88c61677813b686
7
+ data.tar.gz: dd0033c82068f3fc4ac43f62f8f6fa3bc7d649f3ea7b75927df001d2fc34a588c4cca0712e9f5e23d702cf92cfd859bfa9979c8d7240e71c7b5fc56a93263ed6
@@ -2,14 +2,6 @@ module SearchAndResque
2
2
  module Callbacks
3
3
  extend ActiveSupport::Concern
4
4
 
5
- included do
6
- class << self
7
- attr_accessor :elastic_search_type
8
- end
9
- after_save :enqueue_elastic_search_update
10
- after_destroy :enqueue_elastic_search_delete
11
- end
12
-
13
5
  module ClassMethods
14
6
  def enqueue_elastic_search_update(ids)
15
7
  ids = Array(ids).map{ |x| x.is_a?(ActiveRecord::Base) ? x.id : x }
@@ -23,11 +15,13 @@ module SearchAndResque
23
15
  end
24
16
 
25
17
  def enqueue_elastic_search_update
26
- self.class.enqueue_elastic_search_update(id) if should_update_elastic_search?
18
+ if should_update_elastic_search?
19
+ self.class.enqueue_elastic_search_update(elastic_search_id)
20
+ end
27
21
  end
28
22
 
29
23
  def enqueue_elastic_search_delete
30
- self.class.enqueue_elastic_search_delete(id)
24
+ self.class.enqueue_elastic_search_delete(elastic_search_id)
31
25
  end
32
26
 
33
27
  # e.g.
@@ -1,3 +1,3 @@
1
1
  module SearchAndResque
2
- VERSION = "0.3.3"
2
+ VERSION = "0.3.4"
3
3
  end
@@ -31,12 +31,24 @@ module SearchAndResque
31
31
  module ClassMethods
32
32
  def search_and_resque(type_name, options={})
33
33
  unless included_modules.include?(SearchAndResque::Callbacks)
34
- options[:if] ||= ->{ true }
35
- define_method(:should_update_elastic_search?, &options[:if])
36
-
37
34
  @elastic_search_type = "#{type_name}"
35
+ class << self
36
+ attr_accessor :elastic_search_type
37
+ end
38
38
 
39
39
  include SearchAndResque::Callbacks
40
+ after_save :enqueue_elastic_search_update
41
+ if options[:id]
42
+ after_destroy :enqueue_elastic_search_update
43
+ else
44
+ after_destroy :enqueue_elastic_search_delete
45
+ end
46
+
47
+ options[:if] ||= ->{ true }
48
+ define_method(:should_update_elastic_search?, &options[:if])
49
+
50
+ options[:id] ||= ->{ id }
51
+ define_method(:elastic_search_id, &options[:id])
40
52
  end
41
53
  end
42
54
  end
@@ -20,8 +20,9 @@ end
20
20
 
21
21
  def setup!
22
22
  connect!
23
- ActiveRecord::Base.connection.execute 'CREATE TABLE books (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME, title STRING)'
24
- ActiveRecord::Base.connection.execute 'CREATE TABLE films (id INTEGER NOT NULL PRIMARY KEY, deleted_at DATETIME, title STRING, director STRING)'
23
+ ActiveRecord::Base.connection.execute 'CREATE TABLE books (id INTEGER NOT NULL PRIMARY KEY, title STRING)'
24
+ ActiveRecord::Base.connection.execute 'CREATE TABLE films (id INTEGER NOT NULL PRIMARY KEY, title STRING, director STRING)'
25
+ ActiveRecord::Base.connection.execute 'CREATE TABLE scores (id INTEGER NOT NULL PRIMARY KEY, film_id INTEGER NOT NULL, composer STRING)'
25
26
  end
26
27
 
27
28
  setup!
@@ -31,9 +32,15 @@ class Book < ActiveRecord::Base
31
32
  end
32
33
 
33
34
  class Film < ActiveRecord::Base
35
+ has_one :score
34
36
  search_and_resque :films, :if => ->{ title_changed? }
35
37
  end
36
38
 
39
+ class Score < ActiveRecord::Base
40
+ belongs_to :film
41
+ search_and_resque :films, id: ->{ film_id }
42
+ end
43
+
37
44
  class SearchAndResqueTestIndex < Chewy::Index
38
45
  define_type Book, :name => 'books' do
39
46
  field :title
@@ -42,6 +49,7 @@ class SearchAndResqueTestIndex < Chewy::Index
42
49
  define_type Film, :name => 'films' do
43
50
  field :title
44
51
  field :director
52
+ field :score_composer, value: ->{ score.try(:composer) }
45
53
  end
46
54
  end
47
55
 
@@ -91,4 +99,14 @@ class SearchAndResqueTest < test_framework
91
99
  film.update_attributes(:title => 'three', :director => 'four')
92
100
  assert_equal 'four', SearchAndResqueTestIndex::Films.filter(:ids => {:values => [film.id]}).first.director
93
101
  end
102
+
103
+ def test_cross_type_update_and_delete
104
+ film = Film.create(:title => 'one', :director => 'two')
105
+
106
+ score = film.create_score(composer: 'Howard Shore')
107
+ assert_equal 'Howard Shore', SearchAndResqueTestIndex::Films.filter(:ids => {values: [film.id]}).first.score_composer
108
+
109
+ score.destroy
110
+ assert_equal nil, SearchAndResqueTestIndex::Films.filter(:ids => {values: [film.id]}).first.score_composer
111
+ end
94
112
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: search_and_resque
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Woo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-05 00:00:00.000000000 Z
11
+ date: 2015-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler