From cc10fbfb834af5a4ffcb380f3126692c91e8d14d Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sun, 8 Jun 2025 20:59:09 +0200 Subject: [PATCH 1/3] Store website content as HTML --- app/controllers/websites_controller.rb | 19 ++++++++++++++++++ app/models/website.rb | 21 ++++++++++++++++++++ app/models/wedding.rb | 1 + config/routes.rb | 3 +++ db/migrate/20250608181054_create_websites.rb | 10 ++++++++++ db/schema.rb | 11 +++++++++- spec/factories/websites.rb | 6 ++++++ spec/models/website_spec.rb | 5 +++++ 8 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 app/controllers/websites_controller.rb create mode 100644 app/models/website.rb create mode 100644 db/migrate/20250608181054_create_websites.rb create mode 100644 spec/factories/websites.rb create mode 100644 spec/models/website_spec.rb diff --git a/app/controllers/websites_controller.rb b/app/controllers/websites_controller.rb new file mode 100644 index 0000000..82d4a15 --- /dev/null +++ b/app/controllers/websites_controller.rb @@ -0,0 +1,19 @@ +class WebsitesController < ApplicationController + def show + render json: current_tenant.website.as_json(only: %i[content]) || {}, status: :ok + end + + def update + ActiveRecord::Base.transaction do + website = current_tenant.website || current_tenant.create_website + website.update!(website_params) + render json: website.as_json(only: %i[content]), status: :ok + end + end + + private + + def website_params + params.require(:website).permit(:content) + end +end diff --git a/app/models/website.rb b/app/models/website.rb new file mode 100644 index 0000000..95ea759 --- /dev/null +++ b/app/models/website.rb @@ -0,0 +1,21 @@ +# == Schema Information +# +# Table name: websites +# +# id :bigint not null, primary key +# content :text +# created_at :datetime not null +# updated_at :datetime not null +# wedding_id :uuid not null +# +# Indexes +# +# index_websites_on_wedding_id (wedding_id) +# +# Foreign Keys +# +# fk_rails_... (wedding_id => weddings.id) +# +class Website < ApplicationRecord + belongs_to :wedding +end diff --git a/app/models/wedding.rb b/app/models/wedding.rb index e9bed42..36e3ca8 100644 --- a/app/models/wedding.rb +++ b/app/models/wedding.rb @@ -23,4 +23,5 @@ class Wedding < ApplicationRecord has_many :guests, dependent: :delete_all has_many :groups, dependent: :delete_all has_many :invitations, dependent: :delete_all + has_one :website, dependent: :destroy end diff --git a/config/routes.rb b/config/routes.rb index a4b7897..e908b04 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -37,6 +37,9 @@ Rails.application.routes.draw do resources :expenses, only: %i[index create update destroy] do get :summary, on: :collection end + + resource :website, only: [:show, :update] + resources :tables_arrangements, only: %i[index show create] resources :summary, only: :index resources :invitations, only: %i[index create update destroy] diff --git a/db/migrate/20250608181054_create_websites.rb b/db/migrate/20250608181054_create_websites.rb new file mode 100644 index 0000000..63715b7 --- /dev/null +++ b/db/migrate/20250608181054_create_websites.rb @@ -0,0 +1,10 @@ +class CreateWebsites < ActiveRecord::Migration[8.0] + def change + create_table :websites do |t| + t.text :content + t.references :wedding, null: false, foreign_key: true, type: :uuid + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 5a7e2fd..2576583 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2025_01_27_190131) do +ActiveRecord::Schema[8.0].define(version: 2025_06_08_181054) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" @@ -241,6 +241,14 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_27_190131) do t.index ["wedding_id"], name: "index_users_on_wedding_id" end + create_table "websites", force: :cascade do |t| + t.text "content" + t.uuid "wedding_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["wedding_id"], name: "index_websites_on_wedding_id" + end + create_table "weddings", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.string "slug", null: false t.datetime "created_at", null: false @@ -268,4 +276,5 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_27_190131) do add_foreign_key "solid_queue_scheduled_executions", "solid_queue_jobs", column: "job_id", on_delete: :cascade add_foreign_key "tables_arrangements", "weddings", on_delete: :cascade add_foreign_key "users", "weddings", on_delete: :cascade + add_foreign_key "websites", "weddings" end diff --git a/spec/factories/websites.rb b/spec/factories/websites.rb new file mode 100644 index 0000000..9bf0807 --- /dev/null +++ b/spec/factories/websites.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory :website do + content { "MyText" } + wedding { nil } + end +end diff --git a/spec/models/website_spec.rb b/spec/models/website_spec.rb new file mode 100644 index 0000000..97673ba --- /dev/null +++ b/spec/models/website_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Website, type: :model do + it { should belong_to(:wedding) } +end -- 2.47.1 From 3e363ac7dd8b93b17b9a65ee25e3390868a091a5 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sun, 8 Jun 2025 19:03:39 +0000 Subject: [PATCH 2/3] Add copyright notice --- app/controllers/websites_controller.rb | 2 ++ app/models/website.rb | 2 ++ spec/factories/websites.rb | 2 ++ spec/models/website_spec.rb | 2 ++ 4 files changed, 8 insertions(+) diff --git a/app/controllers/websites_controller.rb b/app/controllers/websites_controller.rb index 82d4a15..e801106 100644 --- a/app/controllers/websites_controller.rb +++ b/app/controllers/websites_controller.rb @@ -1,3 +1,5 @@ +# Copyright (C) 2024-2025 LibreWeddingPlanner contributors + class WebsitesController < ApplicationController def show render json: current_tenant.website.as_json(only: %i[content]) || {}, status: :ok diff --git a/app/models/website.rb b/app/models/website.rb index 95ea759..42c719e 100644 --- a/app/models/website.rb +++ b/app/models/website.rb @@ -1,3 +1,5 @@ +# Copyright (C) 2024-2025 LibreWeddingPlanner contributors + # == Schema Information # # Table name: websites diff --git a/spec/factories/websites.rb b/spec/factories/websites.rb index 9bf0807..76e2d8b 100644 --- a/spec/factories/websites.rb +++ b/spec/factories/websites.rb @@ -1,3 +1,5 @@ +# Copyright (C) 2024-2025 LibreWeddingPlanner contributors + FactoryBot.define do factory :website do content { "MyText" } diff --git a/spec/models/website_spec.rb b/spec/models/website_spec.rb index 97673ba..c22f339 100644 --- a/spec/models/website_spec.rb +++ b/spec/models/website_spec.rb @@ -1,3 +1,5 @@ +# Copyright (C) 2024-2025 LibreWeddingPlanner contributors + require 'rails_helper' RSpec.describe Website, type: :model do -- 2.47.1 From fc9911abf4b1c297b4ae84d7c17c4f13618dd156 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sun, 8 Jun 2025 23:09:14 +0200 Subject: [PATCH 3/3] Fix rubocop ofenses --- app/controllers/websites_controller.rb | 4 +++- app/models/website.rb | 2 ++ spec/factories/websites.rb | 4 +++- spec/models/website_spec.rb | 6 ++++-- 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/app/controllers/websites_controller.rb b/app/controllers/websites_controller.rb index e801106..46e0d01 100644 --- a/app/controllers/websites_controller.rb +++ b/app/controllers/websites_controller.rb @@ -1,5 +1,7 @@ # Copyright (C) 2024-2025 LibreWeddingPlanner contributors +# frozen_string_literal: true + class WebsitesController < ApplicationController def show render json: current_tenant.website.as_json(only: %i[content]) || {}, status: :ok @@ -16,6 +18,6 @@ class WebsitesController < ApplicationController private def website_params - params.require(:website).permit(:content) + params.expect(website: [:content]) end end diff --git a/app/models/website.rb b/app/models/website.rb index 42c719e..401b101 100644 --- a/app/models/website.rb +++ b/app/models/website.rb @@ -1,5 +1,7 @@ # Copyright (C) 2024-2025 LibreWeddingPlanner contributors +# frozen_string_literal: true + # == Schema Information # # Table name: websites diff --git a/spec/factories/websites.rb b/spec/factories/websites.rb index 76e2d8b..b8c4048 100644 --- a/spec/factories/websites.rb +++ b/spec/factories/websites.rb @@ -1,8 +1,10 @@ # Copyright (C) 2024-2025 LibreWeddingPlanner contributors +# frozen_string_literal: true + FactoryBot.define do factory :website do - content { "MyText" } + content { 'MyText' } wedding { nil } end end diff --git a/spec/models/website_spec.rb b/spec/models/website_spec.rb index c22f339..a9533cd 100644 --- a/spec/models/website_spec.rb +++ b/spec/models/website_spec.rb @@ -1,7 +1,9 @@ # Copyright (C) 2024-2025 LibreWeddingPlanner contributors +# frozen_string_literal: true + require 'rails_helper' -RSpec.describe Website, type: :model do - it { should belong_to(:wedding) } +RSpec.describe Website do + it { is_expected.to belong_to(:wedding) } end -- 2.47.1