ripple_searchable 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,14 @@
1
- require 'ripple'
2
- require 'ripple/translation'
3
1
  require 'active_support/concern'
2
+ require 'active_model/callbacks'
3
+ require 'active_model/naming'
4
+ require 'active_model/observing'
5
+ require 'ripple/conflict/document_hooks'
6
+ require 'ripple/document'
7
+ require 'ripple/callbacks'
8
+ require 'ripple/observable'
9
+
4
10
  require 'ripple_searchable/version'
11
+ require 'ripple_searchable/searchable_observer'
5
12
  require 'ripple_searchable/searchable'
6
13
  require 'ripple_searchable/criteria'
7
14
  require 'ripple_searchable/loggable'
@@ -15,3 +22,5 @@ Ripple::Document.class_eval do
15
22
  include Ripple::Searchable
16
23
  include Ripple::Scoping
17
24
  end
25
+
26
+
@@ -272,6 +272,36 @@ module Ripple
272
272
  super
273
273
  end
274
274
 
275
+ # Get the count of matching documents in the database for the context.
276
+ #
277
+ # @example Get the count without skip and limit taken into consideration.
278
+ # context.count
279
+ #
280
+ # @example Get the count with skip and limit applied.
281
+ # context.count(true)
282
+ #
283
+ # @param [Boolean] extras True to inclued previous skip/limit
284
+ # statements in the count; false to ignore them. Defaults to `false`.
285
+ #
286
+ # @return [ Integer ] The count of documents.
287
+ def count(extras = false)
288
+ if extras
289
+ self.total
290
+ else
291
+ super()
292
+ end
293
+ end
294
+
295
+ def in_batches(limit=1000)
296
+ skip = 0
297
+ objects = self.limit(limit).skip(skip*limit)
298
+ while objects.count(true) > 0
299
+ yield objects
300
+ skip+=1
301
+ objects = self.limit(limit).skip(skip*limit)
302
+ end
303
+ end
304
+
275
305
  def method_missing(name, *args, &block)
276
306
  if klass.respond_to?(name)
277
307
  klass.send(:with_scope, self) do
@@ -1,4 +1,9 @@
1
1
  require 'active_support/concern'
2
+ require 'active_model/callbacks'
3
+ require 'active_model/observing'
4
+ require 'ripple/conflict/document_hooks'
5
+ require 'ripple/document'
6
+ require 'ripple/observable'
2
7
 
3
8
  module Ripple
4
9
  module Searchable
@@ -6,7 +11,12 @@ module Ripple
6
11
  extend ActiveSupport::Concern
7
12
 
8
13
  included do
9
- extend ClassMethods
14
+ Ripple::SearchableObserver.observed_classes << self
15
+ begin
16
+ Ripple::SearchableObserver.instance
17
+ rescue Exception
18
+ # module not extended yet
19
+ end
10
20
  end
11
21
 
12
22
  unless method_defined? :id
@@ -29,6 +39,15 @@ module Ripple
29
39
  def criteria
30
40
  @criteria = default_scoping.try(:call) || Criteria.new(self)
31
41
  end
42
+
43
+ def index_after_save!
44
+ class_eval do
45
+ after_save do |m|
46
+ Ripple.client.index(m.class.bucket_name, m.attributes.reject {|k,v| v.nil?}.merge(id: m.id))
47
+ end
48
+ end
49
+ end
50
+
32
51
  end
33
52
 
34
53
  end
@@ -0,0 +1,18 @@
1
+ require 'active_support/concern'
2
+ require 'active_model/observing'
3
+ require 'ripple/document'
4
+ require 'ripple/observable'
5
+
6
+ module Ripple
7
+ class SearchableObserver < ActiveModel::Observer
8
+
9
+ def self.observed_classes
10
+ @observed_classes ||= []
11
+ end
12
+
13
+ def after_save(m)
14
+ Ripple.client.index(m.class.bucket_name, m.attributes.reject {|k,v| v.nil?}.merge(id: m.id))
15
+ end
16
+ end
17
+
18
+ end
@@ -1,3 +1,3 @@
1
1
  module RippleSearchable
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ripple_searchable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-30 00:00:00.000000000 Z
12
+ date: 2012-12-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -104,6 +104,7 @@ files:
104
104
  - lib/ripple_searchable/loggable.rb
105
105
  - lib/ripple_searchable/scoping.rb
106
106
  - lib/ripple_searchable/searchable.rb
107
+ - lib/ripple_searchable/searchable_observer.rb
107
108
  - lib/ripple_searchable/version.rb
108
109
  - ripple_searchable.gemspec
109
110
  homepage: ''
@@ -131,4 +132,3 @@ signing_key:
131
132
  specification_version: 3
132
133
  summary: Mongoid / Active Record style query criteria and scoping for Ripple
133
134
  test_files: []
134
- has_rdoc: