From b625144993f77547f102624f1b67b6b6cfcfc7b6 Mon Sep 17 00:00:00 2001 From: nilsding Date: Sat, 27 Dec 2014 14:35:09 +0100 Subject: [PATCH] added moderation models --- app/models/moderation_comment.rb | 8 +++++ app/models/moderation_vote.rb | 6 ++++ app/models/report.rb | 12 +++++++ app/models/reports/answer.rb | 2 ++ app/models/reports/comment.rb | 2 ++ app/models/reports/question.rb | 2 ++ app/models/reports/user.rb | 2 ++ app/models/user.rb | 5 +++ config/initializers/rails_admin.rb | 1 + db/migrate/20141227130438_create_reports.rb | 11 +++++++ .../20141227130545_create_moderation_votes.rb | 15 +++++++++ ...141227130618_create_moderation_comments.rb | 13 ++++++++ db/schema.rb | 32 ++++++++++++++++++- 13 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 app/models/moderation_comment.rb create mode 100644 app/models/moderation_vote.rb create mode 100644 app/models/report.rb create mode 100644 app/models/reports/answer.rb create mode 100644 app/models/reports/comment.rb create mode 100644 app/models/reports/question.rb create mode 100644 app/models/reports/user.rb create mode 100644 db/migrate/20141227130438_create_reports.rb create mode 100644 db/migrate/20141227130545_create_moderation_votes.rb create mode 100644 db/migrate/20141227130618_create_moderation_comments.rb diff --git a/app/models/moderation_comment.rb b/app/models/moderation_comment.rb new file mode 100644 index 00000000..da534d5c --- /dev/null +++ b/app/models/moderation_comment.rb @@ -0,0 +1,8 @@ +class ModerationComment < ActiveRecord::Base + belongs_to :user + belongs_to :report + validates :user_id, presence: true + validates :report_id, presence: true + + validates :content, length: { maximum: 160 } +end diff --git a/app/models/moderation_vote.rb b/app/models/moderation_vote.rb new file mode 100644 index 00000000..6e12c832 --- /dev/null +++ b/app/models/moderation_vote.rb @@ -0,0 +1,6 @@ +class ModerationVote < ActiveRecord::Base + belongs_to :user + belongs_to :report + validates :user_id, presence: true + validates :report_id, presence: true +end diff --git a/app/models/report.rb b/app/models/report.rb new file mode 100644 index 00000000..826d3ae7 --- /dev/null +++ b/app/models/report.rb @@ -0,0 +1,12 @@ +class Report < ActiveRecord::Base + belongs_to :user + has_many :moderation_votes, dependent: :destroy + has_many :moderation_comments, dependent: :destroy + validates :type, presence: true + validates :target_id, presence: true + validates :user_id, presence: true + + def target + type.sub('Reports::', '').constantize.where(id: target_id).first + end +end diff --git a/app/models/reports/answer.rb b/app/models/reports/answer.rb new file mode 100644 index 00000000..a127e4ef --- /dev/null +++ b/app/models/reports/answer.rb @@ -0,0 +1,2 @@ +class Reports::Answer < Report +end \ No newline at end of file diff --git a/app/models/reports/comment.rb b/app/models/reports/comment.rb new file mode 100644 index 00000000..d3f1de43 --- /dev/null +++ b/app/models/reports/comment.rb @@ -0,0 +1,2 @@ +class Reports::Comment < Report +end \ No newline at end of file diff --git a/app/models/reports/question.rb b/app/models/reports/question.rb new file mode 100644 index 00000000..2b7f924d --- /dev/null +++ b/app/models/reports/question.rb @@ -0,0 +1,2 @@ +class Reports::Question < Report +end \ No newline at end of file diff --git a/app/models/reports/user.rb b/app/models/reports/user.rb new file mode 100644 index 00000000..f15bcbcb --- /dev/null +++ b/app/models/reports/user.rb @@ -0,0 +1,2 @@ +class Reports::User < Report +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index 1181ff8b..dcb17ad9 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -22,6 +22,7 @@ class User < ActiveRecord::Base has_many :smiles has_many :services has_many :notifications, foreign_key: :recipient_id + has_many :reports, dependent: :destroy SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/ WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i @@ -131,4 +132,8 @@ class User < ActiveRecord::Base return true if self.moderator? or self.admin? false end + + def report(object) + Report.create(type: "Reports::#{object.class}", target_id: object.id, user_id: self.id) + end end diff --git a/config/initializers/rails_admin.rb b/config/initializers/rails_admin.rb index fbdc70e2..49048511 100644 --- a/config/initializers/rails_admin.rb +++ b/config/initializers/rails_admin.rb @@ -38,6 +38,7 @@ RailsAdmin.config do |config| Notification Question Relationship + Report Service Services::Twitter Smile diff --git a/db/migrate/20141227130438_create_reports.rb b/db/migrate/20141227130438_create_reports.rb new file mode 100644 index 00000000..257ab53a --- /dev/null +++ b/db/migrate/20141227130438_create_reports.rb @@ -0,0 +1,11 @@ +class CreateReports < ActiveRecord::Migration + def change + create_table :reports do |t| + t.string :type, null: false + t.integer :target_id, null: false + t.integer :user_id, null: false + + t.timestamps + end + end +end diff --git a/db/migrate/20141227130545_create_moderation_votes.rb b/db/migrate/20141227130545_create_moderation_votes.rb new file mode 100644 index 00000000..339d2e8a --- /dev/null +++ b/db/migrate/20141227130545_create_moderation_votes.rb @@ -0,0 +1,15 @@ +class CreateModerationVotes < ActiveRecord::Migration + def change + create_table :moderation_votes do |t| + t.integer :report_id, null: false + t.integer :user_id, null: false + t.boolean :upvote, null: false, default: false + + t.timestamps + end + + add_index :moderation_votes, :user_id + add_index :moderation_votes, :report_id + add_index :moderation_votes, [:user_id, :report_id], unique: true + end +end diff --git a/db/migrate/20141227130618_create_moderation_comments.rb b/db/migrate/20141227130618_create_moderation_comments.rb new file mode 100644 index 00000000..13741f39 --- /dev/null +++ b/db/migrate/20141227130618_create_moderation_comments.rb @@ -0,0 +1,13 @@ +class CreateModerationComments < ActiveRecord::Migration + def change + create_table :moderation_comments do |t| + t.integer :report_id + t.integer :user_id + t.string :content + + t.timestamps + end + + add_index :moderation_comments, [:user_id, :created_at] + end +end diff --git a/db/schema.rb b/db/schema.rb index 4a7662d1..6bf9a23c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20141226115905) do +ActiveRecord::Schema.define(version: 20141227130618) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -46,6 +46,28 @@ ActiveRecord::Schema.define(version: 20141226115905) do t.datetime "updated_at" end + create_table "moderation_comments", force: true do |t| + t.integer "report_id" + t.integer "user_id" + t.string "content" + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "moderation_comments", ["user_id", "created_at"], name: "index_moderation_comments_on_user_id_and_created_at", using: :btree + + create_table "moderation_votes", force: true do |t| + t.integer "report_id", null: false + t.integer "user_id", null: false + t.boolean "upvote", default: false, null: false + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "moderation_votes", ["report_id"], name: "index_moderation_votes_on_report_id", using: :btree + add_index "moderation_votes", ["user_id", "report_id"], name: "index_moderation_votes_on_user_id_and_report_id", unique: true, using: :btree + add_index "moderation_votes", ["user_id"], name: "index_moderation_votes_on_user_id", using: :btree + create_table "notifications", force: true do |t| t.string "target_type" t.integer "target_id" @@ -79,6 +101,14 @@ ActiveRecord::Schema.define(version: 20141226115905) do add_index "relationships", ["source_id"], name: "index_relationships_on_source_id", using: :btree add_index "relationships", ["target_id"], name: "index_relationships_on_target_id", using: :btree + create_table "reports", force: true do |t| + t.string "type", null: false + t.integer "target_id", null: false + t.integer "user_id", null: false + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "services", force: true do |t| t.string "type", null: false t.integer "user_id", null: false