diff --git a/Gemfile b/Gemfile index 45f16f7b4..d393a76b3 100644 --- a/Gemfile +++ b/Gemfile @@ -25,7 +25,7 @@ gem 'sdoc', '~> 0.4.0', group: :doc gem 'devise' # Use ActiveModel has_secure_password # gem 'bcrypt', '~> 3.1.7' - +gem 'acts_as_commentable_with_threading' # Use Unicorn as the app server # gem 'unicorn' diff --git a/Gemfile.lock b/Gemfile.lock index 01408b9c2..f9cb656a5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -36,7 +36,13 @@ GEM minitest (~> 5.1) thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) + acts_as_commentable_with_threading (2.0.0) + activerecord (>= 4.0) + activesupport (>= 4.0) + awesome_nested_set (>= 3.0) arel (6.0.2) + awesome_nested_set (3.0.2) + activerecord (>= 4.0.0, < 5) bcrypt (3.1.10) binding_of_caller (0.7.2) debug_inspector (>= 0.0.1) @@ -188,6 +194,7 @@ PLATFORMS ruby DEPENDENCIES + acts_as_commentable_with_threading byebug capybara coffee-rails (~> 4.1.0) diff --git a/app/models/comment.rb b/app/models/comment.rb new file mode 100644 index 000000000..3f8dd83f4 --- /dev/null +++ b/app/models/comment.rb @@ -0,0 +1,19 @@ +class Comment < ActiveRecord::Base + acts_as_nested_set :scope => [:commentable_id, :commentable_type] + + validates :body, :presence => true + validates :user, :presence => true + + belongs_to :commentable, :polymorphic => true + belongs_to :user + + def self.build(commentable, user, body) + new commentable: commentable, + user_id: user.id, + body: body + end + + def self.find_parent(params) + params[:commentable_type].constantize.find(params[:commentable_id]) + end +end diff --git a/app/models/debate.rb b/app/models/debate.rb index c66913c59..5a11c02cb 100644 --- a/app/models/debate.rb +++ b/app/models/debate.rb @@ -1,4 +1,6 @@ class Debate < ActiveRecord::Base + acts_as_commentable + validates :title, presence: true validates :description, presence: true diff --git a/db/migrate/20150718091702_acts_as_commentable_with_threading_migration.rb b/db/migrate/20150718091702_acts_as_commentable_with_threading_migration.rb new file mode 100644 index 000000000..728b26b9e --- /dev/null +++ b/db/migrate/20150718091702_acts_as_commentable_with_threading_migration.rb @@ -0,0 +1,21 @@ +class ActsAsCommentableWithThreadingMigration < ActiveRecord::Migration + def self.up + create_table :comments, :force => true do |t| + t.integer :commentable_id + t.string :commentable_type + t.string :title + t.text :body + t.string :subject + t.integer :user_id, :null => false + t.integer :parent_id, :lft, :rgt + t.timestamps + end + + add_index :comments, :user_id + add_index :comments, [:commentable_id, :commentable_type] + end + + def self.down + drop_table :comments + end +end diff --git a/db/schema.rb b/db/schema.rb index 61fb86a58..56966e1dc 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,24 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150716174358) do +ActiveRecord::Schema.define(version: 20150718091702) do + + create_table "comments", force: :cascade do |t| + t.integer "commentable_id" + t.string "commentable_type" + t.string "title" + t.text "body" + t.string "subject" + t.integer "user_id", null: false + t.integer "parent_id" + t.integer "lft" + t.integer "rgt" + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "comments", ["commentable_id", "commentable_type"], name: "index_comments_on_commentable_id_and_commentable_type" + add_index "comments", ["user_id"], name: "index_comments_on_user_id" create_table "debates", force: :cascade do |t| t.string "title"