radiant / radiant-prototype

Prototype of the user-interface of Radiant CMS.

radiant-prototype / view_helpers.rb
100644 83 lines (65 sloc) 2.668 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
78
79
80
81
82
83
module ViewHelpers
  def params
    request.query.dup
  end
  
  
  # The NavTab Class holds the structure of a navigation tab (including
  # its sub-nav items).
  class NavTab < Array
    attr_reader :name, :proper_name
    
    def initialize(name, proper_name)
      @name, @proper_name = name, proper_name
    end
    
    def [](id)
      unless id.kind_of? Fixnum
        self.find {|subnav_item| subnav_item.name.to_s == id.to_s }
      else
        super
      end
    end
  end
  
  # Simple structure for storing the properties of a tab's sub items.
  class NavSubItem
    attr_reader :name, :proper_name, :url
    
    def initialize(name, proper_name, url = "#")
      @name, @proper_name, @url = name, proper_name, url
    end
  end
  
  def nav_tabs
    content = NavTab.new(:content, "Content")
    content << NavSubItem.new(:pages, "Pages", "/admin/pages/")
    content << NavSubItem.new(:snippets, "Snippets", "/admin/snippets/")
    
    design = NavTab.new(:design, "Design")
    design << NavSubItem.new(:layouts, "Layouts", "/admin/layouts/")
    design << NavSubItem.new(:stylesheets, "Stylesheets", "/admin/stylesheets/")
    design << NavSubItem.new(:javascripts, "Javascripts", "/admin/javascripts/")
    
    media = NavTab.new(:assets, "Assets")
    media << NavSubItem.new(:all, "All", "/admin/assets/")
    media << NavSubItem.new(:all, "Unattached", "/admin/assets/unattached/")
    
    settings = NavTab.new(:settings, "Settings")
    settings << NavSubItem.new(:general, "General", "/admin/settings/")
    settings << NavSubItem.new(:users, "Users", "/admin/users/")
    settings << NavSubItem.new(:extensions, "Extensions", "/admin/extensions/")
    
    [content, design, media, settings]
  end
  
  def body_classes
    @body_classes ||= []
  end
  
  # Returns a Gravatar URL associated with the email parameter.
  # See: http://douglasfshearer.com/blog/gravatar-for-ruby-and-ruby-on-rails
  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.
    options[:default] ||= "http://localhost:4000/admin/images/avatar_32x32.png"
    
    # Build the Gravatar url.
    url = 'http://www.gravatar.com/avatar.php?'
    url << "gravatar_id=#{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]
    url
  end
end