sequence_generator 0.1.27 → 0.1.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 27acd4ab2ab8c4e4b6a073728a71ff8b4763d2b4dd39fad6183f60e715501e76
4
- data.tar.gz: 24e218ecd1e75c3591fc03bc44cb15c02dca0092ebf2305a96313aa9997b9092
3
+ metadata.gz: ab3389b33c8f915726b3311fea9818372344b63a84ff003cf0dd308a35accee9
4
+ data.tar.gz: 3888827dccfdac538a355f1f2e03479cd03e87f4c65725df14127f6f042b21f4
5
5
  SHA512:
6
- metadata.gz: 0dcc1f9677584e7d819d9a16825396cb00406fa4c96669d15e221ee5956de989defe4504d743b03e131839be4d74b5d19af7fdd28a21221edc8569afb8c20364
7
- data.tar.gz: 1acd2835136cc9c5ccd6d2e4cbd0d7c81f518d98d5ddc152d148758092adb833d6b23dcc6825765630330eb11822d3375f4b0108bfd492cd0ef47f521bd76128
6
+ metadata.gz: e0869711f75c25f20769b82994b9625e798b09b7faf29f08a6626a3bb423f37489e38a213b2a7057138845dfb7d887c55c38c56540a4bdad3f63c859526e90f7
7
+ data.tar.gz: 93464acc72e2a7bfcb7792a2e03a2d8bfb00eb82f30ac1b5c0884913684c8c2fef4ee189399c92220dc28f235dcb8a5422fa2f261130d94eac9415f8c3363037
@@ -27,8 +27,10 @@ module SequenceGenerator
27
27
  end
28
28
  options[:validation_options] ||= {on: :create}
29
29
  options[:validation_options][:on] ||= :create
30
- before_validation :set_sequential_ids, options[:validation_options]
31
30
  options = DEFAULT_OPTIONS.merge(options)
31
+ before_validation options[:validation_options] do
32
+ set_sequential_ids(options)
33
+ end
32
34
  column_name = options[:column]
33
35
  purpose = options[:purpose]
34
36
 
@@ -45,60 +47,56 @@ module SequenceGenerator
45
47
  end
46
48
 
47
49
  module InstanceMethods
48
- def set_sequential_ids
49
- self.class.sequenced_options.each do |options|
50
-
51
- return if send(options[:column]).present?
52
- prefix_id = options[:prefix_column] ? self[options[:prefix_column]] : self.sequence_generator_id
53
- if prefix_id.present?
54
- sequence = Sequence.find(prefix_id)
55
- if sequence.scope.to_s == send(options[:scope]).to_s
50
+ def set_sequential_ids(options)
51
+ return if send(options[:column]).present?
52
+ prefix_id = options[:prefix_column] ? self[options[:prefix_column]] : self.sequence_generator_id
53
+ if prefix_id.present?
54
+ sequence = Sequence.find(prefix_id)
55
+ if sequence.scope.to_s == send(options[:scope]).to_s
56
+ assign_attributes(options[:column]=> sequence.generate_sequence_number)
57
+ else
58
+ errors.add(:sequential_id, 'Sequence is not associated with your branch')
59
+ end
60
+ else
61
+ sequence = Sequence.where(purpose: options[:purpose], scope: send(options[:scope]))
62
+ .where('valid_from <= ? AND valid_till >= ?', Time.now, Time.now).first
63
+ unless self.as_json[options[:column]].present?
64
+ if sequence
56
65
  assign_attributes(options[:column]=> sequence.generate_sequence_number)
57
66
  else
58
- errors.add(:sequential_id, 'Sequence is not associated with your branch')
59
- end
60
- else
61
- sequence = Sequence.where(purpose: options[:purpose], scope: send(options[:scope]))
62
- .where('valid_from <= ? AND valid_till >= ?', Time.now, Time.now).first
63
- unless self.as_json[options[:column]].present?
64
- if sequence
65
- assign_attributes(options[:column]=> sequence.generate_sequence_number)
67
+ original_sequence = Sequence.where(purpose: options[:purpose], scope: send(options[:scope])).last
68
+ if original_sequence.nil?
69
+ errors.add(:sequential_id, 'Sequence is not created')
66
70
  else
67
- original_sequence = Sequence.where(purpose: options[:purpose], scope: send(options[:scope])).last
68
- if original_sequence.nil?
69
- errors.add(:sequential_id, 'Sequence is not created')
71
+ valid_from = original_sequence.valid_from
72
+ valid_till = original_sequence.valid_till
73
+ new_start_at = original_sequence.sequential_id
74
+ difference = (valid_till - valid_from).to_i
75
+ new_valid_from = Date.today
76
+ new_valid_till = new_valid_from + difference
77
+ if original_sequence.reset_from_next_year
78
+ sequence = Sequence.create!(original_sequence.as_json.except('id', 'start_at',
79
+ 'valid_from', 'valid_till',
80
+ 'sequential_id',
81
+ 'created_at', 'updated_at')
82
+ .merge!(start_at: 1,
83
+ valid_from: new_valid_from,
84
+ valid_till: new_valid_till,
85
+ sequential_id: 1))
70
86
  else
71
- valid_from = original_sequence.valid_from
72
- valid_till = original_sequence.valid_till
73
- new_start_at = original_sequence.sequential_id
74
- difference = (valid_till - valid_from).to_i
75
- new_valid_from = Date.today
76
- new_valid_till = new_valid_from + difference
77
- if original_sequence.reset_from_next_year
78
- sequence = Sequence.create!(original_sequence.as_json.except('id', 'start_at',
79
- 'valid_from', 'valid_till',
80
- 'sequential_id',
81
- 'created_at', 'updated_at')
82
- .merge!(start_at: 1,
83
- valid_from: new_valid_from,
84
- valid_till: new_valid_till,
85
- sequential_id: 1))
86
- else
87
- sequence = Sequence.create!(original_sequence.as_json.except('id', 'start_at',
88
- 'valid_from', 'valid_till',
89
- 'sequential_id',
90
- 'created_at', 'updated_at')
91
- .merge!(start_at: new_start_at,
92
- valid_from: new_valid_from,
93
- valid_till: new_valid_till,
94
- sequential_id: new_start_at))
95
- end
96
- assign_attributes(options[:column]=> sequence.generate_sequence_number)
87
+ sequence = Sequence.create!(original_sequence.as_json.except('id', 'start_at',
88
+ 'valid_from', 'valid_till',
89
+ 'sequential_id',
90
+ 'created_at', 'updated_at')
91
+ .merge!(start_at: new_start_at,
92
+ valid_from: new_valid_from,
93
+ valid_till: new_valid_till,
94
+ sequential_id: new_start_at))
97
95
  end
96
+ assign_attributes(options[:column]=> sequence.generate_sequence_number)
98
97
  end
99
98
  end
100
99
  end
101
-
102
100
  end
103
101
  end
104
102
  end
@@ -1,3 +1,3 @@
1
1
  module SequenceGenerator
2
- VERSION = '0.1.27'
2
+ VERSION = '0.1.28'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequence_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.27
4
+ version: 0.1.28
5
5
  platform: ruby
6
6
  authors:
7
7
  - vimox-shah
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-06 00:00:00.000000000 Z
11
+ date: 2018-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails