Module: Fugleman::Helpers

Defined in:
lib/fugleman/helpers.rb

Overview

This started out life as a copy of code found in padrino, but was heavily simplified and then forked.

NOTE This includes none of the SafeBuffer madness. Watch your escaping.

Instance Method Summary collapse

Instance Method Details

#asset_path(source = nil) ⇒ Object

Formats a path to an asset, including the assets timestamp for cache invalidation.



94
95
96
97
98
# File 'lib/fugleman/helpers.rb', line 94

def asset_path(source = nil)
  timestamp = asset_timestamp(source)
  result_path = uri_root_path(source)
  "#{result_path}#{timestamp}"
end

#asset_timestamp(file_path) ⇒ Object

Returns the timestamp mtime for an asset.

asset_timestamp("some/path/to/file.png") => "?154543678"


103
104
105
106
107
108
109
110
# File 'lib/fugleman/helpers.rb', line 103

def asset_timestamp(file_path)
  return nil if file_path =~ /\?/ || (self.class.respond_to?(:asset_stamp) && !self.class.asset_stamp)
  public_path = self.class.public_folder if self.class.respond_to?(:public_folder)
  public_file_path = File.join(public_path, file_path) if public_path
  stamp = File.mtime(public_file_path).to_i if public_file_path && File.exist?(public_file_path)
  stamp ||= Time.now.to_i
  "?#{stamp}"
end

#capture_html(*args, &block) ⇒ Object

——————————————————————- captures



129
130
131
# File 'lib/fugleman/helpers.rb', line 129

def capture_html(*args, &block)
  # block.call(*args)
end

#content_tag(name, content = nil, options = nil, &block) ⇒ Object



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

def (name, content = nil, options = nil, &block)
  if block_given?
    options = content if content.is_a?(Hash)
    content = capture_html(&block)
  end

  attributes = tag_attributes(options)
  output = String.new
  output.concat "<#{name}#{attributes}>"
  output.concat content.to_s
  output.concat "</#{name}>"

  output
end

#escape_html(text) ⇒ Object Also known as: h

——————————————————————- escapism



113
114
115
# File 'lib/fugleman/helpers.rb', line 113

def escape_html(text)
  Rack::Utils.escape_html(text)
end

#escape_value(string) ⇒ Object



118
119
120
# File 'lib/fugleman/helpers.rb', line 118

def escape_value(string)
  string.to_s.gsub(ESCAPE_REGEXP) { |c| ESCAPE_VALUES[c] }
end

#extract_options!(args) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/fugleman/helpers.rb', line 11

def extract_options! args
  peek = args.last
  
  if peek.kind_of? Hash
    return args.pop
  end

  Hash.new
end

#form_namespace(path, obj = nil, &block) ⇒ Object

————————————————————– form creation



23
24
25
# File 'lib/fugleman/helpers.rb', line 23

def form_namespace path, obj=nil, &block
  block.call(::Fugleman::Namespace.new(self, path, obj))
end

#image_path(src) ⇒ Object



87
88
89
# File 'lib/fugleman/helpers.rb', line 87

def image_path(src)
  asset_path("images/" + src)
end

#image_tag(url, options = {}) ⇒ Object

——————————————————————— assets



74
75
76
77
# File 'lib/fugleman/helpers.rb', line 74

def image_tag(url, options={})
  options.reverse_merge!(:src => image_path(url))
  tag(:img, options)
end

#input_tag(type, name = nil, options = {}) ⇒ Object

————————————————————– tag rendering



48
49
50
51
52
53
# File 'lib/fugleman/helpers.rb', line 48

def input_tag(type, name=nil, options = {})
  tag(
    :input, 
    {name: name}.update(
      options.reverse_merge!(:type => type)))
end

#javascript_include_tag(*sources) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/fugleman/helpers.rb', line 78

def javascript_include_tag(*sources)
  options = {
    :type => 'text/javascript'
  }.update(extract_options!(sources).symbolize_keys)
  sources.flatten.inject(String.new) do |all,source|
    all << (:script, nil, { :src => asset_path('javascripts/'+source+'.js') }.update(options))
  end
end

Creates a link element with given name, url and options.



31
32
33
34
35
36
37
38
39
40
# File 'lib/fugleman/helpers.rb', line 31

def link_to(*args, &block)
  options  = extract_options!(args)
  name = block_given? ? '' : args.shift


  url = args.first || '#'
  options = {href: url}.merge(options)

  block_given? ? (:a, options, &block) : (:a, name, options)
end

#partial(name, opts = {}, &block) ⇒ Object

——————————————————————- partials



43
44
45
# File 'lib/fugleman/helpers.rb', line 43

def partial name, opts={}, &block
  slim name, { layout: false }.update(opts), &block
end

#tag(name, options = nil, open = false) ⇒ Object



54
55
56
57
# File 'lib/fugleman/helpers.rb', line 54

def tag(name, options = nil, open = false)
  attributes = tag_attributes(options)
  "<#{name}#{attributes}#{open ? '>' : ' />'}"
end

#uri_root_path(*paths) ⇒ Object

Returns the URI root of the application with optional paths appended.

uri_root_path("/some/path") => "/root/some/path"
uri_root_path("javascripts", "test.js") => "/uri/root/javascripts/test.js"


140
141
142
143
# File 'lib/fugleman/helpers.rb', line 140

def uri_root_path(*paths)
  root_uri = self.class.uri_root if self.class.respond_to?(:uri_root)
  File.join(ENV['RACK_BASE_URI'].to_s, root_uri || '/', *paths)
end