setup models for repository list column [fixes SCI-2015]

This commit is contained in:
zmagod 2018-02-07 15:28:28 +01:00
parent 4389c110cf
commit 6b7ff11bcf
10 changed files with 177 additions and 2 deletions

View file

@ -0,0 +1,6 @@
class RepositoryListItem < ApplicationRecord
belongs_to :repository_list_value
validates :name,
presence: true,
length: { maximum: Constants::TEXT_MAX_LENGTH }
end

View file

@ -0,0 +1,21 @@
class RepositoryListValue < ApplicationRecord
has_many :repository_list_items, dependent: :destroy
belongs_to :created_by,
foreign_key: :created_by_id,
class_name: 'User',
optional: true
belongs_to :last_modified_by,
foreign_key: :last_modified_by_id,
class_name: 'User',
optional: true
has_one :repository_cell, as: :value, dependent: :destroy, inverse_of: :value
accepts_nested_attributes_for :repository_cell
validates :repository_cell, presence: true
def formated
item = repository_list_items.find_by_id(selected_item)
return '' unless item
item.name
end
end

View file

@ -41,7 +41,8 @@ class Extends
# Data type name should match corresponding model's name
REPOSITORY_DATA_TYPES = { RepositoryTextValue: 0,
RepositoryDateValue: 1 }
RepositoryDateValue: 1,
RepositoryListValue: 2 }
# List of implemented core API versions
API_VERSIONS = ['20170715']

View file

@ -0,0 +1,14 @@
class CreateRepositoryListValues < ActiveRecord::Migration[5.1]
def change
create_table :repository_list_values do |t|
t.bigint :selected_item
t.references :created_by,
index: true,
foreign_key: { to_table: :users }
t.references :last_modified_by,
index: true,
oreign_key: { to_table: :users }
t.timestamps
end
end
end

View file

@ -0,0 +1,9 @@
class CreateRepositoryListItems < ActiveRecord::Migration[5.1]
def change
create_table :repository_list_items do |t|
t.references :repository_list_value, foreign_key: true
t.text :name, index: true, using: :gin, null: false
t.timestamps
end
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20171026090804) do
ActiveRecord::Schema.define(version: 20180207102347) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -394,6 +394,25 @@ ActiveRecord::Schema.define(version: 20171026090804) do
t.integer "last_modified_by_id", null: false
end
create_table "repository_list_items", force: :cascade do |t|
t.bigint "repository_list_value_id"
t.text "name", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["name"], name: "index_repository_list_items_on_name"
t.index ["repository_list_value_id"], name: "index_repository_list_items_on_repository_list_value_id"
end
create_table "repository_list_values", force: :cascade do |t|
t.bigint "selected_item"
t.bigint "created_by_id"
t.bigint "last_modified_by_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["created_by_id"], name: "index_repository_list_values_on_created_by_id"
t.index ["last_modified_by_id"], name: "index_repository_list_values_on_last_modified_by_id"
end
create_table "repository_rows", id: :serial, force: :cascade do |t|
t.integer "repository_id"
t.integer "created_by_id", null: false
@ -854,6 +873,8 @@ ActiveRecord::Schema.define(version: 20171026090804) do
add_foreign_key "repository_columns", "users", column: "created_by_id"
add_foreign_key "repository_date_values", "users", column: "created_by_id"
add_foreign_key "repository_date_values", "users", column: "last_modified_by_id"
add_foreign_key "repository_list_items", "repository_list_values"
add_foreign_key "repository_list_values", "users", column: "created_by_id"
add_foreign_key "repository_rows", "users", column: "created_by_id"
add_foreign_key "repository_rows", "users", column: "last_modified_by_id"
add_foreign_key "repository_text_values", "users", column: "created_by_id"

View file

@ -0,0 +1,6 @@
FactoryBot.define do
factory :repository_list_item do
repository_list_value nil
name ''
end
end

View file

@ -0,0 +1,6 @@
FactoryBot.define do
factory :repository_list_value do
created_by { User.first || association(:project_user) }
last_modified_by { User.first || association(:project_user) }
end
end

View file

@ -0,0 +1,23 @@
require 'rails_helper'
RSpec.describe RepositoryListItem, type: :model do
it 'should be of class RepositoryListItem' do
expect(subject.class).to eq RepositoryListItem
end
describe 'Database table' do
it { should have_db_column :name }
it { should have_db_column :repository_list_value_id }
end
describe 'Relations' do
it { should belong_to(:repository_list_value) }
end
describe 'Validations' do
it { should validate_presence_of(:name) }
it do
should validate_length_of(:name).is_at_most(Constants::TEXT_MAX_LENGTH)
end
end
end

View file

@ -0,0 +1,68 @@
require 'rails_helper'
RSpec.describe RepositoryListValue, type: :model do
it 'should be of class RepositoryListValue' do
expect(subject.class).to eq RepositoryListValue
end
describe 'Database table' do
it { should have_db_column :created_by_id }
it { should have_db_column :last_modified_by_id }
it { should have_db_column :selected_item }
end
describe 'Relations' do
it { should belong_to(:created_by).class_name('User') }
it { should belong_to(:last_modified_by).class_name('User') }
it { should have_many :repository_list_items }
it { should accept_nested_attributes_for(:repository_cell) }
end
describe '#data' do
let!(:repository_column) do
create :repository_column, data_type: :RepositoryListValue
end
let!(:repository_row) { create :repository_row, name: 'My row' }
let!(:repository_list_value) do
create :repository_list_value, repository_cell_attributes: {
repository_column: repository_column,
repository_row: repository_row
}
end
it 'returns the name of a selected item' do
list_item = create :repository_list_item,
repository_list_value: repository_list_value,
name: 'my item'
repository_list_value.update_attribute(:selected_item, list_item.id)
expect(repository_list_value.reload.formated).to eq 'my item'
end
it 'retuns only the the item related to the list' do
repository_row_two = create :repository_row, name: 'New row'
repository_list_value_two =
create :repository_list_value, repository_cell_attributes: {
repository_column: repository_column,
repository_row: repository_row_two
}
list_item = create :repository_list_item,
repository_list_value: repository_list_value_two,
name: 'new item'
repository_list_value.update_attribute(:selected_item, list_item.id)
expect(repository_list_value.reload.formated).to_not eq 'my item'
expect(repository_list_value.formated).to eq ''
end
it 'returns an empty string if no item selected' do
list_item = create :repository_list_item,
repository_list_value: repository_list_value,
name: 'my item'
expect(repository_list_value.reload.formated).to eq ''
end
it 'returns an empty string if item does not exists' do
repository_list_value.update_attribute(:selected_item, 9999999999)
expect(repository_list_value.reload.formated).to eq ''
end
end
end