static_docs 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (42) hide show
  1. data/app/assets/javascripts/static_docs/pages.js +2 -0
  2. data/app/assets/stylesheets/static_docs/pages.css +4 -0
  3. data/app/controllers/static_docs/application_controller.rb +1 -1
  4. data/app/controllers/static_docs/pages_controller.rb +9 -0
  5. data/app/helpers/static_docs/application_helper.rb +11 -0
  6. data/app/helpers/static_docs/pages_helper.rb +4 -0
  7. data/app/models/static_docs/page.rb +30 -0
  8. data/app/views/static_docs/pages/show.html.erb +3 -0
  9. data/config/routes.rb +4 -0
  10. data/db/migrate/20130502102206_create_static_docs_pages.rb +16 -0
  11. data/lib/static_docs/importer.rb +44 -0
  12. data/lib/static_docs/version.rb +1 -1
  13. data/lib/static_docs.rb +30 -0
  14. data/lib/tasks/static_docs_tasks.rake +11 -4
  15. data/test/dummy/app/helpers/application_helper.rb +3 -0
  16. data/test/dummy/config/environments/test.rb +1 -1
  17. data/test/dummy/config/initializers/static_docs.rb +13 -0
  18. data/test/dummy/config/routes.rb +1 -1
  19. data/test/dummy/db/development.sqlite3 +0 -0
  20. data/test/dummy/db/migrate/20130502133441_create_static_docs_pages.static_docs.rb +17 -0
  21. data/test/dummy/db/schema.rb +29 -0
  22. data/test/dummy/db/test.sqlite3 +0 -0
  23. data/test/dummy/log/development.log +56 -0
  24. data/test/dummy/log/production.log +0 -0
  25. data/test/dummy/log/test.log +8879 -0
  26. data/test/dummy/sources/namespace/home.html +1 -0
  27. data/test/dummy/sources/namespace/index.yml +9 -0
  28. data/test/dummy/sources/namespace/page.html +1 -0
  29. data/test/dummy/sources/root/getting_started.html +1 -0
  30. data/test/dummy/sources/root/home.html +1 -0
  31. data/test/dummy/sources/root/index.yml +13 -0
  32. data/test/dummy/sources/root/page.html +1 -0
  33. data/test/fixtures/static_docs/pages.yml +51 -0
  34. data/test/functional/static_docs/pages_controller_test.rb +9 -0
  35. data/test/integration/navigation_test.rb +22 -4
  36. data/test/static_docs_test.rb +7 -0
  37. data/test/test_helper.rb +0 -2
  38. data/test/unit/helpers/static_docs/pages_helper_test.rb +6 -0
  39. data/test/unit/static_docs/importer_test.rb +52 -0
  40. data/test/unit/static_docs/page_test.rb +46 -0
  41. metadata +52 -5
  42. data/app/views/layouts/static_docs/application.html.erb +0 -14
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -1,4 +1,4 @@
1
1
  module StaticDocs
2
- class ApplicationController < ActionController::Base
2
+ class ApplicationController < ::ApplicationController
3
3
  end
4
4
  end
@@ -0,0 +1,9 @@
1
+ require_dependency "static_docs/application_controller"
2
+
3
+ module StaticDocs
4
+ class PagesController < ApplicationController
5
+ def show
6
+ @page = Page.matched(params[:page_path])
7
+ end
8
+ end
9
+ end
@@ -1,4 +1,15 @@
1
1
  module StaticDocs
2
2
  module ApplicationHelper
3
+ def respond_to?(method)
4
+ super || main_app.respond_to?(method)
5
+ end
6
+
7
+ def method_missing(method, *args, &block)
8
+ if main_app.respond_to?(method)
9
+ main_app.send(method, *args, &block)
10
+ else
11
+ super
12
+ end
13
+ end
3
14
  end
4
15
  end
@@ -0,0 +1,4 @@
1
+ module StaticDocs
2
+ module PagesHelper
3
+ end
4
+ end
@@ -0,0 +1,30 @@
1
+ module StaticDocs
2
+ class Page < ActiveRecord::Base
3
+ attr_accessor :meta
4
+
5
+ class << self
6
+ def matched(path)
7
+ namespace, _, namespaced_path = path.partition('/')
8
+ namespaced_matched(namespaced_path, namespace) || namespaced_matched(path) || raise(ActiveRecord::RecordNotFound)
9
+ end
10
+
11
+ def namespaced_matched(path, namespace = nil)
12
+ if path.present? && StaticDocs.namespaces.include?(namespace)
13
+ where(:namespace => namespace, :path => path).first
14
+ end
15
+ end
16
+ end
17
+
18
+ def meta
19
+ @meta ||= {}
20
+ end
21
+
22
+ def renderer
23
+ @renderer ||= StaticDocs.renderers[namespace].try(:fetch, extension) || StaticDocs.renderers[:default][extension]
24
+ end
25
+
26
+ def rendered_body(context)
27
+ context.instance_exec(body, self, &renderer)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ <div class='content'>
2
+ <%= @page.rendered_body(self) %>
3
+ </div>
data/config/routes.rb CHANGED
@@ -1,2 +1,6 @@
1
1
  StaticDocs::Engine.routes.draw do
2
+ root :to => 'pages#show', :page_path => 'home'
3
+ get '*page_path' => 'pages#show'
4
+
5
+
2
6
  end
@@ -0,0 +1,16 @@
1
+ class CreateStaticDocsPages < ActiveRecord::Migration
2
+ def change
3
+ create_table :static_docs_pages do |t|
4
+ t.string :title
5
+ t.string :path
6
+ t.string :namespace
7
+ t.text :body, :limit => 64.kilobytes + 1
8
+ t.string :extension
9
+
10
+ t.timestamps
11
+ end
12
+
13
+ add_index :static_docs_pages, :path
14
+ add_index :static_docs_pages, :namespace
15
+ end
16
+ end
@@ -0,0 +1,44 @@
1
+ module StaticDocs
2
+ class Importer
3
+ attr_accessor :namespace
4
+
5
+ def self.import(namespace)
6
+ new(namespace).import
7
+ end
8
+
9
+ def initialize(namespace)
10
+ @namespace = namespace
11
+ end
12
+
13
+ def source
14
+ @source ||= Rails.root.join StaticDocs.sources[namespace]
15
+ end
16
+
17
+ def config
18
+ @config ||= YAML.load IO.read File.join source, 'index.yml'
19
+ end
20
+
21
+ def import
22
+ cleanup
23
+ import_collection config['special'], false
24
+ import_collection config['pages']
25
+ end
26
+
27
+ def import_collection(pages, show = true)
28
+ return if pages.empty?
29
+ pages.each_with_index do |data, index|
30
+ page = Page.where(:namespace => namespace, :path => data['path']).first_or_initialize
31
+ page.extension = data['file'][/([^\.]+)$/]
32
+ page.position = show ? index : nil if page.attributes.include?('position')
33
+ page.title = data['title']
34
+ page.body = File.read File.join(source, data['file'])
35
+ page.save
36
+ end
37
+ end
38
+
39
+ def cleanup
40
+ actual_page_urls = (config['pages'] + config['special']).map{ |page| page['path'] }
41
+ Page.where(:namespace => namespace).where('path not in (?)', actual_page_urls).destroy_all
42
+ end
43
+ end
44
+ end
@@ -1,3 +1,3 @@
1
1
  module StaticDocs
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/static_docs.rb CHANGED
@@ -1,4 +1,34 @@
1
1
  require "static_docs/engine"
2
2
 
3
3
  module StaticDocs
4
+ mattr_accessor :sources
5
+ @@sources = {}.with_indifferent_access
6
+
7
+ mattr_accessor :renderers
8
+ @@renderers = {
9
+ :default => {
10
+ :html => proc{ |body| body.html_safe },
11
+ :txt => proc{ |body| body }
12
+ }.with_indifferent_access
13
+ }.with_indifferent_access
14
+
15
+ def self.source(src, options = {})
16
+ namespace = options[:namespace] || options[:to]
17
+ @@sources[namespace] = src
18
+ end
19
+
20
+ def self.renderer(format, options = {}, &block)
21
+ namespace = options[:namespace] || :default
22
+ @@renderers[namespace] ||= {}.with_indifferent_access
23
+ @@renderers[namespace][format] = block
24
+ end
25
+
26
+ def self.setup(&block)
27
+ instance_eval(&block)
28
+ end
29
+
30
+ def self.namespaces
31
+ @namespaces ||= sources.keys
32
+ end
33
+
4
34
  end
@@ -1,4 +1,11 @@
1
- # desc "Explaining what the task does"
2
- # task :static_docs do
3
- # # Task goes here
4
- # end
1
+ require "static_docs/importer"
2
+
3
+ namespace :static_docs do
4
+ desc "Import pages to database. Use `namespace=xxx` or `namespaces=xxx,yyy` to point what namespace to import. Use keyword `root` to point on root namespace."
5
+ task :import => :environment do
6
+ namespaces = (ENV['namespace'] || ENV['namespaces']).try(:split, ',')
7
+ (namespaces ? namespaces.map{ |n| n == 'root' ? nil : n } & c.namespaces : StaticDocs.namespaces).each do |namespace|
8
+ StaticDocs::Importer.import(namespace)
9
+ end
10
+ end
11
+ end
@@ -1,2 +1,5 @@
1
1
  module ApplicationHelper
2
+ def markdown(text)
3
+ "<h1>This is original markdown text</h1><pre>#{text}</pre>".html_safe
4
+ end
2
5
  end
@@ -19,7 +19,7 @@ Dummy::Application.configure do
19
19
  config.action_controller.perform_caching = false
20
20
 
21
21
  # Raise exceptions instead of rendering exception templates
22
- config.action_dispatch.show_exceptions = false
22
+ config.action_dispatch.show_exceptions = true
23
23
 
24
24
  # Disable request forgery protection in test environment
25
25
  config.action_controller.allow_forgery_protection = false
@@ -0,0 +1,13 @@
1
+ StaticDocs.setup do
2
+ source 'sources/root'
3
+ source 'sources/namespace', :namespace => :namespace
4
+
5
+ renderer :md do |body|
6
+ markdown(body)
7
+ end
8
+
9
+ renderer :txt do |body, page|
10
+ page.meta[:test] = 'test'
11
+ page.meta[:test] * 3
12
+ end
13
+ end
@@ -1,4 +1,4 @@
1
1
  Rails.application.routes.draw do
2
2
 
3
- mount StaticDocs::Engine => "/static_docs"
3
+ mount StaticDocs::Engine => "/"
4
4
  end
Binary file
@@ -0,0 +1,17 @@
1
+ # This migration comes from static_docs (originally 20130502102206)
2
+ class CreateStaticDocsPages < ActiveRecord::Migration
3
+ def change
4
+ create_table :static_docs_pages do |t|
5
+ t.string :title
6
+ t.string :path
7
+ t.string :namespace
8
+ t.text :body, :limit => 64.kilobytes + 1
9
+ t.string :extension
10
+
11
+ t.timestamps
12
+ end
13
+
14
+ add_index :static_docs_pages, :path
15
+ add_index :static_docs_pages, :namespace
16
+ end
17
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: UTF-8
2
+ # This file is auto-generated from the current state of the database. Instead
3
+ # of editing this file, please use the migrations feature of Active Record to
4
+ # incrementally modify your database, and then regenerate this schema definition.
5
+ #
6
+ # Note that this schema.rb definition is the authoritative source for your
7
+ # database schema. If you need to create the application database on another
8
+ # system, you should be using db:schema:load, not running all the migrations
9
+ # from scratch. The latter is a flawed and unsustainable approach (the more migrations
10
+ # you'll amass, the slower it'll run and the greater likelihood for issues).
11
+ #
12
+ # It's strongly recommended to check this file into your version control system.
13
+
14
+ ActiveRecord::Schema.define(:version => 20130502133441) do
15
+
16
+ create_table "static_docs_pages", :force => true do |t|
17
+ t.string "title"
18
+ t.string "path"
19
+ t.string "namespace"
20
+ t.text "body", :limit => 65537
21
+ t.string "extension"
22
+ t.datetime "created_at", :null => false
23
+ t.datetime "updated_at", :null => false
24
+ end
25
+
26
+ add_index "static_docs_pages", ["namespace"], :name => "index_static_docs_pages_on_namespace"
27
+ add_index "static_docs_pages", ["path"], :name => "index_static_docs_pages_on_path"
28
+
29
+ end
Binary file
@@ -0,0 +1,56 @@
1
+ Connecting to database specified by database.yml
2
+  (0.2ms) select sqlite_version(*)
3
+  (1.0ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
4
+  (1.4ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
5
+  (2.8ms) SELECT "schema_migrations"."version" FROM "schema_migrations"
6
+ Migrating to CreateStaticDocsPages (20130502133441)
7
+  (0.0ms) begin transaction
8
+  (0.4ms) CREATE TABLE "static_docs_pages" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "path" varchar(255), "body" text(65537), "extension" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
9
+  (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES ('20130502133441')
10
+  (0.9ms) commit transaction
11
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
12
+ Connecting to database specified by database.yml
13
+  (1.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
14
+  (0.2ms) select sqlite_version(*)
15
+  (1.5ms) CREATE TABLE "static_docs_pages" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "path" varchar(255), "body" text(65537), "extension" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) 
16
+  (1.0ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
17
+  (1.1ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
18
+  (0.1ms) SELECT version FROM "schema_migrations"
19
+  (0.8ms) INSERT INTO "schema_migrations" (version) VALUES ('20130502133441')
20
+ Connecting to database specified by database.yml
21
+  (4.0ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
22
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations"
23
+ Migrating to CreateStaticDocsPages (20130502133441)
24
+  (0.0ms) select sqlite_version(*)
25
+  (0.0ms) begin transaction
26
+  (1.4ms) DROP TABLE "static_docs_pages"
27
+  (0.1ms) DELETE FROM "schema_migrations" WHERE "schema_migrations"."version" = '20130502133441'
28
+  (0.8ms) commit transaction
29
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations"
30
+ Connecting to database specified by database.yml
31
+  (1.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
32
+ Migrating to CreateStaticDocsPages (20130502133441)
33
+  (0.1ms) select sqlite_version(*)
34
+  (0.1ms) begin transaction
35
+  (0.1ms) rollback transaction
36
+ Connecting to database specified by database.yml
37
+  (21.8ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
38
+ Migrating to CreateStaticDocsPages (20130502133441)
39
+  (0.1ms) select sqlite_version(*)
40
+  (0.0ms) begin transaction
41
+  (1.6ms) CREATE TABLE "static_docs_pages" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "path" varchar(255), "namespace" varchar(255), "body" text(65537), "extension" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
42
+  (0.5ms) CREATE INDEX "index_static_docs_pages_on_path" ON "static_docs_pages" ("path")
43
+  (0.2ms) CREATE INDEX "index_static_docs_pages_on_namespace" ON "static_docs_pages" ("namespace")
44
+  (0.2ms) INSERT INTO "schema_migrations" ("version") VALUES ('20130502133441')
45
+  (1.1ms) commit transaction
46
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
47
+ Connecting to database specified by database.yml
48
+  (20.9ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
49
+  (0.5ms) select sqlite_version(*)
50
+  (1.4ms) CREATE TABLE "static_docs_pages" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "path" varchar(255), "namespace" varchar(255), "body" text(65537), "extension" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) 
51
+  (1.0ms) CREATE INDEX "index_static_docs_pages_on_namespace" ON "static_docs_pages" ("namespace")
52
+  (0.9ms) CREATE INDEX "index_static_docs_pages_on_path" ON "static_docs_pages" ("path")
53
+  (1.1ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
54
+  (1.0ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
55
+  (0.1ms) SELECT version FROM "schema_migrations"
56
+  (1.1ms) INSERT INTO "schema_migrations" (version) VALUES ('20130502133441')
File without changes