mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-10-01 17:34:59 +08:00
Implement STI for DateTimeValue types
This commit is contained in:
parent
06d87cb4b6
commit
e934e08c17
11 changed files with 95 additions and 68 deletions
|
@ -80,20 +80,8 @@ class RepositoryCell < ApplicationRecord
|
|||
private
|
||||
|
||||
def repository_column_data_type
|
||||
if !repository_column || value_type != repository_column.data_type && !date_time_compatibility_checks
|
||||
if !repository_column || value_type != repository_column.data_type
|
||||
errors.add(:value_type, 'must match column data type')
|
||||
end
|
||||
end
|
||||
|
||||
def date_time_compatibility_checks
|
||||
case value_type
|
||||
when 'RepositoryDateTimeValue'
|
||||
%w(RepositoryTimeValue RepositoryDateTimeValue RepositoryDateValue).include?(repository_column.data_type)
|
||||
when 'RepositoryDateTimeRangeValue'
|
||||
%w(RepositoryTimeRangeValue RepositoryDateTimeRangeValue RepositoryDateRangeValue)
|
||||
.include?(repository_column.data_type)
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
4
app/models/repository_date_range_value.rb
Normal file
4
app/models/repository_date_range_value.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class RepositoryDateRangeValue < RepositoryDateTimeRangeValueBase
|
||||
end
|
|
@ -1,23 +1,4 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class RepositoryDateTimeRangeValue < ApplicationRecord
|
||||
belongs_to :created_by, foreign_key: :created_by_id, class_name: 'User', optional: true,
|
||||
inverse_of: :created_repositroy_date_time_values
|
||||
belongs_to :last_modified_by, foreign_key: :last_modified_by_id, class_name: 'User', optional: true,
|
||||
inverse_of: :modified_repositroy_date_time_values
|
||||
has_one :repository_cell, as: :value, dependent: :destroy, inverse_of: :repository_date_time_value
|
||||
accepts_nested_attributes_for :repository_cell
|
||||
|
||||
validates :repository_cell, :start_time, :end_time, presence: true
|
||||
|
||||
SORTABLE_COLUMN_NAME = 'repository_date_time_values.start_time'
|
||||
SORTABLE_VALUE_INCLUDE = :repository_date_time_range_value
|
||||
|
||||
def formatted
|
||||
data
|
||||
end
|
||||
|
||||
def data
|
||||
[start_time, end_time].compact.join(' - ')
|
||||
end
|
||||
class RepositoryDateTimeRangeValue < RepositoryDateTimeRangeValueBase
|
||||
end
|
||||
|
|
25
app/models/repository_date_time_range_value_base.rb
Normal file
25
app/models/repository_date_time_range_value_base.rb
Normal file
|
@ -0,0 +1,25 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class RepositoryDateTimeRangeValueBase < ApplicationRecord
|
||||
self.table_name = 'repository_date_time_range_values'
|
||||
|
||||
belongs_to :created_by, foreign_key: :created_by_id, class_name: 'User', optional: true,
|
||||
inverse_of: :created_repositroy_date_time_values
|
||||
belongs_to :last_modified_by, foreign_key: :last_modified_by_id, class_name: 'User', optional: true,
|
||||
inverse_of: :modified_repositroy_date_time_values
|
||||
has_one :repository_cell, as: :value, dependent: :destroy, inverse_of: :repository_date_time_value
|
||||
accepts_nested_attributes_for :repository_cell
|
||||
|
||||
validates :repository_cell, :start_time, :end_time, presence: true
|
||||
|
||||
SORTABLE_COLUMN_NAME = 'repository_date_time_values.start_time'
|
||||
SORTABLE_VALUE_INCLUDE = :repository_date_time_range_value
|
||||
|
||||
def formatted
|
||||
data
|
||||
end
|
||||
|
||||
def data
|
||||
[start_time, end_time].compact.join(' - ')
|
||||
end
|
||||
end
|
|
@ -1,35 +1,4 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class RepositoryDateTimeValue < ApplicationRecord
|
||||
belongs_to :created_by, foreign_key: :created_by_id, class_name: 'User', optional: true,
|
||||
inverse_of: :created_repositroy_date_time_values
|
||||
belongs_to :last_modified_by, foreign_key: :last_modified_by_id, class_name: 'User', optional: true,
|
||||
inverse_of: :modified_repositroy_date_time_values
|
||||
has_one :repository_cell, as: :value, dependent: :destroy, inverse_of: :repository_date_time_value
|
||||
accepts_nested_attributes_for :repository_cell
|
||||
|
||||
validates :repository_cell, :data, presence: true
|
||||
|
||||
SORTABLE_COLUMN_NAME = 'repository_date_time_values.data'
|
||||
SORTABLE_VALUE_INCLUDE = :repository_date_time_value
|
||||
|
||||
def formatted
|
||||
data
|
||||
end
|
||||
|
||||
def data_changed?(new_data)
|
||||
new_data != data
|
||||
end
|
||||
|
||||
def update_data!(new_data, user)
|
||||
self.data = new_data
|
||||
self.last_modified_by = user
|
||||
save!
|
||||
end
|
||||
|
||||
def self.new_with_payload(payload, attributes)
|
||||
value = new(attributes)
|
||||
value.data = payload
|
||||
value
|
||||
end
|
||||
class RepositoryDateTimeValue < RepositoryDateTimeValueBase
|
||||
end
|
||||
|
|
37
app/models/repository_date_time_value_base.rb
Normal file
37
app/models/repository_date_time_value_base.rb
Normal file
|
@ -0,0 +1,37 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class RepositoryDateTimeValueBase < ApplicationRecord
|
||||
self.table_name = 'repository_date_time_values'
|
||||
|
||||
belongs_to :created_by, foreign_key: :created_by_id, class_name: 'User', optional: true,
|
||||
inverse_of: :created_repositroy_date_time_values
|
||||
belongs_to :last_modified_by, foreign_key: :last_modified_by_id, class_name: 'User', optional: true,
|
||||
inverse_of: :modified_repositroy_date_time_values
|
||||
has_one :repository_cell, as: :value, dependent: :destroy, inverse_of: :repository_date_time_value
|
||||
accepts_nested_attributes_for :repository_cell
|
||||
|
||||
validates :repository_cell, :data, presence: true
|
||||
|
||||
SORTABLE_COLUMN_NAME = 'repository_date_time_values.data'
|
||||
SORTABLE_VALUE_INCLUDE = :repository_date_time_value
|
||||
|
||||
def formatted
|
||||
data
|
||||
end
|
||||
|
||||
def data_changed?(new_data)
|
||||
new_data != data
|
||||
end
|
||||
|
||||
def update_data!(new_data, user)
|
||||
self.data = new_data
|
||||
self.last_modified_by = user
|
||||
save!
|
||||
end
|
||||
|
||||
def self.new_with_payload(payload, attributes)
|
||||
value = new(attributes)
|
||||
value.data = payload
|
||||
value
|
||||
end
|
||||
end
|
4
app/models/repository_date_value.rb
Normal file
4
app/models/repository_date_value.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class RepositoryDateValue < RepositoryDateTimeValueBase
|
||||
end
|
4
app/models/repository_time_range_value.rb
Normal file
4
app/models/repository_time_range_value.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class RepositoryTimeRangeValue < RepositoryDateTimeRangeValueBase
|
||||
end
|
4
app/models/repository_time_value.rb
Normal file
4
app/models/repository_time_value.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class RepositoryTimeValue < RepositoryDateTimeValueBase
|
||||
end
|
|
@ -0,0 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class AddTypeToRepositoryDateTimeValue < ActiveRecord::Migration[6.0]
|
||||
def change
|
||||
add_column :repository_date_time_values, :type, :string
|
||||
add_column :repository_date_time_range_values, :type, :string
|
||||
end
|
||||
end
|
|
@ -1236,7 +1236,8 @@ CREATE TABLE public.repository_date_time_range_values (
|
|||
last_modified_by_id bigint,
|
||||
created_by_id bigint,
|
||||
created_at timestamp(6) without time zone NOT NULL,
|
||||
updated_at timestamp(6) without time zone NOT NULL
|
||||
updated_at timestamp(6) without time zone NOT NULL,
|
||||
type character varying
|
||||
);
|
||||
|
||||
|
||||
|
@ -1269,7 +1270,8 @@ CREATE TABLE public.repository_date_time_values (
|
|||
created_at timestamp without time zone,
|
||||
updated_at timestamp without time zone,
|
||||
created_by_id bigint NOT NULL,
|
||||
last_modified_by_id bigint NOT NULL
|
||||
last_modified_by_id bigint NOT NULL,
|
||||
type character varying
|
||||
);
|
||||
|
||||
|
||||
|
@ -6845,6 +6847,7 @@ INSERT INTO "schema_migrations" (version) VALUES
|
|||
('20191007144622'),
|
||||
('20191009146101'),
|
||||
('20191105143702'),
|
||||
('20191115143747');
|
||||
('20191115143747'),
|
||||
('20191206105058');
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue