delay_henka 0.2.7 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3b31b86cd41356b8d1c04f5b1083b323db289be8
4
- data.tar.gz: 76eabe3da784f9d0e99760f1f587823cb4e7340c
3
+ metadata.gz: a39adac1df7c9b05c934d9c2f0853f779d9791e5
4
+ data.tar.gz: 9ed97b2a9c49a3cbb8fd4ad1f3cffa402b41ef66
5
5
  SHA512:
6
- metadata.gz: 555290369edf790469236db7ab45afdea3ef5cb30a275df226a83b4f6b9aea25f773c0e7958a991d0047a647e75abf9cb977197f5dc3bf280e8bf0b33c814cce
7
- data.tar.gz: c4c499d7ed6e524c43366935f37e73f9bbce5608eaf5ab050b038b490fe0561bf591b236661763867c87f2fa610e381d77f7b2904d68b9af14bdfd39e73365b6
6
+ metadata.gz: f83331cb2a3b624e0c57d3c0548be8ad6db6db5fed2dc7577c1dc2247222c83bf6a256611c5df89157cbf0002f02e09345d70c70d22582f5ff84ac99b581152a
7
+ data.tar.gz: e7ed972193881fb57fa415e74b65712165fefac008e8b1d8cddf2cda5db5844a8959ea0a42a01f72683d5942be30f368466310d8d475c141a3e648a700fab467
data/README.md CHANGED
@@ -32,11 +32,17 @@ end
32
32
  # In your controller (or factory service),
33
33
  class SomeController
34
34
  def update
35
- product = SomeModel.find(params[:id])
35
+ @product = SomeModel.find(params[:id])
36
36
  delayed_changes = some_params.extract!(:discount_pct, :price)
37
- if product.update some_params.except(:discount_pct, :price)
38
- DelayHenka::ScheduledChange.schedule(record: product, changes: delayed_changes, by_id: current_user.id)
39
- redirect_to #..., success
37
+ if @product.update some_params.except(:discount_pct, :price)
38
+ result = DelayHenka::ScheduledChange.schedule(record: @product, changes: delayed_changes, by_id: current_user.id)
39
+ if result.ok? # returns a Keka object
40
+ redirect_to #..., success
41
+ else
42
+ # no change is scheduled. display error and have user try again.
43
+ flash.now[:error] = result.msg
44
+ render :edit
45
+ end
40
46
  else
41
47
  render :edit
42
48
  end
@@ -18,12 +18,31 @@ module DelayHenka
18
18
  scope :staged, -> { where(state: STATES[:STAGED]) }
19
19
 
20
20
  def self.schedule(record:, changes:, by_id:, schedule_at: Time.current)
21
- changes.each do |attribute, new_val|
22
- old_val = record.public_send(attribute)
23
- cleaned_new_val = cleanup_val(new_val)
24
- record.public_send("#{attribute}=", cleaned_new_val)
25
- next unless record.public_send("#{attribute}_changed?")
26
- create!(changeable: record, submitted_by_id: by_id, attribute_name: attribute, old_value: old_val, new_value: cleaned_new_val, schedule_at: schedule_at)
21
+ Keka.run do
22
+ service = WhetherSchedule.new(record)
23
+ new_changes = changes.each_with_object([]) do |(attribute, new_val), accum|
24
+ old_val = record.public_send(attribute)
25
+ cleaned_new_val = cleanup_val(new_val)
26
+ decision = service.make_decision(attribute, cleaned_new_val)
27
+ if decision.ok?
28
+ accum << new(
29
+ changeable: record,
30
+ submitted_by_id: by_id,
31
+ attribute_name: attribute,
32
+ old_value: old_val,
33
+ new_value: cleaned_new_val,
34
+ schedule_at: schedule_at
35
+ )
36
+ elsif decision.msg
37
+ return decision # error present
38
+ else
39
+ # otherwise do nothing - maybe no change is made
40
+ end
41
+ end
42
+
43
+ transaction do
44
+ new_changes.each(&:save!)
45
+ end
27
46
  end
28
47
  end
29
48
 
@@ -47,7 +66,8 @@ module DelayHenka
47
66
 
48
67
  def self.cleanup_val(val)
49
68
  return val unless val.respond_to?(:strip)
50
- val.strip == '' ? nil : val
69
+ stripped = val.strip
70
+ stripped == '' ? nil : stripped
51
71
  end
52
72
 
53
73
  def set_initial_state
@@ -0,0 +1,24 @@
1
+ require 'pry'
2
+ module DelayHenka
3
+ class WhetherSchedule
4
+
5
+ def initialize(record)
6
+ @record = record.dup
7
+ end
8
+
9
+ def make_decision(attribute, new_val)
10
+ Keka.run do
11
+ old_val = record.public_send(attribute)
12
+ record.public_send("#{attribute}=", new_val)
13
+ # can't use xxx_changed? because #dup gives us a new instance
14
+ Keka.err_if! old_val == record.public_send(attribute)
15
+ Keka.ok_if! record.valid?
16
+ Keka.err_if! true, record.errors.full_messages.join(', ')
17
+ end
18
+ end
19
+
20
+ private
21
+ attr_reader :record
22
+
23
+ end
24
+ end
@@ -2,6 +2,9 @@ module DelayHenka
2
2
  class Engine < ::Rails::Engine
3
3
  isolate_namespace DelayHenka
4
4
 
5
+ # load service directory
6
+ Dir[File.expand_path('../../../app/services/delay_henka/*.rb', __FILE__)].each { |f| require f }
7
+
5
8
  config.generators do |g|
6
9
  g.test_framework :rspec
7
10
  end
@@ -1,3 +1,3 @@
1
1
  module DelayHenka
2
- VERSION = '0.2.7'
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: delay_henka
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - zino
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-13 00:00:00.000000000 Z
11
+ date: 2019-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - '='
53
53
  - !ruby/object:Gem::Version
54
54
  version: 5.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: keka
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: pg
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +80,20 @@ dependencies:
66
80
  - - ">="
67
81
  - !ruby/object:Gem::Version
68
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
69
97
  - !ruby/object:Gem::Dependency
70
98
  name: rspec-rails
71
99
  requirement: !ruby/object:Gem::Requirement
@@ -98,6 +126,7 @@ files:
98
126
  - app/helpers/delay_henka/application_helper.rb
99
127
  - app/models/delay_henka/application_record.rb
100
128
  - app/models/delay_henka/scheduled_change.rb
129
+ - app/services/delay_henka/whether_schedule.rb
101
130
  - app/views/delay_henka/web/admin/scheduled_changes/_summary_table.html.haml
102
131
  - app/views/delay_henka/web/admin/scheduled_changes/index.html.haml
103
132
  - app/views/layouts/delay_henka/application.html.erb