Add task model

parent 040ae7e3
# == Schema Information
#
# Table name: tasks
#
# id :integer not null, primary key
# user_id :integer not null
# project_id :integer not null
# target_id :integer not null
# target_type :string not null
# author_id :integer
# action :integer
# state :string not null
# created_at :datetime
# updated_at :datetime
#
class Task < ActiveRecord::Base
belongs_to :author, class_name: "User"
belongs_to :project
belongs_to :target, polymorphic: true, touch: true
belongs_to :user
validates :action, :project, :target, :user, presence: true
state_machine :state, initial: :pending do
state :pending
state :done
end
end
......@@ -140,7 +140,7 @@ class User < ActiveRecord::Base
has_one :abuse_report, dependent: :destroy
has_many :spam_logs, dependent: :destroy
has_many :builds, dependent: :nullify, class_name: 'Ci::Build'
has_many :tasks, dependent: :destroy
#
# Validations
......
class CreateTasks < ActiveRecord::Migration
def change
create_table :tasks do |t|
t.references :user, null: false, index: true
t.references :project, null: false, index: true
t.references :target, polymorphic: true, null: false, index: true
t.integer :author_id, index: true
t.integer :action, null: false
t.string :state, null: false, index: true
t.timestamps
end
end
end
......@@ -824,6 +824,24 @@ ActiveRecord::Schema.define(version: 20160217100506) do
add_index "tags", ["name"], name: "index_tags_on_name", unique: true, using: :btree
create_table "tasks", force: :cascade do |t|
t.integer "user_id", null: false
t.integer "project_id", null: false
t.integer "target_id", null: false
t.string "target_type", null: false
t.integer "author_id"
t.integer "action", null: false
t.string "state", null: false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "tasks", ["author_id"], name: "index_tasks_on_author_id", using: :btree
add_index "tasks", ["project_id"], name: "index_tasks_on_project_id", using: :btree
add_index "tasks", ["state"], name: "index_tasks_on_state", using: :btree
add_index "tasks", ["target_type", "target_id"], name: "index_tasks_on_target_type_and_target_id", using: :btree
add_index "tasks", ["user_id"], name: "index_tasks_on_user_id", using: :btree
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
......
# == Schema Information
#
# Table name: tasks
#
# id :integer not null, primary key
# user_id :integer not null
# project_id :integer not null
# target_id :integer not null
# target_type :string not null
# author_id :integer
# action :integer
# state :string not null
# created_at :datetime
# updated_at :datetime
#
require 'spec_helper'
describe Task, models: true do
describe 'relationships' do
it { is_expected.to belong_to(:author).class_name("User") }
it { is_expected.to belong_to(:project) }
it { is_expected.to belong_to(:target).touch(true) }
it { is_expected.to belong_to(:user) }
end
describe 'validations' do
it { is_expected.to validate_presence_of(:action) }
it { is_expected.to validate_presence_of(:target) }
it { is_expected.to validate_presence_of(:user) }
end
end
......@@ -92,6 +92,7 @@ describe User, models: true do
it { is_expected.to have_many(:identities).dependent(:destroy) }
it { is_expected.to have_one(:abuse_report) }
it { is_expected.to have_many(:spam_logs).dependent(:destroy) }
it { is_expected.to have_many(:tasks).dependent(:destroy) }
end
describe 'validations' do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment