This repository has been archived by the owner on Jun 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 533
/
persistence.rb
72 lines (57 loc) · 2.09 KB
/
persistence.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
module Tire
module Model
# Allows to use _Elasticsearch_ as a primary database (storage).
#
# Contains all the `Tire::Model::Search` features and provides
# an [_ActiveModel_](http://rubygems.org/gems/activemodel)-compatible
# interface for persistance.
#
# Usage:
#
# class Article
# include Tire::Model::Persistence
#
# property :title
# end
#
# Article.create :id => 1, :title => 'One'
#
# article = Article.find
#
# article.destroy
#
module Persistence
def self.included(base)
base.class_eval do
include ActiveModel::AttributeMethods
include ActiveModel::Validations
include ActiveModel::Serialization
include ActiveModel::Serializers::JSON
include ActiveModel::Naming
include ActiveModel::Conversion
extend ActiveModel::Callbacks
define_model_callbacks :save, :destroy
extend Persistence::Finders::ClassMethods
extend Persistence::Attributes::ClassMethods
include Persistence::Attributes::InstanceMethods
include Persistence::Storage
include Tire::Model::Search
['_score', '_type', '_index', '_version', 'sort', 'highlight', '_explanation'].each do |attr|
define_method("#{attr}=") { |value| @attributes ||= {}; @attributes[attr] = value }
define_method("#{attr}") { @attributes[attr] }
end
def self.search(*args, &block)
args.last.update(:wrapper => self, :version => true) if args.last.is_a? Hash
args << { :wrapper => self, :version => true } unless args.any? { |a| a.is_a? Hash }
self.tire.search(*args, &block)
end
def self.multi_search(*args, &block)
args.last.update(:wrapper => self, :version => true) if args.last.is_a? Hash
args << { :wrapper => self, :version => true } unless args.any? { |a| a.is_a? Hash }
self.tire.multi_search(*args, &block)
end
end
end
end
end
end