wa_bcms_blog 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. data/LICENSE.txt +165 -0
  2. data/README.markdown +101 -0
  3. data/app/controllers/application_controller.rb +10 -0
  4. data/app/controllers/cms/blog_comments_controller.rb +3 -0
  5. data/app/controllers/cms/blog_posts_controller.rb +35 -0
  6. data/app/controllers/cms/blogs_controller.rb +11 -0
  7. data/app/helpers/application_helper.rb +3 -0
  8. data/app/helpers/cms/blog_helper.rb +11 -0
  9. data/app/models/blog.rb +162 -0
  10. data/app/models/blog_comment.rb +32 -0
  11. data/app/models/blog_group_membership.rb +4 -0
  12. data/app/models/blog_post.rb +113 -0
  13. data/app/portlets/blog_post_portlet.rb +38 -0
  14. data/app/portlets/blog_posts_portlet.rb +50 -0
  15. data/app/views/cms/blog_comments/_form.html.erb +5 -0
  16. data/app/views/cms/blog_comments/render.html.erb +2 -0
  17. data/app/views/cms/blog_posts/_form.html.erb +11 -0
  18. data/app/views/cms/blog_posts/no_access.html.erb +9 -0
  19. data/app/views/cms/blog_posts/render.html.erb +1 -0
  20. data/app/views/cms/blogs/_form.html.erb +28 -0
  21. data/app/views/cms/blogs/admin_only.html.erb +9 -0
  22. data/app/views/cms/blogs/render.html.erb +2 -0
  23. data/app/views/partials/_blog_post.html.erb +103 -0
  24. data/app/views/partials/_blog_post.html.haml +91 -0
  25. data/app/views/portlets/blog_post/_form.html.erb +3 -0
  26. data/app/views/portlets/blog_post/render.html.erb +33 -0
  27. data/app/views/portlets/blog_posts/_form.html.erb +13 -0
  28. data/app/views/portlets/blog_posts/render.html.haml +9 -0
  29. data/db/migrate/20090415000000_create_blogs.rb +45 -0
  30. data/db/migrate/20090415000001_create_blog_posts.rb +25 -0
  31. data/db/migrate/20090415000002_create_blog_comments.rb +19 -0
  32. data/db/migrate/20090415000003_add_attachment_to_blog_posts.rb +23 -0
  33. data/db/migrate/20100521042244_add_moderate_comments_to_blog.rb +10 -0
  34. data/doc/README_FOR_APP +2 -0
  35. data/doc/migrate_to_20100427.rb +77 -0
  36. data/doc/release_notes.txt +40 -0
  37. data/lib/bcms_blog.rb +1 -0
  38. data/lib/bcms_blog/routes.rb +9 -0
  39. data/rails/init.rb +4 -0
  40. data/test/factories.rb +48 -0
  41. data/test/functional/blog_post_test.rb +42 -0
  42. data/test/functional/blog_test.rb +74 -0
  43. data/test/functional/cms/blog_posts_controller_test.rb +46 -0
  44. data/test/functional/cms/blogs_controller_test.rb +25 -0
  45. data/test/performance/browsing_test.rb +9 -0
  46. data/test/test_helper.rb +103 -0
  47. data/test/test_logging.rb +64 -0
  48. data/test/unit/blog_comment_test.rb +34 -0
  49. data/test/unit/blog_post_test.rb +43 -0
  50. data/test/unit/blog_test.rb +86 -0
  51. metadata +127 -0
@@ -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.
@@ -0,0 +1,101 @@
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
+ ## Security
56
+
57
+ 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:
58
+
59
+ * 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.
60
+ * 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.
61
+ * Comments - All Editors may edit/deleted comments. Visitors may add new comments.
62
+
63
+ ## Customization
64
+
65
+ The module provides a template that is a good starting point for your blog's layout. If you want
66
+ to further customize the look and feel, just copy the file app/views/partials/\_blog_post.html.erb
67
+ on this repository to app/views/partials on your application and modify it to suit your needs.
68
+
69
+ Keep in mind that both the posts list and individual post pages are handled by the same partial.
70
+
71
+ ## Routes
72
+
73
+ 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.
74
+
75
+ /:blog_path/:year/:month/:day/:slug
76
+ /:blog_path/:year/:month/:day
77
+ /:blog_path/:year/:month
78
+ /:blog_path/:year
79
+ /:blog_path/tag/:tag
80
+ /:blog_path/category/:category
81
+
82
+ ## Architecture
83
+
84
+ This module will add the following new items to your project.
85
+
86
+ * New content types for Blogs, Blog Comments and Blog Posts.
87
+ * New portlets for display in a Single Blog post and for showing many blog posts.
88
+
89
+ ## Contributors
90
+
91
+ 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:
92
+
93
+ * Tyler Rick (http://github.com/TylerRick)
94
+ * Jon Leighton (http://github.com/jonleighton)
95
+
96
+ ## Wishlist Features
97
+
98
+ * RSS feeds - Each blog should have its own RSS feed created as part of the initial blog creation.
99
+ * 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.
100
+ * 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.
101
+
@@ -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
@@ -0,0 +1,3 @@
1
+ class Cms::BlogCommentsController < Cms::ContentBlockController
2
+
3
+ end
@@ -0,0 +1,35 @@
1
+ class Cms::BlogPostsController < Cms::ContentBlockController
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
@@ -0,0 +1,11 @@
1
+ class Cms::BlogsController < Cms::ContentBlockController
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,3 @@
1
+ # Methods added to this helper will be available to all templates in the application.
2
+ module ApplicationHelper
3
+ end
@@ -0,0 +1,11 @@
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
+ end
11
+
@@ -0,0 +1,162 @@
1
+ class Blog < ActiveRecord::Base
2
+ acts_as_content_block
3
+ has_many :posts, :class_name => "BlogPost", :conditions => { :published => true }, :order => "published_at desc"
4
+
5
+ has_many :blog_group_memberships
6
+ has_many :groups, :through => :blog_group_memberships
7
+
8
+ validates_presence_of :name
9
+ validates_uniqueness_of :name
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
+
19
+ def self.default_template
20
+ template_file = ActionController::Base.view_paths.map do |vp|
21
+ path = vp.to_s.first == "/" ? vp.to_s : File.join(Rails.root, vp.to_s)
22
+ File.join(path, "cms/blogs/render.html.erb")
23
+ end.detect{|f| File.exists? f }
24
+ template_file ? open(template_file){|f| f.read } : ""
25
+ end
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
+
43
+ def render
44
+ @blog = self
45
+ finder = @blog.posts.published
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)
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
+
62
+ if params[: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}"
72
+ end
73
+ end
74
+
75
+ def inline_options
76
+ {:inline => self.template}
77
+ end
78
+
79
+ def self.default_order
80
+ "name"
81
+ 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
+
92
+ #-------------------------------------------------------------------------------------------------
93
+
94
+ def name_for_path
95
+ name.to_slug.gsub('-', '_')
96
+ end
97
+
98
+ protected
99
+ def after_create
100
+ section = Section.find_by_name(name) || (
101
+ section = Section.create!(
102
+ :name => name,
103
+ :path => "/#{name_for_path}",
104
+ :parent_id => 1
105
+ )
106
+ section.groups << Group.find_by_code("cms-admin")
107
+ section.groups << Group.find_by_code("guest")
108
+ section.groups << Group.find_by_code("content-editor")
109
+ section.save!
110
+ section
111
+ )
112
+
113
+ page = Page.find_by_name(name) || Page.create!(
114
+ :name => name,
115
+ :path => "/#{name_for_path}",
116
+ :section => section,
117
+ :template_file_name => "default.html.erb",
118
+ :hidden => true
119
+ )
120
+ page.create_connector(self, 'main')
121
+ create_route(page, "#{name}: Posts In Day", "/#{name_for_path}/:year/:month/:day")
122
+ create_route(page, "#{name}: Posts In Month", "/#{name_for_path}/:year/:month")
123
+ create_route(page, "#{name}: Posts In Year", "/#{name_for_path}/:year")
124
+ create_route(page, "#{name}: Posts With Tag", "/#{name_for_path}/tag/:tag")
125
+ create_route(page, "#{name}: Posts In Category", "/#{name_for_path}/category/:category")
126
+
127
+ page = Page.find_by_name(portlet_name = "#{name}: Post") || Page.create!(
128
+ :name => portlet_name,
129
+ :path => "/#{name_for_path}/post",
130
+ :section => section,
131
+ :template_file_name => "default.html.erb",
132
+ :hidden => true)
133
+ page.publish
134
+ create_route( page, portlet_name, "/#{name_for_path}/:year/:month/:day/:slug")
135
+ create_portlet(page, portlet_name, BlogPostPortlet)
136
+
137
+ reload_routes
138
+ end
139
+
140
+ def create_route(page, name, pattern)
141
+ route = page.page_routes.build(:name => name, :pattern => pattern, :code => "")
142
+ route.add_condition(:method, "get")
143
+ route.add_requirement(:year, '\d{4,}') if pattern.include?(":year")
144
+ route.add_requirement(:month, '\d{2,}') if pattern.include?(":month")
145
+ route.add_requirement(:day, '\d{2,}') if pattern.include?(":day")
146
+ route.send(:create_without_callbacks)
147
+ end
148
+
149
+ def create_portlet(page, name, portlet_class)
150
+ portlet_class.create!(
151
+ :name => "#{name} Portlet",
152
+ :blog_id => self.id,
153
+ :template => portlet_class.default_template,
154
+ :connect_to_page_id => page.id,
155
+ :connect_to_container => "main",
156
+ :publish_on_save => true)
157
+ end
158
+
159
+ def reload_routes
160
+ ActionController::Routing::Routes.load!
161
+ end
162
+ end