nxt_support 0.1.4 → 0.1.5

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: 291cef0c8832ad19e20d28d8b74f49aa9f5a3b5fa7de6790f2e63ed8eb509639
4
- data.tar.gz: f5079328af526df44ce25c107cf078a3536ce797962e11d309f8df8707c9b786
3
+ metadata.gz: 187a9226c75ccd071bd9c32bf599f3d0b8badf1888fc4d09dd656d815b0af2ff
4
+ data.tar.gz: fa7e12a3f24c9bc3a0fe2e3bc455b4b09e3ee3fd2fa90371886e49d3eda2f5c3
5
5
  SHA512:
6
- metadata.gz: 8790a32d160b0735eb142cbd08afb37c0a56fe9cbf9481fd1d019c677e0218a039f9e944cb2c80389e922cb961b2cc52e16578eb73fe6cc1af10769713b797ba
7
- data.tar.gz: e73f904229c0752092ee9991ad74d5bf4dbda6f774a8207467ec79cf66e51a37fd51700ad26c77d2936ff3d18dc8ca8a83e111ac3cb976ac10b089288a1c5391
6
+ metadata.gz: 9c9ced17e3a32dd6a91e3863e589d645b0ac6f0b370ce6ba94c8abc7ae645c5f5e3c1d81ef4c15c919afcb8f9c2a931701de67a8304985346c5fc1370e6dad05
7
+ data.tar.gz: c62f77b8707d77af40ea9e32c8151c7b8aae07975ac2032b9b73d31b6664c61d9eac7d8159f0842d7bdc202e9fb67504a785b4ef95366020c9fbffc3a721074f
@@ -1,3 +1,7 @@
1
+ # v0.1.5 2020-06-17
2
+
3
+ - Added `NxtSupport::AssignableValues`
4
+
1
5
  # v0.1.3 2020-02-19
2
6
 
3
7
  - Added `NxtSupport::IndifferentlyAccessibleJsonAttrs`
@@ -1,63 +1,63 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- nxt_support (0.1.4)
4
+ nxt_support (0.1.5)
5
5
  nxt_init
6
6
  rails
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- actioncable (6.0.3.1)
12
- actionpack (= 6.0.3.1)
11
+ actioncable (6.0.3.2)
12
+ actionpack (= 6.0.3.2)
13
13
  nio4r (~> 2.0)
14
14
  websocket-driver (>= 0.6.1)
15
- actionmailbox (6.0.3.1)
16
- actionpack (= 6.0.3.1)
17
- activejob (= 6.0.3.1)
18
- activerecord (= 6.0.3.1)
19
- activestorage (= 6.0.3.1)
20
- activesupport (= 6.0.3.1)
15
+ actionmailbox (6.0.3.2)
16
+ actionpack (= 6.0.3.2)
17
+ activejob (= 6.0.3.2)
18
+ activerecord (= 6.0.3.2)
19
+ activestorage (= 6.0.3.2)
20
+ activesupport (= 6.0.3.2)
21
21
  mail (>= 2.7.1)
22
- actionmailer (6.0.3.1)
23
- actionpack (= 6.0.3.1)
24
- actionview (= 6.0.3.1)
25
- activejob (= 6.0.3.1)
22
+ actionmailer (6.0.3.2)
23
+ actionpack (= 6.0.3.2)
24
+ actionview (= 6.0.3.2)
25
+ activejob (= 6.0.3.2)
26
26
  mail (~> 2.5, >= 2.5.4)
27
27
  rails-dom-testing (~> 2.0)
28
- actionpack (6.0.3.1)
29
- actionview (= 6.0.3.1)
30
- activesupport (= 6.0.3.1)
28
+ actionpack (6.0.3.2)
29
+ actionview (= 6.0.3.2)
30
+ activesupport (= 6.0.3.2)
31
31
  rack (~> 2.0, >= 2.0.8)
32
32
  rack-test (>= 0.6.3)
33
33
  rails-dom-testing (~> 2.0)
34
34
  rails-html-sanitizer (~> 1.0, >= 1.2.0)
35
- actiontext (6.0.3.1)
36
- actionpack (= 6.0.3.1)
37
- activerecord (= 6.0.3.1)
38
- activestorage (= 6.0.3.1)
39
- activesupport (= 6.0.3.1)
35
+ actiontext (6.0.3.2)
36
+ actionpack (= 6.0.3.2)
37
+ activerecord (= 6.0.3.2)
38
+ activestorage (= 6.0.3.2)
39
+ activesupport (= 6.0.3.2)
40
40
  nokogiri (>= 1.8.5)
41
- actionview (6.0.3.1)
42
- activesupport (= 6.0.3.1)
41
+ actionview (6.0.3.2)
42
+ activesupport (= 6.0.3.2)
43
43
  builder (~> 3.1)
44
44
  erubi (~> 1.4)
45
45
  rails-dom-testing (~> 2.0)
46
46
  rails-html-sanitizer (~> 1.1, >= 1.2.0)
47
- activejob (6.0.3.1)
48
- activesupport (= 6.0.3.1)
47
+ activejob (6.0.3.2)
48
+ activesupport (= 6.0.3.2)
49
49
  globalid (>= 0.3.6)
50
- activemodel (6.0.3.1)
51
- activesupport (= 6.0.3.1)
52
- activerecord (6.0.3.1)
53
- activemodel (= 6.0.3.1)
54
- activesupport (= 6.0.3.1)
55
- activestorage (6.0.3.1)
56
- actionpack (= 6.0.3.1)
57
- activejob (= 6.0.3.1)
58
- activerecord (= 6.0.3.1)
50
+ activemodel (6.0.3.2)
51
+ activesupport (= 6.0.3.2)
52
+ activerecord (6.0.3.2)
53
+ activemodel (= 6.0.3.2)
54
+ activesupport (= 6.0.3.2)
55
+ activestorage (6.0.3.2)
56
+ actionpack (= 6.0.3.2)
57
+ activejob (= 6.0.3.2)
58
+ activerecord (= 6.0.3.2)
59
59
  marcel (~> 0.3.1)
60
- activesupport (6.0.3.1)
60
+ activesupport (6.0.3.2)
61
61
  concurrent-ruby (~> 1.0, >= 1.0.2)
62
62
  i18n (>= 0.7, < 2)
63
63
  minitest (~> 5.1)
@@ -73,7 +73,7 @@ GEM
73
73
  activesupport (>= 4.2.0)
74
74
  i18n (1.8.3)
75
75
  concurrent-ruby (~> 1.0)
76
- loofah (2.5.0)
76
+ loofah (2.6.0)
77
77
  crass (~> 1.0.2)
78
78
  nokogiri (>= 1.5.9)
79
79
  mail (2.7.1)
@@ -93,32 +93,32 @@ GEM
93
93
  pry (0.13.1)
94
94
  coderay (~> 1.1)
95
95
  method_source (~> 1.0)
96
- rack (2.2.2)
96
+ rack (2.2.3)
97
97
  rack-test (1.1.0)
98
98
  rack (>= 1.0, < 3)
99
- rails (6.0.3.1)
100
- actioncable (= 6.0.3.1)
101
- actionmailbox (= 6.0.3.1)
102
- actionmailer (= 6.0.3.1)
103
- actionpack (= 6.0.3.1)
104
- actiontext (= 6.0.3.1)
105
- actionview (= 6.0.3.1)
106
- activejob (= 6.0.3.1)
107
- activemodel (= 6.0.3.1)
108
- activerecord (= 6.0.3.1)
109
- activestorage (= 6.0.3.1)
110
- activesupport (= 6.0.3.1)
99
+ rails (6.0.3.2)
100
+ actioncable (= 6.0.3.2)
101
+ actionmailbox (= 6.0.3.2)
102
+ actionmailer (= 6.0.3.2)
103
+ actionpack (= 6.0.3.2)
104
+ actiontext (= 6.0.3.2)
105
+ actionview (= 6.0.3.2)
106
+ activejob (= 6.0.3.2)
107
+ activemodel (= 6.0.3.2)
108
+ activerecord (= 6.0.3.2)
109
+ activestorage (= 6.0.3.2)
110
+ activesupport (= 6.0.3.2)
111
111
  bundler (>= 1.3.0)
112
- railties (= 6.0.3.1)
112
+ railties (= 6.0.3.2)
113
113
  sprockets-rails (>= 2.0.0)
114
114
  rails-dom-testing (2.0.3)
115
115
  activesupport (>= 4.2.0)
116
116
  nokogiri (>= 1.6)
117
117
  rails-html-sanitizer (1.3.0)
118
118
  loofah (~> 2.3)
119
- railties (6.0.3.1)
120
- actionpack (= 6.0.3.1)
121
- activesupport (= 6.0.3.1)
119
+ railties (6.0.3.2)
120
+ actionpack (= 6.0.3.2)
121
+ activesupport (= 6.0.3.2)
122
122
  method_source
123
123
  rake (>= 0.8.7)
124
124
  thor (>= 0.20.3, < 2.0)
data/README.md CHANGED
@@ -39,7 +39,7 @@ This mixin provides the `indifferently_accessible_json_attrs` class method which
39
39
  ```ruby
40
40
  class MyModel < ApplicationRecord
41
41
  include IndifferentlyAccessibleJsonAttrs
42
-
42
+
43
43
  indifferently_accessible_json_attrs :data
44
44
  end
45
45
  ```
@@ -58,6 +58,44 @@ end
58
58
  Book.safely_find_or_create_by!(market: 'de', title: 'Moche!')
59
59
  ```
60
60
 
61
+ #### NxtSupport::AssignableValues
62
+
63
+ The `NxtSupport::AssignableValues` allows you to restrict the possible values for your ActiveRecord model attributes. These values will only be checked on new records or if the attribute in question has been changed, so existing records will not be affected even if they contain invalid values.
64
+
65
+ ```ruby
66
+ class Book < ApplicationRecord
67
+ include NxtSupport::AssignableValues
68
+
69
+ # You can use a block
70
+ assignable_values_for :genre do
71
+ %w[fantasy adventure crime historical]
72
+ end
73
+
74
+ # Or you can also use an array argument
75
+ assignable_values_for :genre, %w[fantasy adventure crime historical]
76
+ end
77
+
78
+ book = Book.new(genre: 'fantasy', title: 'Moche!')
79
+ book.valid? #=> true
80
+
81
+ book.genre = 'comedy'
82
+ book.valid? #=> false
83
+
84
+ book.valid_genres #=> ["fantasy", "adventure", "crime", "historical"]
85
+ ```
86
+
87
+ A default can be included
88
+ ```ruby
89
+ assignable_values_for :genre, default: 'crime' do
90
+ %w[fantasy adventure crime historical]
91
+ end
92
+ ```
93
+ ```ruby
94
+ book = Book.new(title: 'Moche!')
95
+ book.genre #=> crime
96
+ ```
97
+ If the default value is not in the list of assignable values, then validation will fail.
98
+
61
99
  ### NxtSupport/Serializers
62
100
 
63
101
  Enjoy mixins for your serializers.
@@ -69,7 +107,7 @@ This mixin provides your serializer classes with a `attribute_as_iso8601` and a
69
107
  ```ruby
70
108
  class MySerializer < ActiveModel::Serializer
71
109
  include NxtSupport::HasTimeAttributes
72
-
110
+
73
111
  attributes_as_iso8601 :created_at, :updated_at
74
112
  end
75
113
  ```
@@ -80,8 +118,8 @@ Enjoy some useful utilities
80
118
 
81
119
  #### NxtSupport::EnumHash
82
120
 
83
- `NxtSupport::EnumHash` is a simple hash with indifferent access to organize a collection of enums.
84
- Keys will be normalized to be underscore and downcase and access with [] is raising a `KeyError` in case there is
121
+ `NxtSupport::EnumHash` is a simple hash with indifferent access to organize a collection of enums.
122
+ Keys will be normalized to be underscore and downcase and access with [] is raising a `KeyError` in case there is
85
123
  no value for the key.
86
124
 
87
125
  ```ruby
@@ -99,7 +137,7 @@ Book::STATES['in_weird_state'] # 'in weird State'
99
137
 
100
138
  #### NxtSupport::HashTranslator
101
139
 
102
- `NxtSupport::HashTranslator` is a module that allows you to manipulate keys and values of original hash through tuple hash.
140
+ `NxtSupport::HashTranslator` is a module that allows you to manipulate keys and values of original hash through tuple hash.
103
141
  Tuple hash is a hash where `key` - represent's the key of original hash, and `value` - represents the key of result hash.
104
142
  Use `#translate_hash` method to get the result hash.
105
143
 
@@ -112,7 +150,7 @@ TestClass.translate_hash(firstname: 'John', firstname: :first_name)
112
150
  => { 'first_name' => 'John' }
113
151
  ```
114
152
  The `value` also could be a `Hash` where key represents the new key in result hash and value must be a lambda or Proc
115
- that would be used to process value from origin hash. If the tuple hash contains more than 1 key-value paris or value in key value pair
153
+ that would be used to process value from origin hash. If the tuple hash contains more than 1 key-value paris or value in key value pair
116
154
  is not a callable block `InvalidTranslationArgument` error would be raised.
117
155
 
118
156
  ```ruby
@@ -1,3 +1,4 @@
1
1
  require "nxt_support/models/email"
2
2
  require "nxt_support/models/safely_find_or_createable"
3
3
  require "nxt_support/models/indifferently_accessible_json_attrs"
4
+ require "nxt_support/models/assignable_values"
@@ -0,0 +1,84 @@
1
+ module NxtSupport
2
+ module AssignableValues
3
+ extend ActiveSupport::Concern
4
+
5
+ class_methods do
6
+ attr_reader :assignable_values
7
+
8
+ def assignable_values_for(column, values = [], **options, &block)
9
+ @assignable_values ||= {}
10
+ @assignable_values[column.to_sym] = {
11
+ values: (block_given? ? block.call : values),
12
+ default: options[:default],
13
+ allow_blank: options[:allow_blank]
14
+ }
15
+
16
+ define_assignable_instance_methods
17
+ end
18
+
19
+ def assignable_values_list_for(column)
20
+ assignable_values[column][:values]
21
+ end
22
+
23
+ def define_assignable_instance_methods
24
+ assignable_values.keys.each do |key|
25
+ define_method :"valid_#{key.to_s.pluralize}" do
26
+ self.class.assignable_values_list_for(key)
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ included do
33
+ validate :values_are_assignable
34
+ after_initialize :set_default_assignable
35
+ end
36
+
37
+ private
38
+
39
+ def values_are_assignable
40
+ attributes_to_validate.each do |attr|
41
+ next if changes_to_validate[attr].blank? && allow_blank_for?(attr)
42
+ next if changes_to_validate[attr].in?(assignable_values_for(attr))
43
+
44
+ if changes_to_validate[attr].blank?
45
+ errors.add(attr, "can't be blank.")
46
+ else
47
+ errors.add(attr, "the value #{changes_to_validate[attr]} is not in the list of acceptable values.")
48
+ end
49
+ end
50
+ end
51
+
52
+ def set_default_assignable
53
+ self.class.assignable_values.each do |column, assignable|
54
+ next unless new_record? && send(column).nil? && assignable[:default]
55
+
56
+ if assignable[:default].is_a? Proc
57
+ send("#{column}=", instance_exec(&assignable[:default]))
58
+ else
59
+ send("#{column}=", assignable[:default])
60
+ end
61
+ end
62
+ end
63
+
64
+ def allow_blank_for?(attr)
65
+ self.class.assignable_values[attr][:allow_blank]
66
+ end
67
+
68
+ def assignable_values_for(attr)
69
+ self.class.assignable_values[attr][:values]
70
+ end
71
+
72
+ def attributes_to_validate
73
+ new_record? ? self.class.assignable_values.keys : (changed_keys & self.class.assignable_values.keys)
74
+ end
75
+
76
+ def changed_keys
77
+ changes.keys.map(&:to_sym)
78
+ end
79
+
80
+ def changes_to_validate
81
+ new_record? ? attributes.symbolize_keys : changes.transform_values(&:last).symbolize_keys
82
+ end
83
+ end
84
+ end
@@ -1,3 +1,3 @@
1
1
  module NxtSupport
2
- VERSION = "0.1.4".freeze
2
+ VERSION = "0.1.5".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nxt_support
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nils Sommer
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2020-06-17 00:00:00.000000000 Z
12
+ date: 2020-06-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -134,6 +134,7 @@ files:
134
134
  - bin/setup
135
135
  - lib/nxt_support.rb
136
136
  - lib/nxt_support/models.rb
137
+ - lib/nxt_support/models/assignable_values.rb
137
138
  - lib/nxt_support/models/email.rb
138
139
  - lib/nxt_support/models/indifferently_accessible_json_attrs.rb
139
140
  - lib/nxt_support/models/safely_find_or_createable.rb
@@ -167,7 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
167
168
  - !ruby/object:Gem::Version
168
169
  version: '0'
169
170
  requirements: []
170
- rubygems_version: 3.1.2
171
+ rubygems_version: 3.0.3
171
172
  signing_key:
172
173
  specification_version: 4
173
174
  summary: Support through reusable Mixins and Helpers for Ruby on Rails Applications