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
-
#asset_path(source = nil) ⇒ Object
Formats a path to an asset, including the assets timestamp for cache invalidation.
-
#asset_timestamp(file_path) ⇒ Object
Returns the timestamp mtime for an asset.
-
#capture_html(*args, &block) ⇒ Object
——————————————————————- captures.
- #content_tag(name, content = nil, options = nil, &block) ⇒ Object
-
#escape_html(text) ⇒ Object
(also: #h)
——————————————————————- escapism.
- #escape_value(string) ⇒ Object
- #extract_options!(args) ⇒ Object
-
#form_namespace(path, obj = nil, &block) ⇒ Object
————————————————————– form creation.
- #image_path(src) ⇒ Object
-
#image_tag(url, options = {}) ⇒ Object
——————————————————————— assets.
-
#input_tag(type, name = nil, options = {}) ⇒ Object
————————————————————– tag rendering.
- #javascript_include_tag(*sources) ⇒ Object
-
#link_to(*args, &block) ⇒ Object
Creates a link element with given name, url and options.
-
#partial(name, opts = {}, &block) ⇒ Object
——————————————————————- partials.
- #tag(name, options = nil, open = false) ⇒ Object
-
#uri_root_path(*paths) ⇒ Object
Returns the URI root of the application with optional paths appended.
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) = (source) result_path = uri_root_path(source) "#{result_path}#{}" end |
#asset_timestamp(file_path) ⇒ Object
Returns the timestamp mtime for an asset.
("some/path/to/file.png") => "?154543678"
103 104 105 106 107 108 109 110 |
# File 'lib/fugleman/helpers.rb', line 103 def (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 content_tag(name, content = nil, = nil, &block) if block_given? = content if content.is_a?(Hash) content = capture_html(&block) end attributes = tag_attributes() 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 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, ={}) .reverse_merge!(:src => image_path(url)) tag(:img, ) 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, = {}) tag( :input, {name: name}.update( .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) = { :type => 'text/javascript' }.update((sources).symbolize_keys) sources.flatten.inject(String.new) do |all,source| all << content_tag(:script, nil, { :src => asset_path('javascripts/'+source+'.js') }.update()) end end |
#link_to(*args, &block) ⇒ Object
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) = (args) name = block_given? ? '' : args.shift url = args.first || '#' = {href: url}.merge() block_given? ? content_tag(:a, , &block) : content_tag(:a, name, ) 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, = nil, open = false) attributes = tag_attributes() "<#{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 |