diff --git a/app/models/repository_list_item.rb b/app/models/repository_list_item.rb new file mode 100644 index 000000000..ef6837957 --- /dev/null +++ b/app/models/repository_list_item.rb @@ -0,0 +1,6 @@ +class RepositoryListItem < ApplicationRecord + belongs_to :repository_list_value + validates :name, + presence: true, + length: { maximum: Constants::TEXT_MAX_LENGTH } +end diff --git a/app/models/repository_list_value.rb b/app/models/repository_list_value.rb new file mode 100644 index 000000000..0e75e0e90 --- /dev/null +++ b/app/models/repository_list_value.rb @@ -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 diff --git a/config/initializers/extends.rb b/config/initializers/extends.rb index 709c25017..96851f277 100644 --- a/config/initializers/extends.rb +++ b/config/initializers/extends.rb @@ -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'] diff --git a/db/migrate/20180207095200_create_repository_list_values.rb b/db/migrate/20180207095200_create_repository_list_values.rb new file mode 100644 index 000000000..1bd52070e --- /dev/null +++ b/db/migrate/20180207095200_create_repository_list_values.rb @@ -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 diff --git a/db/migrate/20180207102347_create_repository_list_items.rb b/db/migrate/20180207102347_create_repository_list_items.rb new file mode 100644 index 000000000..33411987f --- /dev/null +++ b/db/migrate/20180207102347_create_repository_list_items.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 977909a28..293f8e720 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/spec/factories/repository_list_items.rb b/spec/factories/repository_list_items.rb new file mode 100644 index 000000000..7fe4b1e74 --- /dev/null +++ b/spec/factories/repository_list_items.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory :repository_list_item do + repository_list_value nil + name '' + end +end diff --git a/spec/factories/repository_list_values.rb b/spec/factories/repository_list_values.rb new file mode 100644 index 000000000..c55e3b029 --- /dev/null +++ b/spec/factories/repository_list_values.rb @@ -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 diff --git a/spec/models/repository_list_item_spec.rb b/spec/models/repository_list_item_spec.rb new file mode 100644 index 000000000..cf6807a47 --- /dev/null +++ b/spec/models/repository_list_item_spec.rb @@ -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 diff --git a/spec/models/repository_list_value_spec.rb b/spec/models/repository_list_value_spec.rb new file mode 100644 index 000000000..f26f42ffd --- /dev/null +++ b/spec/models/repository_list_value_spec.rb @@ -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