Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Move blog callbacks to BlogObserver

  • Loading branch information...
commit 568bd152b55566347e8fcbc6ffc367a34b03c43e 1 parent 0702d87
@alce alce authored
View
121 app/models/blog.rb
@@ -7,10 +7,6 @@ class Blog < ActiveRecord::Base
validates_presence_of :name
validates_uniqueness_of :name
-
- before_update :update_section_pages_and_route
- after_update :publish
- after_create :create_section_pages_and_routes
named_scope :editable_by, lambda { |user|
if user.able_to?(:administrate)
@@ -98,122 +94,5 @@ def name_for_path
protected
- # A section, two pages, 6 routes and a portlet are created alongside every blog.
- # This structure provides sensible defaults so users can pretty much start adding
- # posts right after creating a blog without having to worry about where to put
- # their blog and portlets.
-
- def create_section_pages_and_routes
- create_blog_section
- create_blog_block_page
- create_post_portlet_page
- reload_routes
- end
-
- # Every blog is created within a section with the same name.
- # For example, if you create a blog named 'MyBlog', a section 'MyBlog' will be
- # created. This section will hold two pages: one for the blog ContentBlock that
- # will render the list of posts and one for the BlogPost portlet (ie the individual
- # post view)
-
- def create_blog_section
- @section = Section.find_by_name(name) || (
- @section = Section.create!(
- :name => name,
- :path => "/#{name_for_path}",
- :parent_id => 1
- )
- @section.groups << Group.find_by_code("cms-admin")
- @section.groups << Group.find_by_code("guest")
- @section.groups << Group.find_by_code("content-editor")
- @section.save!
- @section
- )
- end
-
- # Following with the above example, the first page that is created is named 'MyBlog' and
- # holds the Blog ContentBlock directly, not a portlet. Together with the 5 created routes,
- # this page and its ContentBlock handle different post listings (all posts, posts in year,
- # month or day and posts by tag or category).
-
- def create_blog_block_page
- page = Page.find_by_name(name) || Page.create!(
- :name => name,
- :path => "/#{name_for_path}",
- :section => @section,
- :template_file_name => "default.html.erb",
- :hidden => true
- )
- page.create_connector(self, 'main')
-
- create_route(page, "#{name}: Posts In Day", "/#{name_for_path}/:year/:month/:day")
- create_route(page, "#{name}: Posts In Month", "/#{name_for_path}/:year/:month")
- create_route(page, "#{name}: Posts In Year", "/#{name_for_path}/:year")
- create_route(page, "#{name}: Posts With Tag", "/#{name_for_path}/tag/:tag")
- create_route(page, "#{name}: Posts In Category", "/#{name_for_path}/category/:category")
- end
-
- # The second page that is created holds the BlogPostPortlet and displays the individual
- # post view, along with it's comments.
-
- def create_post_portlet_page
- page = Page.find_by_name(portlet_name = "#{name}: Post") || Page.create!(
- :name => portlet_name,
- :path => "/#{name_for_path}/post",
- :section => @section,
- :template_file_name => "default.html.erb",
- :hidden => true)
- page.publish
- create_route(page, portlet_name, "/#{name_for_path}/:year/:month/:day/:slug")
- create_portlet(page, portlet_name, BlogPostPortlet)
- end
-
- # When the name of a Blog block changes, we need to change the Post page route.
- # We also change the *names* of the section and pages that hold the blog block and
- # post portlet because presumably, by changing the name of the blog, the intention
- # was to reflect this name change in breadcrumbs and menus.
- #
- # Note that no other routes or paths are updated. This is intentional to be consistent
- # with how BrowserCMS behaves when a Section or Page names change: paths are not
- # updated automatically.
-
- def update_section_pages_and_route
- if name_changed?
- old_blog_name = name_was
-
- Section.find_by_name(old_blog_name).update_attribute(:name, name)
- PageRoute.find_by_name("#{old_blog_name}: Post").update_attribute(:name, "#{name}: Post")
-
- page = Page.find_by_name("#{old_blog_name}: Post")
- page.update_attribute(:name, "#{name}: Post")
- page.publish
-
- page = Page.find_by_name(old_blog_name)
- page.update_attribute(:name, name)
- page.publish
- end
- end
-
- def reload_routes
- ActionController::Routing::Routes.load!
- end
- def create_route(page, name, pattern)
- route = page.page_routes.build(:name => name, :pattern => pattern, :code => "")
- route.add_condition(:method, "get")
- route.add_requirement(:year, '\d{4,}') if pattern.include?(":year")
- route.add_requirement(:month, '\d{2,}') if pattern.include?(":month")
- route.add_requirement(:day, '\d{2,}') if pattern.include?(":day")
- route.send(:create_without_callbacks)
- end
-
- def create_portlet(page, name, portlet_class)
- portlet_class.create!(
- :name => "#{name} Portlet",
- :blog_id => self.id,
- :template => portlet_class.default_template,
- :connect_to_page_id => page.id,
- :connect_to_container => "main",
- :publish_on_save => true)
- end
end
View
140 app/models/blog_observer.rb
@@ -0,0 +1,140 @@
+class BlogObserver < ActiveRecord::Observer
+
+ def after_create(blog)
+ @blog = blog
+ create_section_pages_and_routes
+ end
+
+ def before_update(blog)
+ update_section_pages_and_route(blog)
+ end
+
+ def after_update(blog)
+ blog.publish
+ end
+
+ private
+ # A section, two pages, 6 routes and a portlet are created alongside every blog.
+ # This structure provides sensible defaults so users can pretty much start adding
+ # posts right after creating a blog without having to worry about where to put
+ # their blog and portlets.
+
+ def create_section_pages_and_routes
+ create_blog_section
+ create_blog_block_page
+ create_post_portlet_page
+ reload_routes
+ end
+
+ # Every blog is created within a section with the same name.
+ # For example, if you create a blog named 'MyBlog', a section 'MyBlog' will be
+ # created. This section will hold two pages: one for the blog ContentBlock that
+ # will render the list of posts and one for the BlogPost portlet (ie the individual
+ # post view)
+
+ def create_blog_section
+ @section = Section.find_by_name(@blog.name) || (
+ @section = Section.create!(
+ :name => @blog.name,
+ :path => "/#{@blog.name_for_path}",
+ :parent_id => 1
+ )
+ @section.groups << Group.find_by_code("cms-admin")
+ @section.groups << Group.find_by_code("guest")
+ @section.groups << Group.find_by_code("content-editor")
+ @section.save!
+ @section
+ )
+ end
+
+ # Following with the above example, the first page that is created is named 'MyBlog' and
+ # holds the Blog ContentBlock directly, not a portlet. Together with the 5 created routes,
+ # this page and its ContentBlock handle different post listings (all posts, posts in year,
+ # month or day and posts by tag or category).
+
+ def create_blog_block_page
+ page = Page.find_by_name(@blog.name) || Page.create!(
+ :name => @blog.name,
+ :path => "/#{@blog.name_for_path}",
+ :section => @section,
+ :template_file_name => "default.html.erb",
+ :hidden => true
+ )
+ page.create_connector(@blog, 'main')
+
+ create_route(page, "#{@blog.name}: Posts In Day", "/#{@blog.name_for_path}/:year/:month/:day")
+ create_route(page, "#{@blog.name}: Posts In Month", "/#{@blog.name_for_path}/:year/:month")
+ create_route(page, "#{@blog.name}: Posts In Year", "/#{@blog.name_for_path}/:year")
+ create_route(page, "#{@blog.name}: Posts With Tag", "/#{@blog.name_for_path}/tag/:tag")
+ create_route(page, "#{@blog.name}: Posts In Category", "/#{@blog.name_for_path}/category/:category")
+ end
+
+ # The second page that is created holds the BlogPostPortlet and displays the individual
+ # post view, along with it's comments.
+
+ def create_post_portlet_page
+ page = Page.find_by_name(portlet_name = "#{@blog.name}: Post") || Page.create!(
+ :name => portlet_name,
+ :path => "/#{@blog.name_for_path}/post",
+ :section => @section,
+ :template_file_name => "default.html.erb",
+ :hidden => true)
+ page.publish
+ create_route(page, portlet_name, "/#{@blog.name_for_path}/:year/:month/:day/:slug")
+ create_portlet(page, portlet_name, BlogPostPortlet)
+ end
+
+ # When the name of a Blog block changes, we need to change the Post page route.
+ # We also change the *names* of the section and pages that hold the blog block and
+ # post portlet because presumably, by changing the name of the blog, the intention
+ # was to reflect this name change in breadcrumbs and menus.
+ #
+ # Note that no other routes or paths are updated. This is intentional to be consistent
+ # with how BrowserCMS behaves when a Section or Page names change: paths are not
+ # updated automatically.
+
+ def update_section_pages_and_route(blog)
+ if blog.name_changed?
+ old_blog_name = blog.name_was
+
+ Section.find_by_name(old_blog_name).update_attribute(:name, blog.name)
+ PageRoute.find_by_name("#{old_blog_name}: Post").update_attribute(:name, "#{blog.name}: Post")
+
+ page = Page.find_by_name("#{old_blog_name}: Post")
+ page.update_attribute(:name, "#{blog.name}: Post")
+ page.publish
+
+ page = Page.find_by_name(old_blog_name)
+ page.update_attribute(:name, blog.name)
+ page.publish
+ end
+ end
+
+ def reload_routes
+ ActionController::Routing::Routes.load!
+ end
+
+ def create_route(page, name, pattern)
+ route = page.page_routes.build(:name => name, :pattern => pattern, :code => "")
+ route.add_condition(:method, "get")
+ route.add_requirement(:year, '\d{4,}') if pattern.include?(":year")
+ route.add_requirement(:month, '\d{2,}') if pattern.include?(":month")
+ route.add_requirement(:day, '\d{2,}') if pattern.include?(":day")
+ route.send(:create_without_callbacks)
+ end
+
+ def create_portlet(page, name, portlet_class)
+ portlet_class.create!(
+ :name => "#{name} Portlet",
+ :blog_id => @blog.id,
+ :template => portlet_class.default_template,
+ :connect_to_page_id => page.id,
+ :connect_to_container => "main",
+ :publish_on_save => true)
+ end
+end
+
+
+
+
+
View
2  bcms_blog.gemspec
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
"app/models/blog.rb",
"app/models/blog_comment.rb",
"app/models/blog_group_membership.rb",
+ "app/models/blog_observer.rb",
"app/models/blog_post.rb",
"app/portlets/blog_post_portlet.rb",
"app/portlets/blog_posts_portlet.rb",
@@ -75,6 +76,7 @@ Gem::Specification.new do |s|
"test/test_helper.rb",
"test/test_logging.rb",
"test/unit/blog_comment_test.rb",
+ "test/unit/blog_observer_test.rb",
"test/unit/blog_post_test.rb",
"test/unit/blog_test.rb",
"test/unit/helpers/feeds_helper_test.rb"
View
2  config/environment.rb
@@ -30,7 +30,7 @@
# config.frameworks -= [ :active_record, :active_resource, :action_mailer ]
# Activate observers that should always be running
- # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
+ config.active_record.observers = :blog_observer
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names.
View
4 rails/init.rb
@@ -2,3 +2,7 @@
Cms.add_to_rails_paths gem_root
Cms.add_generator_paths gem_root, "db/migrate/[0-9]*_*.rb"
ApplicationHelper.module_eval { include Cms::BlogHelper }
+
+config.after_initialize do
+ ActiveRecord::Base.observers << BlogObserver
+end
View
8 test/unit/blog_observer_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class BlogObserverTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.