mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-10-02 01:45:38 +08:00
follow @okriuchykhin 's suggestions
This commit is contained in:
parent
557aeca049
commit
ae99c92516
7 changed files with 31 additions and 35 deletions
|
@ -1,6 +1,5 @@
|
|||
class RepositoryListItem < ApplicationRecord
|
||||
has_many :repository_list_values,
|
||||
foreign_key: 'selected_item_id'
|
||||
has_many :repository_list_values, inverse_of: :repository_list_item
|
||||
belongs_to :repository, inverse_of: :repository_list_items
|
||||
belongs_to :created_by,
|
||||
foreign_key: :created_by_id,
|
||||
|
@ -8,7 +7,7 @@ class RepositoryListItem < ApplicationRecord
|
|||
belongs_to :last_modified_by,
|
||||
foreign_key: :last_modified_by_id,
|
||||
class_name: 'User'
|
||||
validates :name,
|
||||
validates :data,
|
||||
presence: true,
|
||||
uniqueness: { scope: :repository, case_sensitive: false },
|
||||
length: { maximum: Constants::TEXT_MAX_LENGTH }
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
class RepositoryListValue < ApplicationRecord
|
||||
belongs_to :selected_item,
|
||||
foreign_key: :selected_item_id,
|
||||
class_name: 'RepositoryListItem',
|
||||
belongs_to :repository_list_item,
|
||||
optional: true
|
||||
belongs_to :created_by,
|
||||
foreign_key: :created_by_id,
|
||||
|
@ -15,7 +13,7 @@ class RepositoryListValue < ApplicationRecord
|
|||
validates :repository_cell, presence: true
|
||||
|
||||
def formatted
|
||||
return '' unless selected_item
|
||||
selected_item.name
|
||||
return '' unless repository_list_item
|
||||
repository_list_item.data
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,7 +2,7 @@ class CreateRepositoryListValues < ActiveRecord::Migration[5.1]
|
|||
def change
|
||||
create_table :repository_list_items do |t|
|
||||
t.references :repository, foreign_key: true
|
||||
t.text :name, index: true, using: :gin, null: false
|
||||
t.text :data, index: true, using: :gin, null: false
|
||||
t.references :created_by,
|
||||
index: true,
|
||||
foreign_key: { to_table: :users }
|
||||
|
@ -13,9 +13,7 @@ class CreateRepositoryListValues < ActiveRecord::Migration[5.1]
|
|||
end
|
||||
|
||||
create_table :repository_list_values do |t|
|
||||
t.references :selected_item,
|
||||
index: true,
|
||||
foreign_key: { to_table: :repository_list_items }
|
||||
t.references :repository_list_item, index: true
|
||||
t.references :created_by,
|
||||
index: true,
|
||||
foreign_key: { to_table: :users }
|
||||
|
|
|
@ -396,26 +396,26 @@ ActiveRecord::Schema.define(version: 20180207095200) do
|
|||
|
||||
create_table "repository_list_items", force: :cascade do |t|
|
||||
t.bigint "repository_id"
|
||||
t.text "name", null: false
|
||||
t.text "data", null: false
|
||||
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_items_on_created_by_id"
|
||||
t.index ["data"], name: "index_repository_list_items_on_data"
|
||||
t.index ["last_modified_by_id"], name: "index_repository_list_items_on_last_modified_by_id"
|
||||
t.index ["name"], name: "index_repository_list_items_on_name"
|
||||
t.index ["repository_id"], name: "index_repository_list_items_on_repository_id"
|
||||
end
|
||||
|
||||
create_table "repository_list_values", force: :cascade do |t|
|
||||
t.bigint "selected_item_id"
|
||||
t.bigint "repository_list_item_id"
|
||||
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"
|
||||
t.index ["selected_item_id"], name: "index_repository_list_values_on_selected_item_id"
|
||||
t.index ["repository_list_item_id"], name: "index_repository_list_values_on_repository_list_item_id"
|
||||
end
|
||||
|
||||
create_table "repository_rows", id: :serial, force: :cascade do |t|
|
||||
|
@ -881,7 +881,6 @@ ActiveRecord::Schema.define(version: 20180207095200) do
|
|||
add_foreign_key "repository_list_items", "repositories"
|
||||
add_foreign_key "repository_list_items", "users", column: "created_by_id"
|
||||
add_foreign_key "repository_list_items", "users", column: "last_modified_by_id"
|
||||
add_foreign_key "repository_list_values", "repository_list_items", column: "selected_item_id"
|
||||
add_foreign_key "repository_list_values", "users", column: "created_by_id"
|
||||
add_foreign_key "repository_list_values", "users", column: "last_modified_by_id"
|
||||
add_foreign_key "repository_rows", "users", column: "created_by_id"
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
FactoryBot.define do
|
||||
factory :repository_list_item do
|
||||
name ''
|
||||
data ''
|
||||
repository { Repository.first || create(:repository) }
|
||||
created_by { User.first || association(:project_user) }
|
||||
last_modified_by { User.first || association(:project_user) }
|
||||
|
|
|
@ -6,8 +6,10 @@ RSpec.describe RepositoryListItem, type: :model do
|
|||
end
|
||||
|
||||
describe 'Database table' do
|
||||
it { should have_db_column :name }
|
||||
it { should have_db_column :data }
|
||||
it { should have_db_column :repository_id }
|
||||
it { should have_db_column :created_by_id }
|
||||
it { should have_db_column :last_modified_by_id }
|
||||
end
|
||||
|
||||
describe 'Relations' do
|
||||
|
@ -16,32 +18,32 @@ RSpec.describe RepositoryListItem, type: :model do
|
|||
end
|
||||
|
||||
describe 'Validations' do
|
||||
it { should validate_presence_of(:name) }
|
||||
it { should validate_presence_of(:data) }
|
||||
it do
|
||||
should validate_length_of(:name).is_at_most(Constants::TEXT_MAX_LENGTH)
|
||||
should validate_length_of(:data).is_at_most(Constants::TEXT_MAX_LENGTH)
|
||||
end
|
||||
|
||||
context 'has a uniq name scoped on repository' do
|
||||
context 'has a uniq data scoped on repository' do
|
||||
let!(:user) { create :user }
|
||||
let!(:repository_one) { create :repository }
|
||||
let!(:repository_two) { create :repository, name: 'New repo' }
|
||||
let!(:repository_list_item) do
|
||||
create :repository_list_item, name: 'Test', repository: repository_one
|
||||
create :repository_list_item, data: 'Test', repository: repository_one
|
||||
end
|
||||
|
||||
it 'creates a repository list item in same repository' do
|
||||
new_item = build :repository_list_item,
|
||||
name: 'Test',
|
||||
data: 'Test',
|
||||
repository: repository_one
|
||||
expect(new_item).to_not be_valid
|
||||
expect(
|
||||
new_item.errors.full_messages.first
|
||||
).to eq 'Name has already been taken'
|
||||
).to eq 'Data has already been taken'
|
||||
end
|
||||
|
||||
it 'create a repository list item in other repository' do
|
||||
new_item = build :repository_list_item,
|
||||
name: 'Test',
|
||||
data: 'Test',
|
||||
repository: repository_two
|
||||
expect(new_item).to be_valid
|
||||
end
|
||||
|
|
|
@ -8,13 +8,13 @@ RSpec.describe RepositoryListValue, type: :model do
|
|||
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_id }
|
||||
it { should have_db_column :repository_list_item_id }
|
||||
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 belong_to(:selected_item).class_name('RepositoryListItem') }
|
||||
it { should belong_to(:repository_list_item) }
|
||||
it { should accept_nested_attributes_for(:repository_cell) }
|
||||
end
|
||||
|
||||
|
@ -31,11 +31,11 @@ RSpec.describe RepositoryListValue, type: :model do
|
|||
}
|
||||
end
|
||||
|
||||
it 'returns the name of a selected item' do
|
||||
it 'returns the data of a selected item' do
|
||||
list_item = create :repository_list_item,
|
||||
name: 'my item',
|
||||
data: 'my item',
|
||||
repository: repository
|
||||
repository_list_value.selected_item = list_item
|
||||
repository_list_value.repository_list_item = list_item
|
||||
repository_list_value.save
|
||||
expect(repository_list_value.reload.formatted).to eq 'my item'
|
||||
end
|
||||
|
@ -48,22 +48,22 @@ RSpec.describe RepositoryListValue, type: :model do
|
|||
repository_row: repository_row_two
|
||||
}
|
||||
list_item = create :repository_list_item,
|
||||
name: 'new item',
|
||||
data: 'new item',
|
||||
repository: repository
|
||||
repository_list_value.selected_item = list_item
|
||||
repository_list_value.repository_list_item = list_item
|
||||
expect(repository_list_value.reload.formatted).to_not eq 'my item'
|
||||
expect(repository_list_value.formatted).to eq ''
|
||||
end
|
||||
|
||||
it 'returns an empty string if no item selected' do
|
||||
list_item = create :repository_list_item,
|
||||
name: 'my item',
|
||||
data: 'my item',
|
||||
repository: repository
|
||||
expect(repository_list_value.reload.formatted).to eq ''
|
||||
end
|
||||
|
||||
it 'returns an empty string if item does not exists' do
|
||||
repository_list_value.selected_item = nil
|
||||
repository_list_value.repository_list_item = nil
|
||||
expect(repository_list_value.reload.formatted).to eq ''
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue