Commit 770f43ad
Changed files (9)
app
controllers
models
views
shared
db
spec
controllers
routing
app/controllers/comments_controller.rb
@@ -1,3 +1,17 @@
class CommentsController < ApplicationController
+ def create
+ creation = Creation.find(creation_id)
+ @comment = current_user.comment_on(creation, comment_params[:text], comment_params[:id])
+ render json: @comment
+ end
+ private
+
+ def creation_id
+ params[:url].match(/\/(\d+)/)[0].gsub("/", "")
+ end
+
+ def comment_params
+ params.require(:comment).permit(:id, :text)
+ end
end
app/models/comment.rb
@@ -0,0 +1,4 @@
+class Comment < ActiveRecord::Base
+ belongs_to :user
+ belongs_to :creation
+end
app/models/creation.rb
@@ -4,6 +4,7 @@ class Creation < ActiveRecord::Base
has_and_belongs_to_many :categories, :join_table => 'creations_categories', :autosave => true
has_many :photos, -> { order :created_at }, :dependent => :destroy
has_many :favorites, :dependent => :destroy
+ has_many :comments, dependent: :destroy
acts_as_taggable
alias_method :author, :user
app/models/user.rb
@@ -18,6 +18,7 @@ class User < ActiveRecord::Base
has_many :favorites, :dependent => :destroy
has_many :tutorials, :dependent => :destroy
has_many :activities
+ has_many :comments
has_and_belongs_to_many :interests, :join_table => 'users_interests', :autosave => true
has_one :avatar
acts_as_tagger
@@ -60,6 +61,10 @@ class User < ActiveRecord::Base
activities.includes(:subject).order(created_at: :desc).limit(limit)
end
+ def comment_on(creation, text, disqus_id)
+ creation.comments.create(text: text, user: self, disqus_id: disqus_id)
+ end
+
class << self
def ordered
User.order(:creations_count => :desc)
db/migrate/20140119052825_create_comments.rb
@@ -0,0 +1,13 @@
+class CreateComments < ActiveRecord::Migration
+ def change
+ create_table :comments do |t|
+ t.integer :user_id
+ t.integer :creation_id
+ t.string :text
+ t.integer :disqus_id
+ end
+
+ add_index :comments, :user_id
+ add_index :comments, :creation_id
+ end
+end
db/schema.rb
@@ -11,11 +11,26 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20140118052950) do
+ActiveRecord::Schema.define(version: 20140119052825) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
+ create_table "active_admin_comments", force: true do |t|
+ t.integer "resource_id", null: false
+ t.string "resource_type", null: false
+ t.integer "author_id"
+ t.string "author_type"
+ t.text "body"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ t.string "namespace"
+ end
+
+ add_index "active_admin_comments", ["author_type", "author_id"], name: "index_active_admin_comments_on_author_type_and_author_id", using: :btree
+ add_index "active_admin_comments", ["namespace"], name: "index_active_admin_comments_on_namespace", using: :btree
+ add_index "active_admin_comments", ["resource_type", "resource_id"], name: "index_active_admin_comments_on_resource_type_and_resource_id", using: :btree
+
create_table "activities", force: true do |t|
t.integer "subject_id", null: false
t.string "subject_type", null: false
@@ -46,6 +61,16 @@ ActiveRecord::Schema.define(version: 20140118052950) do
t.string "slug"
end
+ create_table "comments", force: true do |t|
+ t.integer "user_id"
+ t.integer "creation_id"
+ t.string "text"
+ t.integer "disqus_id"
+ end
+
+ add_index "comments", ["creation_id"], name: "index_comments_on_creation_id", using: :btree
+ add_index "comments", ["user_id"], name: "index_comments_on_user_id", using: :btree
+
create_table "creations", force: true do |t|
t.string "name"
t.text "story"
spec/controllers/comments_controller_spec.rb
@@ -0,0 +1,20 @@
+require "spec_helper"
+
+describe CommentsController do
+ context "#create" do
+ let(:creation) { create(:creation) }
+ let(:user) { create(:user) }
+
+ before :each do
+ http_login(user)
+ end
+
+ it "creates a new comment" do
+ post :create, id: 1207743539, url: "http://localhost:3000/creations/#{creation.to_param}", comment: { id: 1207743539, text: 'very nice' }
+ comment = Comment.last
+ comment.disqus_id.should == 1207743539
+ comment.creation.should == creation
+ comment.text.should == 'very nice'
+ end
+ end
+end
spec/routing/comments_routing_spec.rb
@@ -1,6 +1,5 @@
require "spec_helper"
-
describe "/comments" do
it "routes to the create action" do
expect({ :post => '/comments' }).to route_to(controller: 'comments', action: 'create')