Top Level Namespace

Includes:
Aerogel::Errors

Defined Under Namespace

Modules: Aerogel, Model Classes: Array, Hash

Instance Method Summary collapse

Instance Method Details

#__uncached_partial(name, opts = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/helpers/render.rb', line 53

def __uncached_partial( name, opts = {} )
  name_parts = name.to_s.split('/')
  partial_name = name_parts[-1]
  name_parts[-1] = '_'+partial_name+".html"
  template_name = name_parts.join('/').to_sym
  opts[:layout] = false

  # render single template
  unless opts.key? :collection
    return erb template_name, opts
  end

  # render collection
  out = ""
  opts[:locals] ||= {}
  opts[:collection].each do |object|
    opts[:locals][partial_name.to_sym] = object
    out += opts[:delimiter] if opts[:delimiter].present? && out.present?
    out += erb template_name, opts.except( :collection, :delimiter, :cacheable )
  end
  out
end

#assets(filename = nil) ⇒ Object

Appends to and retrieves from declared assets stack.

To include application assets and retrieve assets tags: <%= assets %>

To include/display controller’s assets: <%= assets ‘controller/name’ %>

To append to assets stack on a per-view basis: # in layout: <%= assets ‘controller/name’ %>

# in view:

<% assets 'controller/name/view' %> # note that helper output is ignored here, view asset tags
                                    # are rendered in layout


17
18
19
20
21
22
23
24
25
# File 'app/helpers/assets.rb', line 17

def assets( filename = nil )
  @assets_stack ||= []
  filename = :application if @assets_stack.blank? && filename.blank?
  @assets_stack.unshift filename if filename.present?
  @assets_stack.map do |assets_filename|
    (stylesheet_tag assets_filename.to_s) +
    (javascript_tag assets_filename.to_s)
  end.join("\n")
end

#button_to(url, text = url, opts = {}) ⇒ Object

Creates a <button …>…</button> tag.



42
43
44
# File 'app/helpers/tags.rb', line 42

def button_to( url, text = url, opts = {} )
  tag :button, text, opts.merge( url: url )
end

#configObject

Quick access to config from views and routes



3
4
5
# File 'app/helpers/config.rb', line 3

def config
  Aerogel.config
end

#csrf_field_nameObject



1
2
3
# File 'app/helpers/csrf.rb', line 1

def csrf_field_name
  'authenticity_token'
end

#csrf_protected?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'app/helpers/csrf.rb', line 13

def csrf_protected?
  true
end

#csrf_tokenObject



5
6
7
# File 'app/helpers/csrf.rb', line 5

def csrf_token
  session[:csrf] ||= SecureRandom.hex(32) if defined?(session)
end

#csrf_token_fieldObject



9
10
11
# File 'app/helpers/csrf.rb', line 9

def csrf_token_field
  tag :input, type: 'hidden', name: csrf_field_name, value: csrf_token
end

#current_hostnameObject

Returns current hostname. If hostname is set in application configuration files, it has precedence. Otherwise, hostname is inferred from the request.host, stripped from potential locale name.



33
34
35
36
37
38
# File 'app/helpers/core.rb', line 33

def current_hostname
  return config.hostname! if config.hostname?
  hostname_parts = request.host.split '.'
  hostname_parts.shift if I18n.available_locales.include?( hostname_parts.first.to_sym )
  hostname_parts.join "."
end

#current_localeObject

Returns current locale



24
25
26
# File 'app/helpers/core.rb', line 24

def current_locale
  I18n.locale
end

#current_url(opts = {}) ⇒ Object

Returns current_url To return current url in a different locale:

current_url locale: :ru


17
18
19
20
# File 'app/helpers/core.rb', line 17

def current_url( opts = {} )
  url = URI.unescape( request.path_info )
  opts.present? ? url_to( url, opts ) : url
end

#find_template(views, name, engine, &block) ⇒ Object



1
2
3
# File 'app/helpers/core.rb', line 1

def find_template(views, name, engine, &block)
  Array(views).each { |v| super(v, name, engine, &block) }
end

#h(str) ⇒ Object

Escapes html string.



4
5
6
# File 'app/helpers/render.rb', line 4

def h( str )
  Rack::Utils.escape_html(str.to_s)
end

#icon(name, opts = {}) ⇒ Object

Renders icon from icon font. First part of the name defines icon family.

Example:

<%= icon 'fa-caret-right' %> # => icon 'caret-right' from Font Awesome (fa) family
<%= icon 'glyphicon-tick' %> #=> icon 'tick' from Boostrap glyphicons


8
9
10
11
12
# File 'app/helpers/icon.rb', line 8

def icon( name, opts = {} )
  icon_family = name.split('-').first
  icon_class = "#{icon_family} #{name} #{opts[:class]}"
  tag :i, "", opts.merge( class: icon_class )
end

#l(*args) ⇒ Object

l helper, alias for Aerogel::I18n.l



9
10
11
# File 'app/helpers/i18n.rb', line 9

def l( *args )
  Aerogel::I18n.l( *args )
end

#layout(value = nil) ⇒ Object

Sets/gets current layout.



93
94
95
96
# File 'app/helpers/render.rb', line 93

def layout( value = nil )
  @layout = value unless value.nil?
  @layout
end

Creates <a href=”..>…</a> tag.



27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/helpers/tags.rb', line 27

def link_to( url, *args, &block )
  opts = (String === args.first) ? args[1] : args[0]
  opts = {
    href: url
  }.deep_merge( opts || {} )
  if String === args.first
    args[1] = opts
  else
    args[0] = opts
  end
  tag :a, *args, &block
end

#logger(name = nil) ⇒ Object



5
6
7
8
9
10
11
# File 'app/helpers/core.rb', line 5

def logger( name = nil )
  if name.nil?
    env['rack.logger']
  else
    env['rack.logger.'+name.to_s] or raise("Logger with name '#{name}' is not registered")
  end
end

#output_capture(inner_block = nil, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/helpers/output_buffer.rb', line 11

def output_capture( inner_block = nil, &block )
  inner_block = block if inner_block.nil?
  if self.respond_to?(:is_haml?) && is_haml? && (block_is_haml?(inner_block) rescue false)
    # haml
    capture_haml(nil, &block)
  elsif Aerogel::Render::OutputBuffer.block_is_erb? block
    # erb
    Aerogel::Render::OutputBuffer.capture( &block )
  else
    block.call
  end
end

#output_concat(text) ⇒ Object



1
2
3
4
5
6
7
8
9
# File 'app/helpers/output_buffer.rb', line 1

def output_concat( text )
  if self.respond_to?(:is_haml?) && is_haml?
    haml_concat(text)
  elsif !Aerogel::Render::OutputBuffer.buffer.nil? # has_erb_buffer?
    Aerogel::Render::OutputBuffer.buffer.concat text
  else # theres no template to concat, return the text directly
    text
  end
end

#page_title(value = nil) ⇒ Object

Sets/gets page title.

Example:

page_title "Home page" # sets page title
page_title # => "Home page"

Or in views:

<% page_title "Home page" %> # sets page title
<%= page_title %> # gets page title


86
87
88
89
# File 'app/helpers/render.rb', line 86

def page_title( value = nil )
  @page_title = value unless value.nil?
  @page_title
end

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

Renders partial erb template.



43
44
45
46
47
48
49
50
51
# File 'app/helpers/render.rb', line 43

def partial( name, opts = {} )
  if opts.key?( :cacheable ) && config.aerogel.cache.enabled?
    Aerogel::Cache.cacheable current_locale, name, opts[:cacheable] do |key|
      __uncached_partial( name, opts )+"<!-- cache #{key} @ #{Time.now} -->"
    end
  else
    __uncached_partial( name, opts )
  end
end

#redirect(uri, *args) ⇒ Object

xhr-conscious redirect.



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/helpers/core.rb', line 81

def redirect(uri, *args)
  if request.xhr?
    if Hash === args.first
      opts = args.first
      [:error, :notice, :warning].each do |flash_key|
        flash[flash_key] = opts[flash_key] if opts[flash_key].present?
      end
    end
    halt 200, {'Content-Type' => 'text/javascript'}, "window.location.href=\"#{uri}\""
  else
    super( uri, *args )
  end
end

#render_once(*args) ⇒ Object

Displays text only first time the helper is called in this request Usage:

render_once "hello, world"

Or:

render_once :hello_message, "hello, world"


14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/helpers/render.rb', line 14

def render_once( *args )
  @render_once_hash ||= {}
  if Symbol === args.first
    key = args.shift
    text = args.shift
  else
    key = args.shift
    text = key
  end
  return '' if @render_once_hash.key? key
  @render_once_hash[key] = true
  text
end

#t(*args) ⇒ Object

t helper, alias for chainable helper Aerogel::I18n.t



3
4
5
# File 'app/helpers/i18n.rb', line 3

def t( *args )
  Aerogel::I18n.t( *args )
end

#tag(name, *args, &block) ⇒ Object

Creates tag.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/helpers/tags.rb', line 3

def tag( name, *args, &block )
  if block_given?
    content = output_capture(&block)
  elsif args.first.is_a? String
    content = args.shift
  end
  attrs = args.shift || {}
#    t_attrs = attrs.map{|k,v| v.nil? ? " #{k}" : " #{k}=\"#{h(v)}\""}
  t_attrs = attrs.map{|k,v| v.nil? ? " #{k}" : " #{k}=\"#{h(v)}\""}
  if content
    output = "<#{name}#{t_attrs.join}>"+content+"</#{name}>"
  else
    output = "<#{name}#{t_attrs.join}/>"
  end
  if Aerogel::Render::OutputBuffer.block_is_template?(block)
    output_concat(output)
    return nil
  else
    return output
  end
end

#url_to(url, opts = {}) ⇒ Object

Constructs an URL to a resource.

Options passed in opts will be appended to URL as a query string, except some reserved options which have special meaning:

:locale => constructs URL with a certain locale
:scheme => constructs URL with a certain protocol
:fqdn => constructs full qualified URL, including hostname and protocol

Example:

url_to "/bar", locale: :de, page: 2, order: :name # => "http://de.example.org/bar?page=2&order=name"


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/helpers/core.rb', line 51

def url_to( url, opts = {} )
  hostname = nil
  if opts[:fqdn] || opts[:scheme]
    opts[:locale] ||= current_locale
  end
  if opts[:locale]
    unless I18n.available_locales.include? opts[:locale]
      raise ArgumentError.new("Unavailable locale '#{opts[:locale]}' passed to #url_to helper")
    end
    if opts[:locale] == I18n.default_locale
      hostname = current_hostname
    else
      hostname = "#{opts[:locale]}.#{current_hostname}"
    end
  end
  query_string = opts.except( :locale, :fqdn, :scheme ).map{|k,v| "#{k}=#{h v}"}.join "&"
  if query_string.present?
    if url =~ /\?/
      query_string = "&#{query_string}"
    else
      query_string = "?#{query_string}"
    end
  end
  scheme = opts[:scheme] || request.scheme || 'http'
  scheme_hostname = hostname.nil? ? '' : "#{scheme}://#{hostname}"
  "#{scheme_hostname}#{url}#{query_string}"
end

#view(name, opts = {}) ⇒ Object

Renders erb template.



30
31
32
33
34
35
36
37
38
39
# File 'app/helpers/render.rb', line 30

def view( name, opts = {} )
  ts = Time.now
  default_opts = {}
  default_opts[:layout] = :"layouts/#{layout}.html" if layout.present?
  #if settings.development?
    erb( "#{name}.html".to_sym, default_opts.merge(opts) )+( "<!-- %s: %.3fs -->" % [name, Time.now - ts] )
  #else
  #  erb( "#{name}.html".to_sym, default_opts.merge(opts) )
  #end
end