File: app/helpers/application_helper.rb

Overview
Module Structure
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: ApplicationHelper#1
includes
  RegionsHelper ( Admin )
  LocalTime   
has properties
method: config #5
method: default_page_title #9
method: title #13
method: subtitle #17
method: logged_in? #21
method: onsubmit_status / 1 #25
method: save_model_button / 2 #29
method: save_model_and_continue_editing_button / 1 #40
method: current_item? / 1 #44
method: current_tab? / 1 #53
method: current_url? / 1 #58
method: clean / 1 #68
method: nav_link_to / 2 #73
method: admin? #81
method: designer? #85
method: focus / 1 #89
method: updated_stamp / 1 #93
method: timestamp / 1 #108
method: meta_visible / 1 #113
method: meta_errors? #123
method: meta_label #127
method: toggle_javascript_for / 1 #131
method: image / 2 #135
method: image_submit / 2 #139
method: admin #143
method: filter_options_for_select / 1 #147
method: body_classes #151
method: nav_tabs #155
method: translate_with_default / 1 #159
method: available_locales_select #163
method: stylesheet_and_javascript_overrides #167
method: gravatar_url / 2 #180
method: pagination_for / 2 #208
method: append_image_extension / 1 #228

Code

   1  module ApplicationHelper
   2    include LocalTime
   3    include Admin::RegionsHelper
   4    
   5    def config
   6      Radiant::Config
   7    end
   8    
   9    def default_page_title
  10      title + ' - ' + subtitle
  11    end
  12    
  13    def title
  14      config['admin.title'] || 'Radiant CMS'
  15    end
  16    
  17    def subtitle
  18      config['admin.subtitle'] || 'Publishing for Small Teams'
  19    end
  20    
  21    def logged_in?
  22      !current_user.nil?
  23    end
  24    
  25    def onsubmit_status(model)
  26      model.new_record? ? t('creating_status', :model => t(model.class.name.downcase)) : "#{I18n.t('saving_changes')}&#8230;"
  27    end
  28    
  29    def save_model_button(model, options = {})
  30      model_name = model.class.name.underscore
  31      human_model_name = model_name.humanize.titlecase
  32      options[:label] ||= model.new_record? ?
  33        t('buttons.create', :name => t(model_name, :default => human_model_name), :default => 'Create ' + human_model_name) :
  34        t('buttons.save_changes', :default => 'Save Changes')
  35      options[:class] ||= "button"
  36      options[:accesskey] ||= 'S'
  37      submit_tag options.delete(:label), options
  38    end
  39    
  40    def save_model_and_continue_editing_button(model)
  41      submit_tag t('buttons.save_and_continue'), :name => 'continue', :class => 'button', :accesskey => "s"
  42    end
  43    
  44    def current_item?(item)
  45      if item.tab && item.tab.many? {|i| current_url?(i.relative_url) }
  46        # Accept only stricter URL matches if more than one matches
  47        current_page?(item.url)
  48      else
  49        current_url?(item.relative_url)
  50      end
  51    end
  52    
  53    def current_tab?(tab)
  54      @current_tab ||= tab if tab.any? {|item| current_url?(item.relative_url) }
  55      @current_tab == tab
  56    end
  57    
  58    def current_url?(options)
  59      url = case options
  60      when Hash
  61        url_for options
  62      else
  63        options.to_s
  64      end
  65      request.request_uri =~ Regexp.new('^' + Regexp.quote(clean(url)))
  66    end
  67    
  68    def clean(url)
  69      uri = URI.parse(url)
  70      uri.path.gsub(%r{/+}, '/').gsub(%r{/$}, '')
  71    end
  72    
  73    def nav_link_to(name, options)
  74      if current_url?(options)
  75        %{<strong>#{ link_to translate_with_default(name), options }</strong>}
  76      else
  77        link_to translate_with_default(name), options
  78      end
  79    end
  80    
  81    def admin?
  82      current_user and current_user.admin?
  83    end
  84    
  85    def designer?
  86      current_user and (current_user.designer? or current_user.admin?)
  87    end
  88    
  89    def focus(field_name)
  90      javascript_tag "Field.activate('#{field_name}');"
  91    end
  92    
  93    def updated_stamp(model)
  94      unless model.new_record?
  95        updated_by = (model.updated_by || model.created_by)
  96        name = updated_by ? updated_by.name : nil
  97        time = (model.updated_at || model.created_at)
  98        if name or time
  99          html = %{<p class="updated_line">#{t('timestamp.last_updated')} } 
 100          html << %{#{t('timestamp.by')} <strong>#{name}</strong> } if name
 101          html << %{#{t('timestamp.at')} #{timestamp(time)}} if time
 102          html << %{</p>}
 103          html
 104        end
 105      end
 106    end
 107    
 108    def timestamp(time)
 109      # time.strftime("%I:%M %p on %B %e, %Y").sub("AM", 'am').sub("PM", 'pm')
 110      I18n.localize(time, :format => :timestamp)    
 111    end 
 112    
 113    def meta_visible(symbol)
 114      v = case symbol
 115      when :meta_more
 116        not meta_errors?
 117      when :meta, :meta_less
 118        meta_errors?
 119      end
 120      v ? {} : {:style => "display: none"}
 121    end
 122    
 123    def meta_errors?
 124      false
 125    end
 126    
 127    def meta_label
 128      meta_errors? ? 'Less' : 'More'
 129    end
 130    
 131    def toggle_javascript_for(id)
 132      "Element.toggle('#{id}'); Element.toggle('more-#{id}'); Element.toggle('less-#{id}'); return false;"
 133    end
 134    
 135    def image(name, options = {})
 136      image_tag(append_image_extension("admin/#{name}"), options)
 137    end
 138    
 139    def image_submit(name, options = {})
 140      image_submit_tag(append_image_extension("admin/#{name}"), options)
 141    end
 142    
 143    def admin
 144      Radiant::AdminUI.instance
 145    end
 146    
 147    def filter_options_for_select(selected=nil)
 148      options_for_select([[t('select.none'), '']] + TextFilter.descendants_names, selected)
 149    end
 150    
 151    def body_classes
 152      @body_classes ||= []
 153    end
 154    
 155    def nav_tabs
 156      admin.nav
 157    end
 158    
 159    def translate_with_default(name)
 160      t(name.underscore.downcase, :default => name)
 161    end
 162    
 163    def available_locales_select
 164      [[t('select.default'),'']] + Radiant::AvailableLocales.locales
 165    end
 166    
 167    def stylesheet_and_javascript_overrides
 168      overrides = ''
 169      if File.exist?("#{Rails.root}/public/stylesheets/admin/overrides.css") || File.exist?("#{Rails.root}/public/stylesheets/sass/admin/overrides.sass")
 170        overrides << stylesheet_link_tag('admin/overrides')
 171      end
 172      if File.exist?("#{Rails.root}/public/javascripts/admin/overrides.js")
 173        overrides << javascript_include_tag('admin/overrides')
 174      end
 175      overrides
 176    end
 177    
 178    # Returns a Gravatar URL associated with the email parameter.
 179    # See: http://douglasfshearer.com/blog/gravatar-for-ruby-and-ruby-on-rails
 180    def gravatar_url(email, options={})
 181      # Default to highest rating. Rating can be one of G, PG, R X.
 182      options[:rating] ||= "G"
 183      
 184      # Default size of the image.
 185      options[:size] ||= "32px"
 186      
 187      # Default image url to be used when no gravatar is found
 188      # or when an image exceeds the rating parameter.
 189      default_avatar_url = "#{request.protocol}#{request.host_with_port}/images/admin/avatar_#{([options[:size].to_i] * 2).join('x')}.png"
 190      options[:default] ||= default_avatar_url
 191      
 192      unless email.blank?
 193        # Build the Gravatar url.
 194        url = 'http://www.gravatar.com/avatar.php?'
 195        url << "gravatar_id=#{Digest::MD5.new.update(email)}" 
 196        url << "&rating=#{options[:rating]}" if options[:rating]
 197        url << "&size=#{options[:size]}" if options[:size]
 198        url << "&default=#{options[:default]}" if options[:default]
 199        url
 200      else
 201        default_avatar_url
 202      end
 203    end
 204    
 205    # returns the usual set of pagination links.
 206    # options are passed through to will_paginate 
 207    # and a 'show all' depagination link is added if relevant.
 208    def pagination_for(list, options={})
 209      if list.respond_to? :total_pages
 210        options = {
 211          :max_per_page => config['pagination.max_per_page'] || 500,
 212          :depaginate => true
 213        }.merge(options.symbolize_keys)
 214        depaginate = options.delete(:depaginate)                                     # supply :depaginate => false to omit the 'show all' link
 215        depagination_limit = options.delete(:max_per_page)                           # supply :max_per_page => false to include the 'show all' link no matter how large the collection
 216        html = will_paginate(list, will_paginate_options.merge(options))
 217        if depaginate && list.total_pages > 1 && (!depagination_limit.blank? || list.total_entries <= depagination_limit.to_i)
 218          html << content_tag(:div, link_to(t('show_all'), :pp => 'all'), :class => 'depaginate')
 219        elsif depaginate && list.total_entries > depagination_limit.to_i
 220          html = content_tag(:div, link_to("paginate", :p => 1), :class => 'pagination')
 221        end
 222        html
 223      end
 224    end
 225    
 226    private
 227    
 228      def append_image_extension(name)
 229        unless name =~ /\.(.*?)$/
 230          name + '.png'
 231        else
 232          name
 233        end
 234      end
 235    
 236  end