public
Description: Starter Kit for developers using Ruby on Rails to quickly get a blog up & running and then add features.
Homepage: http://www.faithfulgeek.org
Clone URL: git://github.com/faithfulgeek/blog-starter-kit.git
Added OpenId authentication for admins, listing of posts for admin index, 
ability to edit/delete posts.
Joe Fiorini (author)
Sun Apr 06 00:44:21 -0700 2008
commit  2ed6704f4cfd04ce34c38839240f4866e2875277
tree    f9e754c1c04a7a47c12d4081b8a537eb3140404e
parent  b1746ceba1c0db9e4221f684749e1f57a9d3f0e7
...
3
4
5
 
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
8
9
 
10
...
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
0
@@ -3,8 +3,32 @@
0
 
0
 class ApplicationController < ActionController::Base
0
   helper :all # include all helpers, all the time
0
+ before_filter :retrieve_user
0
 
0
+ protected
0
+ def for_admin_only
0
+ unless @current_user
0
+ redirect_to(root_url)
0
+ else
0
+ yield
0
+ end
0
+ end
0
+
0
+ def for_users_by_type
0
+ if @current_user
0
+ yield :admin
0
+ else
0
+ yield :anonymous
0
+ end
0
+ end
0
+
0
+ private
0
+ def retrieve_user
0
+ @current_user = User.find(session[:user_id]) if session[:user_id]
0
+ end
0
+
0
   # See ActionController::RequestForgeryProtection for details
0
   # Uncomment the :secret if you're not using the cookie session store
0
   protect_from_forgery # :secret => '8881553f7ebc889994296b716578f818'
0
+
0
 end
...
1
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
4
5
...
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 
26
27
28
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
...
32
33
34
 
 
 
 
35
36
37
 
 
 
 
 
38
39
40
41
0
@@ -1,5 +1,26 @@
0
 class PostsController < ResourceController::Base
0
   
0
+ new_action.wants.html do
0
+ for_admin_only do
0
+ render :html => @posts
0
+ end
0
+ end
0
+
0
+ index.wants.html do
0
+ for_users_by_type do |type|
0
+ case type
0
+ when :anonymous
0
+ render :html => @posts
0
+ when :admin
0
+ if request.request_uri.downcase =~ /home/
0
+ render :html => @posts
0
+ else
0
+ render :template => 'admin/posts/index', :html => @posts
0
+ end
0
+ end
0
+ end
0
+ end
0
+
0
   show.wants.xml { render :xml => @post }
0
   new_action.wants.xml { render :xml => @post }
0
   index.wants.xml { render :xml => @posts }
0
@@ -11,18 +32,10 @@ class PostsController < ResourceController::Base
0
     @extra_stylesheet = 'single.css'
0
   end
0
   
0
- index.before do
0
- @current_month = params[:month]
0
- end
0
-
0
   protected
0
   
0
   def load_collection
0
- if params[:month]
0
- @posts = Post.find_by_date(params[:month])
0
- else
0
- @posts = Post.find(:all, :order => 'created_at desc')
0
- end
0
+ @posts = Post.find(:all, :limit => 10, :order => "created_at desc")
0
   end
0
 
0
 end
...
1
2
3
 
4
5
 
 
 
 
6
7
8
...
1
2
 
3
4
5
6
7
8
9
10
11
12
0
@@ -1,8 +1,12 @@
0
 # Methods added to this helper will be available to all templates in the application.
0
 module ApplicationHelper
0
- def format_date(date)
0
+ def format_date_long(date)
0
     date.strftime("%A, %B #{date.day.ordinalize}, %Y at %I:%M%p")
0
   end
0
+
0
+ def format_date_short(date)
0
+ date.strftime("%D")
0
+ end
0
 end
0
 
0
 # Move this to 'lib' once I find out how to load from 'lib'
...
7
8
9
10
11
12
13
14
15
16
17
...
7
8
9
 
 
 
 
 
 
 
10
0
@@ -7,11 +7,4 @@ class Post < ActiveRecord::Base
0
     self[:body] = value.gsub("\n", "<br>")
0
   end
0
   
0
- def self.find_by_date(month)
0
-
0
- start_date, end_date = month.to_date_range
0
-
0
- find :all, :conditions => ["created_at between ? and ?", start_date, end_date], :order => 'created_at desc'
0
-
0
- end
0
 end
...
12
13
14
 
 
 
 
15
16
17
...
12
13
14
15
16
17
18
19
20
21
0
@@ -12,6 +12,10 @@
0
     / Date: 2008-03-07
0
   %body
0
     #header
0
+ - if @current_user
0
+ #welcome_message
0
+ = "Logged in as: " + @current_user.name
0
+ = link_to 'Logout', logout_url
0
       %img{ :src => "/images/glasses.png" }/
0
       %h1
0
         %a{ :href => "/" }
...
3
4
5
6
 
7
8
9
...
3
4
5
 
6
7
8
9
0
@@ -3,7 +3,7 @@
0
   %h2= post.title
0
   %h3#tags
0
     on
0
- = format_date(post.created_at)
0
+ = format_date_long(post.created_at)
0
     &ndash;
0
     tagged with:
0
     %a{:href => "/tags/personal"} personal
...
1
2
3
4
5
6
...
1
2
3
 
 
4
0
@@ -1,5 +1,3 @@
0
 #posts
0
   - @posts.each do |post|
0
     = render_partial 'post', post
0
-- if @current_month
0
- = link_to "To #{@current_month.previous_month}", month_path(@current_month.previous_month)
0
\ No newline at end of file
...
1
2
3
 
 
 
4
5
6
...
11
12
13
14
15
16
 
 
 
17
18
19
...
1
2
3
4
5
6
7
8
9
...
14
15
16
 
 
 
17
18
19
20
21
22
0
@@ -1,6 +1,9 @@
0
 ActionController::Routing::Routes.draw do |map|
0
   
0
   map.root :controller => 'posts'
0
+ map.home 'home', :controller => 'posts'
0
+ map.login 'login', :controller => 'sessions', :action => 'new'
0
+ map.logout 'logout', :controller => 'sessions', :action => 'destroy'
0
   
0
   map.resources :posts do |post|
0
     post.resources :comments
0
@@ -11,9 +14,9 @@ ActionController::Routing::Routes.draw do |map|
0
               :action => 'index',
0
               :month => /#{(DateTime::MONTHNAMES - [nil]).join('|')}/
0
   
0
- map.month '/:month',
0
- :controller => 'posts'
0
-
0
+ map.open_id_complete 'session', :controller => "sessions", :action => "create", :requirements => { :method => :get }
0
+ map.resource :session
0
+
0
   # The priority is based upon order of creation: first created -> highest priority.
0
 
0
   # Sample of regular route:
...
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
...
6
7
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
10
0
@@ -6,27 +6,4 @@ class String
0
     return ns
0
   end
0
   
0
- def to_month_number
0
- Date::MONTHNAMES.index(self)
0
- end
0
-
0
- def to_date_range
0
-
0
- if(self.to_month_number > DateTime.now.month)
0
- year = 1.year.ago.year
0
- else
0
- year = DateTime.now.year
0
- end
0
-
0
- start_date = DateTime.new(year, self.to_month_number, 1).to_time
0
- end_date = start_date.at_end_of_month
0
-
0
- return start_date, end_date
0
- end
0
-
0
- def previous_month
0
- index = Date::MONTHNAMES.index(self) - 1
0
- index = 12 if index == 0
0
- Date::MONTHNAMES[index]
0
- end
0
 end
0
\ No newline at end of file
...
76
77
78
 
 
 
 
 
 
79
80
81
...
137
138
139
 
 
 
 
 
 
...
76
77
78
79
80
81
82
83
84
85
86
87
...
143
144
145
146
147
148
149
150
151
0
@@ -76,6 +76,12 @@ legend
0
   margin-right:15px;
0
 }
0
 
0
+#welcome_message
0
+{
0
+ float: right;
0
+ margin-right:15px;
0
+}
0
+
0
 #header h1 a
0
 {
0
   height: 89px;
0
@@ -137,3 +143,9 @@ legend
0
     background: url('/images/icons/16-star-hot.png') no-repeat;
0
 }
0
 
0
+.openid-login
0
+{
0
+ background: #fff url(/images/openid-logo.gif) no-repeat scroll 0pt 50%;
0
+ padding-left: 18px;
0
+}
0
+
...
12
13
14
15
16
17
18
19
20
21
...
26
27
28
29
30
 
 
31
32
33
34
35
36
37
38
 
39
40
41
...
59
60
61
62
63
 
 
64
65
66
...
12
13
14
 
 
 
 
15
16
17
...
22
23
24
 
 
25
26
27
28
 
 
 
 
 
 
29
30
31
32
...
50
51
52
 
 
53
54
55
56
57
0
@@ -12,10 +12,6 @@ describe PostsController do
0
       get :index
0
     end
0
     
0
- def do_get_by_month(month)
0
- get :index, :month => month
0
- end
0
-
0
     it "should be successful" do
0
       do_get
0
       response.should be_success
0
@@ -26,16 +22,11 @@ describe PostsController do
0
       response.should render_template('index')
0
     end
0
   
0
- it "should find all posts" do
0
- Post.should_receive(:find).with(:all, :order => 'created_at desc').and_return([@post])
0
+ it "should find the 10 most recent posts" do
0
+ Post.should_receive(:find).with(:all, :limit => 10, :order => 'created_at desc').and_return([@post])
0
       do_get
0
     end
0
-
0
- it "should find all posts for given month" do
0
- Post.should_receive(:find).with(:all, :conditions => ["MONTH(created_at) = 'April'"], :order => 'created_at desc')
0
- do_get_by_month 'April'
0
- end
0
-
0
+
0
     it "should assign the found posts for the view" do
0
       do_get
0
       assigns[:posts].should == [@post]
0
@@ -59,8 +50,8 @@ describe PostsController do
0
       response.should be_success
0
     end
0
 
0
- it "should find all posts" do
0
- Post.should_receive(:find).with(:all, :order => 'created_at desc').and_return([@post])
0
+ it "should find the 10 most recent posts" do
0
+ Post.should_receive(:find).with(:all, :limit => 10, :order => 'created_at desc').and_return([@post])
0
       do_get
0
     end
0
   
...
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 
50
...
36
37
38
 
 
 
 
 
 
 
 
 
 
 
39
40
0
@@ -36,15 +36,5 @@ describe Post do
0
     @post.body = "this has a\nline break"
0
     @post.body.should == "this has a<br>line break"
0
   end
0
-
0
- it "should find by month and return not nil" do
0
- @post.title = 'testing 1 2 3'
0
- @post.body = 'this is a test of the emergency posting system!'
0
- @post.save
0
- @post = Post.find_by_date(Date::MONTHNAMES[DateTime.now.month])
0
- @post.first.should_not be_nil
0
- @post.first.created_at.month.should == DateTime.now.month
0
- @post.first.destroy
0
- end
0
-
0
+
0
 end

Comments

    No one has commented yet.