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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 33432d9dccfa1494661fa1a9e1f6ce1975cfeb7c71a7caff9ecd27808e5470bd
4
- data.tar.gz: 83a9140a6a5ac35e423f0e7669d2c162f6b709ce811bc5bbcc4fd59196dc1fe7
3
+ metadata.gz: 94d8a2972a0f374b1444ddfa75ac5fb17f37476b1f4e9358a0224dc90c59292c
4
+ data.tar.gz: 8c75231567546dc72f66f2555c7f9f11342bc4e077aeb30647f1ea793bbfc18c
5
5
  SHA512:
6
- metadata.gz: b061dae5c2094bead2a467c2f0da0a2ca46496182588c7a468d100f81a336c432c4417366b1659ddcb34bb7bf92906b2e909655b60054a5f2ad4486c9d033259
7
- data.tar.gz: 9421268d549d6cd574853e8b6af5d7c3cc68a3f24c6a8059de68d8455a15e4d95579c3365e165893338628fd0cadec3a547ea8a48c935e456a9ce22f680516a6
6
+ metadata.gz: 0c0e33da8f5af2787be93c424416339f3961d6750634e70bddd52ef3df381dd1fa77d37e8ea6abc5a74f7c132f985a9784ffb1d9463f3beee9c6a32b66c2b575
7
+ data.tar.gz: '0692d2bdab66af736da4a30208c12fa618651e677dc9f94a9cf6dbbb5aa840998bc99ff015177a577dfef0b6a18ae9cd2d583be40dcdbc46c74e56761fcdf687'
@@ -1,2 +1,5 @@
1
+ require_relative 'concerns/get_record'
2
+ require_relative 'concerns/identifiable'
1
3
  require_relative 'concerns/resource_fetch'
2
-
4
+ require_relative 'concerns/respond_to_types'
5
+ require_relative 'concerns/translatable'
@@ -0,0 +1,13 @@
1
+ module TanoshimuUtils
2
+ module Concerns
3
+ module GetRecord
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ def record
8
+ used_by_model.classify.constantize.find(model_id)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -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,2 @@
1
+ require_relative 'validators/presence_one_of'
2
+ require_relative 'validators/user_like'
@@ -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
@@ -1,3 +1,3 @@
1
1
  module TanoshimuUtils
2
- VERSION = '1.1.2'
2
+ VERSION = '1.2.0'
3
3
  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.1.2
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: []