Class: SiteSettings::TypeSupervisor

Inherits:
Object
  • Object
show all
Includes:
Validations
Defined in:
lib/site_settings/type_supervisor.rb

Constant Summary collapse

CONSUMED_OPTS =
%i[
  enum
  choices
  type
  validator
  min
  max
  regex
  hidden
  regex_error
  allow_any
  list_type
  textarea
  json_schema
].freeze
VALIDATOR_OPTS =
%i[min max regex hidden regex_error json_schema].freeze
SUPPORTED_TYPES =

For plugins, so they can tell if a feature is supported

%i[email username list enum].freeze

Constants included from Validations

Validations::PROHIBITED_USER_AGENT_STRINGS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validations

#validate_backup_location, #validate_category_ids, #validate_cors_origins, #validate_default_categories, #validate_default_categories_muted, #validate_default_categories_normal, #validate_default_categories_tracking, #validate_default_categories_watching, #validate_default_categories_watching_first_post, #validate_default_tags, #validate_default_tags_muted, #validate_default_tags_tracking, #validate_default_tags_watching, #validate_default_tags_watching_first_post, #validate_enable_local_logins, #validate_enable_page_publishing, #validate_enable_s3_inventory, #validate_enable_s3_uploads, #validate_enforce_second_factor, #validate_error, #validate_s3_backup_bucket, #validate_s3_upload_bucket, #validate_s3_use_acls, #validate_secure_uploads, #validate_share_quote_buttons, #validate_slow_down_crawler_user_agents, #validate_strip_image_metadata, #validate_twitter_summary_large_image

Constructor Details

#initialize(defaults_provider) ⇒ TypeSupervisor

Returns a new instance of TypeSupervisor.



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/site_settings/type_supervisor.rb', line 82

def initialize(defaults_provider)
  @defaults_provider = defaults_provider
  @enums = {}
  @static_types = {}
  @choices = {}
  @validators = {}
  @types = {}
  @allow_any = {}
  @list_type = {}
  @textareas = {}
  @json_schemas = {}
end

Class Method Details

.parse_value_type(val) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/site_settings/type_supervisor.rb', line 61

def self.parse_value_type(val)
  case val
  when NilClass
    self.types[:null]
  when String
    self.types[:string]
  when Integer
    self.types[:integer]
  when Float
    self.types[:float]
  when TrueClass, FalseClass
    self.types[:bool]
  else
    raise ArgumentError.new :val
  end
end

.supported_typesObject



78
79
80
# File 'lib/site_settings/type_supervisor.rb', line 78

def self.supported_types
  SUPPORTED_TYPES
end

.typesObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/site_settings/type_supervisor.rb', line 29

def self.types
  @types ||=
    Enum.new(
      string: 1,
      time: 2,
      integer: 3,
      float: 4,
      bool: 5,
      null: 6,
      enum: 7,
      list: 8,
      url_list: 9,
      host_list: 10,
      category_list: 11,
      value_list: 12,
      regex: 13,
      email: 14,
      username: 15,
      category: 16,
      uploaded_image_list: 17,
      upload: 18,
      group: 19,
      group_list: 20,
      tag_list: 21,
      color: 22,
      simple_list: 23,
      emoji_list: 24,
      html_deprecated: 25,
      tag_group_list: 26,
    )
end

Instance Method Details

#get_list_type(name) ⇒ Object



199
200
201
# File 'lib/site_settings/type_supervisor.rb', line 199

def get_list_type(name)
  @list_type[name.to_sym]
end

#get_type(name) ⇒ Object



195
196
197
# File 'lib/site_settings/type_supervisor.rb', line 195

def get_type(name)
  self.class.types[@types[name.to_sym]]
end

#load_setting(name_arg, opts = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/site_settings/type_supervisor.rb', line 95

def load_setting(name_arg, opts = {})
  name = name_arg.to_sym

  @textareas[name] = opts[:textarea] if opts[:textarea]

  @json_schemas[name] = opts[:json_schema].constantize if opts[:json_schema]

  if (enum = opts[:enum])
    @enums[name] = enum.is_a?(String) ? enum.constantize : enum
    opts[:type] ||= :enum
  end

  if (new_choices = opts[:choices])
    new_choices = eval(new_choices) if new_choices.is_a?(String) # rubocop:disable Security/Eval

    if @choices.has_key?(name)
      @choices[name].concat(new_choices)
    else
      @choices[name] = new_choices
    end
  end

  if (type = opts[:type])
    @static_types[name] = type.to_sym

    if type.to_sym == :list
      @allow_any[name] = opts[:allow_any] == false ? false : true
      @list_type[name] = opts[:list_type] if opts[:list_type]
    end
  end
  @types[name] = get_data_type(name, @defaults_provider[name])

  opts[:validator] = opts[:validator].try(:constantize)
  if (validator_type = (opts[:validator] || validator_for(@types[name])))
    validator_opts = opts.slice(*VALIDATOR_OPTS)
    validator_opts[:name] = name
    @validators[name] = { class: validator_type, opts: validator_opts }
  end
end

#to_db_value(name, value) ⇒ Object



160
161
162
163
164
# File 'lib/site_settings/type_supervisor.rb', line 160

def to_db_value(name, value)
  val, type = normalize_input(name, value)
  validate_value(name, type, val)
  [val, type]
end

#to_rb_value(name, value, override_type = nil) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/site_settings/type_supervisor.rb', line 135

def to_rb_value(name, value, override_type = nil)
  name = name.to_sym
  @types[name] = (@types[name] || get_data_type(name, value))
  type = (override_type || @types[name])

  case type
  when self.class.types[:float]
    value.to_f
  when self.class.types[:integer]
    value.to_i
  when self.class.types[:bool]
    value == true || value == "t" || value == "true"
  when self.class.types[:null]
    nil
  when self.class.types[:enum]
    @defaults_provider[name].is_a?(Integer) ? value.to_i : value.to_s
  when self.class.types[:string]
    value.to_s
  else
    return value if self.class.types[type]
    # Otherwise it's a type error
    raise ArgumentError.new :type
  end
end

#type_hash(name) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/site_settings/type_supervisor.rb', line 166

def type_hash(name)
  name = name.to_sym
  type = get_type(name)

  result = { type: type.to_s }

  if type == :enum
    if (klass = enum_class(name))
      result.merge!(valid_values: klass.values, translate_names: klass.translate_names?)
    else
      result.merge!(
        valid_values: @choices[name].map { |c| { name: c, value: c } },
        translate_names: false,
      )
    end
  end

  result[:allow_any] = @allow_any[name] if type == :list

  result[:choices] = @choices[name] if @choices.has_key? name
  result[:list_type] = @list_type[name] if @list_type.has_key? name
  result[:textarea] = @textareas[name] if @textareas.has_key? name
  if @json_schemas.has_key?(name) && json_klass = json_schema_class(name)
    result[:json_schema] = json_klass.schema
  end

  result
end