Module: AnyView::Helpers::AssetTagHelpers

Defined in:
lib/any_view/asset_tag_helpers.rb

Constant Summary collapse

MAIL_ATTRIBUTES =
[:cc, :bcc, :subject, :body]

Instance Method Summary collapse

Instance Method Details

#image_path(src) ⇒ Object

Returns the path to the image, either relative or absolute


54
55
56
57
# File 'lib/any_view/asset_tag_helpers.rb', line 54

def image_path(src)
  src.gsub!(/\s/, '')
  src =~ %r{^\s*(/|http)} ? src : uri_root_path('images', src)
end

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

Creates an image element with given url and options image_tag('icons/avatar.png')


35
36
37
38
# File 'lib/any_view/asset_tag_helpers.rb', line 35

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

#javascript_include_tag(*sources) ⇒ Object

javascript_include_tag 'application', 'special'


48
49
50
51
# File 'lib/any_view/asset_tag_helpers.rb', line 48

def javascript_include_tag(*sources)
  options = sources.extract_options!.symbolize_keys
  sources.collect { |script| javascript_tag(script, options) }.join("\n")
end

Creates a link element with given name, url and options link_to(“/url”, :class => “foo”){ “link text” } link_to 'click me', '/dashboard', :class => 'linky'


9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/any_view/asset_tag_helpers.rb', line 9

def link_to(*args, &block)
  options = args.extract_options!
  text, url = args
  if url.nil?
    url = text
    text = capture_content(&block)
  end
  options.reverse_merge!(:href => url)
  options[:content] = text
  result = tag(:a, options)
  block.nil? ? result : concat_content(result)
end

#mail_to(email, caption = nil, options = {}) ⇒ Object

Creates a mail link element with given name and caption mail_to “[email protected]” => <a href=“[email protected][email protected]”>[email protected]> mail_to “[email protected]”, “My Email” => <a href=“My”>[email protected]“>My Email</a>


25
26
27
28
29
30
31
# File 'lib/any_view/asset_tag_helpers.rb', line 25

def mail_to(email, caption=nil, options={})
  mail_options, options = options.partition{|k,v| MAIL_ATTRIBUTES.include?(k)}
  mail_options, options = Hash[mail_options], Hash[options]
  mail_query = Rack::Utils.build_query(mail_options).gsub(/\+/, '%20').gsub('%40', '@')
  mail_href = "mailto:#{email}"; mail_href << "?#{mail_query}" if mail_query.present?
  link_to (caption || email), mail_href, options
end

Returns a stylesheet link tag for the sources specified as arguments stylesheet_link_tag 'style', 'application', 'layout'


42
43
44
45
# File 'lib/any_view/asset_tag_helpers.rb', line 42

def stylesheet_link_tag(*sources)
  options = sources.extract_options!.symbolize_keys
  sources.collect { |sheet| stylesheet_tag(sheet, options) }.join("\n")
end