tanoshimu_utils 1.1.2 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/tanoshimu_utils/concerns.rb +4 -1
- data/lib/tanoshimu_utils/concerns/get_record.rb +13 -0
- data/lib/tanoshimu_utils/concerns/identifiable.rb +25 -0
- data/lib/tanoshimu_utils/concerns/respond_to_types.rb +19 -0
- data/lib/tanoshimu_utils/concerns/translatable.rb +46 -0
- data/lib/tanoshimu_utils/validators.rb +2 -0
- data/lib/tanoshimu_utils/validators/presence_one_of.rb +21 -0
- data/lib/tanoshimu_utils/validators/user_like.rb +17 -0
- data/lib/tanoshimu_utils/version.rb +1 -1
- metadata +8 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 94d8a2972a0f374b1444ddfa75ac5fb17f37476b1f4e9358a0224dc90c59292c
|
4
|
+
data.tar.gz: 8c75231567546dc72f66f2555c7f9f11342bc4e077aeb30647f1ea793bbfc18c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0c0e33da8f5af2787be93c424416339f3961d6750634e70bddd52ef3df381dd1fa77d37e8ea6abc5a74f7c132f985a9784ffb1d9463f3beee9c6a32b66c2b575
|
7
|
+
data.tar.gz: '0692d2bdab66af736da4a30208c12fa618651e677dc9f94a9cf6dbbb5aa840998bc99ff015177a577dfef0b6a18ae9cd2d583be40dcdbc46c74e56761fcdf687'
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module TanoshimuUtils
|
2
|
+
module Concerns
|
3
|
+
module Identifiable
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
before_validation :ensure_identification
|
8
|
+
|
9
|
+
validates :identification, presence: true
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def ensure_identification
|
15
|
+
raise ArgumentError, 'must respond to `identification`' unless respond_to?(:identification)
|
16
|
+
|
17
|
+
self.identification = SecureRandom.hex
|
18
|
+
|
19
|
+
until self.class.where(identification: self.identification)
|
20
|
+
self.identification = SecureRandom.hex
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module TanoshimuUtils
|
2
|
+
module Concerns
|
3
|
+
module RespondToTypes
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
class_methods do
|
7
|
+
def respond_to_types(types)
|
8
|
+
raise ArgumentError, 'Expected types Array' unless types.class == Array
|
9
|
+
|
10
|
+
types.each do |type|
|
11
|
+
send(:define_method, :"#{type}?") do
|
12
|
+
self.user_type == type
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module TanoshimuUtils
|
2
|
+
module Concerns
|
3
|
+
module Translatable
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
def metaclass
|
7
|
+
class << self
|
8
|
+
self
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class_methods do
|
13
|
+
def translates(field, through: [], default:)
|
14
|
+
raise 'Field must be present' unless field.present?
|
15
|
+
raise 'Through must be present' unless through.present?
|
16
|
+
|
17
|
+
if column_names.include?(field.to_s)
|
18
|
+
raise 'Field must not be defined on the model'
|
19
|
+
end
|
20
|
+
|
21
|
+
through = [through] unless through.kind_of?(Array)
|
22
|
+
default = default.to_sym
|
23
|
+
|
24
|
+
unless I18n.available_locales.include?(default)
|
25
|
+
raise "#{default} is not available on your system as a locale"
|
26
|
+
end
|
27
|
+
|
28
|
+
send(:define_method, field.to_sym) do
|
29
|
+
through.each do |possible_locale|
|
30
|
+
possible_locale = possible_locale.to_sym
|
31
|
+
|
32
|
+
unless I18n.available_locales.include?(possible_locale)
|
33
|
+
next
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
return nil unless through.include?(I18n.locale)
|
38
|
+
value = send(I18n.locale)
|
39
|
+
value = send(default) if value.nil? && default
|
40
|
+
value
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module TanoshimuUtils
|
2
|
+
module Validators
|
3
|
+
module PresenceOneOf
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
class AnyPresenceValidator < ActiveModel::Validator
|
7
|
+
def validate(record)
|
8
|
+
unless options[:fields].any?{|attr| record[attr].present?}
|
9
|
+
record.errors.add(:title, 'must be present (at lease one of en, fr, or jp)')
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class_methods do
|
15
|
+
def validate_presence_one_of(one_of)
|
16
|
+
validates_with AnyPresenceValidator, fields: one_of
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module TanoshimuUtils
|
2
|
+
module Validators
|
3
|
+
module UserLike
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
class_methods do
|
7
|
+
def validate_like_user(user_types:)
|
8
|
+
raise ArgumentError, 'Expected users types' unless user_types.class == Array
|
9
|
+
|
10
|
+
validates :username, presence: true, uniqueness: true
|
11
|
+
validates :name, presence: true
|
12
|
+
validates :user_type, inclusion: { in: user_types }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tanoshimu_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Akinyele Cafe-Febrissy
|
@@ -62,7 +62,14 @@ extra_rdoc_files: []
|
|
62
62
|
files:
|
63
63
|
- lib/tanoshimu_utils.rb
|
64
64
|
- lib/tanoshimu_utils/concerns.rb
|
65
|
+
- lib/tanoshimu_utils/concerns/get_record.rb
|
66
|
+
- lib/tanoshimu_utils/concerns/identifiable.rb
|
65
67
|
- lib/tanoshimu_utils/concerns/resource_fetch.rb
|
68
|
+
- lib/tanoshimu_utils/concerns/respond_to_types.rb
|
69
|
+
- lib/tanoshimu_utils/concerns/translatable.rb
|
70
|
+
- lib/tanoshimu_utils/validators.rb
|
71
|
+
- lib/tanoshimu_utils/validators/presence_one_of.rb
|
72
|
+
- lib/tanoshimu_utils/validators/user_like.rb
|
66
73
|
- lib/tanoshimu_utils/version.rb
|
67
74
|
homepage: https://github.com/thedrummeraki
|
68
75
|
licenses: []
|