blogmodule 0.0.1 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,10 @@
1
+ class CommentsController < ApplicationController
2
+
3
+ def create
4
+ @post = Post.find(params[:post_id])
5
+ @comment = @post.comments.build(params[:comment])
6
+ flash[:alert] = 'Fail to receive the comment. Double check the fields' unless @comment.save
7
+
8
+ redirect_to @post
9
+ end
10
+ end
@@ -0,0 +1,2 @@
1
+ module CommentsHelper
2
+ end
@@ -0,0 +1,9 @@
1
+ class Comment < ActiveRecord::Base
2
+ belongs_to :post
3
+
4
+
5
+ with_options :allow_blank => true do |c|
6
+ c.validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
7
+ c.validates_format_of :url, :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/ix
8
+ end
9
+ end
data/app/models/post.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  class Post < ActiveRecord::Base
2
2
 
3
+ has_many :comments, :dependent => :destroy
4
+
3
5
  has_and_belongs_to_many :categories
4
6
 
5
7
  validates_uniqueness_of :title
@@ -0,0 +1,6 @@
1
+ <%= div_for comment, :class => "comment" do %>
2
+
3
+ Posted <%= time_ago_in_words comment.created_at %> ago <br />
4
+ <%= link_to comment.author, comment.url, :target => '_blank' %> <br />
5
+ <%= comment.body %>
6
+ <% end %>
@@ -0,0 +1,38 @@
1
+ <%= form_for [post, comment] do |f| %>
2
+
3
+ <% if comment.errors.any? %>
4
+ <div id="error_explanation">
5
+ <h2><%= pluralize(comment.errors.count, "error") %> prohibited this post 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 :author %><br />
17
+ <%= f.text_field :author %>
18
+ </div>
19
+
20
+ <div class="field">
21
+ <%= f.label :email %><br />
22
+ <%= f.text_field :email %>
23
+ </div>
24
+
25
+ <div class="field">
26
+ <%= f.label :url %><br />
27
+ <%= f.text_field :url %>
28
+ </div>
29
+
30
+ <div class="field">
31
+ <%= f.label :body %><br />
32
+ <%= f.text_area :body, :rows => 10, :cols => 90 %>
33
+ </div>
34
+
35
+ <div class="actions">
36
+ <%= f.submit %>
37
+ </div>
38
+ <% end %>
@@ -1,10 +1,5 @@
1
1
  <p id="notice"><%= notice %></p>
2
2
 
3
- <p>
4
- <b>Indexpage:</b>
5
- <%= @post.published %>
6
- </p>
7
-
8
3
  <p>
9
4
  <b>Title:</b>
10
5
  <%= @post.title %>
@@ -20,8 +15,9 @@
20
15
  <%= @post.body %>
21
16
  </p>
22
17
 
23
-
24
-
18
+ <hr />
19
+ <%= render @post.comments || "Nobody commented this post yet." %>
20
+ <%= render "comments/form", :post => @post, :comment => Comment.new %>
25
21
 
26
22
  <%= link_to 'Edit', edit_post_path(@post) %> |
27
23
  <%= link_to 'Back', posts_path %>
data/config/routes.rb CHANGED
@@ -1,5 +1,9 @@
1
1
  Rails.application.routes.draw do
2
- resources :posts
2
+ get "comments/create"
3
+
4
+ resources :posts do
5
+ resource :comments
6
+ end
3
7
 
4
8
  resources :categories
5
9
 
@@ -0,0 +1,17 @@
1
+ class CreateComments < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :comments do |t|
4
+ t.string :author
5
+ t.string :email
6
+ t.string :url
7
+ t.text :body
8
+ t.references :post
9
+
10
+ t.timestamps
11
+ end
12
+ end
13
+
14
+ def self.down
15
+ drop_table :comments
16
+ end
17
+ end
@@ -1,3 +1,3 @@
1
1
  module Blogmodule
2
- VERSION = "0.0.1"
2
+ VERSION = "1.0.0"
3
3
  end
Binary file
@@ -11,7 +11,7 @@
11
11
  #
12
12
  # It's strongly recommended to check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(:version => 20111231183528) do
14
+ ActiveRecord::Schema.define(:version => 20120102163952) do
15
15
 
16
16
  create_table "categories", :force => true do |t|
17
17
  t.string "name"
@@ -24,6 +24,16 @@ ActiveRecord::Schema.define(:version => 20111231183528) do
24
24
  t.integer "category_id", :null => false
25
25
  end
26
26
 
27
+ create_table "comments", :force => true do |t|
28
+ t.string "author"
29
+ t.string "email"
30
+ t.string "url"
31
+ t.text "body"
32
+ t.integer "post_id"
33
+ t.datetime "created_at"
34
+ t.datetime "updated_at"
35
+ end
36
+
27
37
  create_table "posts", :force => true do |t|
28
38
  t.boolean "published"
29
39
  t.datetime "date"