garland 0.11 → 0.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/garland_rails.rb +39 -5
  3. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2683e3c450c0889b0af41f66a2fcdbef0e8e8caa
4
- data.tar.gz: e52fac122859a535f1903c7c58e8ed5a2e0c8855
3
+ metadata.gz: 633f8442d2f0646ca3e812f01370fd4ebbcc3f93
4
+ data.tar.gz: 78d637e6b0ea8a790a7cb8ec446acb861938a625
5
5
  SHA512:
6
- metadata.gz: b285119071ce1823eebeb8c1a70968491614f1bc65734acf6a8ecf2a41101673f6210ad6aa8a9ecf888b703ecd16c8a573c7374a67fb3d1ec0836fa5465bd0f9
7
- data.tar.gz: 3ba6409d950c83a7a3513fa9eae2e5fc80c6943a0ddc8d111ca28a69b954257db0b1559824edf654ebde43c2c0a96a8c3768585b176ba0c114abf645ab015e1c
6
+ metadata.gz: 321978763b6b8bed65d58cab3b24c855234cc85f2991107f8bd82fe2cf121e0f669c042fa51d061c11719e26d0a242b5eff2214362821151b5cd3884c28b883c
7
+ data.tar.gz: 5981c569dab5ab5467ce1593e22758b166706541804ca3da3d6623326efead50651d612655ab8eafdd55cc8916f136200ac7371789ba7eac658fc16fb888de35
data/lib/garland_rails.rb CHANGED
@@ -2,6 +2,28 @@ module GarlandRails
2
2
  DIFF = true
3
3
  SNAPSHOT = false
4
4
 
5
+ module Extend
6
+ def self.included(base)
7
+ base.extend(self)
8
+ end
9
+
10
+ def has_many(name, scope = nil, options = {}, &extension)
11
+ name_superclass = name.to_s.classify.constantize.superclass
12
+ if name_superclass == GarlandRails::Base
13
+ # if there are no scope and there are some options,
14
+ # scope will contain options, and we need to swap them
15
+ if scope.class == Hash
16
+ options = scope
17
+ scope = nil
18
+ end
19
+
20
+ options = options.merge({ as: :belongs_to })
21
+ end
22
+
23
+ super(name, scope, options, &extension)
24
+ end
25
+ end
26
+
5
27
  class Base < Garland
6
28
  self.table_name = "garlands"
7
29
 
@@ -16,10 +38,16 @@ module GarlandRails
16
38
  :next,
17
39
  ], conditions: -> { where("next IS NULL OR previous IS NULL") }
18
40
 
19
- def self.belongs_to(type)
20
- super type, foreign_key: "belongs_to_id"
21
- validates type, presence: true
22
- validates_inclusion_of :belongs_to_type, in: ["#{type.capitalize}"]
41
+ # everything seems to work without `polymorphic: true`,
42
+ # but we will set it just for accordance to docs
43
+ def self.belongs_to(name, scope = nil, options = {}, &extension)
44
+ if scope.class == Hash
45
+ options = scope
46
+ scope = nil
47
+ end
48
+ options = options.merge({ polymorphic: true })
49
+
50
+ super(name, scope, options, &extension)
23
51
  end
24
52
 
25
53
  def self.push(args)
@@ -106,13 +134,14 @@ module GarlandRails
106
134
  # third+: diffs
107
135
  unless brand_new_tail.save
108
136
  Rails.logger.error("Unable to create new tail with props '#{tail_props}'")
137
+ ActiveRecord::Base.connection.release_savepoint("savepoint_before_init")
109
138
  return nil
110
139
  end
111
140
 
112
141
  # belongs_to validations were in `brand_new_tail.save`
113
142
  # here and below validations may be skipped as long as we check for continuity later
114
- first_diff.save(validate: false)
115
143
  brand_new_head.save(validate: false)
144
+ first_diff.save(validate: false)
116
145
  brand_new_tail.update_attribute(:next, first_diff.id)
117
146
  first_diff.update_attribute(:previous, brand_new_tail.id)
118
147
  first_diff.update_attribute(:next, brand_new_head.id)
@@ -121,6 +150,7 @@ module GarlandRails
121
150
  unless self.continuous?(belongs_to)
122
151
  Rails.logger.error("Initialized garland is not continuous")
123
152
  ActiveRecord::Base.connection.exec_rollback_to_savepoint("savepoint_before_init")
153
+ ActiveRecord::Base.connection.release_savepoint("savepoint_before_init")
124
154
  return nil
125
155
  end
126
156
  end
@@ -151,6 +181,7 @@ module GarlandRails
151
181
  # because we don't want to check for continuity on every push
152
182
  unless new_diff.save
153
183
  Rails.logger.error("Unable to create new_diff with props '#{new_diff_props}'")
184
+ ActiveRecord::Base.connection.release_savepoint("savepoint_before_init")
154
185
  return nil
155
186
  end
156
187
 
@@ -158,6 +189,7 @@ module GarlandRails
158
189
  unless last_diff.save
159
190
  Rails.logger.error("Unable to save last_diff with 'next' = '#{new_diff.id}'")
160
191
  ActiveRecord::Base.connection.exec_rollback_to_savepoint("savepoint_before_insert_diff")
192
+ ActiveRecord::Base.connection.release_savepoint("savepoint_before_init")
161
193
  return nil
162
194
  end
163
195
 
@@ -165,6 +197,7 @@ module GarlandRails
165
197
  unless head.save
166
198
  Rails.logger.error("Unable to save head with 'previous' = '#{new_diff.id}'")
167
199
  ActiveRecord::Base.connection.exec_rollback_to_savepoint("savepoint_before_insert_diff")
200
+ ActiveRecord::Base.connection.release_savepoint("savepoint_before_init")
168
201
  return nil
169
202
  end
170
203
 
@@ -172,6 +205,7 @@ module GarlandRails
172
205
  unless head.save
173
206
  Rails.logger.error("Unable to save head with 'entity' = '#{hash.to_s}'")
174
207
  ActiveRecord::Base.connection.exec_rollback_to_savepoint("savepoint_before_insert_diff")
208
+ ActiveRecord::Base.connection.release_savepoint("savepoint_before_init")
175
209
  return nil
176
210
  end
177
211
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: garland
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.11'
4
+ version: '0.12'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Morozov
@@ -24,8 +24,8 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- description: Provides GarlandRails::Base class for ActiveRecord, which allows to save
28
- Hashes using snapshots and diffs.
27
+ description: Provides GarlandRails::Base class for ActiveRecord, which allows you
28
+ to save Hashes using snapshots and diffs (in short, it's HashDiff Rails storage).
29
29
  email: ntcomp12@gmail.com
30
30
  executables: []
31
31
  extensions: []