Class: Sibu::DynamicStyle

Inherits:
Object
  • Object
show all
Defined in:
app/models/sibu/dynamic_style.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site_id) ⇒ DynamicStyle

Returns a new instance of DynamicStyle.



6
7
8
9
10
11
12
# File 'app/models/sibu/dynamic_style.rb', line 6

def initialize(site_id)
  @site = Sibu::Site.find(site_id)
  @filename = "#{site_id}_#{Time.current.to_i}"
  @scss_file = File.new(scss_file_path, 'w')
  @body = ERB.new(File.read(template_file_path)).result(binding)
  @env = Rails.application.assets
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



4
5
6
# File 'app/models/sibu/dynamic_style.rb', line 4

def body
  @body
end

#envObject (readonly)

Returns the value of attribute env.



4
5
6
# File 'app/models/sibu/dynamic_style.rb', line 4

def env
  @env
end

#filenameObject (readonly)

Returns the value of attribute filename.



4
5
6
# File 'app/models/sibu/dynamic_style.rb', line 4

def filename
  @filename
end

#scss_fileObject (readonly)

Returns the value of attribute scss_file.



4
5
6
# File 'app/models/sibu/dynamic_style.rb', line 4

def scss_file
  @scss_file
end

#siteObject (readonly)

Returns the value of attribute site.



4
5
6
# File 'app/models/sibu/dynamic_style.rb', line 4

def site
  @site
end

Class Method Details

.refresh_stylesObject



32
33
34
35
36
# File 'app/models/sibu/dynamic_style.rb', line 32

def self.refresh_styles
  Sibu::Site.all.each do |s|
    Sibu::DynamicStyle.new(s.id).compile
  end
end

Instance Method Details

#compileObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/models/sibu/dynamic_style.rb', line 14

def compile
  find_or_create_scss

  begin
    scss_file.write generate_css
    scss_file.flush
    scss_file.close
    css_file_path = scss_file_path.gsub('scss', 'css')
    File.rename(scss_file_path, css_file_path)
    site.update(style: File.new(css_file_path))
  rescue Exception => ex
      Rails.logger.error(ex)
  ensure
    File.delete(scss_file_path) if scss_file_path && File.exist?(scss_file_path)
    File.delete(css_file_path) if css_file_path && File.exist?(css_file_path)
  end
end