From 4525e45acfb6f4755c57781604ed49c1fdd31ef6 Mon Sep 17 00:00:00 2001 From: pessi-v Date: Thu, 4 Jul 2024 20:11:19 +0200 Subject: [PATCH] add source Description and Image columns, more work on Source card --- app/models/source.rb | 10 +++++++ app/views/sources/_source.html.slim | 29 ++++++++++++++----- ...939_add_description_and_image_to_source.rb | 6 ++++ db/schema.rb | 4 ++- 4 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 db/migrate/20240704165939_add_description_and_image_to_source.rb diff --git a/app/models/source.rb b/app/models/source.rb index c637f0b..ec3d3a5 100644 --- a/app/models/source.rb +++ b/app/models/source.rb @@ -6,6 +6,7 @@ class Source < ApplicationRecord scope :active, -> { where(active: true) } has_many :articles, dependent: :destroy + before_create :add_description_and_image after_update :update_articles_source_name, if: :saved_change_to_name? def valid_feed @@ -30,6 +31,15 @@ def valid_feed private + def add_description_and_image + uri = URI(s.url) + uri.query = uri.fragment = nil + uri.path = "" + ogp = Ogpr.fetch(uri.to_s) + image_url = ogp.image + description = ogp.description + end + def update_articles_source_name articles.update_all(source_name: name) end diff --git a/app/views/sources/_source.html.slim b/app/views/sources/_source.html.slim index 5358034..8c049c5 100644 --- a/app/views/sources/_source.html.slim +++ b/app/views/sources/_source.html.slim @@ -1,22 +1,32 @@ css: - .error { - color: var(--color-primary); - } .source_card { width: 100%; height: auto; border: var(--border); display: flex; - } + + .section { + width: 50%; + } - .half { - width: 50%; + .error { + color: var(--color-primary); + } + + .image { + height: var(--space-9xl); + aspect-ratio: 1 / 1; + object-fit: cover; + } } + = turbo_frame_tag source .source_card - .half + img.image src=source.image_url + + .section h1 = source.name @@ -49,6 +59,9 @@ css: p.error = source.last_error_status - .half + .section + p + = source.description + = button_to "Change", edit_source_path(source), method: :get = button_to "Unsubscribe", source, method: :delete, form: {data: {turbo_confirm: "Unsubscribing from this source will remove all of it's articles from the database. If you want to keep the articles, you should just deactivate the source."}} diff --git a/db/migrate/20240704165939_add_description_and_image_to_source.rb b/db/migrate/20240704165939_add_description_and_image_to_source.rb new file mode 100644 index 0000000..8ff2ccf --- /dev/null +++ b/db/migrate/20240704165939_add_description_and_image_to_source.rb @@ -0,0 +1,6 @@ +class AddDescriptionAndImageToSource < ActiveRecord::Migration[7.1] + def change + add_column :sources, :description, :string + add_column :sources, :image_url, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 22d2943..081a3de 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[7.1].define(version: 2024_06_13_171946) do +ActiveRecord::Schema[7.1].define(version: 2024_07_04_165939) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" enable_extension "unaccent" @@ -43,6 +43,8 @@ t.datetime "updated_at", null: false t.boolean "allow_video", default: false t.boolean "allow_audio", default: false + t.string "description" + t.string "image_url" end create_table "users", force: :cascade do |t|