Module: ApplicationHelper

Includes:
Admin::RegionsHelper
Defined in:
app/helpers/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#adminObject



113
114
115
# File 'app/helpers/application_helper.rb', line 113

def admin
  TrustyCms::AdminUI.instance
end

#admin?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'app/helpers/application_helper.rb', line 73

def admin?
  current_user and current_user.admin?
end

#available_locales_selectObject



129
130
131
# File 'app/helpers/application_helper.rb', line 129

def available_locales_select
  [[t('select.default'),'']] + TrustyCms::AvailableLocales.locales
end

#body_classesObject



117
118
119
# File 'app/helpers/application_helper.rb', line 117

def body_classes
  @body_classes ||= []
end

#clean(url) ⇒ Object



68
69
70
71
# File 'app/helpers/application_helper.rb', line 68

def clean(url)
  uri = URI.parse(url)
  uri.path.gsub(%r{/+}, '/').gsub(%r{/$}, '')
end

#current_item?(item) ⇒ Boolean

Returns:

  • (Boolean)


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

def current_item?(item)
  if item.tab && item.tab.many? {|i| current_url?(i.relative_url) }
    # Accept only stricter URL matches if more than one matches
    current_page?(item.url)
  else
    current_url?(item.relative_url)
  end
end

#current_tab?(tab) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
# File 'app/helpers/application_helper.rb', line 52

def current_tab?(tab)
  @current_tab ||= tab if tab.any? {|item| current_url?(item.relative_url) }
  @current_tab == tab
end

#current_url?(options) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
# File 'app/helpers/application_helper.rb', line 57

def current_url?(options)
  url = case options
        when Hash
          url_for options
        else
          options.to_s
        end
  #TODO: look for other instances of request_uri
  request.original_fullpath =~ Regexp.new('^' + Regexp.quote(clean(url)))
end

#default_page_titleObject



8
9
10
# File 'app/helpers/application_helper.rb', line 8

def default_page_title
  title + ' - ' + subtitle
end

#designer?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'app/helpers/application_helper.rb', line 77

def designer?
  current_user and (current_user.designer? or current_user.admin?)
end

#gravatar_url(email, options = {}) ⇒ Object

Returns a Gravatar URL associated with the email parameter. See: douglasfshearer.com/blog/gravatar-for-ruby-and-ruby-on-rails



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/helpers/application_helper.rb', line 151

def gravatar_url(email, options={})
  # Default to highest rating. Rating can be one of G, PG, R X.
  options[:rating] ||= "G"

  # Default size of the image.
  options[:size] ||= "32px"

  # Default image url to be used when no gravatar is found
  # or when an image exceeds the rating parameter.
  local_avatar_url = "/production/assets/admin/avatar_#{([options[:size].to_i] * 2).join('x')}.png"
  default_avatar_url = "#{request.protocol}#{request.host_with_port}#{ActionController::Base.relative_url_root}#{local_avatar_url}"
  options[:default] ||= default_avatar_url

  unless email.blank?
    # Build the Gravatar url.
    url = '//gravatar.com/avatar/'
    url << "#{Digest::MD5.new.update(email)}?"
    url << "rating=#{options[:rating]}" if options[:rating]
    url << "&size=#{options[:size]}" if options[:size]
    url << "&default=#{options[:default]}" if options[:default]
    # Test the Gravatar url
    require 'open-uri'
    begin; open "http:#{url}", :proxy => true
    rescue; local_avatar_url
    else; url
    end
  else
    local_avatar_url
  end
end

#image(name, options = {}) ⇒ Object



109
110
111
# File 'app/helpers/application_helper.rb', line 109

def image(name, options = {})
  image_tag(append_image_extension("admin/#{name}"), options)
end

#javascript_overridesObject



141
142
143
144
145
146
147
# File 'app/helpers/application_helper.rb', line 141

def javascript_overrides
  overrides = []
  if File.exist?("#{Rails.root}/public/javascripts/admin/overrides.js")
    overrides << 'admin/overrides'
  end
  overrides
end

#logged_in?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'app/helpers/application_helper.rb', line 20

def logged_in?
  !current_user.nil?
end

#meta_errors?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'app/helpers/application_helper.rb', line 101

def meta_errors?
  false
end

#meta_labelObject



105
106
107
# File 'app/helpers/application_helper.rb', line 105

def meta_label
  meta_errors? ? 'Less' : 'More'
end


121
122
123
# File 'app/helpers/application_helper.rb', line 121

def nav_tabs
  admin.nav
end

#onsubmit_status(model) ⇒ Object



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

def onsubmit_status(model)
  model.new_record? ? t('creating_status', :model => t(model.class.name.downcase)) : "#{I18n.t('saving_changes')}&#8230;"
end

#pagination_for(list, options = {}) ⇒ Object

returns the usual set of pagination links. options are passed through to will_paginate and a ‘show all’ depagination link is added if relevant.



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/helpers/application_helper.rb', line 185

def pagination_for(list, options={})
  if list.respond_to? :total_pages
    options = {
      :max_per_page => @trusty_config['pagination.max_per_page'] || 500,
      :depaginate => true
    }.merge(options.symbolize_keys)
    depaginate = options.delete(:depaginate)                                     # supply :depaginate => false to omit the 'show all' link
    depagination_limit = options.delete(:max_per_page)                           # supply :max_per_page => false to include the 'show all' link no matter how large the collection
    html = will_paginate(list, will_paginate_options.merge(options))
    if depaginate && list.total_pages > 1 && (!depagination_limit.blank? || list.total_entries <= depagination_limit.to_i)
      html << (:div, link_to(t('show_all'), :pp => 'all'), :class => 'depaginate')
    elsif depaginate && list.total_entries > depagination_limit.to_i
      html = (:div, link_to("paginate", :p => 1), :class => 'pagination')
    end
    html
  end
end

#save_model_and_continue_editing_button(model) ⇒ Object



39
40
41
# File 'app/helpers/application_helper.rb', line 39

def save_model_and_continue_editing_button(model)
  submit_tag t('buttons.save_and_continue'), :name => 'continue', :class => 'button', :accesskey => "s"
end

#save_model_button(model, options = {}) ⇒ Object



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

def save_model_button(model, options = {})
  model_name = model.class.name.underscore
  human_model_name = model_name.humanize.titlecase
  options[:label] ||= model.new_record? ?
    t('buttons.create', :name => t(model_name, :default => human_model_name), :default => 'Create ' + human_model_name) :
    t('buttons.save_changes', :default => 'Save Changes')
  options[:class] ||= "button"
  options[:accesskey] ||= 'S'
  submit_tag options.delete(:label), options
end

#stylesheet_overridesObject



133
134
135
136
137
138
139
# File 'app/helpers/application_helper.rb', line 133

def stylesheet_overrides
  overrides = []
  if File.exist?("#{Rails.root}/public/stylesheets/admin/overrides.css") || File.exist?("#{Rails.root}/public/stylesheets/sass/admin/overrides.sass")
    overrides << 'admin/overrides'
  end
  overrides
end

#subtitleObject



16
17
18
# File 'app/helpers/application_helper.rb', line 16

def subtitle
  trusty_config['admin.subtitle'] || 'Publishing for Small Teams'
end

#timestamp(time) ⇒ Object



96
97
98
99
# File 'app/helpers/application_helper.rb', line 96

def timestamp(time)
  # time.strftime("%I:%M %p on %B %e, %Y").sub("AM", 'am').sub("PM", 'pm')
  I18n.localize(time, :format => :timestamp)
end

#titleObject



12
13
14
# File 'app/helpers/application_helper.rb', line 12

def title
  trusty_config['admin.title'] || 'Trusty CMS'
end

#translate_with_default(name) ⇒ Object



125
126
127
# File 'app/helpers/application_helper.rb', line 125

def translate_with_default(name)
  t(name.underscore.downcase, :default => name)
end

#trusty_configObject



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

def trusty_config
  TrustyCms::Config
end

#updated_stamp(model) ⇒ Object



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

def updated_stamp(model)
  unless model.new_record?
    updated_by = (model.updated_by || model.created_by)
    name = updated_by ? updated_by.name : nil
    time = (model.updated_at || model.created_at)
    if name or time
      html = %{<p class="updated_line">#{t('timestamp.last_updated')} }
      html << %{#{t('timestamp.by')} <strong>#{name}</strong> } if name
      html << %{#{t('timestamp.at')} #{timestamp(time)}} if time
      html << %{</p>}
      html.html_safe
    end
  end
end