Module: Dropsite::RenderHelper

Included in:
SiteItem
Defined in:
lib/dropsite/render_helper.rb

Overview

Helper methods, accessible in templates, to be included in renderable items

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rendered_byObject

Returns the value of attribute rendered_by.



5
6
7
# File 'lib/dropsite/render_helper.rb', line 5

def rendered_by
  @rendered_by
end

Instance Method Details

Get a link to a parent page



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dropsite/render_helper.rb', line 61

def back_link(levels_up=1)
  # TODO: factor this out a bit between parent_dir_name
  if levels_up.is_a?(Integer) && levels_up > 0 && levels_up < path.split('/').size
    dir_name = path.split('/')[(levels_up + 1) * -1]
    dir_name = 'index' if dir_name.empty?
    (['..'] * (levels_up)).join('/') + "/#{dir_name}.html"
  else
    # Give the root index if we're confused
    root? ? '' : (['..'] * (path.split('/').size)).join('/') + '/index.html'
  end
end


47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/dropsite/render_helper.rb', line 47

def each_parent_directory_link_tag(*args)
  include_root = true
  include_root = args[0] if args.size > 0 && (!args[0].is_a? Hash)
  options = args.find {|arg| arg.is_a? Hash}
  options = {} if !options

  levels_up = path.split('/').size
  levels_up = levels_up - 1 if !include_root
  levels_up.downto(1) do |level_up|
    yield parent_directory_link_tag(level_up, options)
  end
end

#get_bindingObject



125
126
127
# File 'lib/dropsite/render_helper.rb', line 125

def get_binding
  binding
end

#image_tag(image_file_name) ⇒ Object



96
97
98
# File 'lib/dropsite/render_helper.rb', line 96

def image_tag(image_file_name)
  %{<img src="#{plugin_assets_link_base}images/#{image_file_name}" />}
end

#javascript_include_tag(javascript_file_name) ⇒ Object



91
92
93
94
# File 'lib/dropsite/render_helper.rb', line 91

def javascript_include_tag(javascript_file_name)
  javascript_file_name = "#{javascript_file_name}.js" if not javascript_file_name =~ /\.js$/
  %{<script type="text/javascript" src="#{plugin_assets_link_base}js/#{javascript_file_name}"></script>}
end

Helpers for linking to other Dropsite rendered pages



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/dropsite/render_helper.rb', line 10

def link
  # TODO: URL escape links
  if top_level? && (!is_a? SiteDir)
    name
  else
    if is_a? SiteDir # TODO: do a dynamic renderable test?
      top_level? ? "dropsite/#{@path}.html" : "#{@path}.html".sub(/^\//, '')
    else
      # Builds a link back up to the file in Public root since the pages
      # are built in a parallel directory structure
      dirs_up = @path.split(File::SEPARATOR).size - 1
      (['..'] * dirs_up).join('/') + "/#{@path}"
    end
  end
end

#page_asset_image_tag(image_file_name) ⇒ Object

Creates an img tag for the given page asset image. This links to a file directly in the page asset directory and an ‘image’ folder is not assumed.



106
107
108
# File 'lib/dropsite/render_helper.rb', line 106

def page_asset_image_tag(image_file_name)
  %{<img src="#{page_assets_link_base}#{image_file_name}" />}
end


121
122
123
# File 'lib/dropsite/render_helper.rb', line 121

def page_assets_link_base
  "#{root? ? 'dropsite/' : ''}#{page_assets_dir_name}/"
end

#parent_dir_name(levels_up = 1) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/dropsite/render_helper.rb', line 73

def parent_dir_name(levels_up=1)
  if levels_up.is_a?(Integer) && levels_up > 0 && levels_up < path.split('/').size
    dir_name = path.split('/')[(levels_up + 1) * -1]
    dir_name.empty? ? 'index' : dir_name
  else
    # TODO: make this configurable
    'my files'
  end
end


43
44
45
# File 'lib/dropsite/render_helper.rb', line 43

def parent_directory_link_tag(levels_up=1, options={})
  %{<a href="#{back_link(levels_up)}"#{tag_attributes(options)}>#{parent_dir_name(levels_up)}</a>}
end


111
112
113
114
115
116
117
118
119
# File 'lib/dropsite/render_helper.rb', line 111

def plugin_assets_link_base
  if root?
    "dropsite/dropsite-assets/#{underscorize(rendered_by)}/"
  else
    # Work our way BACK up the path - crazy, right? Gotta do it though.
    dirs_up = path.split(File::SEPARATOR).size - 1
    (['..'] * dirs_up).join('/') + "#{'/' if dirs_up > 0}dropsite-assets/#{underscorize(rendered_by)}/"
  end
end

Helpers for including assets from the plugin assets directory



86
87
88
89
# File 'lib/dropsite/render_helper.rb', line 86

def stylesheet_link_tag(stylesheet_name)
  stylesheet_name = "#{stylesheet_name}.css" if not stylesheet_name =~ /\.css$/
  %{<link rel="stylesheet" href="#{plugin_assets_link_base}css/#{stylesheet_name}" type="text/css" media="screen">}
end

#url_for(entry) ⇒ Object

Creates a relative URL, from the page for the current object, for the given SiteItem. This assumes the current object is a SiteDir.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dropsite/render_helper.rb', line 28

def url_for(entry)
  if entry.top_level? && (!entry.is_a? SiteDir)
    entry.name
  else
    if entry.is_a? SiteDir # TODO: do a dynamic renderable test?
      entry.top_level? ? "dropsite/#{entry.path}.html" : "#{name}/#{entry.name}.html".sub(/^\//, '')
    else
      # Builds a link back up to the file in Public root since the pages
      # are built in a parallel directory structure
      dirs_up = entry.path.split(File::SEPARATOR).size - 1
      (['..'] * dirs_up).join('/') + "/#{entry.path}"
    end
  end
end