added moderation models

This commit is contained in:
nilsding 2014-12-27 14:35:09 +01:00
parent 1a43ae0054
commit b625144993
13 changed files with 110 additions and 1 deletions

View File

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

View File

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

12
app/models/report.rb Normal file
View File

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

View File

@ -0,0 +1,2 @@
class Reports::Answer < Report
end

View File

@ -0,0 +1,2 @@
class Reports::Comment < Report
end

View File

@ -0,0 +1,2 @@
class Reports::Question < Report
end

View File

@ -0,0 +1,2 @@
class Reports::User < Report
end

View File

@ -22,6 +22,7 @@ class User < ActiveRecord::Base
has_many :smiles has_many :smiles
has_many :services has_many :services
has_many :notifications, foreign_key: :recipient_id has_many :notifications, foreign_key: :recipient_id
has_many :reports, dependent: :destroy
SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/ SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/
WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i
@ -131,4 +132,8 @@ class User < ActiveRecord::Base
return true if self.moderator? or self.admin? return true if self.moderator? or self.admin?
false false
end end
def report(object)
Report.create(type: "Reports::#{object.class}", target_id: object.id, user_id: self.id)
end
end end

View File

@ -38,6 +38,7 @@ RailsAdmin.config do |config|
Notification Notification
Question Question
Relationship Relationship
Report
Service Service
Services::Twitter Services::Twitter
Smile Smile

View File

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

View File

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

View File

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

View File

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -46,6 +46,28 @@ ActiveRecord::Schema.define(version: 20141226115905) do
t.datetime "updated_at" t.datetime "updated_at"
end 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| create_table "notifications", force: true do |t|
t.string "target_type" t.string "target_type"
t.integer "target_id" 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", ["source_id"], name: "index_relationships_on_source_id", using: :btree
add_index "relationships", ["target_id"], name: "index_relationships_on_target_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| create_table "services", force: true do |t|
t.string "type", null: false t.string "type", null: false
t.integer "user_id", null: false t.integer "user_id", null: false