bcms_blog 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/LICENSE.txt +165 -0
  2. data/README.markdown +113 -2
  3. data/app/controllers/application_controller.rb +10 -0
  4. data/app/controllers/cms/blog_comments_controller.rb +1 -1
  5. data/app/controllers/cms/blog_posts_controller.rb +34 -1
  6. data/app/controllers/cms/blogs_controller.rb +10 -1
  7. data/app/controllers/feeds_controller.rb +8 -0
  8. data/app/helpers/application_helper.rb +3 -0
  9. data/app/helpers/cms/blog_helper.rb +17 -0
  10. data/app/helpers/feeds_helper.rb +2 -0
  11. data/app/models/blog.rb +70 -14
  12. data/app/models/blog_comment.rb +15 -9
  13. data/app/models/blog_group_membership.rb +4 -0
  14. data/app/models/blog_observer.rb +135 -0
  15. data/app/models/blog_post.rb +76 -32
  16. data/app/portlets/blog_post_portlet.rb +23 -9
  17. data/app/portlets/blog_posts_portlet.rb +50 -0
  18. data/app/views/cms/blog_posts/_form.html.erb +9 -4
  19. data/app/views/cms/blog_posts/no_access.html.erb +9 -0
  20. data/app/views/cms/blog_posts/render.html.erb +1 -5
  21. data/app/views/cms/blogs/_form.html.erb +27 -1
  22. data/app/views/cms/blogs/admin_only.html.erb +9 -0
  23. data/app/views/cms/blogs/render.html.erb +2 -3
  24. data/app/views/feeds/index.rss.builder +18 -0
  25. data/app/views/partials/_blog_post.html.erb +102 -0
  26. data/app/views/partials/_blog_post.html.haml +91 -0
  27. data/app/views/portlets/blog_post/_form.html.erb +2 -1
  28. data/app/views/portlets/blog_post/render.html.erb +33 -36
  29. data/app/views/portlets/blog_posts/_form.html.erb +13 -0
  30. data/app/views/portlets/blog_posts/render.html.haml +9 -0
  31. data/db/migrate/20090415000000_create_blogs.rb +31 -18
  32. data/db/migrate/20090415000001_create_blog_posts.rb +5 -30
  33. data/db/migrate/20090415000002_create_blog_comments.rb +2 -1
  34. data/db/migrate/20090415000003_add_attachment_to_blog_posts.rb +23 -0
  35. data/db/migrate/20100521042244_add_moderate_comments_to_blog.rb +10 -0
  36. data/doc/README_FOR_APP +2 -0
  37. data/doc/migrate_to_20100427.rb +77 -0
  38. data/doc/release_notes.txt +40 -0
  39. data/lib/bcms_blog/routes.rb +3 -0
  40. data/rails/init.rb +6 -1
  41. data/test/factories.rb +47 -1
  42. data/test/functional/blog_post_test.rb +19 -17
  43. data/test/functional/blog_test.rb +47 -25
  44. data/test/functional/cms/blog_posts_controller_test.rb +44 -0
  45. data/test/functional/cms/blogs_controller_test.rb +25 -0
  46. data/test/functional/feeds_controller_test.rb +8 -0
  47. data/test/test_helper.rb +64 -120
  48. data/test/unit/blog_comment_test.rb +34 -0
  49. data/test/unit/blog_observer_test.rb +61 -0
  50. data/test/unit/blog_post_test.rb +43 -0
  51. data/test/unit/blog_test.rb +42 -0
  52. data/test/unit/helpers/feeds_helper_test.rb +4 -0
  53. metadata +64 -7
  54. data/app/views/portlets/blog_post/_blog_post.html.erb +0 -29
data/LICENSE.txt ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/README.markdown CHANGED
@@ -1,3 +1,114 @@
1
- # Blog module for BrowserCMS
1
+ # Blog Module for BrowserCMS
2
+
3
+ A simple blog module that lets users create multiple blogs.
4
+
5
+ ## Features
6
+
7
+ * Multiple Blogs - Contributors can create multiple blogs, each of which can have their own sections and pages.
8
+ * Convenient setup - Creating a new blog will automatically create the pages and portlets needed to get started.
9
+ * Tagging - Leverages BrowserCMS' built-in categorization and tagging and authorization framework.
10
+ * Comments - Visitors can leave comments are that can be managed, versioned and moderated. Comments can optionally be moderated.
11
+ * Flexibility - Blogs can be added to any page like any content block.
12
+ * Blog Security - When you create or edit a Blog, you can choose which groups of users can add or edit posts or comments for that blog.
13
+
14
+ ## Installation
15
+
16
+ The blog module installs like most other BrowserCMS modules (http://guides.browsercms.org/installing_modules.html)
17
+
18
+ gem install bcms_blog
19
+
20
+ ## Set up your application to use the module
21
+
22
+ ### 1. Edit config/environment.rb
23
+
24
+ config.gem "browsercms"
25
+ config.gem "bcms_blog"
26
+
27
+ ### 2. Run the following commands
28
+
29
+ script/generate browser_cms
30
+ rake db:migrate
31
+
32
+ ### 3. Edit config/routes.rb
33
+
34
+ Make sure the routes.rb loads the blog routes.
35
+
36
+ map.routes_for_bcms_blog
37
+ map.routes_for_browser_cms
38
+
39
+ ## Creating a Blog
40
+
41
+ * To get started, go to the Content Library and choose the Blog module in the left hand menu.
42
+ * Then choose the 'Blog' content type and click Add new content.
43
+ * When you create a Blog content block from the content library, all the structure needed for the blog to run is created for you. This includes a new section, 2 pages, a few page routes and a portlet.
44
+ * Go to the sitemap, and choose the first page in the newly created section (which should match the name of the blog). You will need to publish this page to make it live.
45
+ * To add new Posts, you can go to the Content Library, choose 'Blog -> Blog Post' and Add New Content. Each 'Blog Post' is tied to a particular block.
46
+ * You may also want to add Categories via the Categorization Module.
47
+
48
+ ## Comments
49
+
50
+ If a Blog is configured for 'Moderate Comments', then when visitors submit them they will be automatically placed in a moderation queue for approval. In the Content Library,
51
+ under 'Blog Comments', these comments will appear in draft status. They can be Published like any other content. (There is current no automatic notification for new comments).
52
+
53
+ If the blog is set to no moderation, then comments will appear immediately. This obviously increases the likelyhood of spam, but staff can use the Content Library to delete the offending comments like any other content.
54
+
55
+ ## Feeds
56
+
57
+ This module includes an RSS feeds route that can handle multiple blogs. To expose the RSS autodiscovery link, you can call the feeds\_link\_tag\_for helper in your template header:
58
+
59
+ <%= feeds_link_tag_for "MyBlog" %>
60
+
61
+ Where "MyBlog" is the __name__ of the blog.
62
+
63
+ If your site has multiple blogs, you need to call the helper once for every link you intend to expose:
64
+
65
+ <%= feeds_link_tag_for "MyBlog" %>
66
+ <%= feeds_link_tag_for "MyOtherBlog" %>
67
+
68
+
69
+ ## Security
70
+
71
+ This module adds some additional level of security around blogs and content that are slightly different from a vanilla BrowserCMS installation. Here's the highlights:
72
+
73
+ * Blogs - Only CMS Administrators may create or edit blogs themselves. Since creating a new blog has a rippling affect across the site, this keeps that from being inadvertently invoked.
74
+ * Blog Posts - Editors will only be able to see those posts in blogs they are allowed to edit. They will not be able to create posts in blogs they don't have access to.
75
+ * Comments - All Editors may edit/deleted comments. Visitors may add new comments.
76
+
77
+ ## Customization
78
+
79
+ The module provides a template that is a good starting point for your blog's layout. If you want
80
+ to further customize the look and feel, just copy the file app/views/partials/\_blog_post.html.erb
81
+ on this repository to app/views/partials on your application and modify it to suit your needs.
82
+
83
+ Keep in mind that both the posts list and individual post pages are handled by the same partial.
84
+
85
+ ## Routes
86
+
87
+ When each blog is created, there is an additional set of routes that are also created, based on the 'path' of the blog. The initial blog path is based on the name of the blog.
88
+
89
+ /:blog_path/:year/:month/:day/:slug
90
+ /:blog_path/:year/:month/:day
91
+ /:blog_path/:year/:month
92
+ /:blog_path/:year
93
+ /:blog_path/tag/:tag
94
+ /:blog_path/category/:category
95
+
96
+ ## Architecture
97
+
98
+ This module will add the following new items to your project.
99
+
100
+ * New content types for Blogs, Blog Comments and Blog Posts.
101
+ * New portlets for display in a Single Blog post and for showing many blog posts.
102
+
103
+ ## Contributors
104
+
105
+ Special thanks to some amazing folks from the BrowserCMS community for their work in building the essential features for this module. Here are the MVPs that made this possible:
106
+
107
+ * Tyler Rick (http://github.com/TylerRick)
108
+ * Jon Leighton (http://github.com/jonleighton)
109
+
110
+ ## Wishlist Features
111
+
112
+ * Notifications for Comments - Blogs should have an option to be notified when a new comment is created. This will allow for practical management of comments/spam.
113
+ * Messaging for Moderated comments - If moderation is turned on, users get no feedback about the comment they just left. Ideally, they would get some sort of javascript notice that their comment is awaiting notification.
2
114
 
3
- A simple blog module, consisting of the ability to create one or more blogs, posts and comments.
@@ -0,0 +1,10 @@
1
+ # Filters added to this controller apply to all controllers in the application.
2
+ # Likewise, all the methods added will be available for all controllers.
3
+
4
+ class ApplicationController < ActionController::Base
5
+ helper :all # include all helpers, all the time
6
+ protect_from_forgery # See ActionController::RequestForgeryProtection for details
7
+
8
+ # Scrub sensitive parameters from your log
9
+ # filter_parameter_logging :password
10
+ end
@@ -1,3 +1,3 @@
1
1
  class Cms::BlogCommentsController < Cms::ContentBlockController
2
-
2
+
3
3
  end
@@ -1,2 +1,35 @@
1
1
  class Cms::BlogPostsController < Cms::ContentBlockController
2
- end
2
+ before_filter :show_no_access_if_none_editable
3
+
4
+ def build_block
5
+ super
6
+ ensure_blog_editable
7
+ @block.author = current_user
8
+ end
9
+
10
+ def load_block
11
+ super
12
+ ensure_blog_editable
13
+ end
14
+
15
+ def load_blocks
16
+ super
17
+ @blocks.delete_if { |b| !b.editable_by?(current_user) }
18
+ end
19
+
20
+ private
21
+
22
+ # If the current user is not able to edit any blog, just show them a page saying so
23
+ def show_no_access_if_none_editable
24
+ if Blog.editable_by(current_user).empty?
25
+ render :action => "no_access"
26
+ end
27
+ end
28
+
29
+ # Ensure the current user can actually edit the blog this blog post is associated with
30
+ def ensure_blog_editable
31
+ if @block.blog
32
+ raise Cms::Errors::AccessDenied unless @block.blog.editable_by?(current_user)
33
+ end
34
+ end
35
+ end
@@ -1,2 +1,11 @@
1
1
  class Cms::BlogsController < Cms::ContentBlockController
2
- end
2
+ check_permissions :administrate, :except => :index
3
+
4
+ def index
5
+ if current_user.able_to?(:administrate)
6
+ super
7
+ else
8
+ render :action => "admin_only"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ class FeedsController < ApplicationController
2
+
3
+ def index
4
+ @blog = Blog.find(params[:blog_id])
5
+ @blog_posts = @blog.posts.published.all(:limit => 10, :order => "published_at DESC")
6
+ end
7
+
8
+ end
@@ -0,0 +1,3 @@
1
+ # Methods added to this helper will be available to all templates in the application.
2
+ module ApplicationHelper
3
+ end
@@ -0,0 +1,17 @@
1
+ module Cms::BlogHelper
2
+ # We can't call it 'blog_path' because that would conflict with the actual named route method if there's a blog named "Blog".
3
+ def _blog_path(blog, route_name, route_params)
4
+ send("#{blog.name_for_path}_#{route_name}_path", route_params)
5
+ end
6
+
7
+ def _blog_post_path(blog_post)
8
+ send("#{blog_post.route_name}_path", blog_post.route_params)
9
+ end
10
+
11
+ def feeds_link_tag_for(name)
12
+ blog = Blog.find_by_name(name)
13
+ auto_discovery_link_tag(:rss, blog_feeds_url(:blog_id => blog), :title => "#{blog.name}")
14
+ end
15
+
16
+ end
17
+
@@ -0,0 +1,2 @@
1
+ module FeedsHelper
2
+ end
data/app/models/blog.rb CHANGED
@@ -1,38 +1,94 @@
1
1
  class Blog < ActiveRecord::Base
2
2
  acts_as_content_block
3
- has_many :posts, :class_name => "BlogPost"
4
3
 
4
+ has_many :posts, :class_name => "BlogPost", :conditions => { :published => true }, :order => "published_at desc"
5
+ has_many :blog_group_memberships
6
+ has_many :groups, :through => :blog_group_memberships
7
+
5
8
  validates_presence_of :name
6
9
  validates_uniqueness_of :name
7
-
10
+
11
+ named_scope :editable_by, lambda { |user|
12
+ if user.able_to?(:administrate)
13
+ { }
14
+ else
15
+ { :include => :groups, :conditions => ["groups.id IN (?)", user.group_ids.join(",")] }
16
+ end
17
+ }
18
+
8
19
  def self.default_template
9
- template_file = ActionController::Base.view_paths.map do |vp|
20
+ template_file = ActionController::Base.view_paths.map do |vp|
10
21
  path = vp.to_s.first == "/" ? vp.to_s : File.join(Rails.root, vp.to_s)
11
22
  File.join(path, "cms/blogs/render.html.erb")
12
23
  end.detect{|f| File.exists? f }
13
24
  template_file ? open(template_file){|f| f.read } : ""
14
25
  end
15
-
26
+
27
+ def self.posts_finder(finder, options)
28
+ if options[:tags]
29
+ finder = finder.tagged_with(options[:tags])
30
+ end
31
+ if options[:exclude_tags]
32
+ finder = finder.not_tagged_with(options[:exclude_tags])
33
+ end
34
+ if options[:category] || options[:category_id]
35
+ category_type = CategoryType.named("Blog Post").first
36
+ category = category_type.categories.named(options[:category]).first if options[:category]
37
+ category = category_type.categories. find(options[:category_id]) if options[:category_id]
38
+ finder = finder.in_category(category)
39
+ end
40
+ finder
41
+ end
42
+
16
43
  def render
17
44
  @blog = self
18
45
  finder = @blog.posts.published
19
- if params[:tag]
20
- finder = finder.tagged_with(params[:tag])
46
+ finder = Blog.posts_finder(finder, params)
47
+
48
+ if params[:year] && params[:month] && params[:day]
49
+ @date = Date.new(params[:year].to_i, params[:month].to_i, params[:day].to_i)
50
+ finder = posts.published_between(@date, @date + 1.day)
51
+ elsif params[:year] && params[:month]
52
+ @date = Date.new(params[:year].to_i, params[:month].to_i)
53
+ finder = posts.published_between(@date, @date + 1.month)
54
+ elsif params[:year]
55
+ @date = Date.new(params[:year].to_i)
56
+ finder = posts.published_between(@date, @date + 1.year)
21
57
  end
58
+
59
+ @blog_posts = finder.all(:limit => 25, :order => "published_at desc")
60
+ raise ActiveRecord::RecordNotFound.new("No posts found") if @blog_posts.empty?
61
+
22
62
  if params[:category]
23
- @category_type = CategoryType.named("Blog Post").first
24
- @category = @category_type.categories.named(params[:category]).first
25
- finder = finder.in_category(@category)
63
+ @page_title = "#{params[:category]}"
64
+ elsif params[:tag]
65
+ @page_title = "Posts tagged with #{params[:tag]}"
66
+ elsif params[:year] && params[:month] && params[:day]
67
+ @page_title = "Posts from #{@date.to_s(:long)}"
68
+ elsif params[:year] && params[:month]
69
+ @page_title = "Posts from #{Date::MONTHNAMES[@date.month]} #{@date.year}"
70
+ elsif params[:year]
71
+ @page_title = "Posts from #{@date.year}"
26
72
  end
27
- @blog_posts = finder.all(:limit => 15, :order => "published_at desc")
28
73
  end
29
-
74
+
30
75
  def inline_options
31
76
  {:inline => self.template}
32
77
  end
33
-
78
+
34
79
  def self.default_order
35
80
  "name"
36
81
  end
37
-
38
- end
82
+
83
+ def editable_by?(user)
84
+ user.able_to?(:administrate) || !(group_ids & user.group_ids).empty?
85
+ end
86
+
87
+ def potential_authors
88
+ groups.map(&:users).flatten.uniq
89
+ end
90
+
91
+ def name_for_path
92
+ name.to_slug.gsub('-', '_')
93
+ end
94
+ end
@@ -1,26 +1,32 @@
1
1
  class BlogComment < ActiveRecord::Base
2
2
  acts_as_content_block :is_searachable => "body"
3
3
  belongs_to :post, :class_name => "BlogPost", :counter_cache => "comments_count"
4
+
5
+ validates_presence_of :post_id, :author, :body
4
6
 
7
+ def before_create
8
+ self.published = true unless post.blog.moderate_comments?
9
+ end
10
+
5
11
  def self.default_order
6
12
  "blog_comments.created_at desc"
7
13
  end
8
-
14
+
9
15
  def self.default_order_for_search
10
16
  default_order
11
- end
12
-
17
+ end
18
+
13
19
  def self.columns_for_index
14
- [ {:label => "Comment", :method => :name, :order => "blog_comments.body" },
20
+ [ {:label => "Comment", :method => :name, :order => "blog_comments.body" },
15
21
  {:label => "Created At", :method => :formatted_created_at, :order => "blog_comments.created_at"} ]
16
- end
17
-
22
+ end
23
+
18
24
  def name
19
25
  body ? body[0..50] : ""
20
26
  end
21
-
27
+
22
28
  def formatted_created_at
23
29
  created_at.to_s(:date)
24
30
  end
25
-
26
- end
31
+
32
+ end
@@ -0,0 +1,4 @@
1
+ class BlogGroupMembership < ActiveRecord::Base
2
+ belongs_to :blog
3
+ belongs_to :group
4
+ end
@@ -0,0 +1,135 @@
1
+ class BlogObserver < ActiveRecord::Observer
2
+
3
+ def after_create(blog)
4
+ @blog = blog
5
+ create_section_pages_and_routes
6
+ end
7
+
8
+ def before_update(blog)
9
+ update_section_pages_and_route(blog)
10
+ end
11
+
12
+ def after_update(blog)
13
+ blog.publish
14
+ end
15
+
16
+ private
17
+ # A section, two pages, 6 routes and a portlet are created alongside every blog.
18
+ # This structure provides sensible defaults so users can pretty much start adding
19
+ # posts right after creating a blog without having to worry about where to put
20
+ # their blog and portlets.
21
+ def create_section_pages_and_routes
22
+ create_blog_section
23
+ create_blog_block_page
24
+ create_post_portlet_page
25
+ reload_routes
26
+ end
27
+
28
+ # Every blog is created within a section with the same name.
29
+ # For example, if you create a blog named 'MyBlog', a section 'MyBlog' will be
30
+ # created. This section will hold two pages: one for the blog ContentBlock that
31
+ # will render the list of posts and one for the BlogPost portlet (ie the individual
32
+ # post view)
33
+ def create_blog_section
34
+ @section = Section.find_by_name(@blog.name) || (
35
+ @section = Section.create!(
36
+ :name => @blog.name,
37
+ :path => "/#{@blog.name_for_path}",
38
+ :parent_id => 1
39
+ )
40
+ @section.groups << Group.find_by_code("cms-admin")
41
+ @section.groups << Group.find_by_code("guest")
42
+ @section.groups << Group.find_by_code("content-editor")
43
+ @section.save!
44
+ @section
45
+ )
46
+ end
47
+
48
+ # Following with the above example, the first page that is created is named 'MyBlog' and
49
+ # holds the Blog ContentBlock directly, not a portlet. Together with the 5 created routes,
50
+ # this page and its ContentBlock handle different post listings (all posts, posts in year,
51
+ # month or day and posts by tag or category).
52
+ def create_blog_block_page
53
+ page = Page.find_by_name(@blog.name) || Page.create!(
54
+ :name => @blog.name,
55
+ :path => "/#{@blog.name_for_path}",
56
+ :section => @section,
57
+ :template_file_name => "default.html.erb",
58
+ :hidden => true
59
+ )
60
+ page.create_connector(@blog, 'main')
61
+
62
+ create_route(page, "#{@blog.name}: Posts In Day", "/#{@blog.name_for_path}/:year/:month/:day")
63
+ create_route(page, "#{@blog.name}: Posts In Month", "/#{@blog.name_for_path}/:year/:month")
64
+ create_route(page, "#{@blog.name}: Posts In Year", "/#{@blog.name_for_path}/:year")
65
+ create_route(page, "#{@blog.name}: Posts With Tag", "/#{@blog.name_for_path}/tag/:tag")
66
+ create_route(page, "#{@blog.name}: Posts In Category", "/#{@blog.name_for_path}/category/:category")
67
+ end
68
+
69
+ # The second page that is created holds the BlogPostPortlet and displays the individual
70
+ # post view, along with it's comments.
71
+ def create_post_portlet_page
72
+ page = Page.find_by_name(portlet_name = "#{@blog.name}: Post") || Page.create!(
73
+ :name => portlet_name,
74
+ :path => "/#{@blog.name_for_path}/post",
75
+ :section => @section,
76
+ :template_file_name => "default.html.erb",
77
+ :hidden => true)
78
+ page.publish
79
+ create_route(page, portlet_name, "/#{@blog.name_for_path}/:year/:month/:day/:slug")
80
+ create_portlet(page, portlet_name, BlogPostPortlet)
81
+ end
82
+
83
+ # When the name of a Blog block changes, we need to change the Post page route.
84
+ # We also change the *names* of the section and pages that hold the blog block and
85
+ # post portlet because presumably, by changing the name of the blog, the intention
86
+ # was to reflect this name change in breadcrumbs and menus.
87
+ #
88
+ # Note that no other routes or paths are updated. This is intentional to be consistent
89
+ # with how BrowserCMS behaves when a Section or Page names change: paths are not
90
+ # updated automatically.
91
+ def update_section_pages_and_route(blog)
92
+ if blog.name_changed?
93
+ old_blog_name = blog.name_was
94
+
95
+ Section.find_by_name(old_blog_name).update_attribute(:name, blog.name)
96
+ PageRoute.find_by_name("#{old_blog_name}: Post").update_attribute(:name, "#{blog.name}: Post")
97
+
98
+ page = Page.find_by_name("#{old_blog_name}: Post")
99
+ page.update_attribute(:name, "#{blog.name}: Post")
100
+ page.publish
101
+
102
+ page = Page.find_by_name(old_blog_name)
103
+ page.update_attribute(:name, blog.name)
104
+ page.publish
105
+ end
106
+ end
107
+
108
+ def reload_routes
109
+ ActionController::Routing::Routes.load!
110
+ end
111
+
112
+ def create_route(page, name, pattern)
113
+ route = page.page_routes.build(:name => name, :pattern => pattern, :code => "")
114
+ route.add_condition(:method, "get")
115
+ route.add_requirement(:year, '\d{4,}') if pattern.include?(":year")
116
+ route.add_requirement(:month, '\d{2,}') if pattern.include?(":month")
117
+ route.add_requirement(:day, '\d{2,}') if pattern.include?(":day")
118
+ route.send(:create_without_callbacks)
119
+ end
120
+
121
+ def create_portlet(page, name, portlet_class)
122
+ portlet_class.create!(
123
+ :name => "#{name} Portlet",
124
+ :blog_id => @blog.id,
125
+ :template => portlet_class.default_template,
126
+ :connect_to_page_id => page.id,
127
+ :connect_to_container => "main",
128
+ :publish_on_save => true)
129
+ end
130
+ end
131
+
132
+
133
+
134
+
135
+