From 50d3e085495e33f2550ccea4786568e5495efb86 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 3 Sep 2024 11:51:23 +0530 Subject: [PATCH] feat(CE): data app model changes (#353) Co-authored-by: afthab vp --- server/app/models/data_app.rb | 21 +++++++++++++++++++ server/app/models/model.rb | 1 + server/app/models/visual_component.rb | 14 +++++++++++++ server/app/models/workspace.rb | 1 + server/spec/models/data_app_spec.rb | 23 +++++++++++++++++++++ server/spec/models/model_spec.rb | 1 + server/spec/models/visual_component_spec.rb | 16 ++++++++++++++ server/spec/models/workspace_spec.rb | 1 + 8 files changed, 78 insertions(+) create mode 100644 server/app/models/data_app.rb create mode 100644 server/app/models/visual_component.rb create mode 100644 server/spec/models/data_app_spec.rb create mode 100644 server/spec/models/visual_component_spec.rb diff --git a/server/app/models/data_app.rb b/server/app/models/data_app.rb new file mode 100644 index 00000000..628561cc --- /dev/null +++ b/server/app/models/data_app.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +class DataApp < ApplicationRecord + validates :workspace_id, presence: true + validates :status, presence: true + validates :name, presence: true + + enum :status, %i[inactive active draft] + + belongs_to :workspace + has_many :visual_components, dependent: :destroy + has_many :models, through: :visual_components + + after_initialize :set_default_status, if: :new_record? + + private + + def set_default_status + self.status ||= :draft + end +end diff --git a/server/app/models/model.rb b/server/app/models/model.rb index f7db2832..9a4f991c 100644 --- a/server/app/models/model.rb +++ b/server/app/models/model.rb @@ -29,6 +29,7 @@ class Model < ApplicationRecord belongs_to :connector has_many :syncs, dependent: :destroy + has_many :visual_components, dependent: :destroy scope :data, -> { where(query_type: %i[raw_sql dbt soql table_selector]) } scope :ai_ml, -> { where(query_type: :ai_ml) } diff --git a/server/app/models/visual_component.rb b/server/app/models/visual_component.rb new file mode 100644 index 00000000..e0737f2a --- /dev/null +++ b/server/app/models/visual_component.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class VisualComponent < ApplicationRecord + validates :workspace_id, presence: true + validates :component_type, presence: true + validates :model_id, presence: true + validates :data_app_id, presence: true + + enum :component_type, %i[pie bar data_table] + + belongs_to :workspace + belongs_to :data_app + belongs_to :model +end diff --git a/server/app/models/workspace.rb b/server/app/models/workspace.rb index b9bc40a2..97410b97 100644 --- a/server/app/models/workspace.rb +++ b/server/app/models/workspace.rb @@ -20,6 +20,7 @@ class Workspace < ApplicationRecord has_many :catalogs, dependent: :nullify has_many :syncs, dependent: :nullify has_many :sync_runs, dependent: :nullify + has_many :data_apps, dependent: :nullify belongs_to :organization STATUS_ACTIVE = "active" diff --git a/server/spec/models/data_app_spec.rb b/server/spec/models/data_app_spec.rb new file mode 100644 index 00000000..1877f18e --- /dev/null +++ b/server/spec/models/data_app_spec.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe DataApp, type: :model do + it { should validate_presence_of(:workspace_id) } + it { should validate_presence_of(:status) } + it { should validate_presence_of(:name) } + + it { should define_enum_for(:status).with_values(inactive: 0, active: 1, draft: 2) } + + it { should belong_to(:workspace) } + it { should have_many(:visual_components).dependent(:destroy) } + it { should have_many(:models).through(:visual_components) } + + describe "#set_default_status" do + let(:data_app) { DataApp.new } + + it "sets default status" do + expect(data_app.status).to eq("draft") + end + end +end diff --git a/server/spec/models/model_spec.rb b/server/spec/models/model_spec.rb index 1d7d23a1..b28d7a76 100644 --- a/server/spec/models/model_spec.rb +++ b/server/spec/models/model_spec.rb @@ -22,6 +22,7 @@ it { should belong_to(:workspace) } it { should belong_to(:connector) } it { should have_many(:syncs).dependent(:destroy) } + it { should have_many(:visual_components).dependent(:destroy) } end describe "validations" do diff --git a/server/spec/models/visual_component_spec.rb b/server/spec/models/visual_component_spec.rb new file mode 100644 index 00000000..0ba909dd --- /dev/null +++ b/server/spec/models/visual_component_spec.rb @@ -0,0 +1,16 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe VisualComponent, type: :model do + it { should validate_presence_of(:workspace_id) } + it { should validate_presence_of(:component_type) } + it { should validate_presence_of(:model_id) } + it { should validate_presence_of(:data_app_id) } + + it { should define_enum_for(:component_type).with_values(pie: 0, bar: 1, data_table: 2) } + + it { should belong_to(:workspace) } + it { should belong_to(:data_app) } + it { should belong_to(:model) } +end diff --git a/server/spec/models/workspace_spec.rb b/server/spec/models/workspace_spec.rb index 96d1ada1..5e0e1ce3 100644 --- a/server/spec/models/workspace_spec.rb +++ b/server/spec/models/workspace_spec.rb @@ -29,6 +29,7 @@ it { should have_many(:models).dependent(:nullify) } it { should have_many(:catalogs).dependent(:nullify) } it { should have_many(:syncs).dependent(:nullify) } + it { should have_many(:data_apps).dependent(:nullify) } it { should belong_to(:organization) } end