Module: Frails::Monkey::ActionView::PartialRenderer

Defined in:
lib/frails/monkey/action_view/partial_renderer.rb

Instance Method Summary collapse

Instance Method Details

#build_ident(local_name, path) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/frails/monkey/action_view/partial_renderer.rb', line 50

def build_ident(local_name, path)
  hash_digest = Digest::MD5.hexdigest("#{path}+#{local_name}")[0, 6]

  return "#{local_name}-#{hash_digest}" unless Frails.dev_server.running?

  name = path.basename.sub(path.extname, '').sub('.', '-')
  ident = +"#{name}__#{local_name}___#{hash_digest}"
  ident.prepend("#{path.dirname.to_s.tr('/', '-')}-")
  ident
end

#build_rendered_template(content, template, layout = nil) ⇒ Object



27
28
29
30
# File 'lib/frails/monkey/action_view/partial_renderer.rb', line 27

def build_rendered_template(content, template, layout = nil)
  content = transform_css_modules(content).html_safe if @asset_path
  super
end

#class_name_for_style(class_names, path) ⇒ Object



46
47
48
# File 'lib/frails/monkey/action_view/partial_renderer.rb', line 46

def class_name_for_style(class_names, path)
  class_names.to_s.split.map { |class_name| build_ident class_name, path }
end

#render_collection(view, template) ⇒ Object



7
8
9
10
11
12
# File 'lib/frails/monkey/action_view/partial_renderer.rb', line 7

def render_collection(view, template)
  # Side load partial assets - if any.
  @asset_path = @side_load_assets && side_load_assets(view, template)

  super
end

#render_partial(view, template) ⇒ Object



14
15
16
17
18
19
# File 'lib/frails/monkey/action_view/partial_renderer.rb', line 14

def render_partial(view, template)
  # Side load partial assets - if any.
  @asset_path = @side_load_assets && side_load_assets(view, template)

  super
end

#setup(context, options, as, block) ⇒ Object



21
22
23
24
25
# File 'lib/frails/monkey/action_view/partial_renderer.rb', line 21

def setup(context, options, as, block)
  @side_load_assets = options.key?(:side_load_assets) ? options[:side_load_assets] : false

  super
end

#stylesheet_path_for_identObject



61
62
63
64
65
# File 'lib/frails/monkey/action_view/partial_renderer.rb', line 61

def stylesheet_path_for_ident
  return if (globs = Rails.root.glob("app/#{@asset_path}.{css,scss,sass,less}")).empty?

  globs.first.relative_path_from(Rails.root)
end

#transform_css_modules(content) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/frails/monkey/action_view/partial_renderer.rb', line 32

def transform_css_modules(content)
  doc = Nokogiri::HTML::DocumentFragment.parse(content)

  return content if (modules = doc.css('[css_module]')).empty?
  return content unless (path = stylesheet_path_for_ident)

  modules.each do |ele|
    classes = class_name_for_style(ele.delete('css_module'), path)
    ele['class'] = (ele['class'].nil? ? classes : classes << ele['class']).join(' ')
  end

  doc.to_html
end