From 2a20ad68a3769efe27796870e75b83f3071da448 Mon Sep 17 00:00:00 2001 From: Urban Rotnik Date: Tue, 19 Nov 2019 12:10:38 +0100 Subject: [PATCH] Add range column to RepositoryColumn --- app/models/repository_column.rb | 14 ++++++++++---- ...0191118150111_add_range_to_repository_column.rb | 7 +++++++ db/structure.sql | 7 ++++++- spec/models/repository_column_spec.rb | 14 ++++++++++++++ 4 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20191118150111_add_range_to_repository_column.rb diff --git a/app/models/repository_column.rb b/app/models/repository_column.rb index 914d5bc7a..cb09da3e3 100644 --- a/app/models/repository_column.rb +++ b/app/models/repository_column.rb @@ -15,12 +15,11 @@ class RepositoryColumn < ApplicationRecord auto_strip_attributes :name, nullify: false validates :name, - presence: true, length: { maximum: Constants::NAME_MAX_LENGTH }, uniqueness: { scope: :repository_id, case_sensitive: true } - validates :created_by, presence: true - validates :repository, presence: true - validates :data_type, presence: true + validates :name, :data_type, :repository, :created_by, presence: true + validates :range, inclusion: { in: [true, false] }, if: :repository_date_time_value? + validates :range, absence: true, unless: :repository_date_time_value? after_create :update_repository_table_states_with_new_column around_destroy :update_repository_table_states_with_removed_column @@ -33,6 +32,13 @@ class RepositoryColumn < ApplicationRecord where('repository_columns.name ILIKE ?', "%#{query}%") end + # Add enum check method with underscores (eg repository_list_value) + data_types.each do |k, _| + define_method "#{k.underscore}?" do + public_send "#{k}?" + end + end + def update_repository_table_states_with_new_column service = RepositoryTableStateColumnUpdateService.new service.update_states_with_new_column(repository) diff --git a/db/migrate/20191118150111_add_range_to_repository_column.rb b/db/migrate/20191118150111_add_range_to_repository_column.rb new file mode 100644 index 000000000..f1834e02e --- /dev/null +++ b/db/migrate/20191118150111_add_range_to_repository_column.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddRangeToRepositoryColumn < ActiveRecord::Migration[6.0] + def change + add_column :repository_columns, :range, :boolean + end +end diff --git a/db/structure.sql b/db/structure.sql index 407b82f3b..33efe1c0e 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -1202,7 +1202,11 @@ CREATE TABLE public.repository_columns ( data_type integer NOT NULL, created_at timestamp without time zone, updated_at timestamp without time zone, +<<<<<<< HEAD delimiter character varying +======= + range boolean +>>>>>>> eedcec279... Add range column to RepositoryColumn ); @@ -6853,6 +6857,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20191007144622'), ('20191009146101'), ('20191105143702'), -('20191115143747'); +('20191115143747'), +('20191118150111'); diff --git a/spec/models/repository_column_spec.rb b/spec/models/repository_column_spec.rb index 999c91d59..17dd4ec94 100644 --- a/spec/models/repository_column_spec.rb +++ b/spec/models/repository_column_spec.rb @@ -48,5 +48,19 @@ describe RepositoryColumn, type: :model do describe '#data_type' do it { is_expected.to validate_presence_of :data_type } end + + describe '#range' do + it do + allow(subject).to receive(:repository_date_time_value?).and_return(true) + + expect(subject).to allow_value([true, false]).for(:range) + end + + it do + allow(subject).to receive(:repository_date_time_value?).and_return(false) + + expect(subject).to allow_value(nil).for(:range) + end + end end end