commons_yellowme 0.11.1 → 0.16.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: 8647af0312b7a12912e0aa7d1dcf10c2a810b17badfadb626cb4e7f548ae3832
4
- data.tar.gz: a9794e7a96deb33df33d15e2e2b641fa900efcf93ed14661909ae7bd3caf63a9
3
+ metadata.gz: 01eed218ec93ea2954c70f437a2d49752454ae223f7f47bb55d86c04dbe53802
4
+ data.tar.gz: d927cbb2467677e3f3882d01fb647ee184af466034926ab5c3b161f262a6c754
5
5
  SHA512:
6
- metadata.gz: c1c7895faba91c9758b80ab388f3f3966068c8c7758cf8ec84dcf79181f80773d9b83cefd923c70e0b578a4047812e3d52703a310c1c72a3de9c77b0e52b39a0
7
- data.tar.gz: af4f866a77d8fa9295b1add9157c2dc3fdce233f9abb6cec8a30cab999292a83756077c8ae312fda1be8748eccd5c3b051f27d24bf0edca983a72521f3869938
6
+ metadata.gz: 6cc0ce43aeb45e4ac3bc7fc8844a59f27e0df53a93f11330d7388879692b93746402d2b0850db67c50b4ee794482d20beaf7bf070be1c8085206fea3108476a8
7
+ data.tar.gz: d30a9c84bf4c7a6155d9fdb273723049fb757caf04510f353ff67099f9b560c271a3de6754aa4e03500ddd9df78eebbecb83ffc71539d1eff4cb8d842bd0bd63
data/.gitignore CHANGED
@@ -1,3 +1,56 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+ .vscode
13
+
14
+ # Used by dotenv library to load environment variables.
15
+ # .env
16
+
17
+ ## Specific to RubyMotion:
18
+ .dat*
19
+ .repl_history
20
+ build/
21
+ *.bridgesupport
22
+ build-iPhoneOS/
23
+ build-iPhoneSimulator/
24
+
25
+ ## Specific to RubyMotion (use of CocoaPods):
26
+ #
27
+ # We recommend against adding the Pods directory to your .gitignore. However
28
+ # you should judge for yourself, the pros and cons are mentioned at:
29
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
30
+ #
31
+ # vendor/Pods/
32
+
33
+ ## Documentation cache and generated files:
34
+ /.yardoc/
35
+ /_yardoc/
36
+ /doc/
37
+ /rdoc/
38
+
39
+ ## Environment normalization:
40
+ /.bundle/
41
+ /vendor/bundle
42
+ /lib/bundler/man/
43
+
44
+ # for a library or gem, you might want to ignore these files since the code is
45
+ # intended to run in multiple environments; otherwise, check them in:
46
+ # Gemfile.lock
47
+ # .ruby-version
48
+ # .ruby-gemset
49
+
50
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
51
+ .rvmrc
52
+ .DS_Store
53
+
1
54
  .bundle/
2
55
  log/*.log
3
56
  pkg/
@@ -7,4 +60,3 @@ spec/dummy/db/*.sqlite3-*
7
60
  spec/dummy/log/*.log
8
61
  spec/dummy/storage/
9
62
  spec/dummy/tmp/
10
- coverage
@@ -1,83 +1,70 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- commons (0.10.3)
4
+ commons_yellowme (0.13.0)
5
5
  active_model_serializers (~> 0.10.10)
6
6
  dry-validation
7
- json (~> 1.8)
7
+ json (~> 2.0)
8
8
  jwt
9
9
  phonelib (~> 0.6.29)
10
- rails (~> 6.0.1)
10
+ rails (~> 5.2)
11
11
  strip_attributes
12
12
 
13
13
  GEM
14
14
  remote: https://rubygems.org/
15
15
  specs:
16
- actioncable (6.0.2)
17
- actionpack (= 6.0.2)
16
+ actioncable (5.2.4.1)
17
+ actionpack (= 5.2.4.1)
18
18
  nio4r (~> 2.0)
19
19
  websocket-driver (>= 0.6.1)
20
- actionmailbox (6.0.2)
21
- actionpack (= 6.0.2)
22
- activejob (= 6.0.2)
23
- activerecord (= 6.0.2)
24
- activestorage (= 6.0.2)
25
- activesupport (= 6.0.2)
26
- mail (>= 2.7.1)
27
- actionmailer (6.0.2)
28
- actionpack (= 6.0.2)
29
- actionview (= 6.0.2)
30
- activejob (= 6.0.2)
20
+ actionmailer (5.2.4.1)
21
+ actionpack (= 5.2.4.1)
22
+ actionview (= 5.2.4.1)
23
+ activejob (= 5.2.4.1)
31
24
  mail (~> 2.5, >= 2.5.4)
32
25
  rails-dom-testing (~> 2.0)
33
- actionpack (6.0.2)
34
- actionview (= 6.0.2)
35
- activesupport (= 6.0.2)
36
- rack (~> 2.0)
26
+ actionpack (5.2.4.1)
27
+ actionview (= 5.2.4.1)
28
+ activesupport (= 5.2.4.1)
29
+ rack (~> 2.0, >= 2.0.8)
37
30
  rack-test (>= 0.6.3)
38
31
  rails-dom-testing (~> 2.0)
39
- rails-html-sanitizer (~> 1.0, >= 1.2.0)
40
- actiontext (6.0.2)
41
- actionpack (= 6.0.2)
42
- activerecord (= 6.0.2)
43
- activestorage (= 6.0.2)
44
- activesupport (= 6.0.2)
45
- nokogiri (>= 1.8.5)
46
- actionview (6.0.2)
47
- activesupport (= 6.0.2)
32
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
33
+ actionview (5.2.4.1)
34
+ activesupport (= 5.2.4.1)
48
35
  builder (~> 3.1)
49
36
  erubi (~> 1.4)
50
37
  rails-dom-testing (~> 2.0)
51
- rails-html-sanitizer (~> 1.1, >= 1.2.0)
38
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
52
39
  active_model_serializers (0.10.10)
53
40
  actionpack (>= 4.1, < 6.1)
54
41
  activemodel (>= 4.1, < 6.1)
55
42
  case_transform (>= 0.2)
56
43
  jsonapi-renderer (>= 0.1.1.beta1, < 0.3)
57
- activejob (6.0.2)
58
- activesupport (= 6.0.2)
44
+ activejob (5.2.4.1)
45
+ activesupport (= 5.2.4.1)
59
46
  globalid (>= 0.3.6)
60
- activemodel (6.0.2)
61
- activesupport (= 6.0.2)
62
- activerecord (6.0.2)
63
- activemodel (= 6.0.2)
64
- activesupport (= 6.0.2)
65
- activestorage (6.0.2)
66
- actionpack (= 6.0.2)
67
- activejob (= 6.0.2)
68
- activerecord (= 6.0.2)
47
+ activemodel (5.2.4.1)
48
+ activesupport (= 5.2.4.1)
49
+ activerecord (5.2.4.1)
50
+ activemodel (= 5.2.4.1)
51
+ activesupport (= 5.2.4.1)
52
+ arel (>= 9.0)
53
+ activestorage (5.2.4.1)
54
+ actionpack (= 5.2.4.1)
55
+ activerecord (= 5.2.4.1)
69
56
  marcel (~> 0.3.1)
70
- activesupport (6.0.2)
57
+ activesupport (5.2.4.1)
71
58
  concurrent-ruby (~> 1.0, >= 1.0.2)
72
59
  i18n (>= 0.7, < 2)
73
60
  minitest (~> 5.1)
74
61
  tzinfo (~> 1.1)
75
- zeitwerk (~> 2.2)
62
+ arel (9.0.0)
76
63
  builder (3.2.4)
77
64
  case_transform (0.2)
78
65
  activesupport
79
66
  concurrent-ruby (1.1.5)
80
- crass (1.0.5)
67
+ crass (1.0.6)
81
68
  diff-lcs (1.3)
82
69
  docile (1.3.2)
83
70
  dry-configurable (0.9.0)
@@ -91,7 +78,7 @@ GEM
91
78
  dry-equalizer (0.3.0)
92
79
  dry-inflector (0.2.0)
93
80
  dry-initializer (3.0.3)
94
- dry-logic (1.0.5)
81
+ dry-logic (1.0.6)
95
82
  concurrent-ruby (~> 1.0)
96
83
  dry-core (~> 0.2)
97
84
  dry-equalizer (~> 0.2)
@@ -103,7 +90,7 @@ GEM
103
90
  dry-initializer (~> 3.0)
104
91
  dry-logic (~> 1.0)
105
92
  dry-types (~> 1.2)
106
- dry-types (1.2.2)
93
+ dry-types (1.3.0)
107
94
  concurrent-ruby (~> 1.0)
108
95
  dry-container (~> 0.3)
109
96
  dry-core (~> 0.4, >= 0.4.4)
@@ -127,9 +114,9 @@ GEM
127
114
  i18n (>= 1.6, < 1.8)
128
115
  globalid (0.4.2)
129
116
  activesupport (>= 4.2.0)
130
- i18n (1.7.0)
117
+ i18n (1.7.1)
131
118
  concurrent-ruby (~> 1.0)
132
- json (1.8.6)
119
+ json (2.3.0)
133
120
  jsonapi-renderer (0.2.2)
134
121
  jwt (2.2.1)
135
122
  loofah (2.4.0)
@@ -143,40 +130,38 @@ GEM
143
130
  mimemagic (0.3.4)
144
131
  mini_mime (1.0.2)
145
132
  mini_portile2 (2.4.0)
146
- minitest (5.13.0)
133
+ minitest (5.14.0)
147
134
  nio4r (2.5.2)
148
135
  nokogiri (1.10.7)
149
136
  mini_portile2 (~> 2.4.0)
150
137
  phonelib (0.6.41)
151
- rack (2.0.7)
138
+ rack (2.1.2)
152
139
  rack-test (1.1.0)
153
140
  rack (>= 1.0, < 3)
154
- rails (6.0.2)
155
- actioncable (= 6.0.2)
156
- actionmailbox (= 6.0.2)
157
- actionmailer (= 6.0.2)
158
- actionpack (= 6.0.2)
159
- actiontext (= 6.0.2)
160
- actionview (= 6.0.2)
161
- activejob (= 6.0.2)
162
- activemodel (= 6.0.2)
163
- activerecord (= 6.0.2)
164
- activestorage (= 6.0.2)
165
- activesupport (= 6.0.2)
141
+ rails (5.2.4.1)
142
+ actioncable (= 5.2.4.1)
143
+ actionmailer (= 5.2.4.1)
144
+ actionpack (= 5.2.4.1)
145
+ actionview (= 5.2.4.1)
146
+ activejob (= 5.2.4.1)
147
+ activemodel (= 5.2.4.1)
148
+ activerecord (= 5.2.4.1)
149
+ activestorage (= 5.2.4.1)
150
+ activesupport (= 5.2.4.1)
166
151
  bundler (>= 1.3.0)
167
- railties (= 6.0.2)
152
+ railties (= 5.2.4.1)
168
153
  sprockets-rails (>= 2.0.0)
169
154
  rails-dom-testing (2.0.3)
170
155
  activesupport (>= 4.2.0)
171
156
  nokogiri (>= 1.6)
172
157
  rails-html-sanitizer (1.3.0)
173
158
  loofah (~> 2.3)
174
- railties (6.0.2)
175
- actionpack (= 6.0.2)
176
- activesupport (= 6.0.2)
159
+ railties (5.2.4.1)
160
+ actionpack (= 5.2.4.1)
161
+ activesupport (= 5.2.4.1)
177
162
  method_source
178
163
  rake (>= 0.8.7)
179
- thor (>= 0.20.3, < 2.0)
164
+ thor (>= 0.19.0, < 2.0)
180
165
  rake (13.0.1)
181
166
  rspec-core (3.9.0)
182
167
  rspec-support (~> 3.9.0)
@@ -210,20 +195,19 @@ GEM
210
195
  sqlite3 (1.4.1)
211
196
  strip_attributes (1.9.0)
212
197
  activemodel (>= 3.0, < 7.0)
213
- thor (1.0.0)
198
+ thor (1.0.1)
214
199
  thread_safe (0.3.6)
215
- tzinfo (1.2.5)
200
+ tzinfo (1.2.6)
216
201
  thread_safe (~> 0.1)
217
202
  websocket-driver (0.7.1)
218
203
  websocket-extensions (>= 0.1.0)
219
204
  websocket-extensions (0.1.4)
220
- zeitwerk (2.2.2)
221
205
 
222
206
  PLATFORMS
223
207
  ruby
224
208
 
225
209
  DEPENDENCIES
226
- commons!
210
+ commons_yellowme!
227
211
  factory_bot_rails (~> 5.1.1)
228
212
  faker
229
213
  rspec-rails
@@ -231,4 +215,4 @@ DEPENDENCIES
231
215
  sqlite3
232
216
 
233
217
  BUNDLED WITH
234
- 2.0.2
218
+ 2.1.4
@@ -15,20 +15,13 @@ Gem::Specification.new do |spec|
15
15
  spec.homepage = 'https://github.com/yellowme/commons-rails'
16
16
  spec.license = 'MIT'
17
17
 
18
- #spec.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"]
19
- #spec.files = [
20
- # "lib/commons.rb",
21
- # "spec/support/shared-examples/capitalizable.rb",
22
- # "spec/support/shared-examples/undestroyable.rb",
23
- # "spec/support/shared-examples/stripable.rb"
24
- # ]
25
18
  spec.files = `git ls-files`.split($/)
26
19
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
27
20
  spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
28
21
 
29
22
 
30
- spec.add_dependency "rails", "~> 6.0.1"
31
- spec.add_dependency 'json', '~> 1.8'
23
+ spec.add_dependency "rails", ">= 5.2"
24
+ spec.add_dependency 'json', '~> 2.0'
32
25
  spec.add_dependency "active_model_serializers", "~> 0.10.10"
33
26
  spec.add_dependency "strip_attributes"
34
27
  spec.add_dependency "jwt"
@@ -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,6 +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][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/
7
9
  end
8
10
  end
9
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.11.1'
2
+ VERSION = '0.16.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
 
@@ -19,7 +19,7 @@ require "commons"
19
19
  module Dummy
20
20
  class Application < Rails::Application
21
21
  # Initialize configuration defaults for originally generated Rails version.
22
- config.load_defaults 6.0
22
+ config.load_defaults 5.2
23
23
 
24
24
  # Settings in config/environments/* take precedence over those specified here.
25
25
  # Application configuration can go into files in config/initializers
@@ -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.11.1
4
+ version: 0.16.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
@@ -14,30 +14,30 @@ dependencies:
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 6.0.1
19
+ version: '5.2'
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: 6.0.1
26
+ version: '5.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: json
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.8'
33
+ version: '2.0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.8'
40
+ version: '2.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: active_model_serializers
43
43
  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