readymade 0.3.9 → 0.4.1

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: 37150c92279da3c7ecb29441fccbf188572039fe69712cf99824121c8ebeb889
4
- data.tar.gz: f94ab00a9af6b37097958c1a386932f8667e7e37ea55ec30530a2e92a423738d
3
+ metadata.gz: 8f3a8c90117d3207e0e228fdc410d1799955f07bacd3753ab4b721916bd9c6a3
4
+ data.tar.gz: 53c6a9b974ab6b3a70d7df5ad60031855bb1228d9a53b1b7f8915b9c28a9df24
5
5
  SHA512:
6
- metadata.gz: b7b562dd8614ca64c8821d71c89ecff73c9556d8c8b5ea4ed95046ba8841354e193ae5e6d61ce05e673cdf8c8ef3946322165761862ca1aee7d8cdc44ecc6b99
7
- data.tar.gz: 2608dd0e7def9ea166ec4c0edb391a748d02602e4c8a2e6fd3f0a536397fae5c0e286330fdd07901196f4316bdcf578630f14c5ff985e32896fee35a8f284cb1
6
+ metadata.gz: c0907befa7f6054fab2c7cbb899741fd02c4c357f2cf959c248af99c06b78666462dbb22a5aca201f0761471624847ada443187c1520c803243e0a75c38ec458
7
+ data.tar.gz: 0cc7164cab6a23ef602a283ff27cd0bcc68a4c7053fa0469b52e499cbdf5ca0d5106dc1b6bd64ba79539b3862bd80c84a18763937e8feb1d2ea9ebc99dbafcfd
data/CHANGELOG.md CHANGED
@@ -1,6 +1,14 @@
1
1
  Changelog
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
+ ## [0.4.1] - 2024-07-15
5
+
6
+ * Add `message` to `Reaymade::UnSuccessError` exception
7
+
8
+ ## [0.4.0] - 2024-02-23
9
+
10
+ * Add `Reaymade::Model::ValidatableEnum` to validate enum values
11
+
4
12
  ## [0.3.9] - 2024-01-04
5
13
 
6
14
  * Update `Readymade::Model::Filterable` to support and/or joins
data/Gemfile.lock CHANGED
@@ -1,31 +1,42 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- readymade (0.3.9)
4
+ readymade (0.4.1)
5
5
  activejob
6
6
  activemodel
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- activejob (7.0.8)
12
- activesupport (= 7.0.8)
11
+ activejob (7.1.3)
12
+ activesupport (= 7.1.3)
13
13
  globalid (>= 0.3.6)
14
- activemodel (7.0.8)
15
- activesupport (= 7.0.8)
16
- activesupport (7.0.8)
14
+ activemodel (7.1.3)
15
+ activesupport (= 7.1.3)
16
+ activesupport (7.1.3)
17
+ base64
18
+ bigdecimal
17
19
  concurrent-ruby (~> 1.0, >= 1.0.2)
20
+ connection_pool (>= 2.2.5)
21
+ drb
18
22
  i18n (>= 1.6, < 2)
19
23
  minitest (>= 5.1)
24
+ mutex_m
20
25
  tzinfo (~> 2.0)
26
+ base64 (0.2.0)
27
+ bigdecimal (3.1.6)
21
28
  byebug (11.1.3)
22
- concurrent-ruby (1.2.2)
29
+ concurrent-ruby (1.2.3)
30
+ connection_pool (2.4.1)
23
31
  diff-lcs (1.4.4)
32
+ drb (2.2.0)
33
+ ruby2_keywords
24
34
  globalid (1.2.1)
25
35
  activesupport (>= 6.1)
26
36
  i18n (1.14.1)
27
37
  concurrent-ruby (~> 1.0)
28
- minitest (5.20.0)
38
+ minitest (5.22.1)
39
+ mutex_m (0.2.0)
29
40
  rake (13.0.6)
30
41
  rspec (3.10.0)
31
42
  rspec-core (~> 3.10.0)
@@ -40,6 +51,7 @@ GEM
40
51
  diff-lcs (>= 1.2.0, < 2.0)
41
52
  rspec-support (~> 3.10.0)
42
53
  rspec-support (3.10.3)
54
+ ruby2_keywords (0.0.5)
43
55
  tzinfo (2.0.6)
44
56
  concurrent-ruby (~> 1.0)
45
57
 
data/README.md CHANGED
@@ -287,6 +287,26 @@ User.all.filter_collection({ by_status: 'active', by_role: 'manager' })
287
287
  User.all.filter_collection({ by_status: 'active', by_role: 'manager' }, chain_with: :or) # active OR manager
288
288
  ```
289
289
 
290
+ ### Readymade::Model::ValidatableEnum
291
+
292
+ Instead of raised error when enum value is not valid, it adds error to the record
293
+
294
+ ```ruby
295
+ class User < ApplicationRecord
296
+ include Readymade::Model::ValidatableEnum
297
+
298
+ enum status: { inactive: 0, active: 10 }
299
+ enum role: { customer: 0, admin: 10 }
300
+ validatable_enum :status, :role
301
+ end
302
+ ```
303
+
304
+ ```ruby
305
+ user = User.new(status: 'archived', role: 'superadmin')
306
+ user.validate # false
307
+ user.errors.full_messages # ["Role 'superadmin' is not a valid role", "Status 'archived' is not a valid status"]
308
+ ```
309
+
290
310
  ## Development
291
311
 
292
312
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -17,7 +17,7 @@ module Readymade
17
17
  new(*args, &block).call!.then do |res|
18
18
  return res if res.try(:success?) || res.try(:consider_success?)
19
19
 
20
- raise UnSuccessError
20
+ raise UnSuccessError.new(res.humanized_errors)
21
21
  end
22
22
  end
23
23
 
@@ -55,7 +55,7 @@ module Readymade
55
55
  end,
56
56
  :ok
57
57
  ]
58
- when :validation_fail, :bad_request
58
+ when :validation_fail, :bad_request, :fail
59
59
  [{ errors: response_obj.data[:errors] }, :bad_request]
60
60
  else
61
61
  [response_obj.status.to_sym, {}]
@@ -2,8 +2,6 @@ require 'active_support/concern'
2
2
 
3
3
  module Readymade
4
4
  module Model
5
- # frozen_string_literal: true
6
-
7
5
  module Filterable
8
6
  extend ActiveSupport::Concern
9
7
 
@@ -0,0 +1,32 @@
1
+ require 'active_support/concern'
2
+
3
+ module Readymade
4
+ module Model
5
+ module ValidatableEnum
6
+ extend ActiveSupport::Concern
7
+
8
+ class_methods do
9
+ def validatable_enum(*enums_to_fix)
10
+ enums_to_fix.each do |enum_name|
11
+ define_method(:"#{enum_name}=") do |value|
12
+ super(value)
13
+ rescue StandardError
14
+ instance_variable_set(:"@__bad_#{enum_name}", value)
15
+ super(nil)
16
+ end
17
+
18
+ validates enum_name, inclusion: {
19
+ in: send(enum_name.to_s.pluralize).keys,
20
+ message: lambda { |record, error|
21
+ val = record.instance_variable_get(:"@__bad_#{enum_name}")
22
+ return "#{error[:attribute]} is required" if val.nil?
23
+
24
+ "#{val.inspect} is not a valid #{error[:attribute]}"
25
+ }
26
+ }
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -28,9 +28,23 @@ module Readymade
28
28
  end
29
29
 
30
30
  def errors
31
+ return {} unless args.is_a?(Hash)
32
+
31
33
  args[:errors].presence || {}
32
34
  end
33
35
 
36
+ def humanized_errors
37
+ humanize_errors(errors)
38
+ end
39
+
40
+ def humanize_errors(errors)
41
+ if errors.is_a?(ActiveModel::Errors)
42
+ errors.full_messages.join('. ')
43
+ else
44
+ errors.map { |k, values| "#{k.to_s.humanize}: #{values.join(', ')}" }.join('. ')
45
+ end
46
+ end
47
+
34
48
  def consider_success?
35
49
  @consider_success
36
50
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Readymade
4
- VERSION = '0.3.9'
4
+ VERSION = '0.4.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: readymade
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.9
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - OrestF
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-04 00:00:00.000000000 Z
11
+ date: 2024-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug
@@ -108,6 +108,7 @@ files:
108
108
  - lib/readymade/instant_form.rb
109
109
  - lib/readymade/model/api_attachable.rb
110
110
  - lib/readymade/model/filterable.rb
111
+ - lib/readymade/model/validatable_enum.rb
111
112
  - lib/readymade/operation.rb
112
113
  - lib/readymade/response.rb
113
114
  - lib/readymade/version.rb