hoodoo 1.2.2 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZDIzZWU2OWE2MmEyY2RiZjRjMDk1OTlmZGFhM2YyNzA3ZTE0NDQ5MA==
4
+ MDRkMTlhM2I2NjQyNWE2OTQxZDA3MjdiMjQ2MjgzMzVkYjhkMWEwOQ==
5
5
  data.tar.gz: !binary |-
6
- MDc4NDgwNjM1MDcwZmE3OWE0NTAzYjExN2Y0MzMxYmJhYzQyZmI0OQ==
6
+ NjMxZTIwOTI0OGFiMzgxMDEyYjRmY2JjM2MwODI3MDJmOTNjMjgyNQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- M2IyM2U5ZGI3NjBiMzRhYjUyOTBhZDk5ODE4YmY2N2RlOGUyNTA0ODgxODdk
10
- N2Y2OGYxNTE0NjYwOWRiMmZkOGJkMTUyMzQ2Yzk1YmE0ZGY0ZTE2ODkyN2Nj
11
- MDg3NjQwZjI0NTUxODRhODIzNTFhOTg3Yzg1MzFiYzk0OWYwNTE=
9
+ YzdiYTY3MTIyY2VkMWZjZjI5N2RmYjBkY2M1NTYyZWVjZjFiOWM0NDU4YjVk
10
+ MGM4MGI1MWFlYzNhNjg0MzNlYmU0YzYwMzZhMDViNDgwYmVjN2ExZWY3MjJi
11
+ NGQ0NDRmOThhZjE4MWQwNmEyODk5NmYxNmE0NTkzMmY2YTliMjE=
12
12
  data.tar.gz: !binary |-
13
- MGU2ZDA2Yzg3NmRmNGE3MTRiMzczOWU0N2JmZDc0ZmFmNmJlNTc5NGE3ZWJl
14
- YmY2Mjk1YWZkNzY2NjEwNjIwYTFiYzhkM2Y0MTExZmVjMmRmOWIxNDRkOGY5
15
- OWE3MjZkODUwYjEzZjM4MzU3MWQ1MDNjYTk5ZmNiMTA4YjE2MzI=
13
+ NGM2NmNlMmQ2ZGYwZmVhMmEwZWMyYjU1NjJmZTBhYTU1M2UzNzMyMjRlZjRj
14
+ ZTAzNzM5MDI0MmRlMmU4OWU4MzBlZWUxNTRiMGU4MzE4YWI4YTE3M2M1MGMy
15
+ ZjI1MWY1YWM5NmQwNWYyYWZhNTk0MTA1OGU4NWVkN2Y4YTUzNDE=
@@ -30,7 +30,7 @@ module Hoodoo
30
30
  #
31
31
  # ...to create model instances and participate "for free" in whatever
32
32
  # plug-in ActiveRecord modules are mixed into the model classes, such as
33
- # Hoodoo::ActiveRecord::Dated.
33
+ # Hoodoo::ActiveRecord::Dated and Hoodoo::ActiveRecord::ManuallyDated.
34
34
  #
35
35
  # See also:
36
36
  #
@@ -117,12 +117,13 @@ module Hoodoo
117
117
 
118
118
  # TODO: Refactor this to use the scope chain plugin approach in due
119
119
  # course, but for now, pragmatic implementation does the only
120
- # thing we currently need to do - set "created_at".
120
+ # things we currently require - set "created_at"/"updated_at".
121
121
  #
122
- if self.include?( Hoodoo::ActiveRecord::Dated )
123
- unless context.request.dated_from.nil?
124
- instance.created_at = instance.updated_at = context.request.dated_from
125
- end
122
+ unless context.request.dated_from.nil?
123
+ instance.created_at = instance.updated_at = context.request.dated_from if (
124
+ ( self.include?( Hoodoo::ActiveRecord::Dated ) && self.dating_enabled?() ) ||
125
+ ( self.include?( Hoodoo::ActiveRecord::ManuallyDated ) && self.manual_dating_enabled?() )
126
+ )
126
127
  end
127
128
 
128
129
  return instance
@@ -97,14 +97,14 @@ module Hoodoo
97
97
  #
98
98
  # == Model instance creation
99
99
  #
100
- # It is _VERY_ _IMPORTANT_ that you use
101
- # Hoodoo::ActiveRecord::Creator::ClassMethods::new_in to create new
102
- # resource instances when using Dating. You _could_ just manually read the
100
+ # It is _VERY_ _IMPORTANT_ that you use method
101
+ # Hoodoo::ActiveRecord::Creator::ClassMethods.new_in to create new
102
+ # resource instances when using dating. You _could_ just manually read the
103
103
  # `context.request.dated_from` value to ensure that an appropriate creation
104
104
  # time is set; presently, `created_at` and `updated_at` are set from the
105
105
  # `dated_from` value. However, using `new_in` for this isolates your code
106
106
  # from any possible under-the-hood implementation changes therein and
107
- # future-proof your code.
107
+ # future-proofs your code.
108
108
  #
109
109
  module Dated
110
110
 
@@ -183,7 +183,7 @@ module Hoodoo
183
183
  # returns +true+, else +false+.
184
184
  #
185
185
  def dating_enabled?
186
- return self.dated_with() != nil?
186
+ return self.dated_with().present?
187
187
  end
188
188
 
189
189
  # Return an ActiveRecord::Relation containing the model instances which
@@ -109,6 +109,8 @@ module Hoodoo
109
109
  # you *MUST* include the ActiveRecord::Relation instances (scopes) inside
110
110
  # any query chain used to read or write data.
111
111
  #
112
+ # === Show and List
113
+ #
112
114
  # You might use Hoodoo::ActiveRecord::Finder#list_in or
113
115
  # Hoodoo::ActiveRecord::Finder#acquire_in for +list+ or +show+ actions;
114
116
  # such code changes from e.g.:
@@ -119,7 +121,20 @@ module Hoodoo
119
121
  #
120
122
  # SomeModel.manually_dated( context ).list_in( context )
121
123
  #
122
- # You MUST NOT update or delete records using conventional ActiveRecord
124
+ # === Create
125
+ #
126
+ # As with automatic dating - see Hoodoo::ActiveRecord::Dated - you should
127
+ # use method Hoodoo::ActiveRecord::Creator::ClassMethods.new_in to create
128
+ # new resource instances, to help ensure correct initial date setup and to
129
+ # help isolate your code from future functionality extensions/changes. An
130
+ # ActiveRecord +before_create+ filter deals with some of the "behind the
131
+ # scenes" maintenance but the initial acquisition of dating information
132
+ # from the prevailing request context only happens for you if you use
133
+ # Hoodoo::ActiveRecord::Creator::ClassMethods::new_in.
134
+ #
135
+ # === Update and Delete
136
+ #
137
+ # You *MUST* *NOT* update or delete records using conventional ActiveRecord
123
138
  # methods if you want to use manual dating to record state changes.
124
139
  # Instead, use
125
140
  # Hoodoo::ActiveRecord::ManuallyDated::ClassMethods#manually_dated_update_in
@@ -143,6 +158,8 @@ module Hoodoo
143
158
  # information on overriding the identifier used to find the target record
144
159
  # and the attribute data used for updates.
145
160
  #
161
+ # == Rendering
162
+ #
146
163
  # When rendering, you *MUST* remember to set the resource's +id+ field
147
164
  # from the model's +uuid+ field:
148
165
  #
@@ -155,8 +172,20 @@ module Hoodoo
155
172
  # }
156
173
  # )
157
174
  #
158
- # Likewise, remember to set foreign keys for any relational declarations
159
- # via the +uuid+ column - e.g. go from this:
175
+ # == Associations
176
+ #
177
+ # Generally, use of ActiveRecord associations is minimal in most services
178
+ # because there is an implied database-level coupling of resources and a
179
+ # temptation to use cross-table ActiveRecord mechanisms for things like
180
+ # relational UUID integrity checks, rather than inter-resource calls.
181
+ # Doing so couples resources together at the database rather than keeping
182
+ # them isolated purely by API, which is often a really bad idea. It is,
183
+ # however, sometimes necessary for best possible performance, or sometimes
184
+ # one complex resource may be represented by several models with
185
+ # relationships between them.
186
+ #
187
+ # In such cases, remember to set foreign keys for relational declarations
188
+ # to a manually dated table via the +uuid+ column - e.g. go from this:
160
189
  #
161
190
  # member.account_id = account.id
162
191
  #
@@ -408,10 +437,6 @@ module Hoodoo
408
437
  def manual_dating_enabled
409
438
  self.nz_co_loyalty_hoodoo_manually_dated = true
410
439
 
411
- rounder = Proc.new do | timelike |
412
- timelike.to_time.round( SECONDS_DECIMAL_PLACES )
413
- end
414
-
415
440
  # This is the 'tightest'/innermost callback available for creation.
416
441
  # Intentionally have nothing for updates/deletes as the high level
417
442
  # API here must be used; we don't want to introduce any more magic.
@@ -12,6 +12,6 @@ module Hoodoo
12
12
  # The Hoodoo gem version. If this changes, ensure that the date in
13
13
  # "hoodoo.gemspec" is correct and run "bundle install" (or "update").
14
14
  #
15
- VERSION = '1.2.2'
15
+ VERSION = '1.2.3'
16
16
 
17
17
  end
@@ -15,6 +15,7 @@ describe Hoodoo::ActiveRecord::Creator do
15
15
  class RSpecModelCreatorTest < ActiveRecord::Base
16
16
  include Hoodoo::ActiveRecord::Creator
17
17
  include Hoodoo::ActiveRecord::Dated
18
+ include Hoodoo::ActiveRecord::ManuallyDated
18
19
  end
19
20
  end
20
21
 
@@ -45,14 +46,57 @@ describe Hoodoo::ActiveRecord::Creator do
45
46
  expect( instance.updated_at ).to be_nil
46
47
  end
47
48
 
48
- it 'creates with specified dated-from value' do
49
- time = Time.now.iso8601
50
- @context.request.dated_from = time
49
+ context 'with "dated_from" given' do
50
+ before :each do
51
+ @time = Time.now
52
+ @context.request.dated_from = @time
53
+ end
51
54
 
52
- instance = RSpecModelCreatorTest.new_in( @context )
55
+ shared_examples 'a dated model' do
56
+ it 'using the specified dated-from value' do
57
+ instance = RSpecModelCreatorTest.new_in( @context )
58
+
59
+ expect( instance.created_at ).to eq( @time )
60
+ expect( instance.updated_at ).to eq( @time )
61
+ end
62
+ end
63
+
64
+ shared_examples 'a normal model' do
65
+ it 'creating with no default timestamps' do
66
+ instance = RSpecModelCreatorTest.new_in( @context )
67
+
68
+ expect( instance.created_at ).to eq( nil )
69
+ expect( instance.updated_at ).to eq( nil )
70
+ end
71
+ end
53
72
 
54
- expect( instance.created_at.iso8601 ).to eq( time )
55
- expect( instance.updated_at.iso8601 ).to eq( time )
73
+ context 'automatic dating present' do
74
+ context 'and enabled it' do
75
+ before :each do
76
+ expect( RSpecModelCreatorTest ).to receive( :dating_enabled? ).once.and_return( true )
77
+ end
78
+
79
+ it_behaves_like 'a dated model'
80
+ end
81
+
82
+ context 'and not enabled it' do
83
+ it_behaves_like 'a normal model'
84
+ end
85
+ end
86
+
87
+ context 'manual dating present' do
88
+ context 'and enabled it' do
89
+ before :each do
90
+ expect( RSpecModelCreatorTest ).to receive( :manual_dating_enabled? ).once.and_return( true )
91
+ end
92
+
93
+ it_behaves_like 'a dated model'
94
+ end
95
+
96
+ context 'and not enabled it' do
97
+ it_behaves_like 'a normal model'
98
+ end
99
+ end
56
100
  end
57
101
 
58
102
  it 'creates with provided attributes' do
@@ -124,9 +124,15 @@ describe Hoodoo::ActiveRecord::Dated do
124
124
  end
125
125
 
126
126
  context '#dating_enabled?' do
127
+ class RSpecNotDated < Hoodoo::ActiveRecord::Base; end
128
+
127
129
  it 'says it is automatically dated' do
128
130
  expect( model_klass.dating_enabled? ).to eq( true )
129
131
  end
132
+
133
+ it 'knows when something is not automatically dated' do
134
+ expect( RSpecNotDated.dating_enabled? ).to eq( false )
135
+ end
130
136
  end
131
137
 
132
138
  context '#dated_at' do
@@ -139,9 +139,15 @@ describe Hoodoo::ActiveRecord::ManuallyDated do
139
139
  end
140
140
 
141
141
  context '#manual_dating_enabled?' do
142
+ class RSpecNotManuallyDated < Hoodoo::ActiveRecord::Base; end
143
+
142
144
  it 'says it is manually dated' do
143
145
  expect( RSpecModelManualDateTest.manual_dating_enabled? ).to eq( true )
144
146
  end
147
+
148
+ it 'knows when something is not automatically dated' do
149
+ expect( RSpecNotManuallyDated.manual_dating_enabled? ).to eq( false )
150
+ end
145
151
  end
146
152
 
147
153
  context '#manually_dated_at' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hoodoo
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Loyalty New Zealand
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-15 00:00:00.000000000 Z
11
+ date: 2016-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: kgio