mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-11-10 17:36:33 +08:00
Add migrations and models for custom repositories [SCI-1267]
This commit is contained in:
parent
39203a16f4
commit
72a651c7de
8 changed files with 126 additions and 0 deletions
13
app/models/repository.rb
Normal file
13
app/models/repository.rb
Normal 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
|
16
app/models/repository_cell.rb
Normal file
16
app/models/repository_cell.rb
Normal 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
|
18
app/models/repository_column.rb
Normal file
18
app/models/repository_column.rb
Normal 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
|
6
app/models/repository_date_value.rb
Normal file
6
app/models/repository_date_value.rb
Normal 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
|
14
app/models/repository_row.rb
Normal file
14
app/models/repository_row.rb
Normal 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
|
6
app/models/repository_text_value.rb
Normal file
6
app/models/repository_text_value.rb
Normal 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
|
|
@ -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
|
||||
|
|
50
db/migrate/20170515141252_add_custom_repositories.rb
Normal file
50
db/migrate/20170515141252_add_custom_repositories.rb
Normal 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
|
Loading…
Reference in a new issue