commons_yellowme 0.12.0 → 0.15.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
  SHA256:
3
- metadata.gz: 176fc50a7143d11e3ad6c7f2ec42c30cb3a08537ad93bb2c5c3274eaab29cf86
4
- data.tar.gz: 625ceba15ebb3fedae9c00fb6a2289952e83c21d43942d8e3c42f8c8897e21ad
3
+ metadata.gz: d79985617cba32b33138c8436f656da439a49a01edb041982449e0ac4d59cffc
4
+ data.tar.gz: 97b8f3428026c1e89eedad0c4d80ad626805098ec2bf260d73f47916e1a9cb3c
5
5
  SHA512:
6
- metadata.gz: b9b1f51d10f3ff7976ee0d78f829700345a3e0681bb72cc7ea0f127f6a11d749db565728208ced72a868a5caed7cda4e49f6fced625d254dad2554af2b9de664
7
- data.tar.gz: a6589d26c77d5c44575dcdc6df79ff774fdb09b5c2d2bec2909bf62c35aba4bbb10a1c2813821c1f3804b6a1cfe993135028ca0a2b5b9e39765b57847edf83fc
6
+ metadata.gz: 05621d09bb7a699c797f8edb87362a046769ca960855ded4f6323d6d61ae8c1b3e49ab34489e494ac8371387fa4bc02c3e02c7550ca5bea2b9e9f73300da25ad
7
+ data.tar.gz: b008246db762536c3dadd45db51023eb9db7edfe30acf69225db58b15529290a49e727585964fe23f7feb59a163b8bf24c5cbdbef09ac157643d1a6b9fa3278e
data/.gitignore CHANGED
@@ -9,6 +9,7 @@
9
9
  /test/tmp/
10
10
  /test/version_tmp/
11
11
  /tmp/
12
+ .vscode
12
13
 
13
14
  # Used by dotenv library to load environment variables.
14
15
  # .env
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- commons_yellowme (0.11.2)
4
+ commons_yellowme (0.13.0)
5
5
  active_model_serializers (~> 0.10.10)
6
6
  dry-validation
7
7
  json (~> 2.0)
@@ -78,7 +78,7 @@ GEM
78
78
  dry-equalizer (0.3.0)
79
79
  dry-inflector (0.2.0)
80
80
  dry-initializer (3.0.3)
81
- dry-logic (1.0.5)
81
+ dry-logic (1.0.6)
82
82
  concurrent-ruby (~> 1.0)
83
83
  dry-core (~> 0.2)
84
84
  dry-equalizer (~> 0.2)
@@ -90,7 +90,7 @@ GEM
90
90
  dry-initializer (~> 3.0)
91
91
  dry-logic (~> 1.0)
92
92
  dry-types (~> 1.2)
93
- dry-types (1.2.2)
93
+ dry-types (1.3.0)
94
94
  concurrent-ruby (~> 1.0)
95
95
  dry-container (~> 0.3)
96
96
  dry-core (~> 0.4, >= 0.4.4)
@@ -215,4 +215,4 @@ DEPENDENCIES
215
215
  sqlite3
216
216
 
217
217
  BUNDLED WITH
218
- 2.0.2
218
+ 2.1.4
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
21
 
22
22
 
23
- spec.add_dependency "rails", "~> 5.2"
23
+ spec.add_dependency "rails", "~> 6.0"
24
24
  spec.add_dependency 'json', '~> 2.0'
25
25
  spec.add_dependency "active_model_serializers", "~> 0.10.10"
26
26
  spec.add_dependency "strip_attributes"
@@ -24,7 +24,7 @@ require 'commons/services/concerns/callable'
24
24
 
25
25
  # models
26
26
  require 'commons/concerns/attributes/sex'
27
- require 'commons/concerns/extensions/deleted'
27
+ require 'commons/concerns/extensions/soft_deleted'
28
28
  require 'commons/concerns/guard/capitalizable'
29
29
  require 'commons/concerns/validations/undestroyable'
30
30
 
@@ -0,0 +1,34 @@
1
+ module Commons
2
+ module Concerns
3
+ module Extensions
4
+ module SoftDeleted
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ before_validation :check_not_deleted, on: [:update]
9
+
10
+ def deleted?
11
+ raise ActiveModel::MissingAttributeError unless has_required_fields?
12
+ self.deleted_at.present?
13
+ end
14
+
15
+ def self.default_scope
16
+ raise ActiveModel::MissingAttributeError unless has_attribute?(:deleted_at)
17
+ where(deleted_at: nil)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def check_not_deleted
24
+ raise ActiveModel::MissingAttributeError unless has_required_fields?
25
+ raise ActiveRecord::RecordInvalid if self.deleted_at_in_database.present?
26
+ end
27
+
28
+ def has_required_fields?
29
+ self.has_attribute?(:deleted_at)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -4,8 +4,8 @@ module Commons
4
4
  PROPER_NOUN = /\A\p{L}[\p{L}'\.\-]*( [\p{L}'\.\-]+)*\z/u
5
5
  CURP = /\A([A-Z][AEIOUX][A-Z]{2}\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])[HM](?:AS|B[CS]|C[CLMSH]|D[FG]|G[TR]|HG|JC|M[CNS]|N[ETL]|OC|PL|Q[TR]|S[PLR]|T[CSL]|VZ|YN|ZS)[B-DF-HJ-NP-TV-Z]{3}[A-Z\d])(\d)\z/
6
6
  RFC = /\A([A-ZÑ\x26]{3,4}(\d{2})(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[0-1])([A-Z0-9]){2}([A0-9]))?\z/i
7
- ELECTOR_KEY = /\A[A-Z]{6}\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])\d{2}[A-Z]\d{3}\z/
8
- MX_MONEY = /\A-?\d{1,12}(\.\d{0,6})?\z/
7
+ ELECTOR_KEY = /\A(?:[A-Z][B-DF-HJ-NP-TV-Z]){3}\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])(?:0[1-9]|[12]\d|3[0-2]|99)[A-Z]\d{3}\z/
8
+ MONEY = /\A[+-]?\d+(\.\d{1,6})?\z/
9
9
  end
10
10
  end
11
11
  end
@@ -4,54 +4,47 @@ module Commons
4
4
  include Singleton
5
5
 
6
6
  #
7
- # Método que devuelve todos los objetos
7
+ # Método que devuelve todos los objetos activos
8
8
  #
9
- # @return [Object,nil]
9
+ # @return [Arrat<Object>, nil]
10
10
  #
11
- def all
12
- @db_client.all
13
- end
11
+ delegate :all, to: :@db_client
14
12
 
15
13
  #
16
14
  # Método que devuelve todos los objetos que no han sido eliminados
17
15
  #
18
16
  # @return [Object,nil]
19
17
  #
20
- def kept
21
- raise ActiveModel::MissingAttributeError unless @db_client.column_names.include? "deleted_at"
22
-
23
- @db_client.where(deleted_at: nil)
24
- end
18
+ delegate :where, to: :@db_client
19
+ alias_method :query, :where
25
20
 
26
21
  #
27
22
  # Método que devuelve todos los objetos que han sido eliminados
28
23
  #
29
- # @return [Object,nil]
24
+ # @return [Object, nil]
30
25
  #
31
26
  def deleted
32
- raise ActiveModel::MissingAttributeError unless @db_client.column_names.include? "deleted_at"
27
+ raise ActiveModel::MissingAttributeError unless @db_client.include? Commons::Concerns::Extensions::SoftDeleted
33
28
 
34
- @db_client.where.not(deleted_at: nil)
29
+ @db_client.unscoped.where.not(deleted_at: nil)
35
30
  end
36
31
 
37
32
  #
38
33
  # Método que devuelve el objeto según su ID
39
34
  #
40
- # @return [Object,nil]
35
+ # @return [Object, nil]
41
36
  #
42
- def find(id)
43
- @db_client.find(id)
44
- end
37
+ delegate :find, to: :@db_client
45
38
 
46
39
  #
47
40
  # Método que devuelve el objeto según su ID
48
41
  #
49
42
  # @return [Object,nil]
50
43
  #
51
- def find_kept(id)
52
- raise ActiveModel::MissingAttributeError unless @db_client.column_names.include? "deleted_at"
44
+ def find_deleted(id)
45
+ raise ActiveModel::MissingAttributeError unless @db_client.include? Commons::Concerns::Extensions::SoftDeleted
53
46
 
54
- @db_client.find_by!(id: id, deleted_at: nil)
47
+ @db_client.unscoped.where(id: id).where.not(deleted_at: nil).first
55
48
  end
56
49
 
57
50
  #
@@ -59,22 +52,17 @@ module Commons
59
52
  #
60
53
  # @return [Object,nil]
61
54
  #
62
- def find_by(params)
63
- @db_client.find_by(params)
64
- end
55
+ delegate :find_by, to: :@db_client
65
56
 
66
57
  #
67
58
  # Método que devuelve el objeto según parámetros
68
59
  #
69
60
  # @return [Object,nil]
70
61
  #
71
- def find_kept_by(params)
72
- raise ActiveModel::MissingAttributeError unless @db_client.column_names.include? "deleted_at"
62
+ def find_deleted_by(params)
63
+ raise ActiveModel::MissingAttributeError unless @db_client.include? Commons::Concerns::Extensions::SoftDeleted
73
64
 
74
- @db_client.find_by(
75
- deleted_at: nil,
76
- **params
77
- )
65
+ @db_client.unscoped.where.not(deleted_at: nil).where(**params).first
78
66
  end
79
67
 
80
68
  #
@@ -84,9 +72,7 @@ module Commons
84
72
  #
85
73
  # @raise [ActiveRecord::RecordNotFound]
86
74
  #
87
- def find_by!(params)
88
- @db_client.find_by!(params)
89
- end
75
+ delegate :find_by!, to: :@db_client
90
76
 
91
77
  #
92
78
  # Método que devuelve el objeto según parámetros
@@ -95,13 +81,10 @@ module Commons
95
81
  #
96
82
  # @raise [ActiveRecord::RecordNotFound]
97
83
  #
98
- def find_kept_by!(params)
99
- raise ActiveModel::MissingAttributeError unless @db_client.column_names.include? "deleted_at"
84
+ def find_deleted_by!(params)
85
+ raise ActiveModel::MissingAttributeError unless @db_client.include? Commons::Concerns::Extensions::SoftDeleted
100
86
 
101
- @db_client.find_by!(
102
- deleted_at: nil,
103
- **params
104
- )
87
+ @db_client.unscoped.where.not(deleted_at: nil).where(**params).first!
105
88
  end
106
89
 
107
90
  #
@@ -115,9 +98,10 @@ module Commons
115
98
  # @raise [ActiveRecord::RecordNotSaved]
116
99
  #
117
100
  def create!(object)
118
- raise ArgumentError unless object.is_a? @db_client
101
+ raise ArgumentError unless object.is_a?(@db_client)
102
+ raise ActiveRecord::RecordInvalid.new(object) unless object.valid?
119
103
 
120
- object.save!
104
+ save_object(object)
121
105
  end
122
106
 
123
107
  #
@@ -143,10 +127,7 @@ module Commons
143
127
  #
144
128
  # @raises [ActiveRecord::RecordInvalid]
145
129
  #
146
- def find_or_create_by!(params, &block)
147
- object = @db_client.find_by(params) || @db_client.create!(params, &block)
148
- object
149
- end
130
+ delegate :find_or_create_by!, to: :@db_client
150
131
 
151
132
  #
152
133
  # Método que realiza una busqueda de la primer entrada,
@@ -174,7 +155,13 @@ module Commons
174
155
  # @raise [ActiveRecord::RecordNotSaved]
175
156
  #
176
157
  def update!(object)
177
- create!(object)
158
+ if !object.is_a?(@db_client) || object.id.blank?
159
+ raise ArgumentError
160
+ end
161
+
162
+ raise ActiveRecord::RecordInvalid.new(object) unless object.valid?
163
+
164
+ save_object(object)
178
165
  end
179
166
 
180
167
  #
@@ -203,25 +190,43 @@ module Commons
203
190
  # @raises [ActiveRecord::RecordInvalid]
204
191
  # @raises [ActiveModel::MissingAttributeError]
205
192
  #
206
- def soft_delete!(id)
207
- raise ActiveModel::MissingAttributeError unless @db_client.column_names.include? "deleted_at"
208
-
209
- object = @db_client.find_by!(id: id, deleted_at: nil)
210
- object.update!(deleted_at: Time.current)
211
-
212
- object
193
+ def destroy!(object)
194
+ if @db_client.include? Commons::Concerns::Extensions::SoftDeleted
195
+ soft_delete!(object)
196
+ else
197
+ hard_delete!(object)
198
+ end
213
199
  end
214
200
 
215
- private
201
+ protected
216
202
 
217
203
  def initialize
218
- @db_client ||= class_object
204
+ @db_client = class_object
219
205
  end
220
206
 
221
207
  def class_object
222
208
  model_name = self.class.to_s.gsub("Repository", "")
223
209
  Object.const_get model_name
224
210
  end
211
+
212
+ private
213
+
214
+ def save_object(object)
215
+ object.save
216
+ end
217
+
218
+ def soft_delete!(object)
219
+ raise ActiveModel::MissingAttributeError unless @db_client.column_names.include?("deleted_at")
220
+ object.update!(deleted_at: Time.current)
221
+
222
+ object
223
+ end
224
+
225
+ def hard_delete!(object)
226
+ object.destroy!
227
+
228
+ object
229
+ end
225
230
  end
226
231
  end
227
232
  end
@@ -5,8 +5,13 @@ module Commons
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  class_methods do
8
- def call(*args)
9
- new(*args).call
8
+ def call(*args, **kwargs)
9
+ # This approach allows thge class initializer to use regular arguments,
10
+ # named argumets or the mix of both.
11
+ # This at the same time allows the usage of Dry::Initializer
12
+ # https://dry-rb.org/gems/dry-initializer/3.0/
13
+ # And perhaps this can eventually be a defualt part of Callable Module
14
+ new(*args, **kwargs).call
10
15
  end
11
16
  end
12
17
  end
@@ -1,3 +1,3 @@
1
1
  module Commons
2
- VERSION = '0.12.0'
2
+ VERSION = '0.15.0'.freeze
3
3
  end
@@ -1,4 +1,4 @@
1
- RSpec.describe Commons::Concerns::Extensions::Deleted do
1
+ RSpec.describe Commons::Concerns::Extensions::SoftDeleted do
2
2
  let(:user) { create(:user) }
3
3
 
4
4
  subject do
@@ -18,7 +18,7 @@ RSpec.describe Commons::Concerns::Extensions::Deleted do
18
18
  it 'when deleted user' do
19
19
  # given
20
20
  user = subject
21
- user = UserRepository.instance.soft_delete!(user.id)
21
+ user = UserRepository.instance.destroy!(user)
22
22
  # do
23
23
  expect(user.deleted?).to eq true
24
24
  end
@@ -26,17 +26,15 @@ RSpec.describe Commons::Concerns::Extensions::Deleted do
26
26
  it 'when deleted user denies save' do
27
27
  # given
28
28
  user = subject
29
- user = UserRepository.instance.soft_delete!(user.id)
29
+ user = UserRepository.instance.destroy!(user)
30
30
  user.name = Faker::Name.first_name
31
31
  # do
32
32
  expect{ user.save }.to raise_error(ActiveRecord::RecordInvalid)
33
33
  end
34
34
 
35
- it 'when model is not deletable' do
36
- # given
37
- employee = Employee.new
35
+ it 'when model is not soft_deletable' do
38
36
  # do
39
- expect{ employee.deleted? }.to raise_error(ActiveModel::MissingAttributeError)
37
+ expect{ Employee.new }.to raise_error(ActiveModel::MissingAttributeError)
40
38
  end
41
39
  end
42
40
  end
@@ -39,7 +39,7 @@ RSpec.describe Commons::Formatter::RegexConstants do
39
39
  end
40
40
  end
41
41
 
42
- describe 'CRUP' do
42
+ describe 'CURP' do
43
43
  context 'works when valid' do
44
44
  curp_list = [
45
45
  'BEML920313HMCLNS09',
@@ -99,4 +99,67 @@ RSpec.describe Commons::Formatter::RegexConstants do
99
99
  end
100
100
  end
101
101
  end
102
+
103
+ describe 'ELECTOR_KEY' do
104
+ context 'works when valid' do
105
+ elector_list = [
106
+ 'PXMXJX94021709H000',
107
+ 'OXSXJX96062909H000',
108
+ ]
109
+ elector_list.each do |k|
110
+ it "for clave #{k}" do
111
+ expect(k =~ Commons::Formatter::RegexConstants::ELECTOR_KEY).to be >= 0
112
+ end
113
+ end
114
+ end
115
+
116
+ context 'fail when not valid' do
117
+ elector_list = [
118
+ 'xxxx',
119
+ '11111',
120
+ 'XAXX000000HXXYYY00'
121
+ ]
122
+ elector_list.each do |k|
123
+ it "for clave #{k}" do
124
+ expect(k =~ Commons::Formatter::RegexConstants::ELECTOR_KEY).to be_falsey
125
+ end
126
+ end
127
+ end
128
+ end
129
+
130
+ describe 'MONEY' do
131
+ context 'works when valid' do
132
+ number_list = [
133
+ '10.00',
134
+ '1',
135
+ '-1.0',
136
+ '+1.0',
137
+ '0.000001',
138
+ '10000.000',
139
+ '99999999999999999999999999999999999999',
140
+ '99999999999999999999999999999999999999.999999',
141
+ '0',
142
+ ]
143
+ number_list.each do |number|
144
+ it "for number #{number}" do
145
+ expect(number =~ Commons::Formatter::RegexConstants::MONEY).to be >= 0
146
+ end
147
+ end
148
+ end
149
+
150
+ context 'fail when not valid' do
151
+ number_list = [
152
+ '',
153
+ ' 0.0 ',
154
+ '0.0000001',
155
+ '$0.00',
156
+ '.1'
157
+ ]
158
+ number_list.each do |number|
159
+ it "for number #{number}" do
160
+ expect(number =~ Commons::Formatter::RegexConstants::MONEY).to be_falsey
161
+ end
162
+ end
163
+ end
164
+ end
102
165
  end
@@ -1,4 +1,4 @@
1
- RSpec.describe 'Commons::Repositories::BaseRepository' do
1
+ RSpec.describe Commons::Repositories::BaseRepository do
2
2
  let(:user) { create(:user) }
3
3
  let(:valid_params) do
4
4
  {
@@ -17,13 +17,13 @@ RSpec.describe 'Commons::Repositories::BaseRepository' do
17
17
  }
18
18
  deleted_users_amount.times {
19
19
  user = UserRepository.instance.create_from_params!(**valid_params.to_h.symbolize_keys)
20
- UserRepository.instance.soft_delete!(user.id)
20
+ UserRepository.instance.destroy!(user)
21
21
  }
22
22
  end
23
23
  subject { UserRepository.instance.all }
24
24
 
25
25
  it do
26
- expect(subject.count).to eq users_amount + deleted_users_amount
26
+ expect(subject.count).to eq users_amount
27
27
  expect(subject.first).to be_an_instance_of User
28
28
  end
29
29
  end
@@ -33,7 +33,7 @@ RSpec.describe 'Commons::Repositories::BaseRepository' do
33
33
  end
34
34
  end
35
35
 
36
- describe 'kept' do
36
+ describe 'query' do
37
37
  context 'when data exists' do
38
38
  let(:users_amount) { 10 }
39
39
  let(:deleted_users_amount) { 5 }
@@ -43,10 +43,11 @@ RSpec.describe 'Commons::Repositories::BaseRepository' do
43
43
  }
44
44
  deleted_users_amount.times {
45
45
  user = UserRepository.instance.create_from_params!(**valid_params.to_h.symbolize_keys)
46
- UserRepository.instance.soft_delete!(user.id)
46
+ UserRepository.instance.destroy!(user)
47
47
  }
48
48
  end
49
- subject { UserRepository.instance.kept }
49
+
50
+ subject { UserRepository.instance.query.not(name: nil) }
50
51
 
51
52
  it do
52
53
  expect(subject.count).to eq users_amount
@@ -58,11 +59,13 @@ RSpec.describe 'Commons::Repositories::BaseRepository' do
58
59
  before do
59
60
  deleted_users_amount.times {
60
61
  user = UserRepository.instance.create_from_params!(**valid_params.to_h.symbolize_keys)
61
- UserRepository.instance.soft_delete!(user.id)
62
+ UserRepository.instance.destroy!(user)
62
63
  }
63
64
  end
64
65
 
65
- it { expect(UserRepository.instance.kept).to be_empty }
66
+ subject { UserRepository.instance.query.not(name: nil) }
67
+
68
+ it { expect(subject).to be_empty }
66
69
  end
67
70
  end
68
71
 
@@ -76,7 +79,7 @@ RSpec.describe 'Commons::Repositories::BaseRepository' do
76
79
  }
77
80
  deleted_users_amount.times {
78
81
  user = UserRepository.instance.create_from_params!(**valid_params.to_h.symbolize_keys)
79
- UserRepository.instance.soft_delete!(user.id)
82
+ UserRepository.instance.destroy!(user)
80
83
  }
81
84
  end
82
85
  subject { UserRepository.instance.deleted }
@@ -114,30 +117,27 @@ RSpec.describe 'Commons::Repositories::BaseRepository' do
114
117
  end
115
118
  end
116
119
 
117
- describe 'find_kept' do
120
+ describe 'find_deleted' do
118
121
  context 'by a valid id' do
119
- subject { UserRepository.instance.find_kept(user.id) }
122
+ subject { UserRepository.instance.find_deleted(user.id) }
120
123
 
121
- it { is_expected.to be_an_instance_of User }
124
+ it { is_expected.to be_nil }
122
125
  end
123
126
 
124
127
  context 'by non-existent id' do
125
- it do
126
- expect do
127
- UserRepository.instance.find_kept('my totally non-existent id')
128
- end.to raise_error(ActiveRecord::RecordNotFound)
129
- end
128
+ subject { UserRepository.instance.find_deleted('my totally non-existent id') }
129
+
130
+ it { is_expected.to be_nil }
130
131
  end
131
132
 
132
133
  context 'by previously deleted user' do
133
134
  before do
134
- UserRepository.instance.soft_delete!(user.id)
135
+ UserRepository.instance.destroy!(user)
135
136
  end
136
- subject { UserRepository.instance.find_kept(user.id) }
137
137
 
138
- it do
139
- expect{ subject }.to raise_error(ActiveRecord::RecordNotFound)
140
- end
138
+ subject { UserRepository.instance.find_deleted(user.id) }
139
+
140
+ it { is_expected.to be_an_instance_of User }
141
141
  end
142
142
  end
143
143
 
@@ -153,22 +153,27 @@ RSpec.describe 'Commons::Repositories::BaseRepository' do
153
153
  end
154
154
  end
155
155
 
156
- describe 'find_kept_by' do
156
+ describe 'find_deleted_by' do
157
157
  context 'by a valid id' do
158
- subject { UserRepository.instance.find_kept_by(id: user.id) }
158
+ subject { UserRepository.instance.find_deleted_by(id: user.id) }
159
159
 
160
- it { is_expected.to be_an_instance_of User }
160
+ it { is_expected.to be_falsey }
161
161
  end
162
162
 
163
163
  context 'by non-existent id' do
164
- it { expect(UserRepository.instance.find_kept_by(id: 'my totally non-existent id')).to be_falsey }
164
+ subject { UserRepository.instance.find_deleted_by(id: 'my totally non-existent id') }
165
+
166
+ it { is_expected.to be_falsey }
165
167
  end
166
168
 
167
169
  context 'by previously deleted user' do
168
170
  before do
169
- UserRepository.instance.soft_delete!(user.id)
171
+ UserRepository.instance.destroy!(user)
170
172
  end
171
- it { expect(UserRepository.instance.find_kept_by(id: 'my totally non-existent id')).to be_falsey }
173
+
174
+ subject { UserRepository.instance.find_deleted_by(id: user.id) }
175
+
176
+ it { is_expected.to be_an_instance_of User }
172
177
  end
173
178
  end
174
179
 
@@ -190,34 +195,31 @@ RSpec.describe 'Commons::Repositories::BaseRepository' do
190
195
  end
191
196
  end
192
197
 
193
- describe 'find_kept_by!' do
194
- context 'by a valid id!' do
195
- subject { UserRepository.instance.find_kept_by!(id: user.id) }
198
+ describe 'find_deleted_by!' do
199
+ context 'by a valid active id' do
200
+ subject { UserRepository.instance.find_deleted_by!(id: user.id) }
196
201
 
197
- it { is_expected.to be_an_instance_of User }
202
+ it do
203
+ expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
204
+ end
198
205
  end
199
206
 
200
207
  context 'by non-existent id!' do
208
+ subject { UserRepository.instance.find_deleted_by!(id: 'my totally non-existent id') }
209
+
201
210
  it do
202
- expect do
203
- UserRepository.instance.find_kept_by!(
204
- id: 'my totally non-existent id'
205
- )
206
- end.to raise_error(ActiveRecord::RecordNotFound)
211
+ expect { subject }.to raise_error(ActiveRecord::RecordNotFound)
207
212
  end
208
213
  end
209
214
 
210
215
  context 'by previously deleted user' do
211
216
  before do
212
- UserRepository.instance.soft_delete!(user.id)
213
- end
214
- it do
215
- expect do
216
- UserRepository.instance.find_kept_by!(
217
- id: 'my totally non-existent id'
218
- )
219
- end.to raise_error(ActiveRecord::RecordNotFound)
217
+ UserRepository.instance.destroy!(user)
220
218
  end
219
+
220
+ subject { UserRepository.instance.find_deleted_by!(id: user.id) }
221
+
222
+ it { is_expected.to be_an_instance_of User }
221
223
  end
222
224
  end
223
225
 
@@ -345,12 +347,12 @@ RSpec.describe 'Commons::Repositories::BaseRepository' do
345
347
  end
346
348
 
347
349
  describe 'fails when' do
348
- context 'is not a user' do
349
- let(:employee) { build(:employee) }
350
+ context 'is not a employee' do
351
+ let(:user) { build(:user) }
350
352
 
351
353
  it do
352
354
  expect do
353
- UserRepository.instance.create!(employee)
355
+ EmployeeRepository.instance.create!(user)
354
356
  end.to raise_error(ArgumentError)
355
357
  end
356
358
  end
@@ -400,11 +402,21 @@ RSpec.describe 'Commons::Repositories::BaseRepository' do
400
402
 
401
403
  describe 'fails when' do
402
404
  context 'is not a user' do
403
- let(:employee) { create(:employee) }
405
+ let(:user) { create(:user) }
406
+
407
+ it do
408
+ expect do
409
+ EmployeeRepository.instance.update!(user)
410
+ end.to raise_error(ArgumentError)
411
+ end
412
+ end
413
+
414
+ context 'is not a previously saved' do
415
+ let(:user) { build(:user) }
404
416
 
405
417
  it do
406
418
  expect do
407
- UserRepository.instance.update!(employee)
419
+ UserRepository.instance.update!(user)
408
420
  end.to raise_error(ArgumentError)
409
421
  end
410
422
  end
@@ -473,7 +485,7 @@ RSpec.describe 'Commons::Repositories::BaseRepository' do
473
485
  context 'using a valid user' do
474
486
  let(:user) { create(:user) }
475
487
 
476
- subject { UserRepository.instance.soft_delete!(user.id) }
488
+ subject { UserRepository.instance.destroy!(user) }
477
489
 
478
490
  it { expect(subject.deleted_at).not_to be nil }
479
491
  end
@@ -485,7 +497,7 @@ RSpec.describe 'Commons::Repositories::BaseRepository' do
485
497
 
486
498
  it do
487
499
  expect do
488
- EmployeeRepository.instance.soft_delete!(employee.id)
500
+ EmployeeRepository.instance.destroy!(employee)
489
501
  end.to raise_error(ActiveModel::MissingAttributeError)
490
502
  end
491
503
  end
@@ -495,8 +507,8 @@ RSpec.describe 'Commons::Repositories::BaseRepository' do
495
507
 
496
508
  it do
497
509
  expect do
498
- UserRepository.instance.soft_delete!(user)
499
- end.to raise_error(ActiveRecord::RecordNotFound)
510
+ UserRepository.instance.destroy!(user)
511
+ end.to raise_error(ActiveRecord::RecordInvalid)
500
512
  end
501
513
  end
502
514
  end
@@ -1,3 +1,3 @@
1
1
  class Employee < ApplicationRecord
2
- include Commons::Concerns::Extensions::Deleted
2
+ include Commons::Concerns::Extensions::SoftDeleted
3
3
  end
@@ -1,6 +1,6 @@
1
1
  class User < ApplicationRecord
2
2
  include Commons::Concerns::Attributes::Sex
3
- include Commons::Concerns::Extensions::Deleted
3
+ include Commons::Concerns::Extensions::SoftDeleted
4
4
  include Commons::Concerns::Guard::Capitalizable
5
5
  include Commons::Concerns::Validations::Undestroyable
6
6
 
@@ -48,6 +48,10 @@ RSpec.configure do |config|
48
48
  # triggering implicit auto-inclusion in groups with matching metadata.
49
49
  config.shared_context_metadata_behavior = :apply_to_host_groups
50
50
 
51
+ # This setting enables warnings. It's recommended, but in some cases may
52
+ # be too noisy due to issues in dependencies.
53
+ config.warnings = true
54
+
51
55
  # The settings below are suggested to provide a good initial experience
52
56
  # with RSpec, but feel free to customize to your heart's content.
53
57
  =begin
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: commons_yellowme
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.12.0
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yellowme
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2019-12-10 00:00:00.000000000 Z
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '5.2'
19
+ version: '6.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '5.2'
26
+ version: '6.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: json
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -198,7 +198,7 @@ files:
198
198
  - lib/commons/authentication/authenticate_by_jwt.rb
199
199
  - lib/commons/authentication/json_web_token.rb
200
200
  - lib/commons/concerns/attributes/sex.rb
201
- - lib/commons/concerns/extensions/deleted.rb
201
+ - lib/commons/concerns/extensions/soft_deleted.rb
202
202
  - lib/commons/concerns/guard/capitalizable.rb
203
203
  - lib/commons/concerns/validations/undestroyable.rb
204
204
  - lib/commons/config.rb
@@ -353,7 +353,7 @@ homepage: https://github.com/yellowme/commons-rails
353
353
  licenses:
354
354
  - MIT
355
355
  metadata: {}
356
- post_install_message:
356
+ post_install_message:
357
357
  rdoc_options: []
358
358
  require_paths:
359
359
  - lib
@@ -368,9 +368,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
368
368
  - !ruby/object:Gem::Version
369
369
  version: '0'
370
370
  requirements: []
371
- rubyforge_project:
372
- rubygems_version: 2.7.6
373
- signing_key:
371
+ rubygems_version: 3.1.2
372
+ signing_key:
374
373
  specification_version: 4
375
374
  summary: Commons is Yellowme's Rails APIs utilities gem
376
375
  test_files:
@@ -1,25 +0,0 @@
1
- module Commons
2
- module Concerns
3
- module Extensions
4
- module Deleted
5
- extend ActiveSupport::Concern
6
-
7
- included do
8
- before_validation :check_not_deleted, on: [:update]
9
-
10
- def deleted?
11
- raise ActiveModel::MissingAttributeError unless self.has_attribute?(:deleted_at)
12
- self.deleted_at.present?
13
- end
14
- end
15
-
16
- private
17
-
18
- def check_not_deleted
19
- raise ActiveModel::MissingAttributeError unless self.has_attribute?(:deleted_at)
20
- raise ActiveRecord::RecordInvalid if self.deleted_at_in_database.present?
21
- end
22
- end
23
- end
24
- end
25
- end