diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-02-12 11:17:42 -0200 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-02-20 12:10:26 -0200 |
commit | 41d8f5649e3c8a1e37be4608fd03153005c3fa58 (patch) | |
tree | 1a76ece3e4bab3327539f4417d911d3656444212 | |
parent | 040ae7e3e0c6f4d97fe6366bff9b05831283db7e (diff) | |
download | gitlab-ce-41d8f5649e3c8a1e37be4608fd03153005c3fa58.tar.gz |
Add task model
-rw-r--r-- | app/models/task.rb | 29 | ||||
-rw-r--r-- | app/models/user.rb | 2 | ||||
-rw-r--r-- | db/migrate/20160212123307_create_tasks.rb | 14 | ||||
-rw-r--r-- | db/schema.rb | 18 | ||||
-rw-r--r-- | spec/models/task_spec.rb | 32 | ||||
-rw-r--r-- | spec/models/user_spec.rb | 1 |
6 files changed, 95 insertions, 1 deletions
diff --git a/app/models/task.rb b/app/models/task.rb new file mode 100644 index 00000000000..5f102dd66b8 --- /dev/null +++ b/app/models/task.rb @@ -0,0 +1,29 @@ +# == 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 diff --git a/app/models/user.rb b/app/models/user.rb index 9fe94b13e52..d108ba78e4b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/db/migrate/20160212123307_create_tasks.rb b/db/migrate/20160212123307_create_tasks.rb new file mode 100644 index 00000000000..c3f6f3abc26 --- /dev/null +++ b/db/migrate/20160212123307_create_tasks.rb @@ -0,0 +1,14 @@ +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 diff --git a/db/schema.rb b/db/schema.rb index af5bac63b42..183227a91ca 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 diff --git a/spec/models/task_spec.rb b/spec/models/task_spec.rb new file mode 100644 index 00000000000..950d9b74196 --- /dev/null +++ b/spec/models/task_spec.rb @@ -0,0 +1,32 @@ +# == 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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 47ce409fe4b..d2769836526 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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 |