public
Description: Help documentation extension for radiant cms
Homepage: http://www.saturnflyer.com
Clone URL: git://github.com/saturnflyer/radiant-help-extension.git
Click here to lend your support to: radiant-help-extension and make a donation at www.pledgie.com !
saturnflyer (author)
Fri Jul 03 09:45:05 -0700 2009
commit  290a353e313e42b3b7d54af415e03c7b9121d568
tree    ff3b9eb16e4ec7bed7d3550423d97626e209e069
parent  0ef4c8150da07cabaa38bb160f5620155dec8781
radiant-help-extension / help_extension.rb
100644 86 lines (75 sloc) 3.547 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
84
85
86
# Uncomment this if you reference any of your controllers in activate
require_dependency 'application_controller'
# You'll need this if you are going to add regions into your extension interface.
require 'ostruct'
# These are needed for parsing the HELP.rdoc files
require 'rdoc/markup/simple_markup'
require 'rdoc/markup/simple_markup/to_html'
 
class HelpExtension < Radiant::Extension
  version "1.0"
  description "Help Documentation for Radiant CMS"
  url "http://saturnflyer.com/"
  
  # These routes are added to the radiant routes file and works just like any rails routes.
  define_routes do |map|
    map.with_options :controller => 'admin/help', :conditions => {:method => :get} do |help|
      help.help_role 'admin/role_help/:role', :action => 'role', :role => nil
      help.help_extension_doc 'admin/extension_help/:extension_name/:role', :action => 'extension_doc', :role => 'all'
      help.help 'admin/help', :action => 'index'
    end
  end
  
  def activate
    # This adds a tab to the interface after the Layouts tab, and allows all users to access it.
    admin.tabs.add "Help", "/admin/help", :after => "Layouts", :visibility => [:all]
    
    # This adds information to the Radiant interface. In this extension, we're dealing with "help" views
    # so :help is an attr_accessor. If you're creating an extension for tracking moons and stars, you might
    # put attr_accessor :moon, :star
    Radiant::AdminUI.class_eval do
      attr_accessor :help
    end
    
    # initialize regions for help (which we created above)
    admin.help = load_default_help_regions
    
    # Provide the ability to replace regions...
    # Don't like how the regions are setup? Hack it without changing this extension's code
    # Be forewarned, this allows you to completely change the UI
    Radiant::AdminUI::RegionSet.class_eval do
      def replace(region=nil, partial=nil)
        raise ArgumentError, "You must specify a region and a partial" unless region and partial
        self[region].replace([partial])
      end
    end
    # You could, for example create your own role based interface with this
    # admin.help.main.replace('main','main_for_admins_only')
    # But I only threw this in here to allow you to change the help docs easily if you want.
    # I am merely providing the rope...
    
    # Finally, allow all the helpers to be used anywhere
    ApplicationController.class_eval {
      helper Admin::HelpHelper
    }
  end
  
  def deactivate
    # This never happens
  end
  
  private
  
  # This is where we define all of the regions to be used in the views and partials
  def load_default_help_regions
    returning OpenStruct.new do |help|
      help.index = Radiant::AdminUI::RegionSet.new do |index|
        index.main.concat %w{introduction organizing editing}
        index.page_details.concat %w{page_details_introduction slug breadcrumb}
        index.filter.concat %w{filter_basics}
        index.available_tags.concat %w{available_tags_basics}
        index.layout.concat %w{layout_basics}
        index.page_type.concat %w{page_type_basics}
        index.saving.concat %w{saving_basics}
        index.extras.concat %w{extension_docs_list}
      end
      help.role = Radiant::AdminUI::RegionSet.new do |role|
        role.extras.concat %w{extension_docs_list regions}
        role.regions.concat %w{regions_introduction}
      end
      help.extension_doc = Radiant::AdminUI::RegionSet.new do |extension_doc|
        extension_doc.extras.concat %w{extension_docs_list}
      end
    end
  end
  
end