Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

parrot comments form partial

  • Loading branch information...
commit 151171a3483c07cea48a36238e45cc3acdaf45d1 1 parent ee3d416
Tute Costa authored
1  .gitignore
@@ -3,4 +3,5 @@
3 3 .bundle
4 4 Gemfile.lock
5 5 rdoc
  6 +test/dummy/log*
6 7 test/dummy/tmp
23 app/controllers/parrot/application_controller.rb
... ... @@ -0,0 +1,23 @@
  1 +module Parrot
  2 + class ApplicationController < ActionController::Base
  3 + def commentable_fk
  4 + commentable_fk = params.select{|k,v| k =~ /_id/ }.keys.first
  5 + end
  6 +
  7 + def commentable_type
  8 + commentable_fk.gsub('_id', '')
  9 + end
  10 +
  11 + def commentable_id
  12 + params[commentable_fk]
  13 + end
  14 +
  15 + def commentable
  16 + commentable_type.capitalize.constantize.find(commentable_id)
  17 + end
  18 +
  19 + def debug(object)
  20 + logger.warn "DEBUGGING: #{object.inspect}"
  21 + end
  22 + end
  23 +end
20 app/controllers/parrot/comments_controller.rb
... ... @@ -1,5 +1,5 @@
1 1 module Parrot
2   - class CommentsController < ActionController::Base
  2 + class CommentsController < ApplicationController
3 3 respond_to :html
4 4
5 5 def index
@@ -23,23 +23,5 @@ def create
23 23 @commentable_type = commentable_type
24 24 respond_with @comment, :location => commentable
25 25 end
26   -
27   - private
28   - def commentable_fk
29   - commentable_fk = params.select{|k,v| k =~ /_id/ }.keys.first
30   - end
31   - def commentable_type
32   - commentable_fk.gsub('_id', '')
33   - end
34   - def commentable_id
35   - params[commentable_fk]
36   - end
37   - def commentable
38   - commentable_type.capitalize.constantize.find(commentable_id)
39   - end
40   -
41   - def debug(object)
42   - logger.warn "DEBUGGING: #{object.inspect}"
43   - end
44 26 end
45 27 end
4 app/models/parrot/comment.rb
@@ -12,5 +12,9 @@ class Comment < ActiveRecord::Base
12 12 def self.following(other_comment)
13 13 where("created_at > '#{other_comment.created_at}'")
14 14 end
  15 +
  16 + def author
  17 + Parrot.author_class.find(author_id)
  18 + end
15 19 end
16 20 end
3  app/views/parrot/comments/_comment.html.erb
... ... @@ -1,2 +1 @@
1   -<%= comment.inspect %>
2   -<hr>
  1 +<p><strong><%= comment.author %></strong>: <%= comment.body %></p>
22 app/views/parrot/comments/_form.html.erb
... ... @@ -0,0 +1,22 @@
  1 +<% @comment ||= Parrot::Comment.new %>
  2 +<%= form_for([object, @comment]) do |f| %>
  3 + <% if @comment.errors.any? %>
  4 + <div id="error_explanation">
  5 + <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>
  6 +
  7 + <ul>
  8 + <% @comment.errors.full_messages.each do |msg| %>
  9 + <li><%= msg %></li>
  10 + <% end %>
  11 + </ul>
  12 + </div>
  13 + <% end %>
  14 +
  15 + <div class="field">
  16 + <%= f.label :body %><br />
  17 + <%= f.text_area :body %>
  18 + </div>
  19 + <div class="actions">
  20 + <%= f.submit %>
  21 + </div>
  22 +<% end %>
22 app/views/parrot/comments/new.html.erb
... ... @@ -1,21 +1 @@
1   -<%= form_for([@commentable_type, @comment]) do |f| %>
2   - <% if @comment.errors.any? %>
3   - <div id="error_explanation">
4   - <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>
5   -
6   - <ul>
7   - <% @comment.errors.full_messages.each do |msg| %>
8   - <li><%= msg %></li>
9   - <% end %>
10   - </ul>
11   - </div>
12   - <% end %>
13   -
14   - <div class="field">
15   - <%= f.label :body %><br />
16   - <%= f.text_area :body %>
17   - </div>
18   - <div class="actions">
19   - <%= f.submit %>
20   - </div>
21   -<% end %>
  1 +<%= render 'form', object: @commentable_type %>
2  lib/parrot.rb
@@ -8,7 +8,7 @@ def self.author_class
8 8
9 9 class Engine < Rails::Engine
10 10 engine_name 'parrot'
11   - isolate_namespace Parrot
  11 + # isolate_namespace Parrot # FIXME: tries application url helpers (undefined method `post_comments_path')
12 12
13 13 initializer 'parrot' do |app|
14 14 ActiveSupport.on_load(:active_record) do
4 test/dummy/app/models/user.rb
@@ -7,4 +7,8 @@ class User < ActiveRecord::Base
7 7 # Setup accessible (or protected) attributes for your model
8 8 attr_accessible :email, :password, :password_confirmation, :remember_me
9 9 # attr_accessible :title, :body
  10 +
  11 + def to_s
  12 + email.split('@').first
  13 + end
10 14 end
1  test/dummy/app/views/posts/show.html.erb
@@ -11,6 +11,7 @@
11 11 <hr>
12 12 <h2>Comments</h2>
13 13 <%= render @post.comments %>
  14 +<%= render 'parrot/comments/form', :object => @post %>
14 15
15 16 <%= link_to 'Edit', edit_post_path(@post) %> |
16 17 <%= link_to 'Back', posts_path %>
BIN  test/dummy/db/development.sqlite3
Binary file not shown

0 comments on commit 151171a

Please sign in to comment.
Something went wrong with that request. Please try again.