spreadhead 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. data/CHANGELOG.textile +24 -0
  2. data/LICENSE +20 -0
  3. data/README.textile +72 -0
  4. data/Rakefile +76 -0
  5. data/TODO.textile +6 -0
  6. data/TUTORIAL.textile +248 -0
  7. data/VERSION +1 -0
  8. data/app/controllers/spreadhead/pages_controller.rb +62 -0
  9. data/app/views/spreadhead/pages/_form.html.erb +40 -0
  10. data/app/views/spreadhead/pages/edit.html.erb +8 -0
  11. data/app/views/spreadhead/pages/index.html.erb +25 -0
  12. data/app/views/spreadhead/pages/new.html.erb +8 -0
  13. data/app/views/spreadhead/pages/show.html.erb +5 -0
  14. data/config/spreadhead_routes.rb +4 -0
  15. data/generators/spreadhead/USAGE +1 -0
  16. data/generators/spreadhead/lib/insert_commands.rb +33 -0
  17. data/generators/spreadhead/lib/rake_commands.rb +22 -0
  18. data/generators/spreadhead/spreadhead_generator.rb +42 -0
  19. data/generators/spreadhead/templates/README +24 -0
  20. data/generators/spreadhead/templates/factories.rb +12 -0
  21. data/generators/spreadhead/templates/initializer.rb +27 -0
  22. data/generators/spreadhead/templates/migrations/create_pages.rb +20 -0
  23. data/generators/spreadhead/templates/migrations/update_pages.rb +41 -0
  24. data/generators/spreadhead/templates/page.rb +3 -0
  25. data/lib/spreadhead.rb +7 -0
  26. data/lib/spreadhead/extensions/routes.rb +14 -0
  27. data/lib/spreadhead/filter.rb +7 -0
  28. data/lib/spreadhead/page.rb +82 -0
  29. data/lib/spreadhead/render.rb +35 -0
  30. data/lib/tasks/spreadhead_tasks.rake +16 -0
  31. data/rails/init.rb +1 -0
  32. data/test/controllers/pages_controller_test.rb +118 -0
  33. data/test/models/page_test.rb +67 -0
  34. data/test/models/render_test.rb +50 -0
  35. data/test/test_helper.rb +4 -0
  36. metadata +122 -0
@@ -0,0 +1,82 @@
1
+ module Spreadhead
2
+ module Page
3
+
4
+ # Hook for all Spreadhead::Page modules.
5
+ #
6
+ # If you need to override parts of Spreadhead::Page,
7
+ # extend and include à la carte.
8
+ #
9
+ # @example
10
+ # extend ClassMethods
11
+ # include InstanceMethods
12
+ # include AttrAccessor
13
+ # include Callbacks
14
+ #
15
+ # @see ClassMethods
16
+ # @see InstanceMethods
17
+ # @see Validations
18
+ # @see Scopes
19
+ # @see Callbacks
20
+ def self.included(model)
21
+ model.extend(ClassMethods)
22
+ model.send(:include, InstanceMethods)
23
+ model.send(:include, Validations)
24
+ model.send(:include, Scopes)
25
+ model.send(:include, Callbacks)
26
+ end
27
+
28
+ module Validations
29
+ # Hook for validations.
30
+ #
31
+ # :title must be present and unique
32
+ # :text must be present
33
+ # :url must be unique
34
+ def self.included(model)
35
+ model.class_eval do
36
+ validates_presence_of :text
37
+ validates_presence_of :title
38
+ validates_uniqueness_of :title
39
+ validates_uniqueness_of :url
40
+ validates_presence_of :url
41
+ end
42
+ end
43
+ end
44
+
45
+ module Scopes
46
+ # Hook for scopes.
47
+ #
48
+ # :published should have its own scope
49
+ def self.included(model)
50
+ model.class_eval do
51
+ named_scope :published, :conditions => ['published = ?', true]
52
+ end
53
+ end
54
+ end
55
+
56
+ module Callbacks
57
+ # Hook for callbacks.
58
+ #
59
+ # :title should act like a url.
60
+ def self.included(model)
61
+ model.class_eval do
62
+ acts_as_url :title, :sync_url => true, :only_when_blank => true
63
+ end
64
+ end
65
+ end
66
+
67
+ module InstanceMethods
68
+ end
69
+
70
+ module ClassMethods
71
+ def to_fixtures(path=nil)
72
+ path ||= File.expand_path("db/data/#{table_name}.yml", RAILS_ROOT)
73
+ path = File.join(path, "#{table_name}.yml") if File.directory?(path)
74
+ file = File.open(path, "w")
75
+ file.puts(self.find(:all).inject({}) { |hash, record|
76
+ hash.merge("\"#{record.title}\"" => record.attributes)
77
+ }.to_yaml(:SortKeys => true))
78
+ file.close
79
+ end
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,35 @@
1
+ require 'redcloth'
2
+ require 'bluecloth'
3
+
4
+ module Spreadhead
5
+ module Render
6
+
7
+ def self.included(controller) # :nodoc:
8
+ controller.send(:include, InstanceMethods)
9
+ controller.class_eval do
10
+ helper_method :spreadhead
11
+ hide_action :spreadhead
12
+ end
13
+ end
14
+
15
+ module InstanceMethods
16
+ # Show the contents of the specified page to be used when rendering. The
17
+ # page parameter can be either a string (the page url) or a Page object.
18
+ #
19
+ # @return The page text as a string
20
+ def spreadhead(page)
21
+ return '' unless page
22
+ page = ::Page.find_by_url!(page) if page.is_a? String
23
+
24
+ case page.formatting
25
+ when 'markdown'
26
+ BlueCloth.new(page.text).to_html
27
+ when 'textile'
28
+ RedCloth.new(page.text).to_html
29
+ else
30
+ page.text
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,16 @@
1
+ require 'active_record/fixtures'
2
+
3
+ namespace :spreadhead do
4
+ desc "Export a set of fixtures from the current database pages"
5
+ task :export => [:environment] do
6
+ data = File.join(RAILS_ROOT, 'db', 'data')
7
+ Dir.mkdir(data) unless File.exists?(data)
8
+ Page.to_fixtures(data)
9
+ end
10
+
11
+ desc "Import a set of page fixtures into the current database (overwrites existing pages)"
12
+ task :import => [:environment] do
13
+ puts 'Importing fixtures'
14
+ Fixtures.create_fixtures("db/data/", 'pages')
15
+ end
16
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'spreadhead'
@@ -0,0 +1,118 @@
1
+ require 'test_helper'
2
+ require File.expand_path(File.dirname(__FILE__) + "/../rails/test/factories/spreadhead")
3
+
4
+ class PagesControllerTest < ActionController::TestCase
5
+
6
+ tests Spreadhead::PagesController
7
+
8
+ context "default setup" do
9
+ setup do
10
+ module Spreadhead::PagesAuth
11
+ def self.filter(controller); controller.send(:head, 403); end
12
+ end
13
+ end
14
+
15
+ context "on GET to #new" do
16
+ setup { get :new }
17
+ should_respond_with 403
18
+ end
19
+
20
+ context "on GET to #show" do
21
+ setup do
22
+ page = Factory(:page)
23
+ get :show, :url => page.url
24
+ end
25
+
26
+ should_respond_with :success
27
+ should_render_template :show
28
+ should_not_set_the_flash
29
+ end
30
+ end
31
+
32
+ context "authorized" do
33
+ setup do
34
+ module Spreadhead::PagesAuth
35
+ def self.filter(controller); true; end
36
+ end
37
+ end
38
+
39
+ context "on GET to #new" do
40
+ setup { get :new }
41
+
42
+ should_respond_with :success
43
+ should_render_template :new
44
+ should_not_set_the_flash
45
+ end
46
+
47
+ context "on GET to #index" do
48
+ setup { get :index }
49
+
50
+ should_respond_with :success
51
+ should_render_template :index
52
+ should_not_set_the_flash
53
+ end
54
+
55
+ context "on GET to #show" do
56
+ setup do
57
+ page = Factory(:page)
58
+ get :show, :url => page.url
59
+ end
60
+
61
+ should_respond_with :success
62
+ should_render_template :show
63
+ should_not_set_the_flash
64
+ end
65
+
66
+ context "on GET to #edit" do
67
+ setup do
68
+ page = Factory(:page)
69
+ get :edit, :id => page.id
70
+ end
71
+
72
+ should_respond_with :success
73
+ should_render_template :edit
74
+ should_not_set_the_flash
75
+ end
76
+
77
+ context "on POST to #create with valid attributes" do
78
+ setup do
79
+ page_attributes = Factory.attributes_for(:page)
80
+ post :create, :page => page_attributes
81
+ end
82
+
83
+ should_respond_with :redirect
84
+ should_not_set_the_flash
85
+ should_redirect_to("The list of pages") { pages_url }
86
+ end
87
+
88
+ context "on PUT to #update with valid attributes" do
89
+ setup do
90
+ page = Factory(:page)
91
+ put :update, :id => page.id, :page => page.attributes
92
+ end
93
+
94
+ should_respond_with :redirect
95
+ should_not_set_the_flash
96
+ should_redirect_to("The list of pages") { pages_url }
97
+ end
98
+
99
+ context "on DELETE to #destroy with valid attributes" do
100
+ setup do
101
+ page = Factory(:page)
102
+ delete :destroy, :id => page.id
103
+ end
104
+
105
+ should_respond_with :redirect
106
+ should_not_set_the_flash
107
+ should_redirect_to("The list of pages") { pages_url }
108
+
109
+ should "Destroy the page" do
110
+ count = Page.count
111
+ page = Factory(:page)
112
+ delete :destroy, :id => page.id
113
+ assert_equal Page.count, count
114
+ end
115
+
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,67 @@
1
+ require 'test_helper'
2
+ require File.expand_path(File.dirname(__FILE__) + "/../rails/test/factories/spreadhead")
3
+
4
+ class PageTest < ActiveSupport::TestCase
5
+
6
+ context "When creating a page" do
7
+ setup { Factory(:page) }
8
+
9
+ should_validate_presence_of :text, :title
10
+ should_validate_uniqueness_of :title
11
+
12
+ should "generate a url" do
13
+ page = Factory.build(:page, :title => 'smack!')
14
+ assert page.save
15
+ assert_not_nil page.url
16
+ assert_equal 'smack', page.url
17
+ end
18
+
19
+ should "accept a url" do
20
+ page = Factory.build(:page, :title => 'smackdown!', :url => 'whammo')
21
+ assert page.save
22
+ assert_not_nil page.url
23
+ assert_equal 'whammo', page.url
24
+ end
25
+
26
+ should "not duplicate urls" do
27
+ page = Factory.build(:page, :title => 'smurf', :url => 'bunnies')
28
+ assert page.save
29
+ assert_not_nil page.url
30
+ assert_equal 'bunnies', page.url
31
+ page = Factory.build(:page, :title => 'bunnies', :url => nil)
32
+ assert page.save
33
+ assert_not_nil page.url
34
+ assert_not_equal 'bunnies', page.url
35
+ end
36
+
37
+ should "modify the submitted url and be valid if duplicate url is submitted" do
38
+ page = Factory(:page, :title => 'smurf', :url => 'bunnies')
39
+ dup = Factory.build(:page, :title => 'smurffette', :url => 'bunnies')
40
+ assert dup.valid?
41
+ assert_not_equal page.url, dup.url
42
+ assert dup.save
43
+ assert !dup.errors.on(:url)
44
+ end
45
+
46
+ should "add suffix and be valid when two different titles generate the same url" do
47
+ page = Factory(:page, :title => 'smurf', :url => 'smurf')
48
+ dup = Factory(:page, :title => 'smurf!', :url => 'buttons')
49
+ assert dup.update_attributes(:url => nil)
50
+ assert_equal 'smurf-1', dup.url
51
+ end
52
+
53
+ should "be valid when a url is a subset of another url" do
54
+ page = Factory(:page, :title => 'smurf', :url => 'woozles')
55
+ dup = Factory(:page, :title => 'Woo', :url => 'brick-spin-brachiosaurus')
56
+ assert dup.update_attributes(:url => nil)
57
+ assert_equal 'woo', dup.url
58
+ end
59
+
60
+ end
61
+
62
+ context "When retrieving a page" do
63
+ setup { Factory(:page) }
64
+
65
+ should_have_named_scope :published
66
+ end
67
+ end
@@ -0,0 +1,50 @@
1
+ require 'test_helper'
2
+ require File.expand_path(File.dirname(__FILE__) + "/../rails/test/factories/spreadhead")
3
+
4
+ class Renderer < ApplicationController
5
+ include Spreadhead::Render
6
+ end
7
+
8
+ class RenderTest < ActiveSupport::TestCase
9
+
10
+ context "When rendering a page" do
11
+ setup do
12
+ @renderer = Renderer.new
13
+ @page = Factory(:page)
14
+ end
15
+
16
+ should "render a page by url" do
17
+ assert_equal "Paging Mrs. Smith", @renderer.spreadhead(@page.url)
18
+ end
19
+
20
+ should "render a page by object" do
21
+ assert_equal "Paging Mrs. Smith", @renderer.spreadhead(@page)
22
+ end
23
+
24
+ should "render an empty page" do
25
+ assert_equal "", @renderer.spreadhead(nil)
26
+ end
27
+ end
28
+
29
+ context "When rendering a textile page" do
30
+ setup do
31
+ @renderer = Renderer.new
32
+ @page = Factory(:page, :text => 'h2. Sunday Sunday Sunday', :formatting => 'textile')
33
+ end
34
+
35
+ should "render a page with textile" do
36
+ assert_equal "<h2>Sunday Sunday Sunday</h2>", @renderer.spreadhead(@page)
37
+ end
38
+ end
39
+
40
+ context "When rendering a markdown page" do
41
+ setup do
42
+ @renderer = Renderer.new
43
+ @page = Factory(:page, :text => "Sunday Sunday Sunday\n---", :formatting => 'markdown')
44
+ end
45
+
46
+ should "render a page with textile" do
47
+ assert_equal "<h2>Sunday Sunday Sunday</h2>", @renderer.spreadhead(@page)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,4 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/rails/config/environment")
2
+ require 'test_help'
3
+ require 'factory_girl'
4
+ require 'shoulda'
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spreadhead
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Rafter
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-20 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: BlueCloth
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: RedCloth
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: rsl-stringex
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ description: Rails content mangement for pages that shouldn't be views.
46
+ email: jeff@socialrange.org
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - LICENSE
53
+ - README.textile
54
+ files:
55
+ - CHANGELOG.textile
56
+ - LICENSE
57
+ - README.textile
58
+ - Rakefile
59
+ - TODO.textile
60
+ - TUTORIAL.textile
61
+ - VERSION
62
+ - app/controllers/spreadhead/pages_controller.rb
63
+ - app/views/spreadhead/pages/_form.html.erb
64
+ - app/views/spreadhead/pages/edit.html.erb
65
+ - app/views/spreadhead/pages/index.html.erb
66
+ - app/views/spreadhead/pages/new.html.erb
67
+ - app/views/spreadhead/pages/show.html.erb
68
+ - config/spreadhead_routes.rb
69
+ - generators/spreadhead/USAGE
70
+ - generators/spreadhead/lib/insert_commands.rb
71
+ - generators/spreadhead/lib/rake_commands.rb
72
+ - generators/spreadhead/spreadhead_generator.rb
73
+ - generators/spreadhead/templates/README
74
+ - generators/spreadhead/templates/factories.rb
75
+ - generators/spreadhead/templates/initializer.rb
76
+ - generators/spreadhead/templates/migrations/create_pages.rb
77
+ - generators/spreadhead/templates/migrations/update_pages.rb
78
+ - generators/spreadhead/templates/page.rb
79
+ - lib/spreadhead.rb
80
+ - lib/spreadhead/extensions/routes.rb
81
+ - lib/spreadhead/filter.rb
82
+ - lib/spreadhead/page.rb
83
+ - lib/spreadhead/render.rb
84
+ - lib/tasks/spreadhead_tasks.rake
85
+ - rails/init.rb
86
+ - test/controllers/pages_controller_test.rb
87
+ - test/models/page_test.rb
88
+ - test/models/render_test.rb
89
+ - test/test_helper.rb
90
+ has_rdoc: true
91
+ homepage: http://github.com/jeffrafter/spreadhead
92
+ licenses: []
93
+
94
+ post_install_message:
95
+ rdoc_options:
96
+ - --charset=UTF-8
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: "0"
104
+ version:
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: "0"
110
+ version:
111
+ requirements: []
112
+
113
+ rubyforge_project:
114
+ rubygems_version: 1.3.4
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: Rails content mangement for pages that shouldn't be views.
118
+ test_files:
119
+ - test/controllers/pages_controller_test.rb
120
+ - test/models/page_test.rb
121
+ - test/models/render_test.rb
122
+ - test/test_helper.rb