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 +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +53 -53
- data/README.md +44 -6
- data/lib/nxt_support/models.rb +1 -0
- data/lib/nxt_support/models/assignable_values.rb +84 -0
- data/lib/nxt_support/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 187a9226c75ccd071bd9c32bf599f3d0b8badf1888fc4d09dd656d815b0af2ff
|
4
|
+
data.tar.gz: fa7e12a3f24c9bc3a0fe2e3bc455b4b09e3ee3fd2fa90371886e49d3eda2f5c3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9c9ced17e3a32dd6a91e3863e589d645b0ac6f0b370ce6ba94c8abc7ae645c5f5e3c1d81ef4c15c919afcb8f9c2a931701de67a8304985346c5fc1370e6dad05
|
7
|
+
data.tar.gz: c62f77b8707d77af40ea9e32c8151c7b8aae07975ac2032b9b73d31b6664c61d9eac7d8159f0842d7bdc202e9fb67504a785b4ef95366020c9fbffc3a721074f
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,63 +1,63 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
nxt_support (0.1.
|
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.
|
12
|
-
actionpack (= 6.0.3.
|
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.
|
16
|
-
actionpack (= 6.0.3.
|
17
|
-
activejob (= 6.0.3.
|
18
|
-
activerecord (= 6.0.3.
|
19
|
-
activestorage (= 6.0.3.
|
20
|
-
activesupport (= 6.0.3.
|
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.
|
23
|
-
actionpack (= 6.0.3.
|
24
|
-
actionview (= 6.0.3.
|
25
|
-
activejob (= 6.0.3.
|
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.
|
29
|
-
actionview (= 6.0.3.
|
30
|
-
activesupport (= 6.0.3.
|
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.
|
36
|
-
actionpack (= 6.0.3.
|
37
|
-
activerecord (= 6.0.3.
|
38
|
-
activestorage (= 6.0.3.
|
39
|
-
activesupport (= 6.0.3.
|
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.
|
42
|
-
activesupport (= 6.0.3.
|
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.
|
48
|
-
activesupport (= 6.0.3.
|
47
|
+
activejob (6.0.3.2)
|
48
|
+
activesupport (= 6.0.3.2)
|
49
49
|
globalid (>= 0.3.6)
|
50
|
-
activemodel (6.0.3.
|
51
|
-
activesupport (= 6.0.3.
|
52
|
-
activerecord (6.0.3.
|
53
|
-
activemodel (= 6.0.3.
|
54
|
-
activesupport (= 6.0.3.
|
55
|
-
activestorage (6.0.3.
|
56
|
-
actionpack (= 6.0.3.
|
57
|
-
activejob (= 6.0.3.
|
58
|
-
activerecord (= 6.0.3.
|
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.
|
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.
|
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.
|
96
|
+
rack (2.2.3)
|
97
97
|
rack-test (1.1.0)
|
98
98
|
rack (>= 1.0, < 3)
|
99
|
-
rails (6.0.3.
|
100
|
-
actioncable (= 6.0.3.
|
101
|
-
actionmailbox (= 6.0.3.
|
102
|
-
actionmailer (= 6.0.3.
|
103
|
-
actionpack (= 6.0.3.
|
104
|
-
actiontext (= 6.0.3.
|
105
|
-
actionview (= 6.0.3.
|
106
|
-
activejob (= 6.0.3.
|
107
|
-
activemodel (= 6.0.3.
|
108
|
-
activerecord (= 6.0.3.
|
109
|
-
activestorage (= 6.0.3.
|
110
|
-
activesupport (= 6.0.3.
|
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.
|
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.
|
120
|
-
actionpack (= 6.0.3.
|
121
|
-
activesupport (= 6.0.3.
|
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
|
data/lib/nxt_support/models.rb
CHANGED
@@ -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
|
data/lib/nxt_support/version.rb
CHANGED
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
|
+
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-
|
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.
|
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
|