Class: EmailStyleUpdater

Inherits:
Object
  • Object
show all
Defined in:
app/services/email_style_updater.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ EmailStyleUpdater

Returns a new instance of EmailStyleUpdater.



6
7
8
9
# File 'app/services/email_style_updater.rb', line 6

def initialize(user)
  @user = user
  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



4
5
6
# File 'app/services/email_style_updater.rb', line 4

def errors
  @errors
end

Instance Method Details

#update(attrs) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/services/email_style_updater.rb', line 11

def update(attrs)
  if attrs.has_key?(:html) && !attrs[:html].include?("%{email_content}")
    @errors << I18n.t("email_style.html_missing_placeholder", placeholder: "%{email_content}")
  end

  if attrs.has_key?(:css)
    begin
      compiled_css = SassC::Engine.new(attrs[:css], style: :compressed).render
    rescue SassC::SyntaxError => e
      # @errors << I18n.t('email_style.css_syntax_error')
      @errors << e.message[0...(e.message.index("\n"))]
    end
  end

  return false unless @errors.empty?

  if attrs.has_key?(:html)
    if attrs[:html] == EmailStyle.default_template
      SiteSetting.remove_override!(:email_custom_template)
    else
      SiteSetting.email_custom_template = attrs[:html]
    end
  end

  if attrs.has_key?(:css)
    if attrs[:css] == EmailStyle.default_css
      SiteSetting.remove_override!(:email_custom_css)
      SiteSetting.remove_override!(:email_custom_css_compiled)
    else
      SiteSetting.email_custom_css = attrs[:css]
      SiteSetting.email_custom_css_compiled = compiled_css
    end
  end

  @errors.empty?
end