configures act_as_commentable_with_threading gem [#7]

This commit is contained in:
rgarcia
2015-07-18 17:38:33 +02:00
parent 3ee102c0c5
commit 05705f3174
6 changed files with 68 additions and 2 deletions

View File

@@ -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'

View File

@@ -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)

19
app/models/comment.rb Normal file
View File

@@ -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

View File

@@ -1,4 +1,6 @@
class Debate < ActiveRecord::Base
acts_as_commentable
validates :title, presence: true
validates :description, presence: true

View File

@@ -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

View File

@@ -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"