Module: Ksk::FrontendHelper

Defined in:
app/helpers/ksk/frontend_helper.rb

Instance Method Summary collapse

Instance Method Details

#kskmd(model_instance) ⇒ Object



22
23
24
# File 'app/helpers/ksk/frontend_helper.rb', line 22

def kskmd(model_instance)
  mdap(model_instance.content_long, model_instance.assets.only_images, model_instance.assets.only_data_files)
end

#md(text) ⇒ Object



26
27
28
29
30
# File 'app/helpers/ksk/frontend_helper.rb', line 26

def md(text)
  return '' if text.blank?
  markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML.new(), fenced_code_blocks: true, autolink: true)
  markdown.render(text).html_safe
end

#mdap(text, images, data_files) ⇒ Object



32
33
34
35
36
37
# File 'app/helpers/ksk/frontend_helper.rb', line 32

def mdap(text, images, data_files)
  return '' if text.blank?
  t = place_images(text, images)
  t = place_data_files(t, data_files)
  md(t)
end

#place_content(text, files, type, &html) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'app/helpers/ksk/frontend_helper.rb', line 39

def place_content(text, files, type, &html)
  t = text
  Ksk::Markdown.parse_text(text, type).each do |match|
    relpace = if asset = files[match[1].to_i-1]
      html.call(asset, match[2])
    end
    t = t.gsub(match[0], relpace.to_s)
  end
  t.html_safe
end

#place_data_files(text, data_files) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/helpers/ksk/frontend_helper.rb', line 57

def place_data_files(text, data_files)
  place_content(text, data_files, 'file') do |asset, option|
    link_to asset.file.url, title: asset.file.original_filename, class: asset.has_preview? ? :uploaded_file_preview : :uploaded_file do
      if asset.has_preview?
        if asset.has_preview_image?
          t_capition = !asset.preview.name.blank? ? asset.preview.name : t('ksk.asset.file_preview.button.download')
          image_tag( asset.preview_file.url(:banner) )+
          "<span>#{t_capition}</span>".html_safe
        else
          asset.preview.name
        end
      else
        asset.file_file_name
      end
    end
  end
end

#place_images(text, images) ⇒ Object



50
51
52
53
54
55
# File 'app/helpers/ksk/frontend_helper.rb', line 50

def place_images(text, images)
  place_content(text, images, 'img') do |asset, option|
    c = option || 'thumb'
    image_tag asset.file.url(c), class: ['uploaded_image', c]
  end
end

#recursive_navi(navis, current_level = 0, max_level = 99) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/helpers/ksk/frontend_helper.rb', line 3

def recursive_navi(navis, current_level = 0, max_level = 99)
  a = ' <ul>'
  navis.each do |navi|
    active = navi.slug == @slugs[current_level] ? ' class="active"' : nil
    next unless nt = navi.navigation_type
    link = if nt.name == 'news'
      link_to navi.title, posts_path
    else
      link_to navi.title, navi.link
    end
    a += " <li#{active}>#{link}"
    if active && current_level <= max_level && navi.children.not_hidden.any?
      a += recursive_navi(navi.children.not_hidden, current_level+1, max_level)
    end
    a += '</li> '
  end
  a += '</ul> '
end