Module: Admin::BaseHelper

Defined in:
app/helpers/admin/base_helper.rb

Instance Method Summary collapse

Instance Method Details

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

Overwrites content_for helper to set has_content?.



6
7
8
9
10
# File 'app/helpers/admin/base_helper.rb', line 6

def content_for(name, content = nil, &block)
  @has_content ||= {}
  @has_content[name] = true
  super(name, content, &block)
end

#has_content?(name) ⇒ Boolean

Returns if we have content for a specific yield.

Returns:

  • (Boolean)


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

def has_content?(name)
  (@has_content && @has_content[name]) || false
end

#l(time) ⇒ Object

Overwrites l helper. Make the I18n localize method work with nil dates! Attribute must be Date, DateTime or Time object or nil :) Returns ” if attribute is nil.



44
45
46
# File 'app/helpers/admin/base_helper.rb', line 44

def l(time)
  time.nil? ? '' : I18n.l(time)
end

Shows paginate, paginate_info and perinate. Like: ‘1 2 3 4 5 … Next › Last » (1-25/4028) | Per page: 25, 50, 100’



59
60
61
62
63
# File 'app/helpers/admin/base_helper.rb', line 59

def navigate(collection)
  return if collection.blank?
  html = paginate(collection) + paginate_info(collection) + ' | ' + perinate(collection)
  (:div, html.html_safe, :class => 'navigation').html_safe
end

This is a replacement for Ransack’s sort_link (github.com/ernie/ransack/blob/master/lib/ransack/helpers/form_helper.rb). It works for @search stuff and without @search stuff.



50
51
52
# File 'app/helpers/admin/base_helper.rb', line 50

def order_link(ransack, attribute, options = {})
  ransack ? sort_link(ransack, attribute, options) : attribute.to_s.humanize # FIXME: Model.human_attribute_name("title")
end

#paginate_info(collection) ⇒ Object

Shows pagination info like ‘(51-75/815)’. Uses I18n ‘en.views.pagination_info’ translation key.



67
68
69
70
71
72
73
74
# File 'app/helpers/admin/base_helper.rb', line 67

def paginate_info(collection)
  return if collection.blank?
  first = collection.offset_value + 1
  last = [collection.offset_value + collection.limit_value, collection.total_count].min
  total = collection.total_count
  html = I18n.t('views.pagination_info', :default => "(%{first}-%{last}/%{total})", :first => number_with_delimiter(first), :last => number_with_delimiter(last), :total => number_with_delimiter(total))
  (:nav, html.html_safe, :class => 'pagination info').html_safe
end

#perinate(collection, options = {}) ⇒ Object

Shows a ‘Per page: 25 50 100 500’. Uses I18n ‘en.views.perinate’ translation key.



78
79
80
81
82
83
84
85
86
87
88
# File 'app/helpers/admin/base_helper.rb', line 78

def perinate(collection, options = {})
  return if collection.blank?
  options.reverse_merge!(:values => [25, 50, 100, 500])
  page = (collection.offset_value / collection.limit_value).to_i + 1
  html = I18n.t('views.perinate', :default => "Per page: ")
  html += options[:values].collect do |value|
    page = (collection.offset_value / value).to_i + 1 # This makes sure we always show the same first entry
    link_to_if(value != collection.limit_value, value, params.merge(:per => value, :page => page))
  end.join(', ')
  (:nav, html.html_safe, :class => 'perination').html_safe
end

#render_flash_messagesObject

Renders flash messages.



18
19
20
21
22
23
24
# File 'app/helpers/admin/base_helper.rb', line 18

def render_flash_messages
  flash_string = ''
  flash.each do |k,v|
    flash_string << ('div', v, :class => "flash #{k}")
  end
  flash_string.html_safe
end

#true_false_flag_class(boolean) ⇒ Object

Raises:

  • (ArgumentError)


34
35
36
37
38
# File 'app/helpers/admin/base_helper.rb', line 34

def true_false_flag_class(boolean)
  return 'icon-flag_black' if boolean.nil?
  raise ArgumentError, "Expected a Boolean as argument!" unless boolean.is_a?(FalseClass) || boolean.is_a?(TrueClass)
  boolean ? 'icon-flag_green' : 'icon-flag_red'
end

#true_false_image(boolean) ⇒ Object

Following are other helper methods.

Raises:

  • (ArgumentError)


28
29
30
31
32
# File 'app/helpers/admin/base_helper.rb', line 28

def true_false_image(boolean)
  return image_tag("admin/dash.png") if boolean.nil?
  raise ArgumentError, "Expected a Boolean as argument!" unless boolean.is_a?(FalseClass) || boolean.is_a?(TrueClass)
  image_tag("admin/#{boolean}.png")
end