Module: Marfa::Helpers::Style

Included in:
Controllers::CssController
Defined in:
lib/marfa/helpers/style.rb

Instance Method Summary collapse

Instance Method Details

#create_style(scss_path, device) ⇒ Object

Create styles



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/marfa/helpers/style.rb', line 60

def create_style(scss_path, device)
  dynamic_vars(device)

  if Marfa.config.minify_css
    output = scss(:"#{scss_path}", { style: :compressed, cache: false })
    output = Csso.optimize(output)
  else
    output = scss(:"#{scss_path}", { style: :expanded, cache: false })
  end

  output
end

#dynamic_vars(device, section = 'root') ⇒ Object

Pass dynamic vars to sass-files



12
13
14
15
16
17
# File 'lib/marfa/helpers/style.rb', line 12

def dynamic_vars(device, section = 'root')
  Sass::Plugin.options[:custom] ||= {}
  Sass::Plugin.options[:custom][:device] = device
  Sass::Plugin.options[:custom][:section] = section
  Sass::Plugin.options[:custom][:contentPath] = Marfa.config.content_path
end

#render_style(options) ⇒ Object

Rendering style



27
28
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
# File 'lib/marfa/helpers/style.rb', line 27

def render_style(options)
  return if options[:device].nil?

  root_path = options[:root_path] || '/'
  path_to_css = settings.public_folder.to_s + '/css' + root_path

  file_name =
    [options[:section], options[:range], options[:device]]
    .reject { |opt| opt.nil? }
    .join('.') + '.css'

  full_path = path_to_css + file_name

  scss_path =
    root_path +
    [options[:section], options[:range]]
      .reject { |opt| opt.nil? }
      .join('/')

  if File.exist?(full_path) && Marfa.config.cache_styles
    send_file(full_path, type: 'text/css')
  else
    FileUtils.mkdir_p(path_to_css) unless Dir.exist?(path_to_css)
    styles = create_style(scss_path, options[:device])
    File.write(full_path, styles) if Marfa.config.cache_styles
    content_type 'text/css', charset: 'utf-8', cache: 'false'
    styles
  end
end