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
Search Repo:
Added initial comments implementation; implemented suggestions from code 
review with Corey; design changes to allow for better resizing
Joe Fiorini (author)
Sat Apr 12 22:00:11 -0700 2008
commit  3ec125e4465956c74a77b3a8d43e6828f947364f
tree    36b5fac1140f124b61de5b23f27be0cfaab88464
parent  754d289290aa6bac67a4dbce9f04c1d82f0a87cb
...
1
 
 
2
...
1
2
3
4
0
@@ -1,3 +1,5 @@
0
 class CommentsController < ResourceController::Base
0
+ belongs_to :post
0
+
0
 end
...
1
2
 
3
4
5
...
1
 
2
3
4
5
0
@@ -1,5 +1,5 @@
0
 class PostsController < ResourceController::Base
0
-
0
+
0
   new_action.wants.html do
0
     for_admin_only do
0
       render :html => @posts
...
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 
50
51
...
5
6
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
9
10
0
@@ -5,48 +5,7 @@
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'
0
-module ActionView
0
- class Base
0
-
0
- def method_missing(symbol, *args)
0
- if symbol.to_s. =~ /_label$/
0
- self.class.instance_eval do
0
-
0
- define_method(symbol){
0
- options = args[1] || {}
0
- options = args[0] if args[0].class == Hash
0
-
0
- prompt_char = options[:prompt_char] || ':'
0
- is_required = options[:is_required] || false
0
- required_char = options[:required_char] || '*'
0
- label_text = args[0].class == String ? args[0] : symbol.to_s.gsub(/_label/, '').to_friendly_name
0
-
0
- label(:post, symbol.to_s.gsub(/_label/, ''), label_text) + (is_required ? required_char : '') + prompt_char
0
- }
0
-
0
- send symbol
0
-
0
- end
0
-
0
- end
0
-
0
- end
0
-
0
- end
0
-
0
-end
0
-
0
-class String
0
- def to_friendly_name
0
- s = self.gsub(/[_]/, ' ')
0
- ns = ""
0
- s.each("\s"){|w| ns << w.capitalize }
0
- return ns
0
+ date.to_s(:short_date_only)
0
   end
0
 end
...
 
 
...
1
2
0
@@ -1 +1,3 @@
0
+module SessionsHelper
0
+end
...
1
2
3
...
 
 
 
0
@@ -1,4 +1 @@
0
-class Comment < ActiveRecord::Base
0
- belongs_to :post
0
-end
...
1
2
3
4
 
5
6
7
...
 
 
1
 
2
3
4
5
0
@@ -1,7 +1,5 @@
0
-require 'lib/string'
0
-
0
 class Post < ActiveRecord::Base
0
- has_many :comments
0
+ acts_as_commentable
0
   
0
   def body=(value)
0
     self[:body] = value.gsub("\n", "<br>")
...
19
20
21
22
23
 
24
25
26
 
 
 
27
28
29
...
19
20
21
 
 
22
23
 
 
24
25
26
27
28
29
0
@@ -19,11 +19,11 @@
0
       %img{ :src => "/images/glasses.png" }/
0
       %h1
0
         %a{ :href => "/" }
0
- %em
0
- faithfulgeek.org
0
+ faithfulgeek.org
0
     #main
0
- #page-content
0
- = yield
0
+ #wrapper
0
+ #page-content
0
+ = yield
0
       #modules
0
         .twitter.module
0
           %fieldset
...
4
5
6
7
 
8
9
10
11
12
 
13
14
15
16
17
 
18
19
20
21
22
 
23
24
25
...
4
5
6
 
7
8
9
10
11
 
12
13
14
15
16
 
17
18
19
20
21
 
22
23
24
25
0
@@ -4,22 +4,22 @@
0
 
0
 <% form_for(@post) do |f| %>
0
   <p>
0
- <b>Title</b><br />
0
+ <b><%= title_label %></b><br />
0
     <%= f.text_field :title %>
0
   </p>
0
 
0
   <p>
0
- <b>Body</b><br />
0
+ <b><%= body_label %></b><br />
0
     <%= f.text_area :body %>
0
   </p>
0
 
0
   <p>
0
- <b>Excerpt</b><br />
0
+ <b><%= excerpt_label %></b><br />
0
     <%= f.text_area :excerpt %>
0
   </p>
0
 
0
   <p>
0
- <b>Is published</b><br />
0
+ <b><%= is_published_label %></b><br />
0
     <%= f.check_box :is_published %>
0
   </p>
0
 
...
1
2
3
4
5
6
7
8
9
10
11
 
 
 
 
 
 
 
 
 
 
12
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
27
28
29
30
0
@@ -1,14 +1,31 @@
0
 = render_partial 'post', @post
0
 
0
-- form_for([@post, Comment.new], :html => {:id => 'comment-submission'}) do |f|
0
-/
0
- %p
0
- .form-field
0
- = #label :name, 'Name:'
0
- = #f.text_field :name
0
- = #f.text_field :email
0
- = #f.text_field :url
0
- = #f.text_area :body
0
+.comments
0
+ - @post.comments.each do |c|
0
+ .comment
0
+ - if c.url
0
+ = link_to c.name, c.url
0
+ - else
0
+ = c.name
0
+ says:
0
+ %p.comment-body
0
+ = c.comment
0
 
0
-- end
0
+%fieldset
0
+ %legend Voice Your Opinion!
0
+ - form_for([@post, Comment.new], :html => {:id => 'comment-submission'}) do |f|
0
+
0
+ %p
0
+ .form-field
0
+ = label :name, 'Name:'
0
+ = f.text_field :name
0
+ .form-field
0
+ = label :email, 'Email:'
0
+ = f.text_field :email
0
+ .form-field
0
+ = label :url, 'URL:'
0
+ = f.text_field :url
0
+ .form-field
0
+ = f.text_area :comment
0
+ = submit_tag
...
10
11
12
 
 
13
14
15
...
57
58
59
 
 
...
10
11
12
13
14
15
16
17
...
59
60
61
62
63
0
@@ -10,6 +10,8 @@
0
 # Bootstrap the Rails environment, frameworks, and default configuration
0
 require File.join(File.dirname(__FILE__), 'boot')
0
 
0
+Time::DATE_FORMATS[:short_date_only] = "%D"
0
+
0
 Rails::Initializer.run do |config|
0
   # Settings in config/environments/* take precedence over those specified here.
0
   # Application configuration should go into files in config/initializers
0
@@ -57,4 +59,6 @@
0
   # Make Active Record use UTC-base instead of local time
0
   # config.active_record.default_timezone = :utc
0
 end
0
+
0
+require 'lib/ActionView/base'
...
5
6
7
8
9
10
11
 
 
12
13
14
...
5
6
7
 
 
 
 
8
9
10
11
12
0
@@ -5,10 +5,8 @@
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
- end
0
-
0
+ map.resources :posts, :has_many => :comments
0
+
0
   map.connect '/:month',
0
               :controller => 'posts',
0
               :action => 'index',
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
0
@@ -1 +1,17 @@
0
+class RemoveComments < ActiveRecord::Migration
0
+ def self.up
0
+ drop_table :comments
0
+ end
0
+
0
+ def self.down
0
+ create_table :comments do |t|
0
+ t.string :name
0
+ t.string :email
0
+ t.string :url
0
+ t.string :body
0
+
0
+ t.timestamps
0
+ end
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
0
@@ -1 +1,18 @@
0
+class CreateCommentableSchema < ActiveRecord::Migration
0
+ def self.up
0
+ create_table "comments", :force => true do |t|
0
+ t.string :name
0
+ t.string :email
0
+ t.string :url
0
+ t.text "comment", :default => ""
0
+ t.datetime "created_at", :null => false
0
+ t.integer "commentable_id", :default => 0, :null => false
0
+ t.string "commentable_type", :limit => 15, :default => "", :null => false
0
+ end
0
+ end
0
+
0
+ def self.down
0
+ drop_table :comments
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
27
28
29
0
@@ -1 +1,30 @@
0
+module ActionView
0
+ class Base
0
+
0
+ def method_missing(symbol, *args)
0
+ if symbol.to_s. =~ /_label$/
0
+ self.class.instance_eval do
0
+
0
+ return if method_defined? symbol
0
+
0
+ define_method(symbol){
0
+ options = args[1] || {}
0
+ options = args[0] if args[0].class == Hash
0
+
0
+ prompt_char = options[:prompt_char] || ':'
0
+ is_required = options[:is_required] || false
0
+ required_char = options[:required_char] || '*'
0
+ label_text = args[0].class == String ? args[0] : symbol.to_s[0..-7].titleize
0
+
0
+ label(:post, symbol.to_s.gsub(/_label/, ''), label_text) + (is_required ? required_char : '') + prompt_char
0
+ }
0
+
0
+ end
0
+
0
+ send symbol
0
+
0
+ end
0
+ end
0
+ end
0
+end
...
1
2
3
4
5
6
7
8
9
...
 
 
 
 
 
 
 
 
 
0
@@ -1,10 +1 @@
0
-class String
0
- def to_friendly_name
0
- s = self.gsub(/[_]/, ' ')
0
- ns = ""
0
- s.each("\s"){|w| ns << w.capitalize }
0
- return ns
0
- end
0
-
0
-end
...
34
35
36
37
 
38
39
40
41
42
43
44
45
46
47
48
49
50
51
...
55
56
57
58
 
59
60
61
62
63
64
 
 
 
 
 
 
65
66
67
68
...
89
90
91
 
92
93
94
95
96
97
98
99
100
101
...
108
109
110
111
112
113
114
115
 
116
117
118
...
147
148
149
 
 
 
 
 
 
 
150
...
34
35
36
 
37
38
39
40
 
 
 
 
 
 
 
41
42
43
44
...
48
49
50
 
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
...
88
89
90
91
92
93
 
 
 
 
 
94
95
96
...
103
104
105
 
 
 
 
 
106
107
108
109
...
138
139
140
141
142
143
144
145
146
147
148
0
@@ -34,17 +34,10 @@
0
 
0
 fieldset
0
 {
0
- border: 1px inset #999;
0
+ border: 1px dashed #999;
0
   padding: .5em;
0
 }
0
 
0
-legend
0
-{
0
- font-size: 1em;
0
- background: url('/images/icons/16-tool-a.png') no-repeat;
0
- padding-left:18px;
0
-}
0
-
0
 #main
0
 {
0
   margin-left: auto;
0
0
@@ -55,13 +48,19 @@
0
   margin-top: 1em;
0
 }
0
 
0
-#page-content
0
+#wrapper
0
 {
0
   float: left;
0
   width: 100%;
0
   padding-right: .75em;
0
 }
0
 
0
+#page-content
0
+{
0
+ margin-right: 310px;
0
+ padding-bottom: 1em;
0
+}
0
+
0
 #modules
0
 {
0
   margin-left: -310px;
0
0
@@ -89,13 +88,9 @@
0
   margin:55px 0 30px 15px;
0
   background: url('/images/logo.jpg') no-repeat;
0
   display:block;
0
+ text-indent: -9999px;
0
 }
0
 
0
-#header h1 em
0
-{
0
- display: none;
0
-}
0
-
0
 #footer
0
 {
0
   clear: both;
0
@@ -108,11 +103,7 @@
0
 {
0
   margin-bottom: 1em;
0
 }
0
-.post
0
-{
0
- margin-right: 310px;
0
- padding-bottom: 1em;
0
-}
0
+
0
 .post-content
0
 {
0
   text-align: left;
0
@@ -147,5 +138,12 @@
0
 {
0
   background: #fff url(/images/openid-logo.gif) no-repeat scroll 0pt 50%;
0
   padding-left: 18px;
0
+}
0
+
0
+div.post-content legend
0
+{
0
+ font-size: 1em;
0
+ background: url('/images/icons/16-tool-a.png') no-repeat;
0
+ padding-left:18px;
0
 }
...
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
0
@@ -1 +1,12 @@
0
+require File.dirname(__FILE__) + '/../spec_helper'
0
+
0
+describe SessionsHelper do
0
+
0
+ #Delete this example and add some real ones or delete this file
0
+ it "should include the SessionsHelper" do
0
+ included_modules = self.metaclass.send :included_modules
0
+ included_modules.should include(SessionsHelper)
0
+ end
0
+
0
+end
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0
@@ -1,19 +1 @@
0
-require File.dirname(__FILE__) + '/../spec_helper'
0
-
0
-describe Comment do
0
- it "should belong to a post" do
0
- Comment.should_receive(:belongs_to).with(:post)
0
- load 'comment.rb'
0
- end
0
-end
0
-
0
-describe Comment do
0
- before(:each) do
0
- @comment = Comment.new
0
- end
0
-
0
- it "should be valid" do
0
- @comment.should be_valid
0
- end
0
-end
...
2
3
4
5
 
6
7
8
...
29
30
31
32
 
33
34
35
...
2
3
4
 
5
6
7
8
...
29
30
31
 
32
33
34
35
0
@@ -2,7 +2,7 @@
0
 
0
 describe Post do
0
   it "should have many comments" do
0
- Post.should_receive(:has_many).with(:comments)
0
+ Post.should_receive(:acts_as_commentable)
0
     load 'post.rb'
0
   end
0
 end
0
@@ -29,7 +29,7 @@
0
     @post.save
0
     
0
     post = Post.find(:first)
0
- post.comments.size.should == 1
0
+ post.comments.length.should == 1
0
   end
0
   
0
   it "should replace new lines with <br> when setting body" do
...
18
19
20
21
22
23
24
25
26
27
28
29
 
30
31
32
...
18
19
20
 
21
22
23
24
25
26
 
 
27
28
29
30
0
@@ -18,15 +18,13 @@
0
   end
0
   
0
   it "should render add comment form" do
0
- pending "saving comments for a future sprint" do
0
       render "/posts/show"
0
       response.should have_tag("form#comment-submission") do
0
         response.should have_tag("label", "Name:")
0
         response.should have_tag("input#comment_name")
0
         response.should have_tag("input#comment_email")
0
         response.should have_tag("input#comment_url")
0
- response.should have_tag("textarea#comment_body")
0
- end
0
+ response.should have_tag("textarea#comment_comment")
0
     end
0
   end
0
 end
...
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
0
@@ -1 +1,8 @@
0
+* revision 8: Changed has_many :dependent => true to :dependent => :destroy for Rails 1.2.2
0
+ + Thanks Josh Martin
0
+ Added an order clause in the has_many relationship.
0
+ Made comment column type to text from string in migration example in README
0
+ + Thanks Patrick Crowley
0
+ Added this CHANGELOG file.
0
+
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
0
@@ -1 +1,21 @@
0
+Copyright (c) 2006 Cosmin Radoi
0
+
0
+Permission is hereby granted, free of charge, to any person obtaining
0
+a copy of this software and associated documentation files (the
0
+"Software"), to deal in the Software without restriction, including
0
+without limitation the rights to use, copy, modify, merge, publish,
0
+distribute, sublicense, and/or sell copies of the Software, and to
0
+permit persons to whom the Software is furnished to do so, subject to
0
+the following conditions:
0
+
0
+The above copyright notice and this permission notice shall be
0
+included in all copies or substantial portions of the Software.
0
+
0
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
0
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
0
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
0
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
0
@@ -1 +1,60 @@
0
+Acts As Commentable
0
+=================
0
+
0
+Allows for comments to be added to multiple and different models.
0
+
0
+== Resources
0
+
0
+Install
0
+ * Run the following command:
0
+
0
+ script/plugin install http://juixe.com/svn/acts_as_commentable
0
+
0
+ * Create a new rails migration and add the following self.up and self.down methods
0
+
0
+ def self.up
0
+ create_table "comments", :force => true do |t|
0
+ t.column "title", :string, :limit => 50, :default => ""
0
+ t.column "comment", :text, :default => ""
0
+ t.column "created_at", :datetime, :null => false
0
+ t.column "commentable_id", :integer, :default => 0, :null => false
0
+ t.column "commentable_type", :string, :limit => 15, :default => "", :null => false
0
+ t.column "user_id", :integer, :default => 0, :null => false
0
+ end
0
+
0
+ add_index "comments", ["user_id"], :name => "fk_comments_user"
0
+ end
0
+
0
+ def self.down
0
+ drop_table :comments
0
+ end
0
+
0
+== Usage
0
+
0
+ * Make you ActiveRecord model act as commentable.
0
+
0
+ class Model < ActiveRecord::Base
0
+ acts_as_commentable
0
+ end
0
+
0
+ * Add a comment to a model instance
0
+
0
+ model = Model.new
0
+ comment = Comment.new
0
+ comment.comment = 'Some comment'
0
+ model.comments << comment
0
+
0
+ * Each comment reference commentable object
0
+
0
+ model = Model.find(1)
0
+ model.comments.get(0).commtable == model
0
+
0
+== Credits
0
+
0
+Xelipe - This plugin is heavily influced by Acts As Tagglable.
0
+
0
+== More
0
+
0
+http://www.juixe.com/techknow/index.php/2006/06/18/acts-as-commentable-plugin/
0
+http://www.juixe.com/projects/acts_as_commentable
...
 
 
 
...
1
2
3
0
@@ -1 +1,4 @@
0
+# Include hook code here
0
+require 'acts_as_commentable'
0
+ActiveRecord::Base.send(:include, Juixe::Acts::Commentable)
...
 
...
1
0
@@ -1 +1,2 @@
0
+# Install hook code here
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
0
@@ -1 +1,63 @@
0
+# ActsAsCommentable
0
+module Juixe
0
+ module Acts #:nodoc:
0
+ module Commentable #:nodoc:
0
+
0
+ def self.included(base)
0
+ base.extend ClassMethods
0
+ end
0
+
0
+ module ClassMethods
0
+ def acts_as_commentable
0
+ has_many :comments, :as => :commentable, :dependent => :destroy, :order => 'created_at ASC'
0
+ include Juixe::Acts::Commentable::InstanceMethods
0
+ extend Juixe::Acts::Commentable::SingletonMethods
0
+ end
0
+ end
0
+
0
+ # This module contains class methods
0
+ module SingletonMethods
0
+ # Helper method to lookup for comments for a given object.
0
+ # This method is equivalent to obj.comments.
0
+ def find_comments_for(obj)
0
+ commentable = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
0
+
0
+ Comment.find(:all,
0
+ :conditions => ["commentable_id = ? and commentable_type = ?", obj.id, commentable],
0
+ :order => "created_at DESC"
0
+ )
0
+ end
0
+
0
+ # Helper class method to lookup comments for
0
+ # the mixin commentable type written by a given user.
0
+ # This method is NOT equivalent to Comment.find_comments_for_user
0
+ def find_comments_by_user(user)
0
+ commentable = ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s
0
+
0
+ Comment.find(:all,
0
+ :conditions => ["user_id = ? and commentable_type = ?", user.id, commentable],
0
+ :order => "created_at DESC"
0
+ )
0
+ end
0
+ end
0
+
0
+ # This module contains instance methods
0
+ module InstanceMethods
0
+ # Helper method to sort comments by date
0
+ def comments_ordered_by_submitted
0
+ Comment.find(:all,
0
+ :conditions => ["commentable_id = ? and commentable_type = ?", id, self.type.name],
0
+ :order => "created_at DESC"
0
+ )
0
+ end
0
+
0
+ # Helper method that defaults the submitted time.
0
+ def add_comment(comment)
0
+ comments << comment
0
+ end
0
+ end
0
+
0
+ end
0
+ end
0
+end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
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
27
28
29
30
31
32
33
34
0
@@ -1 +1,35 @@
0
+class Comment < ActiveRecord::Base
0
+ belongs_to :commentable, :polymorphic => true
0
+
0
+ # NOTE: install the acts_as_votable plugin if you
0
+ # want user to vote on the quality of comments.
0
+ #acts_as_voteable