Module: Quiver::Adapter::ActiveRecord
- Includes:
- HelpersHelpers
- Defined in:
- lib/quiver/adapter/active_record_helpers.rb,
lib/quiver/adapter/arec_low_level_creator.rb,
lib/quiver/adapter/arec_low_level_deleter.rb,
lib/quiver/adapter/arec_low_level_updater.rb
Defined Under Namespace
Modules: ClassMethods
Classes: ARecLowLevelCreator, ARecLowLevelDeleter, ARecLowLevelUpdater
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.included(host) ⇒ Object
12
13
14
15
16
17
|
# File 'lib/quiver/adapter/active_record_helpers.rb', line 12
def self.included(host)
super
host.adapter_type(:active_record)
host.send(:extend, ClassMethods)
end
|
Instance Method Details
#count ⇒ Object
65
66
67
68
69
|
# File 'lib/quiver/adapter/active_record_helpers.rb', line 65
def count
count = default_record_class.count
Quiver::Adapter::AdapterResult.new(count)
end
|
#create(attributes, transaction) ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/quiver/adapter/active_record_helpers.rb', line 71
def create(attributes, transaction)
persister = ARecLowLevelCreator.new(self.class, attributes)
if attributes[:__type__]
attributes = attributes.merge(
attributes[:__type__][:name] => attributes[:__type__][:value]
)
attributes.delete(:__type__)
end
begin
create_mappings(attributes, persister)
rescue ::ActiveRecord::ActiveRecordError
transaction.rollback!
persister.failed!
end
Quiver::Adapter::AdapterResult.new do |errors|
if persister.success?
persister.result
else
errors << Quiver::Error.new('record', 'not_persisted')
nil
end
end
end
|
#find(primary_key) ⇒ Object
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/quiver/adapter/active_record_helpers.rb', line 54
def find(primary_key)
Quiver::Adapter::AdapterResult.new do |errors|
if record = base_query.find_by(primary_key_name => primary_key)
load_additional([record.attributes.symbolize_keys]).first
else
errors << Quiver::Mapper::NotFoundError.new('record', 'not_found')
nil
end
end
end
|
#hard_delete(attributes, transaction) ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/quiver/adapter/active_record_helpers.rb', line 127
def hard_delete(attributes, transaction)
unpersister = ARecLowLevelDeleter.new(self.class, attributes)
if attributes[:__type__]
attributes = attributes.merge(
attributes[:__type__][:name] => attributes[:__type__][:value]
)
attributes.delete(:__type__)
end
begin
delete_mappings(attributes, unpersister)
rescue ::ActiveRecord::ActiveRecordError
transaction.rollback!
unpersister.failed!
end
Quiver::Adapter::AdapterResult.new do |errors|
if unpersister.success?
{}
else
errors << Quiver::Error.new('record', 'not_deleted')
nil
end
end
end
|
#query(q = {}) ⇒ Object
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/quiver/adapter/active_record_helpers.rb', line 155
def query(q={})
filter_params = q[:filter] || {}
sort_params = q[:sort] || {}
= q[:page] || {}
query = filter_klass.new(
base_query,
filter_params
).filter
count_query = filter_klass.new(
base_count_query,
filter_params
).filter
sort_params.each do |attr, asc|
order = asc ? 'ASC' : 'DESC'
query = query.order("#{attr} #{order}")
count_query = count_query.order("#{attr} #{order}")
end
total_count = count_query.count
if ['limit'] && ['limit'] != -1
query = query.limit(['limit'])
end
if ['offset']
query = query.offset(['offset'])
end
objects = load_additional(query.map(&:attributes).map(&:symbolize_keys))
result = Quiver::Adapter::AdapterResult.new(objects)
if .any?
result.data[:pagination_offset] = ['offset'] || 0
result.data[:pagination_limit] = ['limit'] || -1
result.data[:total_count] = total_count
end
result
end
|
#update(attributes, transaction) ⇒ Object
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/quiver/adapter/active_record_helpers.rb', line 99
def update(attributes, transaction)
persister = ARecLowLevelUpdater.new(self.class, attributes)
if attributes[:__type__]
attributes = attributes.merge(
attributes[:__type__][:name] => attributes[:__type__][:value]
)
attributes.delete(:__type__)
end
begin
update_mappings(attributes, persister)
rescue ::ActiveRecord::ActiveRecordError
transaction.rollback!
persister.failed!
end
Quiver::Adapter::AdapterResult.new do |errors|
if persister.success?
persister.result
else
errors << Quiver::Error.new('record', 'not_updated')
nil
end
end
end
|