radiant 0.5.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of radiant might be problematic. Click here for more details.

Files changed (205) hide show
  1. data/CHANGELOG +1 -0
  2. data/CONTRIBUTORS +12 -0
  3. data/LICENSE +9 -0
  4. data/README +91 -0
  5. data/Rakefile +10 -0
  6. data/app/behaviors/archive_behavior.rb +42 -0
  7. data/app/behaviors/archive_day_index_behavior.rb +30 -0
  8. data/app/behaviors/archive_month_index_behavior.rb +30 -0
  9. data/app/behaviors/archive_year_index_behavior.rb +30 -0
  10. data/app/behaviors/env_behavior.rb +18 -0
  11. data/app/behaviors/page_missing_behavior.rb +31 -0
  12. data/app/controllers/admin/export_controller.rb +8 -0
  13. data/app/controllers/admin/layout_controller.rb +23 -0
  14. data/app/controllers/admin/model_controller.rb +119 -0
  15. data/app/controllers/admin/page_controller.rb +119 -0
  16. data/app/controllers/admin/snippet_controller.rb +6 -0
  17. data/app/controllers/admin/user_controller.rb +45 -0
  18. data/app/controllers/admin/welcome_controller.rb +39 -0
  19. data/app/controllers/application.rb +32 -0
  20. data/app/controllers/site_controller.rb +54 -0
  21. data/app/filters/markdown_filter.rb +9 -0
  22. data/app/filters/textile_filter.rb +9 -0
  23. data/app/helpers/admin/export_helper.rb +2 -0
  24. data/app/helpers/admin/layout_helper.rb +2 -0
  25. data/app/helpers/admin/page_helper.rb +10 -0
  26. data/app/helpers/admin/snippet_helper.rb +2 -0
  27. data/app/helpers/admin/user_helper.rb +2 -0
  28. data/app/helpers/admin/welcome_helper.rb +2 -0
  29. data/app/helpers/application_helper.rb +141 -0
  30. data/app/helpers/site_helper.rb +2 -0
  31. data/app/models/archive_finder.rb +66 -0
  32. data/app/models/behavior.rb +194 -0
  33. data/app/models/layout.rb +13 -0
  34. data/app/models/page.rb +91 -0
  35. data/app/models/page_context.rb +526 -0
  36. data/app/models/page_part.rb +13 -0
  37. data/app/models/radiant/config.rb +53 -0
  38. data/app/models/radiant/exporter.rb +11 -0
  39. data/app/models/response_cache.rb +112 -0
  40. data/app/models/snippet.rb +16 -0
  41. data/app/models/status.rb +31 -0
  42. data/app/models/text_filter.rb +11 -0
  43. data/app/models/user.rb +70 -0
  44. data/app/models/user_action_observer.rb +13 -0
  45. data/app/views/admin/layout/index.rhtml +38 -0
  46. data/app/views/admin/layout/new.rhtml +37 -0
  47. data/app/views/admin/layout/remove.rhtml +17 -0
  48. data/app/views/admin/page/_node.rhtml +51 -0
  49. data/app/views/admin/page/_part.rhtml +17 -0
  50. data/app/views/admin/page/children.rhtml +4 -0
  51. data/app/views/admin/page/index.rhtml +172 -0
  52. data/app/views/admin/page/new.rhtml +164 -0
  53. data/app/views/admin/page/remove.rhtml +14 -0
  54. data/app/views/admin/snippet/index.rhtml +36 -0
  55. data/app/views/admin/snippet/new.rhtml +28 -0
  56. data/app/views/admin/snippet/remove.rhtml +16 -0
  57. data/app/views/admin/user/index.rhtml +43 -0
  58. data/app/views/admin/user/new.rhtml +48 -0
  59. data/app/views/admin/user/preferences.rhtml +29 -0
  60. data/app/views/admin/user/remove.rhtml +16 -0
  61. data/app/views/admin/welcome/login.rhtml +51 -0
  62. data/app/views/layouts/application.rhtml +75 -0
  63. data/app/views/site/not_found.rhtml +3 -0
  64. data/bin/radiant +305 -0
  65. data/config/boot.rb +80 -0
  66. data/config/database.mysql.yml +20 -0
  67. data/config/database.postgresql.yml +20 -0
  68. data/config/database.sqlite.yml +20 -0
  69. data/config/environment.rb +76 -0
  70. data/config/environments/development.rb +20 -0
  71. data/config/environments/production.rb +22 -0
  72. data/config/environments/test.rb +20 -0
  73. data/config/locomotive.yml +6 -0
  74. data/config/routes.rb +64 -0
  75. data/db/development_structure.sql +80 -0
  76. data/db/migrate/001_create_radiant_tables.rb +73 -0
  77. data/db/migrate/002_insert_initial_data.rb +45 -0
  78. data/db/migrate/003_rename_behavior_column.rb +9 -0
  79. data/db/migrate/004_rename_filter_column.rb +11 -0
  80. data/db/migrate/005_add_virtual_column_to_page.rb +9 -0
  81. data/db/migrate/006_integer_columns_to_boolean.rb +11 -0
  82. data/db/migrate/007_remove_virtual_column_from_page.rb +9 -0
  83. data/db/migrate/008_add_virtual_column_to_page_again.rb +9 -0
  84. data/db/migrate/009_add_content_type_field_to_layout.rb +9 -0
  85. data/db/schema.rb +74 -0
  86. data/db/templates/empty.yml +2 -0
  87. data/db/templates/simple-blog.yml +197 -0
  88. data/db/templates/styled-blog.yml +472 -0
  89. data/lib/advanced_delegation.rb +21 -0
  90. data/lib/archive_index_behavior_tags_and_methods.rb +48 -0
  91. data/lib/generators/behavior/USAGE +16 -0
  92. data/lib/generators/behavior/behavior_generator.rb +22 -0
  93. data/lib/generators/behavior/templates/model.rb.template +9 -0
  94. data/lib/generators/behavior/templates/unit_test.rb.template +16 -0
  95. data/lib/generators/filter/USAGE +16 -0
  96. data/lib/generators/filter/filter_generator.rb +22 -0
  97. data/lib/generators/filter/templates/model.rb.template +8 -0
  98. data/lib/generators/filter/templates/unit_test.rb.template +7 -0
  99. data/lib/inheritable_class_attributes.rb +65 -0
  100. data/lib/login_system.rb +80 -0
  101. data/lib/plugins/index_quoting_fix/init.rb +32 -0
  102. data/lib/plugins/string_io/init.rb +2 -0
  103. data/lib/radiant.rb +5 -0
  104. data/lib/registerable.rb +70 -0
  105. data/lib/tasks/release.rake +84 -0
  106. data/public/404.html +8 -0
  107. data/public/500.html +8 -0
  108. data/public/dispatch.cgi +10 -0
  109. data/public/dispatch.fcgi +24 -0
  110. data/public/dispatch.rb +10 -0
  111. data/public/favicon.ico +0 -0
  112. data/public/images/add-child.png +0 -0
  113. data/public/images/brown-bottom-line.gif +0 -0
  114. data/public/images/clear-page-cache.png +0 -0
  115. data/public/images/collapse.png +0 -0
  116. data/public/images/expand.png +0 -0
  117. data/public/images/minus.png +0 -0
  118. data/public/images/new-homepage.png +0 -0
  119. data/public/images/new-layout.png +0 -0
  120. data/public/images/new-snippet.png +0 -0
  121. data/public/images/new-user.png +0 -0
  122. data/public/images/page.png +0 -0
  123. data/public/images/plus.png +0 -0
  124. data/public/images/remove-disabled.png +0 -0
  125. data/public/images/remove.png +0 -0
  126. data/public/images/snippet.png +0 -0
  127. data/public/images/spinner.gif +0 -0
  128. data/public/images/view-site.gif +0 -0
  129. data/public/images/virtual-page.png +0 -0
  130. data/public/javascripts/application.js +2 -0
  131. data/public/javascripts/controls.js +815 -0
  132. data/public/javascripts/dragdrop.js +913 -0
  133. data/public/javascripts/effects.js +958 -0
  134. data/public/javascripts/pngfix.js +78 -0
  135. data/public/javascripts/prototype.js +2006 -0
  136. data/public/javascripts/ruledtable.js +28 -0
  137. data/public/javascripts/string.js +23 -0
  138. data/public/javascripts/tabcontrol.js +140 -0
  139. data/public/robots.txt +1 -0
  140. data/public/stylesheets/admin.css +464 -0
  141. data/script/about +3 -0
  142. data/script/breakpointer +3 -0
  143. data/script/console +3 -0
  144. data/script/destroy +3 -0
  145. data/script/generate +3 -0
  146. data/script/performance/benchmarker +3 -0
  147. data/script/performance/profiler +3 -0
  148. data/script/plugin +3 -0
  149. data/script/process/reaper +3 -0
  150. data/script/process/spawner +3 -0
  151. data/script/process/spinner +3 -0
  152. data/script/runner +3 -0
  153. data/script/server +3 -0
  154. data/script/setup_database +297 -0
  155. data/test/fixtures/layouts.yml +26 -0
  156. data/test/fixtures/page_parts.yml +99 -0
  157. data/test/fixtures/pages.yml +359 -0
  158. data/test/fixtures/pages.yml.rej +28 -0
  159. data/test/fixtures/snippets.yml +18 -0
  160. data/test/fixtures/users.yml +30 -0
  161. data/test/functional/admin/export_controller_test.rb +22 -0
  162. data/test/functional/admin/layout_controller_test.rb +40 -0
  163. data/test/functional/admin/model_controller_test.rb +152 -0
  164. data/test/functional/admin/page_controller_test.rb +179 -0
  165. data/test/functional/admin/snippet_controller_test.rb +11 -0
  166. data/test/functional/admin/user_controller_test.rb +71 -0
  167. data/test/functional/admin/welcome_controller_test.rb +48 -0
  168. data/test/functional/application_controller_test.rb +44 -0
  169. data/test/functional/login_system_test.rb +155 -0
  170. data/test/functional/site_controller_test.rb +172 -0
  171. data/test/helpers/archive_index_test_helper.rb +35 -0
  172. data/test/helpers/behavior_render_test_helper.rb +34 -0
  173. data/test/helpers/behavior_test_helper.rb +47 -0
  174. data/test/helpers/caching_test_helper.rb +41 -0
  175. data/test/helpers/layout_test_helper.rb +35 -0
  176. data/test/helpers/page_part_test_helper.rb +49 -0
  177. data/test/helpers/page_test_helper.rb +36 -0
  178. data/test/helpers/snippet_test_helper.rb +32 -0
  179. data/test/helpers/user_test_helper.rb +34 -0
  180. data/test/helpers/validation_test_helper.rb +42 -0
  181. data/test/test_helper.rb +54 -0
  182. data/test/unit/behavior_test.rb +196 -0
  183. data/test/unit/behaviors/archive_behavior_test.rb +33 -0
  184. data/test/unit/behaviors/archive_day_index_behavior_test.rb +21 -0
  185. data/test/unit/behaviors/archive_month_index_behavior_test.rb +21 -0
  186. data/test/unit/behaviors/archive_year_index_behavior_test.rb +21 -0
  187. data/test/unit/behaviors/page_missing_behavior_test.rb +27 -0
  188. data/test/unit/filters/markdown_filter_test.rb +14 -0
  189. data/test/unit/filters/textile_filter_test.rb +14 -0
  190. data/test/unit/inheritable_class_attributes_test.rb +47 -0
  191. data/test/unit/layout_test.rb +29 -0
  192. data/test/unit/page_context_test.rb +375 -0
  193. data/test/unit/page_context_test.rb.rej +26 -0
  194. data/test/unit/page_part_test.rb +44 -0
  195. data/test/unit/page_test.rb +224 -0
  196. data/test/unit/radiant/config_test.rb +46 -0
  197. data/test/unit/radiant/exporter_test.rb +26 -0
  198. data/test/unit/registerable_test.rb +68 -0
  199. data/test/unit/response_cache_test.rb +133 -0
  200. data/test/unit/snippet_test.rb +47 -0
  201. data/test/unit/status_test.rb +43 -0
  202. data/test/unit/text_filter_test.rb +14 -0
  203. data/test/unit/user_action_observer_test.rb +40 -0
  204. data/test/unit/user_test.rb +138 -0
  205. metadata +355 -0
@@ -0,0 +1 @@
1
+ = Change Log
@@ -0,0 +1,12 @@
1
+ = Contributors
2
+
3
+ The following people have submitted changes which have been applied to the core:
4
+
5
+ * Matt McCray
6
+ * Paul Smith
7
+ * Scott Walter
8
+ * Sven Lauer
9
+ * Tristan Boniver
10
+ * Chris Corriveau
11
+ * Ryan Platte
12
+ * Luis Lavena
data/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ == MIT License
2
+
3
+ Copyright (c) 2006, John W. Long
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,91 @@
1
+ == Welcome to Radiant
2
+
3
+ Radiant is a no-fluff, open source content management system designed
4
+ for small teams. It is similar to Textpattern or MovableType, but is
5
+ a general purpose content management system (not just a blogging
6
+ engine).
7
+
8
+ Radiant features:
9
+
10
+ * An elegant user interface
11
+ * The ability to arrange pages in a hierarchy
12
+ * Flexible templating with layouts, snippets, page parts, and a
13
+ custom tagging language (Radius: http://radius.rubyforge.org)
14
+ * Special page-oriented plugins called behaviors
15
+ * A simple user management/permissions system
16
+ * Support for Markdown and Textile as well as traditional HTML
17
+ (it's easy to create other filters)
18
+ * Operates in two modes: dev and production depending on the URL
19
+ * A caching system which expires pages every 5 minutes
20
+ * Built using Ruby on Rails (which means that extending Radiant is
21
+ as easy as any other Rails application)
22
+ * And much more...
23
+
24
+
25
+ == License
26
+
27
+ Radiant is released under the MIT license and is copyright (c) 2006
28
+ John W. Long. A copy of the MIT license can be found in the LICENSE
29
+ file.
30
+
31
+
32
+ == Installation and Setup
33
+
34
+ Radiant is a traditional Ruby on Rails application, meaning that you
35
+ can configure and run it the way you would a normal Rails
36
+ application.
37
+
38
+ Once you have extracted the files into the directory where you would
39
+ like to install Radiant:
40
+
41
+ 1. Create a MySQL/PostgreSQL/SQLite database for your Web site.
42
+
43
+ 2. Create config/database.yml for your database setup. (There are
44
+ several examples in the config directory.)
45
+
46
+ 3. Run the database setup script:
47
+
48
+ % script/setup_database production
49
+
50
+ 4. Start it like a normal Rails application. To test execute:
51
+
52
+ % script/server production
53
+
54
+ And open your Web browser on port 3000 (http://localhost:3000).
55
+
56
+ When using Radiant on a production system you may also need to set
57
+ permissions on the public and cache directories so that your Web server
58
+ can access those directories with the user that it runs under.
59
+
60
+ Once you've installed Radiant on your own Web site, be sure to add your
61
+ name and Web site to the list of radiant users:
62
+
63
+ http://dev.radiantcms.org/radiant/wiki/RadiantUsers
64
+
65
+
66
+ == Radiant Admin Interface
67
+
68
+ The administrative interface is available at /admin/. By default the
69
+ `setup_database` script creates a user called "admin" with a password of
70
+ "radiant".
71
+
72
+
73
+ == Support
74
+
75
+ The best place to get support is on the mailing list:
76
+
77
+ http://radiantcms.org/mailing-list/
78
+
79
+ There is also a Trac powered dev site available here:
80
+
81
+ http://dev.radiantcms.org/
82
+
83
+ Please Note: Before filing a ticket on the dev site discuss your question
84
+ or problem on the mailing list. This makes it much easier to manage
85
+ legitimate tickets.
86
+
87
+
88
+ Enjoy!
89
+
90
+ --
91
+ John Long :: http://wiseheartdesign.com
@@ -0,0 +1,10 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/switchtower.rake, and they will automatically be available to Rake.
3
+
4
+ require(File.join(File.dirname(__FILE__), 'config', 'boot'))
5
+
6
+ require 'rake'
7
+ require 'rake/testtask'
8
+ require 'rake/rdoctask'
9
+
10
+ require 'tasks/rails'
@@ -0,0 +1,42 @@
1
+ require_dependency 'archive_finder'
2
+
3
+ class ArchiveBehavior < Behavior::Base
4
+
5
+ register "Archive"
6
+
7
+ description %{
8
+ The Archive behavior causes a page and its children to behave in a
9
+ fashion similar to a blog archive or a news archive.
10
+
11
+ Child URLs are altered to be in %Y/%m/%d format (2004/05/06).
12
+
13
+ If you would like to have custom index pages for the year, month,
14
+ or day, use the "Archive Year Index", "Archive Month Index", and
15
+ "Archive Day Index" behaviors respectively for three separate child
16
+ pages.
17
+ }
18
+
19
+ def child_url(child)
20
+ date = child.published_at || Time.now
21
+ clean_url "#{ page_url }/#{ date.strftime '%Y/%m/%d' }/#{ child.slug }"
22
+ end
23
+
24
+ def find_page_by_url(url, live = true, clean = false)
25
+ url = clean_url(url) if clean
26
+ if url =~ %r{^#{ page.url }(\d{4})(?:/(\d{2})(?:/(\d{2}))?)?/?$}
27
+ year, month, day = $1, $2, $3
28
+ @page.children.find_by_behavior_id(
29
+ case
30
+ when day
31
+ 'Archive Day Index'
32
+ when month
33
+ 'Archive Month Index'
34
+ else
35
+ 'Archive Year Index'
36
+ end
37
+ )
38
+ else
39
+ super
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,30 @@
1
+ require_dependency 'archive_finder'
2
+ require_dependency 'archive_index_behavior_tags_and_methods'
3
+
4
+ class ArchiveDayIndexBehavior < Behavior::Base
5
+
6
+ register "Archive Day Index"
7
+
8
+ description %{
9
+ To create a day index for an archive, create a child page for the
10
+ archive and apply the "Archive Day Index" behavior to it.
11
+
12
+ The following tags are then made accessible to you:
13
+
14
+ <r:archive:children>...</r:archive:children>
15
+ Grants access to a subset of the children of the archive page
16
+ that match the specific day which the index page is rendering.
17
+ }
18
+
19
+ include ArchiveIndexBehaviorTagsAndMethods
20
+
21
+ define_tags do
22
+ url = request.request_uri unless request.nil?
23
+ tag "archive:children" do |tag|
24
+ year, month, day = $1, $2, $3 if url =~ %r{/(\d{4})/(\d{2})/(\d{2})/?$}
25
+ tag.locals.children = ArchiveFinder.day_finder(page.parent.children, year, month, day)
26
+ tag.expand
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,30 @@
1
+ require_dependency 'archive_finder'
2
+ require_dependency 'archive_index_behavior_tags_and_methods'
3
+
4
+ class ArchiveMonthIndexBehavior < Behavior::Base
5
+
6
+ register "Archive Month Index"
7
+
8
+ description %{
9
+ To create a month index for an archive, create a child page for the
10
+ archive and apply the "Archive Month Index" behavior to it.
11
+
12
+ The following tags are then made accessible to you:
13
+
14
+ <r:archive:children>...</r:archive:children>
15
+ Grants access to a subset of the children of the archive page
16
+ that match the specific month which the index page is rendering.
17
+ }
18
+
19
+ include ArchiveIndexBehaviorTagsAndMethods
20
+
21
+ define_tags do
22
+ url = request.request_uri unless request.nil?
23
+ tag "archive:children" do |tag|
24
+ year, month = $1, $2 if url =~ %r{/(\d{4})/(\d{2})/?$}
25
+ tag.locals.children = ArchiveFinder.month_finder(page.parent.children, year, month)
26
+ tag.expand
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,30 @@
1
+ require_dependency 'archive_finder'
2
+ require_dependency 'archive_index_behavior_tags_and_methods'
3
+
4
+ class ArchiveYearIndexBehavior < Behavior::Base
5
+
6
+ register "Archive Year Index"
7
+
8
+ description %{
9
+ To create a year index for an archive, create a child page for the
10
+ archive and apply the "Archive Year Index" behavior to it.
11
+
12
+ The following tags are then made accessible to you:
13
+
14
+ <r:archive:children>...</r:archive:children>
15
+ Grants access to a subset of the children of the archive page
16
+ that match the specific year which the index page is rendering.
17
+ }
18
+
19
+ include ArchiveIndexBehaviorTagsAndMethods
20
+
21
+ define_tags do
22
+ url = request.request_uri unless request.nil?
23
+ tag "archive:children" do |tag|
24
+ year = $1 if url =~ %r{/(\d{4})/?$}
25
+ tag.locals.children = ArchiveFinder.year_finder(page.parent.children, year)
26
+ tag.expand
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,18 @@
1
+ class EnvDumpBehavior < Behavior::Base
2
+
3
+ register "Env Dump"
4
+
5
+ description %{
6
+ Instead of rendering a page in the normal fashion the Env Dump
7
+ behavior will output all of the environment variables on the
8
+ request. This is occasionally useful for debugging.
9
+ }
10
+
11
+ def render_page
12
+ %{<html><body><pre>#{ @request.env.collect { |k,v| "#{k} => #{v}\n" } }</pre></body></html>}
13
+ end
14
+
15
+ def cache_page?
16
+ false
17
+ end
18
+ end
@@ -0,0 +1,31 @@
1
+ class PageMissingBehavior < Behavior::Base
2
+
3
+ register 'Page Missing'
4
+
5
+ description %{
6
+ The Page Missing behavior is used to create a "File Not Found" error
7
+ page in the event that a page is not found among a page's children.
8
+
9
+ To create a "File Not Found" error page for an entire Web site, create
10
+ a page that is a child of the root page and assign it the Missing Page
11
+ behavior.
12
+ }
13
+
14
+ define_tags do
15
+ url = request.request_uri if request
16
+ tag("attempted_url") { url }
17
+ end
18
+
19
+ def page_virtual?
20
+ true
21
+ end
22
+
23
+ def page_headers
24
+ { 'Status' => '404 Not Found' }
25
+ end
26
+
27
+ def cache_page?
28
+ false
29
+ end
30
+
31
+ end
@@ -0,0 +1,8 @@
1
+ class Admin::ExportController < ApplicationController
2
+ model :exporter, :user, :page, :page_part, :snippet, :layout
3
+
4
+ def yaml
5
+ @response.headers['Content-Type'] = "text/yaml"
6
+ render_text Radiant::Exporter.export
7
+ end
8
+ end
@@ -0,0 +1,23 @@
1
+ require_dependency 'admin/model_controller'
2
+
3
+ class Admin::LayoutController < Admin::AbstractModelController
4
+ model :layout
5
+
6
+ attr_accessor :cache
7
+
8
+ only_allow_access_to :index, :new, :edit, :remove,
9
+ :when => [:developer, :admin],
10
+ :denied_url => { :controller => 'page', :action => 'index' },
11
+ :denied_message => 'You must have developer privileges to perform this action.'
12
+
13
+ def initialize
14
+ super
15
+ @cache = ResponseCache.instance
16
+ end
17
+
18
+ def save
19
+ saved = super
20
+ model.pages.each { |page| @cache.expire_response(page.url) } if saved
21
+ saved
22
+ end
23
+ end
@@ -0,0 +1,119 @@
1
+ class Admin::AbstractModelController < ApplicationController
2
+ attr_accessor :cache
3
+
4
+ def self.model(*symbols)
5
+ first = symbols.first
6
+ class_eval %{ @@model_class = "#{first}".singularize.camelize.constantize }
7
+ super
8
+ end
9
+
10
+ def initialize
11
+ super
12
+ @cache = ResponseCache.instance
13
+ end
14
+
15
+ def index
16
+ self.models = model_class.find(:all)
17
+ end
18
+
19
+ def new
20
+ self.model = model_class.new
21
+ handle_new_or_edit_post
22
+ end
23
+
24
+ def edit
25
+ self.model = model_class.find_by_id(params[:id])
26
+ render :template => "admin/#{ model_symbol }/new" if handle_new_or_edit_post
27
+ end
28
+
29
+ def remove
30
+ self.model = model_class.find(params[:id])
31
+ if request.post?
32
+ model.destroy
33
+ announce_removed
34
+ redirect_to model_index_url
35
+ end
36
+ end
37
+
38
+ protected
39
+
40
+ def model_class
41
+ self.class.class_eval %{ @@model_class }
42
+ end
43
+
44
+ def model
45
+ instance_variable_get("@#{model_symbol}")
46
+ end
47
+ def models
48
+ instance_variable_get("@#{plural_model_symbol}")
49
+ end
50
+
51
+ def model=(object)
52
+ instance_variable_set("@#{model_symbol}", object)
53
+ end
54
+ def models=(objects)
55
+ instance_variable_set("@#{plural_model_symbol}", objects)
56
+ end
57
+
58
+ def model_name
59
+ model_class.name
60
+ end
61
+ def plural_model_name
62
+ model_name.pluralize
63
+ end
64
+
65
+ def model_symbol
66
+ model_name.underscore.intern
67
+ end
68
+ def plural_model_symbol
69
+ model_name.pluralize.underscore.intern
70
+ end
71
+
72
+ def humanized_model_name
73
+ model_name.underscore.humanize
74
+ end
75
+
76
+ def model_index_url(params = {})
77
+ send("#{ model_symbol }_index_url", params)
78
+ end
79
+
80
+ def model_edit_url(params = {})
81
+ send("#{ model_symbol }_edit_url", params)
82
+ end
83
+
84
+ def continue_url(options)
85
+ options[:redirect_to] || (params[:continue] ? model_edit_url(:id => model.id) : model_index_url)
86
+ end
87
+
88
+ def save
89
+ model.save
90
+ end
91
+
92
+ def announce_saved(message = nil)
93
+ flash[:notice] = message || "#{humanized_model_name} saved below."
94
+ end
95
+
96
+ def announce_validation_errors
97
+ flash[:error] = "Validation errors occurred while processing this form. Please take a moment to review the form and correct any input errors before continuing."
98
+ end
99
+
100
+ def announce_removed
101
+ flash[:notice] = "#{humanized_model_name} has been deleted."
102
+ end
103
+
104
+ def handle_new_or_edit_post(options = {})
105
+ options.symbolize_keys
106
+ if request.post?
107
+ model.attributes = params[model_symbol]
108
+ if save
109
+ cache.clear
110
+ announce_saved(options[:saved_message])
111
+ redirect_to continue_url(options)
112
+ return false
113
+ else
114
+ announce_validation_errors
115
+ end
116
+ end
117
+ true
118
+ end
119
+ end