Class: Decidim::DecidimAwesome::Admin::ConfigForm

Inherits:
Form
  • Object
show all
Includes:
ActionView::Helpers::SanitizeHelper
Defined in:
app/forms/decidim/decidim_awesome/admin/config_form.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#valid_keysObject

collect all keys anything not specified in the params (UpdateConfig command ignores it)



35
36
37
# File 'app/forms/decidim/decidim_awesome/admin/config_form.rb', line 35

def valid_keys
  @valid_keys
end

Class Method Details

.from_params(params, additional_params = {}) ⇒ Object

TODO: validate non general admins are here



48
49
50
51
52
53
# File 'app/forms/decidim/decidim_awesome/admin/config_form.rb', line 48

def self.from_params(params, additional_params = {})
  instance = super(params, additional_params)
  instance.valid_keys = params.keys.map(&:to_sym) || []
  instance.sanitize_labels!
  instance
end

Instance Method Details

#css_syntaxObject



55
56
57
58
59
60
61
62
63
64
# File 'app/forms/decidim/decidim_awesome/admin/config_form.rb', line 55

def css_syntax
  scoped_styles.each do |key, code|
    next unless code

    SassC::Engine.new(code).render
  rescue SassC::SyntaxError => e
    errors.add(:scoped_styles, I18n.t("config.form.errors.incorrect_css", key: key, scope: "decidim.decidim_awesome.admin"))
    errors.add(key.to_sym, e.message)
  end
end

#json_syntaxObject



66
67
68
69
70
71
72
73
74
75
# File 'app/forms/decidim/decidim_awesome/admin/config_form.rb', line 66

def json_syntax
  proposal_custom_fields.each do |key, code|
    next unless code

    JSON.parse(code)
  rescue JSON::ParserError => e
    errors.add(:scoped_styles, I18n.t("config.form.errors.incorrect_json", key: key, scope: "decidim.decidim_awesome.admin"))
    errors.add(key.to_sym, e.message)
  end
end

#sanitize_labels!Object

formBuilder has a bug and do not sanitize text if users copy/paste text with format in the label input



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/forms/decidim/decidim_awesome/admin/config_form.rb', line 78

def sanitize_labels!
  return unless proposal_custom_fields

  proposal_custom_fields.transform_values! do |code|
    next unless code

    json = JSON.parse(code)
    json.map! do |item|
      item["label"] = strip_tags(item["label"])
      item
    end
    JSON.generate(json)
  rescue JSON::ParserError
    code
  end
end