Add migrations and models for custom repositories [SCI-1267]

This commit is contained in:
Oleksii Kriuchykhin 2017-05-16 15:27:36 +02:00
parent 39203a16f4
commit 72a651c7de
8 changed files with 126 additions and 0 deletions

13
app/models/repository.rb Normal file
View file

@ -0,0 +1,13 @@
class Repository < ActiveRecord::Base
belongs_to :team
belongs_to :created_by, foreign_key: :created_by_id, class_name: 'User'
has_many :repository_columns
has_many :repository_rows
auto_strip_attributes :name, nullify: false
validates :name,
presence: true,
length: { maximum: Constants::NAME_MAX_LENGTH }
validates :team, presence: true
validates :created_by, presence: true
end

View file

@ -0,0 +1,16 @@
class RepositoryCell < ActiveRecord::Base
belongs_to :repository_row
belongs_to :repository_column
belongs_to :value, polymorphic: true, dependent: :destroy
validate :repository_column_data_type
validates :repository_row, uniqueness: { scope: :repository_column }
private
def repository_column_data_type
if value_type != repository_column.data_type
errors.add(:value_type, 'must match column data type')
end
end
end

View file

@ -0,0 +1,18 @@
class RepositoryColumn < ActiveRecord::Base
belongs_to :repository
belongs_to :team
belongs_to :created_by, foreign_key: :created_by_id, class_name: 'User'
has_many :repository_cells, dependent: :destroy
has_many :repository_rows, through: :repository_cells
enum data_type: Extends::REPOSITORY_DATA_TYPES
auto_strip_attributes :name, nullify: false
validates :name,
presence: true,
length: { maximum: Constants::NAME_MAX_LENGTH },
uniqueness: { scope: :team, case_sensitive: true }
validates :team, :data_type, presence: true
validates :created_by, presence: true
validates :repository, presence: true
end

View file

@ -0,0 +1,6 @@
class RepositoryDateValue < ActiveRecord::Base
has_one :repository_cell, as: :value
accepts_nested_attributes_for :repository_cell
validates :repository_cell, presence: true
end

View file

@ -0,0 +1,14 @@
class RepositoryRow < ActiveRecord::Base
belongs_to :repository
belongs_to :team
belongs_to :created_by, foreign_key: :created_by_id, class_name: 'User'
has_many :repository_cells, dependent: :destroy
has_many :repository_columns, through: :repository_cells
auto_strip_attributes :name, nullify: false
validates :name,
presence: true,
length: { maximum: Constants::NAME_MAX_LENGTH }
validates :team, presence: true
validates :created_by, presence: true
end

View file

@ -0,0 +1,6 @@
class RepositoryTextValue < ActiveRecord::Base
has_one :repository_cell, as: :value
accepts_nested_attributes_for :repository_cell
validates :repository_cell, presence: true
end

View file

@ -37,4 +37,7 @@ class Extends
project_samples: 14, # TODO
experiment: 15 }
# Data type name should match corresponding model's name
REPOSITORY_DATA_TYPES = { RepositoryTextValue: 0,
RepositoryDateValue: 1 }
end

View file

@ -0,0 +1,50 @@
class AddCustomRepositories < ActiveRecord::Migration
def change
create_table :repositories do |t|
t.belongs_to :team, index: true
t.integer :created_by_id, null: false
t.string :name
t.timestamps null: true
end
add_foreign_key :repositories, :users, column: :created_by_id
create_table :repository_columns do |t|
t.belongs_to :repository, index: true
t.belongs_to :team, index: true
t.integer :created_by_id, null: false
t.string :name
t.integer :data_type, null: false
t.timestamps null: true
end
add_foreign_key :repository_columns, :users, column: :created_by_id
create_table :repository_rows do |t|
t.belongs_to :repository, index: true
t.belongs_to :team, index: true
t.integer :created_by_id, null: false
t.string :name
t.timestamps null: true
end
add_foreign_key :repository_rows, :users, column: :created_by_id
create_table :repository_cells do |t|
t.belongs_to :repository_row, index: true
t.belongs_to :repository_column, index: true
t.references :value, polymorphic: true, index: true
t.timestamps null: true
end
create_table :repository_date_values do |t|
t.datetime :value
t.timestamps null: true
end
create_table :repository_text_values do |t|
t.string :value
t.timestamps null: true
end
end
end