mleung / feather

Uber lightweight Merb blogging engine. Make sure you check out the feather-plugins repo as well!

This URL has Read+Write access

feather / lib / feather.rb
100644 131 lines (110 sloc) 5.519 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
if defined?(Merb::Plugins)
 
  $:.unshift File.dirname(__FILE__)
  $:.unshift File.join(File.dirname(__FILE__), "..", "app", "models")
  $:.unshift File.join(File.dirname(__FILE__), "..", "app", "controllers")
  $:.unshift File.join(File.dirname(__FILE__), "..", "app", "helpers")
 
  dependency 'merb-slices'
  Merb::Plugins.add_rakefiles "feather/merbtasks", "feather/slicetasks", "feather/spectasks"
 
  # Register the Slice for the current host application
  Merb::Slices::register(__FILE__)
  
  # Slice configuration - set this in a before_app_loads callback.
  # By default a Slice uses its own layout, so you can swicht to
  # the main application layout or no layout at all if needed.
  #
  # Configuration options:
  # :layout - the layout to use; defaults to :feather-slice
  # :mirror - which path component types to use on copy operations; defaults to all
  Merb::Slices::config[:feather][:layout] ||= :application
  
  require 'config/dependencies.rb'
  
  # All Slice code is expected to be namespaced inside a module
  module Feather
    
    # Slice metadata
    self.description = "The slice version of Feather, the extensible, lightweight blogging engine for Merb"
    self.version = "0.5"
    self.author = "El Draper"
    
    # Stub classes loaded hook - runs before LoadClasses BootLoader
    # right after a slice's classes have been loaded internally.
    def self.loaded
    end
    
    # Initialization hook - runs before AfterAppLoads BootLoader
    def self.init
      Merb::Authentication.user_class = Feather::User
    end
    
    # Activation hook - runs after AfterAppLoads BootLoader
    def self.activate
    end
    
    # Deactivation hook - triggered by Merb::Slices.deactivate(Feather)
    def self.deactivate
    end
    
    # Setup routes inside the host application
    #
    # @param scope<Merb::Router::Behaviour>
    # Routes will be added within this scope (namespace). In fact, any
    # router behaviour is a valid namespace, so you can attach
    # routes at any level of your router setup.
    #
    # @note prefix your named routes with :feather_slice_
    # to avoid potential conflicts with global named routes.
    def self.setup_router(scope)
      require File.join(File.dirname(__FILE__), "feather", "padding")
      require File.join(File.dirname(__FILE__), "feather", "hooks")
      require File.join(File.dirname(__FILE__), "feather", "database")
      require File.join(File.dirname(__FILE__), "feather", "plugin_dependencies")
 
      # This loads the plugins
      begin
       Feather::Plugin.all.each do |p|
         begin
           p.load
           Merb.logger.info("\"#{p.name}\" loaded")
         rescue Exception => e
           Merb.logger.info("\"#{p.name}\" failed to load : #{e.message}")
         end
       end
      rescue Exception => e
       Merb.logger.info("Error loading plugins: #{e.message}")
      end
       
      # Load all plugin routes
      Feather::Hooks::Routing.load_routes(scope)
      
      # This deferred route allows permalinks to be handled, without a separate rack handler
      scope.match("/:controller", :controller => '.*').defer_to do |request, params|
        # Permalinks are relative to the slice mount point, so we'll need to remove that from the request before we look up the article
        permalink = request.uri.to_s.chomp("/")
        permalink.gsub!("/#{::Feather.config[:path_prefix]}", "") unless ::Feather.config[:path_prefix].blank?
        # Attempt to find the article
        unless (id = Feather::Article.routing[permalink]).nil?
          params.merge!({:controller => "feather/articles", :action => "show", :id => id})
        end
      end
 
      # Admin namespace
      scope.namespace "admin", :path => "admin", :name_prefix => "admin" do
        scope.resource :configuration, :path => "admin/configuration", :name_prefix => "admin", :controller => "admin/configurations"
        scope.resources :plugins, :path => "admin/plugins", :name_prefix => "admin", :controller => "admin/plugins"
        scope.resources :articles, :path => "admin/articles", :name_prefix => "admin", :controller => "admin/articles"
        scope.resources :users, :path => "admin/users", :name_prefix => "admin", :controller => "admin/users"
        scope.resource :dashboard, :path => "admin/dashboard", :name_prefix => "admin", :controller => "admin/dashboards"
      end
      scope.match("/admin").to(:action => "show", :controller => "admin/dashboards")
 
      # Year/month/day routes
      scope.match("/:year").to(:controller => "articles", :action => "index").name(:year)
      scope.match("/:year/:month").to(:controller => "articles", :action => "index").name(:month)
      scope.match("/:year/:month/:day").to(:controller => "articles", :action => "index").name(:day)
 
      # Default routes, and index
      scope.match("/").to(:controller => 'articles', :action =>'index')
    end
  end
  
  # Setup the slice layout for Feather
  #
  # Use Feather.push_path and Feather.push_app_path
  # to set paths to feather-level and app-level paths. Example:
  #
  # Feather.push_path(:application, Feather.root)
  # Feather.push_app_path(:application, Merb.root / 'slices' / 'feather-slice')
  # ...
  #
  # Any component path that hasn't been set will default to Feather.root
  #
  # Or just call setup_default_structure! to setup a basic Merb MVC structure.
  Feather.setup_default_structure!
  
  # Add dependencies for other Feather classes below. Example:
  # dependency "feather/other"
  
end