spud_blog 0.2.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,41 @@
1
+ Spud = (typeof(Spud) == 'undefined') ? {} : Spud;
2
+
3
+ Spud.Blog = new function(){
4
+
5
+ var self = this;
6
+
7
+ this.init = function(){
8
+ $('.spud_blog_filter_form').live('submit', self.didSubmitFilterForm)
9
+ };
10
+
11
+ this.didSubmitFilterForm = function(event){
12
+ event.preventDefault();
13
+ var form = $(this);
14
+ var base = form.attr('action');
15
+ var url = '';
16
+
17
+ // find filter values
18
+ var category_select = $(this).find('select[rel=category]');
19
+ if(category_select){
20
+ var category = category_select.val();
21
+ }
22
+ var archive_select = $(this).find('select[rel=archive]');
23
+ if(archive_select){
24
+ var archive = archive_select.val();
25
+ }
26
+
27
+ // build url and redirect
28
+ if(category && archive){
29
+ url = '/category/' + category + '/' + archive;
30
+ }
31
+ else if(category){
32
+ url = '/category/' + category;
33
+ }
34
+ else if(archive){
35
+ url = '/archive/' + archive;
36
+ }
37
+ window.location = base + url;
38
+ };
39
+ };
40
+
41
+ $(document).ready(Spud.Blog.init);
@@ -9,30 +9,34 @@ class BlogController < ApplicationController
9
9
  respond_with @posts
10
10
  end
11
11
 
12
- def category
13
- @post_category = SpudPostCategory.find_by_url_name(params[:category_url_name])
14
- if @post_category.nil?
12
+ # The sole purpose of this action is to redirect from a POST to an seo-friendly url
13
+ def filter
14
+ if !params[:category_url_name].blank? && !params[:archive_date].blank?
15
+ redirect_to blog_category_archive_path(params[:category_url_name], params[:archive_date])
16
+ elsif !params[:category_url_name].blank?
17
+ redirect_to blog_category_path(params[:category_url_name])
18
+ elsif !params[:archive_date].blank?
19
+ redirect_to blog_archive_path(params[:archive_date])
20
+ else
15
21
  redirect_to blog_path
22
+ end
23
+ end
24
+
25
+ def category
26
+ if @post_category = SpudPostCategory.find_by_url_name(params[:category_url_name])
27
+ @posts = @post_category.posts_with_children.public_blog_posts(params[:page], Spud::Blog.config.posts_per_page).from_archive(params[:archive_date])
16
28
  else
17
- if request.post?
18
- redirect_to blog_category_path(params[:category_url_name])
19
- else
20
- @posts = @post_category.posts_with_children.public_blog_posts(params[:page], Spud::Blog.config.posts_per_page)
21
- respond_with @posts do |format|
22
- format.html { render 'index' }
23
- end
24
- end
29
+ @posts = []
30
+ end
31
+ respond_with @posts do |format|
32
+ format.html { render 'index' }
25
33
  end
26
34
  end
27
35
 
28
36
  def archive
29
- if request.post?
30
- redirect_to blog_archive_path(params[:archive_date])
31
- else
32
- @posts = SpudPost.public_blog_posts(params[:page], Spud::Blog.config.posts_per_page).from_archive(params[:archive_date])
33
- respond_with @posts do |format|
34
- format.html { render 'index' }
35
- end
37
+ @posts = SpudPost.public_blog_posts(params[:page], Spud::Blog.config.posts_per_page).from_archive(params[:archive_date])
38
+ respond_with @posts do |format|
39
+ format.html { render 'index' }
36
40
  end
37
41
  end
38
42
 
@@ -1,37 +1,41 @@
1
1
  class NewsController < ApplicationController
2
2
 
3
3
  respond_to :html, :xml, :json
4
- layout Spud::Blog.base_layout
4
+ layout Spud::Blog.news_layout
5
5
 
6
6
  def index
7
7
  @posts = SpudPost.public_news_posts(params[:page], Spud::Blog.config.posts_per_page)
8
8
  respond_with @posts
9
9
  end
10
10
 
11
- def category
12
- @post_category = SpudPostCategory.find_by_url_name(params[:category_url_name])
13
- if @post_category.nil?
11
+ # The sole purpose of this action is to redirect from a POST to an seo-friendly url
12
+ def filter
13
+ if !params[:category_url_name].blank? && !params[:archive_date].blank?
14
+ redirect_to news_category_archive_path(params[:category_url_name], params[:archive_date])
15
+ elsif !params[:category_url_name].blank?
16
+ redirect_to news_category_path(params[:category_url_name])
17
+ elsif !params[:archive_date].blank?
18
+ redirect_to news_archive_path(params[:archive_date])
19
+ else
14
20
  redirect_to news_path
21
+ end
22
+ end
23
+
24
+ def category
25
+ if @post_category = SpudPostCategory.find_by_url_name(params[:category_url_name])
26
+ @posts = @post_category.posts_with_children.public_news_posts(params[:page], Spud::Blog.config.posts_per_page).from_archive(params[:archive_date])
15
27
  else
16
- if request.post?
17
- redirect_to news_category_path(params[:category_url_name])
18
- else
19
- @posts = @post_category.posts.public_news_posts(params[:page], Spud::Blog.config.posts_per_page)
20
- respond_with @posts do |format|
21
- format.html { render 'index' }
22
- end
23
- end
28
+ @posts = []
29
+ end
30
+ respond_with @posts do |format|
31
+ format.html { render 'index' }
24
32
  end
25
33
  end
26
34
 
27
35
  def archive
28
- if request.post?
29
- redirect_to news_archive_path(params[:archive_date])
30
- else
31
- @posts = SpudPost.public_news_posts(params[:page], Spud::Blog.config.posts_per_page).from_archive(params[:archive_date])
32
- respond_with @posts do |format|
33
- format.html { render 'index' }
34
- end
36
+ @posts = SpudPost.public_news_posts(params[:page], Spud::Blog.config.posts_per_page).from_archive(params[:archive_date])
37
+ respond_with @posts do |format|
38
+ format.html { render 'index' }
35
39
  end
36
40
  end
37
41
 
@@ -1,16 +1,17 @@
1
1
  module BlogHelper
2
2
 
3
3
  def spud_post_category_select
4
- select_tag 'category_url_name',
4
+ return select_tag 'category_url_name',
5
5
  options_for_select(SpudPostCategory.options_for_categories(:value => :url_name), params[:category_url_name]),
6
- :include_blank => true
6
+ :include_blank => true,
7
+ :rel => 'category'
7
8
  end
8
9
 
9
10
  def spud_post_archive_select
10
11
  dates = SpudPost.months_with_public_posts
11
12
  return select_tag 'archive_date', options_for_select(SpudPost.months_with_public_posts.collect{ |d|
12
13
  [d.strftime('%B %Y'), d.strftime('%Y-%b').downcase]
13
- }, params[:archive_date]), :include_blank => true
14
+ }, params[:archive_date]), :include_blank => true, :rel => 'archive'
14
15
  end
15
16
 
16
17
  end
@@ -12,7 +12,9 @@ class SpudPost < ActiveRecord::Base
12
12
  before_validation :set_url_name
13
13
 
14
14
  def self.public_posts(page, per_page)
15
- return where('visible = 1 AND published_at <= ?', Time.now.utc).order('published_at desc').includes(:comments, :categories).paginate(:page => page, :per_page => per_page)
15
+
16
+ return where('visible = ? AND published_at <= ?', true,Time.now.utc).order('published_at desc').includes(:comments, :categories).paginate(:page => page, :per_page => per_page)
17
+
16
18
  end
17
19
 
18
20
  def self.public_blog_posts(page, per_page)
@@ -24,7 +26,7 @@ class SpudPost < ActiveRecord::Base
24
26
  end
25
27
 
26
28
  def self.recent_posts(limit=5)
27
- return where('visible = 1 AND published_at <= ?', Time.now.utc).order('published_at desc').limit(limit)
29
+ return where('visible = ? AND published_at <= ?', true, Time.now.utc).order('published_at desc').limit(limit)
28
30
  end
29
31
 
30
32
  def self.recent_blog_posts(limit=5)
@@ -44,6 +46,8 @@ class SpudPost < ActiveRecord::Base
44
46
  end
45
47
  end
46
48
 
49
+ #def self.posts_for_category_archive(category, )
50
+
47
51
  # Returns an array of Date objects for months with public posts
48
52
  def self.months_with_public_posts
49
53
  # Select
@@ -54,8 +58,12 @@ class SpudPost < ActiveRecord::Base
54
58
  # And published_at < '2012-01-30'
55
59
  # Group By published_month, published_year
56
60
  # Order By published_year desc, published_month desc
57
- records = select('Month(published_at) as published_month, Year(published_at) as published_year').where('visible = 1 And published_at < ?', DateTime.now).group('published_month, published_year').order('published_year desc, published_month desc')
58
- return records.collect{ |r| Date.new(r[:published_year], r[:published_month]) }
61
+ records = SpudPost.select('Extract(Month from published_at) as published_month, Extract(Year from published_at) as published_year').where('visible = ? AND published_at < ?', true, DateTime.now).group('published_month, published_year').order('published_year desc, published_month desc')
62
+ begin
63
+ return records.collect{ |r| Date.new(r[:published_year].to_i, r[:published_month].to_i) }
64
+ rescue Exception => e
65
+ logger.fatal "Exception occurred while fetching post archive dates:\n #{e.to_s}"
66
+ end
59
67
  end
60
68
 
61
69
  def display_date
@@ -1,14 +1,8 @@
1
- <div id="spud_blog_categories">
2
- <%= form_tag '/blog/category' do %>
3
- <label>Filter By Category:</label>
1
+ <div id="spud_blog_filters">
2
+ <%= form_tag blog_path, :class => 'spud_blog_filter_form' do %>
3
+ <label>Category:</label>
4
4
  <%= spud_post_category_select %>
5
- <input type="submit" value="Submit" />
6
- <% end %>
7
- </div>
8
-
9
- <div id="spud_blog_archive">
10
- <%= form_tag '/blog/archive' do %>
11
- <label>Filter By Month:</label>
5
+ <label>Month:</label>
12
6
  <%= spud_post_archive_select %>
13
7
  <input type="submit" value="Submit" />
14
8
  <% end %>
@@ -1,14 +1,8 @@
1
- <div id="spud_news_categories">
2
- <%= form_tag '/news/category' do %>
3
- <label>Filter By Category:</label>
1
+ <div id="spud_news_filters">
2
+ <%= form_tag news_path, :class => 'spud_blog_filter_form' do %>
3
+ <label>Category:</label>
4
4
  <%= spud_post_category_select %>
5
- <input type="submit" value="Submit" />
6
- <% end %>
7
- </div>
8
-
9
- <div id="spud_news_archive">
10
- <%= form_tag '/news/archive' do %>
11
- <label>Filter By Month:</label>
5
+ <label>Month:</label>
12
6
  <%= spud_post_archive_select %>
13
7
  <input type="submit" value="Submit" />
14
8
  <% end %>
@@ -1,3 +1,3 @@
1
- <%= form_for @post, :url => new_spud_admin_post_path, :html => {:class => 'right_aligned_form'} do |f| %>
1
+ <%= form_for @post, :url => spud_admin_post_path, :html => {:class => 'right_aligned_form'} do |f| %>
2
2
  <%= render :partial => 'form', :locals => {:f => f} %>
3
3
  <% end %>
data/config/routes.rb CHANGED
@@ -18,18 +18,33 @@ Rails.application.routes.draw do
18
18
  scope Spud::Blog.config.blog_path do
19
19
 
20
20
  # Blog Post Categories
21
- get 'category/:category_url_name', :controller => 'blog', :action => 'category', :page => 1, :as => 'blog_category'
22
- get 'category/:category_url_name/page/:page', :controller => 'blog', :action => 'category'
23
- post 'category', :controller => 'blog', :action => 'category'
21
+ get 'category/:category_url_name(/page/:page)',
22
+ :controller => 'blog',
23
+ :action => 'category',
24
+ :as => 'blog_category',
25
+ :defaults => {:page => 1}
26
+ get 'category/:category_url_name/:archive_date(/page/:page)',
27
+ :controller => 'blog',
28
+ :action => 'category',
29
+ :as => 'blog_category_archive',
30
+ :defaults => {:page => 1}
24
31
 
25
32
  # Blog Post Archives
26
- get 'archive/:archive_date', :controller => 'blog', :action => 'archive', :page => 1, :as => 'blog_archive'
27
- get 'archive/:archive_date/page/:page', :controller => 'blog', :action => 'archive'
28
- post 'archive', :controller => 'blog', :action => 'archive'
33
+ get 'archive/:archive_date(/page/:page)',
34
+ :controller => 'blog',
35
+ :action => 'archive',
36
+ :as => 'blog_archive',
37
+ :defaults => {:page => 1}
38
+
39
+ # Category/Archive filtering
40
+ post '/', :controller => 'blog', :action => 'filter'
29
41
 
30
42
  # Blog Posts
31
- get '/', :controller => 'blog', :action => 'index', :page => 1, :as => 'blog'
32
- get 'page/:page', :controller => 'blog', :action => 'index'
43
+ get '/(page/:page)',
44
+ :controller => 'blog',
45
+ :action => 'index',
46
+ :as => 'blog',
47
+ :defaults => {:page => 1}
33
48
  resources :blog_posts, :path => '/', :controller => 'blog', :only => [:show] do
34
49
  post '/', :on => :member, :controller => 'blog', :action => 'create_comment'
35
50
  end
@@ -40,18 +55,33 @@ Rails.application.routes.draw do
40
55
  scope Spud::Blog.config.news_path do
41
56
 
42
57
  # News Post Categories
43
- get 'category/:category_url_name', :controller => 'news', :action => 'category', :page => 1, :as => 'news_category'
44
- get 'category/:category_url_name/page/:page', :controller => 'news', :action => 'category'
45
- post 'category', :controller => 'news', :action => 'category'
58
+ get 'category/:category_url_name(/page/:page)',
59
+ :controller => 'news',
60
+ :action => 'category',
61
+ :as => 'news_category',
62
+ :defaults => {:page => 1}
63
+ get 'category/:category_url_name/:archive_date(/page/:page)',
64
+ :controller => 'news',
65
+ :action => 'category',
66
+ :as => 'news_category_archive',
67
+ :defaults => {:page => 1}
46
68
 
47
69
  # News Post Archives
48
- get 'archive/:archive_date', :controller => 'news', :action => 'archive', :page => 1, :as => 'news_archive'
49
- get 'archive/:archive_date/page/:page', :controller => 'news', :action => 'archive'
50
- post 'archive', :controller => 'news', :action => 'archive'
70
+ get 'archive/:archive_date(/page/:page)',
71
+ :controller => 'news',
72
+ :action => 'archive',
73
+ :as => 'news_archive',
74
+ :defaults => {:page => 1}
75
+
76
+ # Category/Archive filtering
77
+ post '/', :controller => 'news', :action => 'filter'
51
78
 
52
79
  # News Posts
53
- get '/', :controller => 'news', :action => 'index', :page => 1, :as => 'news'
54
- get 'page/:page', :controller => 'news', :action => 'index'
80
+ get '/(page/:page)',
81
+ :controller => 'news',
82
+ :action => 'index',
83
+ :as => 'news',
84
+ :defaults => {:page => 1}
55
85
  resources :news_posts, :path => '/', :controller => 'news', :only => [:show]
56
86
  end
57
87
  end
@@ -0,0 +1,19 @@
1
+ require 'rails/generators/migration'
2
+
3
+ class Spud::Blog::ViewsGenerator < ::Rails::Generators::Base
4
+
5
+ source_root File.expand_path('../../../../../app/views', __FILE__)
6
+
7
+ def install
8
+ if Spud::Blog.config.blog_enabled
9
+ copy_file 'blog/_comment.html.erb', 'app/views/blog/_comment.html.erb'
10
+ copy_file 'blog/_comment_form.html.erb', 'app/views/blog/_comment_form.html.erb'
11
+ copy_file 'blog/index.html.erb', 'app/views/blog/index.html.erb'
12
+ copy_file 'blog/show.html.erb', 'app/views/blog/show.html.erb'
13
+ end
14
+ if Spud::Blog.config.news_enabled
15
+ copy_file 'news/index.html.erb', 'app/views/news/index.html.erb'
16
+ copy_file 'news/show.html.erb', 'app/views/news/show.html.erb'
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,31 @@
1
+ # Quick script for faking up some data
2
+
3
+ def random_word
4
+ chars = 'abcdefghjkmnpqrstuvwxyz'
5
+ return (1..6).collect{ chars[rand(chars.length)] }.join('').capitalize
6
+ end
7
+
8
+ def random_title
9
+ return (1..4).collect{ random_word }.join(' ')
10
+ end
11
+
12
+ def random_time
13
+ return Time.at(1.year.ago + rand * (Time.now.to_f - 1.year.ago.to_f))
14
+ end
15
+
16
+ 100.times do
17
+ post = SpudPost.create({
18
+ :title => random_title,
19
+ :content => 'Hello, World!',
20
+ :published_at => random_time,
21
+ :visible => 1,
22
+ :spud_user_id => 1
23
+ })
24
+ end
25
+
26
+ category_ids = SpudPostCategory.all.collect{ |c| c.id }
27
+
28
+ SpudPost.all.each do |p|
29
+ p.category_ids = [category_ids[rand(category_ids.length)]]
30
+ p.save
31
+ end
@@ -1,8 +1,10 @@
1
1
  module Spud
2
2
  module Blog
3
3
  include ActiveSupport::Configurable
4
- config_accessor :base_layout, :blog_enabled, :news_enabled, :posts_per_page, :blog_path, :news_path,:enable_sitemap
4
+ config_accessor :base_layout,:news_layout, :blog_enabled, :news_enabled, :posts_per_page, :blog_path, :news_path,:enable_sitemap
5
+
5
6
  self.base_layout = 'application'
7
+ self.news_layout = nil
6
8
  self.news_enabled = false
7
9
  self.blog_enabled = true
8
10
  self.posts_per_page = 5
@@ -39,6 +39,11 @@ module Spud
39
39
  has_many :posts, :class_name => 'SpudPost'
40
40
  end
41
41
  end
42
+ initializer :news_layout do
43
+ if Spud::Core.config.news_layout.nil?
44
+ Spud::Core.config.news_layout = Spud::Core.config.base_layout
45
+ end
46
+ end
42
47
  end
43
48
  end
44
49
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spud_blog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-17 00:00:00.000000000 Z
12
+ date: 2012-02-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: spud_core
16
- requirement: &70355129523080 !ruby/object:Gem::Requirement
16
+ requirement: &70273322548400 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 0.5.2
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70355129523080
24
+ version_requirements: *70273322548400
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: will_paginate
27
- requirement: &70355129522600 !ruby/object:Gem::Requirement
27
+ requirement: &70273322547920 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,18 +32,18 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70355129522600
35
+ version_requirements: *70273322547920
36
36
  description:
37
37
  email: greg@westlakedesign.com
38
38
  executables: []
39
39
  extensions: []
40
40
  extra_rdoc_files: []
41
41
  files:
42
- - app/assets/javascripts/news.js
43
42
  - app/assets/javascripts/spud/admin/news_posts.js
44
43
  - app/assets/javascripts/spud/admin/post_categories.js
45
44
  - app/assets/javascripts/spud/admin/post_comments.js
46
45
  - app/assets/javascripts/spud/admin/posts.js
46
+ - app/assets/javascripts/spud/blog.js
47
47
  - app/assets/javascripts/spud/blog/sitemaps.js
48
48
  - app/assets/stylesheets/news.css
49
49
  - app/assets/stylesheets/spud/admin/news_posts.css
@@ -92,6 +92,8 @@ files:
92
92
  - config/application.rb
93
93
  - config/boot.rb
94
94
  - config/routes.rb
95
+ - lib/generators/spud/blog/views_generator.rb
96
+ - lib/scripts/random_data.rb
95
97
  - lib/spud_blog.rb
96
98
  - lib/spud_blog/configuration.rb
97
99
  - lib/spud_blog/engine.rb
@@ -1,2 +0,0 @@
1
- // Place all the behaviors and hooks related to the matching controller here.
2
- // All this logic will automatically be available in application.js.