Module: ThecoreSettings::Validation

Included in:
Setting
Defined in:
lib/thecore_settings/validation.rb

Class Method Summary collapse

Class Method Details

.add_color_validator(base) ⇒ Object



28
29
30
# File 'lib/thecore_settings/validation.rb', line 28

def add_color_validator(base)
  base.validates_with(ThecoreSettings::HexColorValidator, attributes: :raw, if: :color_kind?)
end

.add_email_validator(base) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/thecore_settings/validation.rb', line 40

def add_email_validator(base)
  base.validate if: :email_kind? do
    require_validates_email_format_of do
      errors.add(:raw, I18n.t('admin.settings.email_invalid')) unless raw.blank? || ValidatesEmailFormatOf.validate_email_format(raw).nil?
    end
  end
end

.add_file_validator(base) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/thecore_settings/validation.rb', line 32

def add_file_validator(base)
  base.validate if: :file_kind? do
    unless Settings.file_uploads_supported
      raise '[thecore_settings] File kind requires either CarrierWave or Paperclip or Shrine. Check that thecore_settings is below them in Gemfile'
    end
  end
end

.add_geo_validator(base) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/thecore_settings/validation.rb', line 83

def add_geo_validator(base)
  base.validate if: :address_kind? do
    require_geocoder do
      # just raise error if we are trying to use address kind without geocoder
    end
  end
  if Object.const_defined?('Geocoder')
    if ThecoreSettings.mongoid?
      base.field(:coordinates, type: Array)
      base.send(:include, Geocoder::Model::Mongoid)
    end
    base.geocoded_by(:raw)
    base.after_validation(:geocode, if: :address_kind?)
  end
end

.add_json_validator(base) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/thecore_settings/validation.rb', line 113

def add_json_validator(base)
  base.validate if: :json_kind? do
    unless raw.blank?
      begin
        JSON.load(raw)
      rescue JSON::ParserError => e
        errors.add(:raw, I18n.t('admin.settings.json_invalid'))
      end
    end
  end
end

.add_phone_validator(base) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/thecore_settings/validation.rb', line 62

def add_phone_validator(base)
  base.validate if: :phone_kind? do
    require_russian_phone do
      errors.add(:raw, I18n.t('admin.settings.phone_invalid')) unless raw.blank? || RussianPhone::Number.new(raw).valid?
    end
  end

  base.validate if: :phones_kind? do
    require_russian_phone do
      unless raw.blank?
        invalid_phones = raw.gsub("\r", '').split("\n").inject([]) do |memo, value|
          memo << value unless RussianPhone::Number.new(value).valid?
          memo
        end
        errors.add(:raw, I18n.t('admin.settings.phones_invalid', phones: invalid_phones * ', ')) unless invalid_phones.empty?
      end
    end
  end

end

.add_url_validator(base) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/thecore_settings/validation.rb', line 48

def add_url_validator(base)
  base.before_validation if: :url_kind? do
    require_addressable do
      self.raw = Addressable::URI.heuristic_parse(self.raw) unless self.raw.blank?
    end
  end

  base.before_validation if: :domain_kind? do
    require_addressable do
      self.raw = Addressable::URI.heuristic_parse(self.raw).host unless self.raw.blank?
    end
  end
end

.add_validators(base) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/thecore_settings/validation.rb', line 17

def add_validators(base)
  add_color_validator(base)
  add_file_validator(base)
  add_email_validator(base)
  add_url_validator(base)
  add_phone_validator(base)
  add_geo_validator(base)
  add_yaml_validator(base)
  add_json_validator(base)
end

.add_yaml_validator(base) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/thecore_settings/validation.rb', line 99

def add_yaml_validator(base)
  base.validate if: :yaml_kind? do
    require_safe_yaml do
      unless raw.blank?
        begin
          YAML.safe_load(raw)
        rescue Psych::SyntaxError => e
          errors.add(:raw, I18n.t('admin.settings.yaml_invalid'))
        end
      end
    end
  end
end

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/thecore_settings/validation.rb', line 4

def included(base)
  base.before_validation do
    self.raw = default_serializable_value if raw.blank?
  end
  base.before_validation :preprocess_value, if: :preprocessed_kind?
  base.validates_uniqueness_of :key, scope: :ns
  base.validates_inclusion_of :kind, in: ThecoreSettings.kinds
  base.validates_numericality_of :raw, if: :integer_kind?
  base.validates_numericality_of :raw, if: :float_kind?

  add_validators(base)
end