Module: CMS::Helper

Included in:
ViewTags
Defined in:
lib/cms/helper.rb

Instance Method Summary collapse

Instance Method Details

#cms_content_parse(content) ⇒ Object



90
91
92
93
# File 'lib/cms/helper.rb', line 90

def cms_content_parse content
  content = content.gsub(' ', ' ')
  CMS::ViewTags.instance.parse content, context: self
end

#cms_file(name, size = false) ⇒ Object



8
9
10
11
12
13
# File 'lib/cms/helper.rb', line 8

def cms_file name, size = false
  if file = CMS::FileUpload.find_by_name(name) then return file end
  opts = {name: name}
  opts[:description] = (size.sub('x', ' by ') << ' pixels') if size
  CMS::FileUpload.create(opts, without_protection: true)
end

#cms_image(name, size = false, width = '', height = '') ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cms/helper.rb', line 15

def cms_image name, size = false, width = '', height = ''
  width, height = size.split('x') if size
  image = cms_file(name, size)
  style = if size then "width: #{width}px ; height: #{height}px" else '' end

  if image.file?
    image_tag image.file.url, class: 'cms-image', style: style
  else
    if current_user && current_user.role.admin?
      link_to(name, edit_cms_file_upload_path(image), class: 'cms-image missing-cms-image', style: style)
    else
       :div, name, class: 'cms-image missing-cms-image', style: style
    end
  end
end

#cms_page_area(name, options = {}, &block) ⇒ Object



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
56
57
58
59
60
61
62
63
64
# File 'lib/cms/helper.rb', line 31

def cms_page_area name, options = {}, &block
  page_area = if area = CMS::PageArea.find_by_name(name) then area else CMS::PageArea.new({name: name}, without_protection: true) end
  options[:editable] = true unless options.key?(:editable)

  if admin? && options[:editable]
    role = if options[:format] == 'markdown'
      'markdown-editor'
    else
      'html-editor'
    end

     :div, role: role do
      out = (:div, class: 'cms-page-area', role: 'display') do
        display  = ''.html_safe
        display << cms_page_area_edit_link if admin?
        display << render_cms_page_area_content(page_area, options, &block)
      end

      out << (:div, role: 'editor') do
        form_for([:cms, page_area], format: 'json', remote: true) do |f|
          form  = f.hidden_field(:name)
          form << f.hidden_field(:content, class: 'content')
          form << f.actions(save: 'done')
        end
      end
    end
  else
     :div, class: 'cms-page-area' do
      render_cms_page_area_content(page_area, options, &block)
    end
  end
rescue Exception => e
  if Rails.env.production? then '' else raise e end
end


84
85
86
87
88
# File 'lib/cms/helper.rb', line 84

def cms_page_area_edit_link
   :div, class: 'cms-edit-link' do
    link_to('Edit', '#edit')
  end
end

#markdown(text) ⇒ Object



4
5
6
# File 'lib/cms/helper.rb', line 4

def markdown text
  Markdown.render(text).html_safe
end

#render_cms_page_area_content(page_area, options = {}, &block) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cms/helper.rb', line 66

def render_cms_page_area_content page_area, options = {}, &block
  if page_area.content.present? && !page_area.default
    content = page_area.content
  elsif block_given?
    content = capture(&block)
    page_area.content = ''.concat(content)
    page_area.save!
  end

  if options[:format].try(:to_s) == 'markdown'
    content = markdown(content)
  end

  (:div, class: 'content') do
    cms_content_parse(content) if content.present?
  end
end