From 1b5d1b9b9a760f431af48e5069296e1514c5b715 Mon Sep 17 00:00:00 2001 From: Ernesto Tagwerker Date: Sun, 8 Nov 2026 22:57:54 -0500 Subject: [PATCH 1/3] Add a JSON API to list and create social media snippets Since #39 removed the ChatGPT step, nothing writes new social media snippets and there is no API for them. This adds the same Bearer-token JSON API shares already have: GET /links/:link_id/social_media_snippets.json (optional ?social_media_type= filter) POST /links/:link_id/social_media_snippets.json A snippet takes social_media_type (Bluesky, GoogleBusiness, LinkedIn, Mastodon, Twitter), content, and an optional share_id that ties the copy to the tracked short URL it was posted with. share_id is a new nullable reference on social_media_snippets; deleting a share nullifies it. Creating a snippet is rejected with a 422 when the type is missing or unknown, when the same content already exists for that link and type, or when share_id points at a share that doesn't exist or belongs to another link. The type, duplicate, and presence checks run on create only, so snippets saved before this change stay editable. Co-Authored-By: Claude Sonnet 5.5 --- app/controllers/application_controller.rb | 3 +- .../social_media_snippets_controller.rb | 50 ++++ app/models/share.rb | 1 + app/models/social_media_snippet.rb | 28 +++ config/routes.rb | 1 + ...5414_add_share_to_social_media_snippets.rb | 10 + db/schema.rb | 5 +- spec/factories/social_media_snippets.rb | 7 + spec/models/social_media_snippet_spec.rb | 86 ++++++- .../api_social_media_snippets_spec.rb | 224 ++++++++++++++++++ 10 files changed, 411 insertions(+), 4 deletions(-) create mode 100644 app/controllers/social_media_snippets_controller.rb create mode 100644 db/migrate/20261109035414_add_share_to_social_media_snippets.rb create mode 100644 spec/factories/social_media_snippets.rb create mode 100644 spec/requests/api_social_media_snippets_spec.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ba157a0..19d06f6 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -4,7 +4,8 @@ class ApplicationController < ActionController::Base # so a PAT cannot reach write endpoints via content negotiation. API_ENDPOINTS = { "links" => %w[index show], - "shares" => %w[index create] + "shares" => %w[index create], + "social_media_snippets" => %w[index create] }.freeze skip_forgery_protection if: :api_endpoint? diff --git a/app/controllers/social_media_snippets_controller.rb b/app/controllers/social_media_snippets_controller.rb new file mode 100644 index 0000000..9b929bf --- /dev/null +++ b/app/controllers/social_media_snippets_controller.rb @@ -0,0 +1,50 @@ +class SocialMediaSnippetsController < ApplicationController + before_action :find_link + + # GET /links/1/social_media_snippets.json + # Optional ?social_media_type=LinkedIn narrows the list to one network. + def index + snippets = @link.social_media_snippets.order(:created_at, :id) + snippets = snippets.where(social_media_type: params[:social_media_type]) if params[:social_media_type].present? + + respond_to do |format| + format.json { render json: snippets.map { |s| snippet_json(s) } } + end + end + + # POST /links/1/social_media_snippets.json + def create + @snippet = @link.social_media_snippets.build(snippet_params) + + respond_to do |format| + format.json do + if @snippet.save + render json: snippet_json(@snippet), status: :created + else + render json: { errors: @snippet.errors.full_messages }, status: :unprocessable_entity + end + end + end + end + + private + + def find_link + @link = Link.find(params[:link_id]) + end + + def snippet_params + params.require(:social_media_snippet).permit(:social_media_type, :content, :share_id) + end + + def snippet_json(snippet) + { + id: snippet.id, + link_id: snippet.link_id, + social_media_type: snippet.social_media_type, + content: snippet.content, + share_id: snippet.share_id, + created_at: snippet.created_at + } + end +end diff --git a/app/models/share.rb b/app/models/share.rb index 4bfc4f7..a884673 100644 --- a/app/models/share.rb +++ b/app/models/share.rb @@ -1,5 +1,6 @@ class Share < ApplicationRecord belongs_to :link + has_many :social_media_snippets, dependent: :nullify UTM_SOURCES = %w[ Bing diff --git a/app/models/social_media_snippet.rb b/app/models/social_media_snippet.rb index 03bea17..dcbcaf5 100644 --- a/app/models/social_media_snippet.rb +++ b/app/models/social_media_snippet.rb @@ -1,5 +1,33 @@ class SocialMediaSnippet < ApplicationRecord + SOCIAL_MEDIA_TYPES = %w[ + Bluesky + GoogleBusiness + LinkedIn + Mastodon + Twitter + ].sort! + belongs_to :link + belongs_to :share, optional: true validates :content, presence: true + # on: :create so snippets saved before this list existed stay editable. + validates :social_media_type, presence: true, on: :create + validates :social_media_type, inclusion: { in: SOCIAL_MEDIA_TYPES, allow_blank: true }, on: :create + validates :content, uniqueness: { scope: %i[link_id social_media_type], + message: "has already been saved for this link and social media type", + allow_blank: true }, on: :create + validate :share_belongs_to_link + + private + + def share_belongs_to_link + return if share_id.blank? + + if share.nil? + errors.add(:share, "must exist") + elsif share.link_id != link_id + errors.add(:share, "must belong to the same link") + end + end end diff --git a/config/routes.rb b/config/routes.rb index f21ad14..83887cc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,7 @@ root to: "links#index" resources :links, only: [:index, :show] do resources :shares + resources :social_media_snippets, only: [:index, :create] get '/shares/:id/clone', to: 'shares#clone', as: "clone" end post 'execute_read_task', to: 'application#execute_read_task' diff --git a/db/migrate/20261109035414_add_share_to_social_media_snippets.rb b/db/migrate/20261109035414_add_share_to_social_media_snippets.rb new file mode 100644 index 0000000..617de6a --- /dev/null +++ b/db/migrate/20261109035414_add_share_to_social_media_snippets.rb @@ -0,0 +1,10 @@ +class AddShareToSocialMediaSnippets < ActiveRecord::Migration[7.0] + # Optional link back to the tracked share (short URL) a snippet was posted + # with. Nullable because every existing snippet predates it, and nullified + # (not blocked) when a share is deleted, so removing a share never fails + # because of the copy that points at it. + def change + add_reference :social_media_snippets, :share, null: true, index: true, + foreign_key: { on_delete: :nullify } + end +end diff --git a/db/schema.rb b/db/schema.rb index fc71bf8..2fee03f 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.0].define(version: 2026_04_29_134259) do +ActiveRecord::Schema[7.0].define(version: 2026_11_09_035414) do # These are extensions that must be enabled in order to support this database enable_extension "pg_trgm" enable_extension "plpgsql" @@ -59,7 +59,9 @@ t.bigint "link_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.bigint "share_id" t.index ["link_id"], name: "index_social_media_snippets_on_link_id" + t.index ["share_id"], name: "index_social_media_snippets_on_share_id" end create_table "users", force: :cascade do |t| @@ -80,4 +82,5 @@ add_foreign_key "personal_access_tokens", "users" add_foreign_key "shares", "links" add_foreign_key "social_media_snippets", "links" + add_foreign_key "social_media_snippets", "shares", on_delete: :nullify end diff --git a/spec/factories/social_media_snippets.rb b/spec/factories/social_media_snippets.rb new file mode 100644 index 0000000..0785b15 --- /dev/null +++ b/spec/factories/social_media_snippets.rb @@ -0,0 +1,7 @@ +FactoryBot.define do + factory :social_media_snippet do + social_media_type { 'LinkedIn' } + sequence(:content) { |n| "Snippet copy ##{n}" } + link + end +end diff --git a/spec/models/social_media_snippet_spec.rb b/spec/models/social_media_snippet_spec.rb index 6a845bd..21d6a63 100644 --- a/spec/models/social_media_snippet_spec.rb +++ b/spec/models/social_media_snippet_spec.rb @@ -7,8 +7,90 @@ link = SocialMediaSnippet.new expect(link.valid?).to be_falsey - expect(link.errors.full_messages).to eq ["Link must exist", "Content can't be blank"] + expect(link.errors.full_messages).to eq [ + "Link must exist", + "Content can't be blank", + "Social media type can't be blank" + ] + end + end + + context "social_media_type" do + let(:link) { FactoryBot.create(:link) } + + it "accepts each supported type" do + SocialMediaSnippet::SOCIAL_MEDIA_TYPES.each do |type| + snippet = SocialMediaSnippet.new(link: link, content: "Copy for #{type}", social_media_type: type) + expect(snippet).to be_valid, "expected #{type} to be valid" + end + end + + it "is case sensitive" do + snippet = SocialMediaSnippet.new(link: link, content: "Copy", social_media_type: "linkedin") + + expect(snippet).not_to be_valid + expect(snippet.errors.full_messages).to eq ["Social media type is not included in the list"] + end + + it "does not block editing a snippet saved before the list existed" do + legacy = SocialMediaSnippet.new(link: link, content: "Old copy", social_media_type: "Facebook") + legacy.save!(validate: false) + + expect(legacy.update(content: "Edited old copy")).to be true + end + end + + context "duplicates" do + let(:link) { FactoryBot.create(:link) } + let!(:existing) { FactoryBot.create(:social_media_snippet, link: link, social_media_type: "LinkedIn", content: "Same copy") } + + it "rejects the same content for the same link and type" do + duplicate = SocialMediaSnippet.new(link: link, social_media_type: "LinkedIn", content: "Same copy") + + expect(duplicate).not_to be_valid + expect(duplicate.errors[:content]).to eq ["has already been saved for this link and social media type"] + end + + it "allows the same content for another type" do + expect(SocialMediaSnippet.new(link: link, social_media_type: "Bluesky", content: "Same copy")).to be_valid + end + + it "allows the same content for another link" do + other = FactoryBot.create(:link, url: "https://www.fastruby.io/blog/other.html") + + expect(SocialMediaSnippet.new(link: other, social_media_type: "LinkedIn", content: "Same copy")).to be_valid + end + end + + context "share" do + let(:link) { FactoryBot.create(:link) } + + it "is optional" do + expect(SocialMediaSnippet.new(link: link, social_media_type: "LinkedIn", content: "Copy")).to be_valid + end + + it "must belong to the same link" do + other_share = FactoryBot.create(:share, link: FactoryBot.create(:link, url: "https://www.fastruby.io/blog/other.html")) + snippet = SocialMediaSnippet.new(link: link, social_media_type: "LinkedIn", content: "Copy", share: other_share) + + expect(snippet).not_to be_valid + expect(snippet.errors[:share]).to eq ["must belong to the same link"] + end + + it "must exist when a share_id is given" do + snippet = SocialMediaSnippet.new(link: link, social_media_type: "LinkedIn", content: "Copy", share_id: 0) + + expect(snippet).not_to be_valid + expect(snippet.errors[:share]).to eq ["must exist"] + end + + it "is nullified, not blocked, when the share is destroyed" do + share = FactoryBot.create(:share, link: link) + snippet = FactoryBot.create(:social_media_snippet, link: link, share: share) + + expect { share.destroy }.to change(Share, :count).by(-1) + expect(snippet.reload.share_id).to be_nil end end end -end \ No newline at end of file +end diff --git a/spec/requests/api_social_media_snippets_spec.rb b/spec/requests/api_social_media_snippets_spec.rb new file mode 100644 index 0000000..f43b24e --- /dev/null +++ b/spec/requests/api_social_media_snippets_spec.rb @@ -0,0 +1,224 @@ +require 'rails_helper' + +RSpec.describe 'JSON API for social media snippets', type: :request do + let(:user) { FactoryBot.create(:user) } + let(:token) { FactoryBot.create(:personal_access_token, user: user) } + let(:auth_headers) { { 'Authorization' => "Bearer #{token.token}", 'Accept' => 'application/json' } } + + let!(:link) { FactoryBot.create(:link, url: 'https://www.fastruby.io/blog/some-post.html', title: 'Some Post') } + let!(:other_link) { FactoryBot.create(:link, url: 'https://www.fastruby.io/blog/another-post.html', title: 'Another Post') } + + describe 'GET /links/:link_id/social_media_snippets.json' do + let!(:linkedin_snippet) { FactoryBot.create(:social_media_snippet, link: link, social_media_type: 'LinkedIn', content: 'On LinkedIn') } + let!(:mastodon_snippet) { FactoryBot.create(:social_media_snippet, link: link, social_media_type: 'Mastodon', content: 'On Mastodon') } + let!(:other_snippet) { FactoryBot.create(:social_media_snippet, link: other_link, content: 'Another link') } + + it "returns only the link's snippets" do + get "/links/#{link.id}/social_media_snippets.json", headers: auth_headers + + expect(response).to have_http_status(:ok) + body = JSON.parse(response.body) + expect(body.map { |s| s['content'] }).to eq(['On LinkedIn', 'On Mastodon']) + expect(body.first.keys).to match_array(%w[id link_id social_media_type content share_id created_at]) + end + + it 'filters by social_media_type' do + get "/links/#{link.id}/social_media_snippets.json", + params: { social_media_type: 'Mastodon' }, headers: auth_headers + + expect(response).to have_http_status(:ok) + expect(JSON.parse(response.body).map { |s| s['content'] }).to eq(['On Mastodon']) + end + + it 'returns an empty list for a link with no snippets' do + empty_link = FactoryBot.create(:link, url: 'https://www.fastruby.io/blog/empty.html') + get "/links/#{empty_link.id}/social_media_snippets.json", headers: auth_headers + + expect(response).to have_http_status(:ok) + expect(JSON.parse(response.body)).to eq([]) + end + + it 'raises RecordNotFound for an unknown link (Rails answers 404 outside the test environment)' do + expect { + get '/links/0/social_media_snippets.json', headers: auth_headers + }.to raise_error(ActiveRecord::RecordNotFound) + end + + it 'returns 401 without a token' do + get "/links/#{link.id}/social_media_snippets.json", headers: { 'Accept' => 'application/json' } + expect(response).to have_http_status(:unauthorized) + end + + it 'returns 401 with an invalid token' do + headers = { 'Authorization' => 'Bearer wrong', 'Accept' => 'application/json' } + get "/links/#{link.id}/social_media_snippets.json", headers: headers + expect(response).to have_http_status(:unauthorized) + end + end + + describe 'POST /links/:link_id/social_media_snippets.json' do + let(:valid_params) do + { social_media_snippet: { social_media_type: 'LinkedIn', content: 'A fresh take on the post.' } } + end + + def post_snippet(params, headers: auth_headers, to: link) + post "/links/#{to.id}/social_media_snippets.json", params: params, headers: headers + end + + it 'creates a snippet for a valid token' do + expect { post_snippet(valid_params) }.to change(SocialMediaSnippet, :count).by(1) + + expect(response).to have_http_status(:created) + body = JSON.parse(response.body) + expect(body['link_id']).to eq(link.id) + expect(body['social_media_type']).to eq('LinkedIn') + expect(body['content']).to eq('A fresh take on the post.') + expect(body['share_id']).to be_nil + expect(body['id']).to be_present + expect(body['created_at']).to be_present + end + + it 'accepts every supported social media type' do + SocialMediaSnippet::SOCIAL_MEDIA_TYPES.each do |type| + post_snippet({ social_media_snippet: { social_media_type: type, content: "Copy for #{type}" } }) + expect(response).to have_http_status(:created), "expected #{type} to be accepted" + end + end + + it 'shows up in GET /links/:id.json' do + post_snippet(valid_params) + + get "/links/#{link.id}.json", headers: auth_headers + contents = JSON.parse(response.body)['social_media_snippets'].map { |s| s['content'] } + expect(contents).to eq(['A fresh take on the post.']) + end + + context 'with a share_id' do + let!(:share) { FactoryBot.create(:share, link: link) } + + it 'ties the snippet to the share' do + post_snippet({ social_media_snippet: valid_params[:social_media_snippet].merge(share_id: share.id) }) + + expect(response).to have_http_status(:created) + expect(JSON.parse(response.body)['share_id']).to eq(share.id) + expect(SocialMediaSnippet.last.share).to eq(share) + end + + it 'returns 422 when the share belongs to another link' do + other_share = FactoryBot.create(:share, link: other_link) + + expect { + post_snippet({ social_media_snippet: valid_params[:social_media_snippet].merge(share_id: other_share.id) }) + }.not_to change(SocialMediaSnippet, :count) + + expect(response).to have_http_status(:unprocessable_entity) + expect(JSON.parse(response.body)['errors']).to include('Share must belong to the same link') + end + + it 'returns 422, not a server error, when the share does not exist' do + expect { + post_snippet({ social_media_snippet: valid_params[:social_media_snippet].merge(share_id: 0) }) + }.not_to change(SocialMediaSnippet, :count) + + expect(response).to have_http_status(:unprocessable_entity) + expect(JSON.parse(response.body)['errors']).to include('Share must exist') + end + end + + it 'returns 422 when content is missing' do + expect { + post_snippet({ social_media_snippet: { social_media_type: 'LinkedIn', content: '' } }) + }.not_to change(SocialMediaSnippet, :count) + + expect(response).to have_http_status(:unprocessable_entity) + expect(JSON.parse(response.body)['errors']).to include("Content can't be blank") + end + + it 'returns 422 for an unknown social media type' do + expect { + post_snippet({ social_media_snippet: { social_media_type: 'Myspace', content: 'Hello' } }) + }.not_to change(SocialMediaSnippet, :count) + + expect(response).to have_http_status(:unprocessable_entity) + expect(JSON.parse(response.body)['errors']).to include('Social media type is not included in the list') + end + + it 'returns 422 when the social media type is missing' do + post_snippet({ social_media_snippet: { content: 'Hello' } }) + + expect(response).to have_http_status(:unprocessable_entity) + expect(JSON.parse(response.body)['errors']).to include("Social media type can't be blank") + end + + it 'returns 422 for an exact duplicate, so a retry is safe' do + post_snippet(valid_params) + + expect { post_snippet(valid_params) }.not_to change(SocialMediaSnippet, :count) + + expect(response).to have_http_status(:unprocessable_entity) + expect(JSON.parse(response.body)['errors'].first).to match(/has already been saved/) + end + + it 'accepts the same content on another network or another link' do + post_snippet(valid_params) + + post_snippet({ social_media_snippet: valid_params[:social_media_snippet].merge(social_media_type: 'Mastodon') }) + expect(response).to have_http_status(:created) + + post_snippet(valid_params, to: other_link) + expect(response).to have_http_status(:created) + end + + it 'accepts a JSON body, with or without the social_media_snippet wrapper' do + headers = auth_headers.merge('Content-Type' => 'application/json') + + post "/links/#{link.id}/social_media_snippets.json", + params: { social_media_type: 'Bluesky', content: 'Flat JSON body' }.to_json, headers: headers + expect(response).to have_http_status(:created) + expect(JSON.parse(response.body)['content']).to eq('Flat JSON body') + + post "/links/#{link.id}/social_media_snippets.json", + params: { social_media_snippet: { social_media_type: 'Mastodon', content: 'Wrapped JSON body' } }.to_json, + headers: headers + expect(response).to have_http_status(:created) + expect(JSON.parse(response.body)['content']).to eq('Wrapped JSON body') + end + + it 'raises ParameterMissing without any snippet params (Rails answers 400 outside the test environment)' do + expect { post_snippet({}) }.to raise_error(ActionController::ParameterMissing) + end + + it 'raises RecordNotFound for an unknown link and creates nothing' do + expect { + expect { + post '/links/0/social_media_snippets.json', params: valid_params, headers: auth_headers + }.to raise_error(ActiveRecord::RecordNotFound) + }.not_to change(SocialMediaSnippet, :count) + end + + it 'returns 401 without a token' do + expect { + post_snippet(valid_params, headers: { 'Accept' => 'application/json' }) + }.not_to change(SocialMediaSnippet, :count) + + expect(response).to have_http_status(:unauthorized) + end + + it 'returns 401 with an invalid token' do + headers = { 'Authorization' => 'Bearer wrong', 'Accept' => 'application/json' } + + expect { post_snippet(valid_params, headers: headers) }.not_to change(SocialMediaSnippet, :count) + expect(response).to have_http_status(:unauthorized) + end + + it 'returns 401 for cookie-authenticated browsers without a Bearer token' do + sign_in user + + expect { + post_snippet(valid_params, headers: { 'Accept' => 'application/json' }) + }.not_to change(SocialMediaSnippet, :count) + + expect(response).to have_http_status(:unauthorized) + end + end +end From 68c75fa5314e10551625039333f17bb9510f6237 Mon Sep 17 00:00:00 2001 From: Ernesto Tagwerker Date: Sun, 8 Nov 2026 23:00:10 -0500 Subject: [PATCH 2/3] Fix the link page copy that says snippets are created by Open AI The Social Media Snippets section told readers the snippets are created by Open AI. That stopped being true when #39 removed the ChatGPT step, and snippets can now be stored over the JSON API, so it's wrong for new ones too. The copy no longer says where a snippet came from, and points to the shortened links in the Shares section above it. Adds a view spec for the section: snippets are listed under their social media type, the Open AI claim is gone, and the section is left out when a link has no snippets. Co-Authored-By: Claude Sonnet 5.5 --- app/views/links/show.html.erb | 2 +- spec/views/links/show.html.erb_spec.rb | 37 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 spec/views/links/show.html.erb_spec.rb diff --git a/app/views/links/show.html.erb b/app/views/links/show.html.erb index e76582d..675e9ea 100644 --- a/app/views/links/show.html.erb +++ b/app/views/links/show.html.erb @@ -15,7 +15,7 @@ <% if @link.social_media_snippets.any? %>

Social Media Snippets

-

These prompts are created by Open AI, and are suggestions to get you started when posting one of our blog posts, they should be paired with a link from above.

+

Suggested copy for posting this article. Pair each snippet with one of the shortened links above.

<% @grouped_social_media_snippets.each do |media_type, snippets| %>

<%= media_type %>

diff --git a/spec/views/links/show.html.erb_spec.rb b/spec/views/links/show.html.erb_spec.rb new file mode 100644 index 0000000..0673676 --- /dev/null +++ b/spec/views/links/show.html.erb_spec.rb @@ -0,0 +1,37 @@ +require "rails_helper" + +RSpec.describe "links/show", type: :view do + let(:link) { FactoryBot.create(:link) } + + def render_page + assign(:link, link) + assign(:grouped_social_media_snippets, link.social_media_snippets.group_by(&:social_media_type)) + render + end + + context "with snippets" do + before do + FactoryBot.create(:social_media_snippet, link: link, social_media_type: "LinkedIn", content: "Copy for LinkedIn") + FactoryBot.create(:social_media_snippet, link: link, social_media_type: "Mastodon", content: "Copy for Mastodon") + end + + it "lists each snippet under its social media type" do + render_page + + expect(rendered).to include("Social Media Snippets", "LinkedIn", "Copy for LinkedIn", "Mastodon", "Copy for Mastodon") + end + + it "doesn't say where the copy came from, since it isn't only generated anymore" do + render_page + + expect(rendered).not_to match(/open ?ai/i) + expect(rendered).to include("Suggested copy for posting this article. Pair each snippet with one of the shortened links above.") + end + end + + it "leaves out the section when the link has no snippets" do + render_page + + expect(rendered).not_to include("Social Media Snippets") + end +end From 802f5226b232af36f1dfcf6a49ea394da965a145 Mon Sep 17 00:00:00 2001 From: Ernesto Tagwerker Date: Sun, 8 Nov 2026 23:09:10 -0500 Subject: [PATCH 3/3] Drop share_id from the snippets API social_media_snippets already has the columns the API needs (social_media_type, content, link_id), so this PR shouldn't change the table. Removes the share_id migration and schema change, the share association and its validations, the share_id param and JSON key, and the specs that covered them. Co-Authored-By: Claude Sonnet 5.5 --- .../social_media_snippets_controller.rb | 3 +- app/models/share.rb | 1 - app/models/social_media_snippet.rb | 14 -------- ...5414_add_share_to_social_media_snippets.rb | 10 ------ db/schema.rb | 5 +-- spec/models/social_media_snippet_spec.rb | 31 ---------------- .../api_social_media_snippets_spec.rb | 35 +------------------ 7 files changed, 3 insertions(+), 96 deletions(-) delete mode 100644 db/migrate/20261109035414_add_share_to_social_media_snippets.rb diff --git a/app/controllers/social_media_snippets_controller.rb b/app/controllers/social_media_snippets_controller.rb index 9b929bf..8bb5e88 100644 --- a/app/controllers/social_media_snippets_controller.rb +++ b/app/controllers/social_media_snippets_controller.rb @@ -34,7 +34,7 @@ def find_link end def snippet_params - params.require(:social_media_snippet).permit(:social_media_type, :content, :share_id) + params.require(:social_media_snippet).permit(:social_media_type, :content) end def snippet_json(snippet) @@ -43,7 +43,6 @@ def snippet_json(snippet) link_id: snippet.link_id, social_media_type: snippet.social_media_type, content: snippet.content, - share_id: snippet.share_id, created_at: snippet.created_at } end diff --git a/app/models/share.rb b/app/models/share.rb index a884673..4bfc4f7 100644 --- a/app/models/share.rb +++ b/app/models/share.rb @@ -1,6 +1,5 @@ class Share < ApplicationRecord belongs_to :link - has_many :social_media_snippets, dependent: :nullify UTM_SOURCES = %w[ Bing diff --git a/app/models/social_media_snippet.rb b/app/models/social_media_snippet.rb index dcbcaf5..1aa229e 100644 --- a/app/models/social_media_snippet.rb +++ b/app/models/social_media_snippet.rb @@ -8,7 +8,6 @@ class SocialMediaSnippet < ApplicationRecord ].sort! belongs_to :link - belongs_to :share, optional: true validates :content, presence: true # on: :create so snippets saved before this list existed stay editable. @@ -17,17 +16,4 @@ class SocialMediaSnippet < ApplicationRecord validates :content, uniqueness: { scope: %i[link_id social_media_type], message: "has already been saved for this link and social media type", allow_blank: true }, on: :create - validate :share_belongs_to_link - - private - - def share_belongs_to_link - return if share_id.blank? - - if share.nil? - errors.add(:share, "must exist") - elsif share.link_id != link_id - errors.add(:share, "must belong to the same link") - end - end end diff --git a/db/migrate/20261109035414_add_share_to_social_media_snippets.rb b/db/migrate/20261109035414_add_share_to_social_media_snippets.rb deleted file mode 100644 index 617de6a..0000000 --- a/db/migrate/20261109035414_add_share_to_social_media_snippets.rb +++ /dev/null @@ -1,10 +0,0 @@ -class AddShareToSocialMediaSnippets < ActiveRecord::Migration[7.0] - # Optional link back to the tracked share (short URL) a snippet was posted - # with. Nullable because every existing snippet predates it, and nullified - # (not blocked) when a share is deleted, so removing a share never fails - # because of the copy that points at it. - def change - add_reference :social_media_snippets, :share, null: true, index: true, - foreign_key: { on_delete: :nullify } - end -end diff --git a/db/schema.rb b/db/schema.rb index 2fee03f..fc71bf8 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.0].define(version: 2026_11_09_035414) do +ActiveRecord::Schema[7.0].define(version: 2026_04_29_134259) do # These are extensions that must be enabled in order to support this database enable_extension "pg_trgm" enable_extension "plpgsql" @@ -59,9 +59,7 @@ t.bigint "link_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.bigint "share_id" t.index ["link_id"], name: "index_social_media_snippets_on_link_id" - t.index ["share_id"], name: "index_social_media_snippets_on_share_id" end create_table "users", force: :cascade do |t| @@ -82,5 +80,4 @@ add_foreign_key "personal_access_tokens", "users" add_foreign_key "shares", "links" add_foreign_key "social_media_snippets", "links" - add_foreign_key "social_media_snippets", "shares", on_delete: :nullify end diff --git a/spec/models/social_media_snippet_spec.rb b/spec/models/social_media_snippet_spec.rb index 21d6a63..7d3132c 100644 --- a/spec/models/social_media_snippet_spec.rb +++ b/spec/models/social_media_snippet_spec.rb @@ -61,36 +61,5 @@ expect(SocialMediaSnippet.new(link: other, social_media_type: "LinkedIn", content: "Same copy")).to be_valid end end - - context "share" do - let(:link) { FactoryBot.create(:link) } - - it "is optional" do - expect(SocialMediaSnippet.new(link: link, social_media_type: "LinkedIn", content: "Copy")).to be_valid - end - - it "must belong to the same link" do - other_share = FactoryBot.create(:share, link: FactoryBot.create(:link, url: "https://www.fastruby.io/blog/other.html")) - snippet = SocialMediaSnippet.new(link: link, social_media_type: "LinkedIn", content: "Copy", share: other_share) - - expect(snippet).not_to be_valid - expect(snippet.errors[:share]).to eq ["must belong to the same link"] - end - - it "must exist when a share_id is given" do - snippet = SocialMediaSnippet.new(link: link, social_media_type: "LinkedIn", content: "Copy", share_id: 0) - - expect(snippet).not_to be_valid - expect(snippet.errors[:share]).to eq ["must exist"] - end - - it "is nullified, not blocked, when the share is destroyed" do - share = FactoryBot.create(:share, link: link) - snippet = FactoryBot.create(:social_media_snippet, link: link, share: share) - - expect { share.destroy }.to change(Share, :count).by(-1) - expect(snippet.reload.share_id).to be_nil - end - end end end diff --git a/spec/requests/api_social_media_snippets_spec.rb b/spec/requests/api_social_media_snippets_spec.rb index f43b24e..7cccdef 100644 --- a/spec/requests/api_social_media_snippets_spec.rb +++ b/spec/requests/api_social_media_snippets_spec.rb @@ -19,7 +19,7 @@ expect(response).to have_http_status(:ok) body = JSON.parse(response.body) expect(body.map { |s| s['content'] }).to eq(['On LinkedIn', 'On Mastodon']) - expect(body.first.keys).to match_array(%w[id link_id social_media_type content share_id created_at]) + expect(body.first.keys).to match_array(%w[id link_id social_media_type content created_at]) end it 'filters by social_media_type' do @@ -73,7 +73,6 @@ def post_snippet(params, headers: auth_headers, to: link) expect(body['link_id']).to eq(link.id) expect(body['social_media_type']).to eq('LinkedIn') expect(body['content']).to eq('A fresh take on the post.') - expect(body['share_id']).to be_nil expect(body['id']).to be_present expect(body['created_at']).to be_present end @@ -93,38 +92,6 @@ def post_snippet(params, headers: auth_headers, to: link) expect(contents).to eq(['A fresh take on the post.']) end - context 'with a share_id' do - let!(:share) { FactoryBot.create(:share, link: link) } - - it 'ties the snippet to the share' do - post_snippet({ social_media_snippet: valid_params[:social_media_snippet].merge(share_id: share.id) }) - - expect(response).to have_http_status(:created) - expect(JSON.parse(response.body)['share_id']).to eq(share.id) - expect(SocialMediaSnippet.last.share).to eq(share) - end - - it 'returns 422 when the share belongs to another link' do - other_share = FactoryBot.create(:share, link: other_link) - - expect { - post_snippet({ social_media_snippet: valid_params[:social_media_snippet].merge(share_id: other_share.id) }) - }.not_to change(SocialMediaSnippet, :count) - - expect(response).to have_http_status(:unprocessable_entity) - expect(JSON.parse(response.body)['errors']).to include('Share must belong to the same link') - end - - it 'returns 422, not a server error, when the share does not exist' do - expect { - post_snippet({ social_media_snippet: valid_params[:social_media_snippet].merge(share_id: 0) }) - }.not_to change(SocialMediaSnippet, :count) - - expect(response).to have_http_status(:unprocessable_entity) - expect(JSON.parse(response.body)['errors']).to include('Share must exist') - end - end - it 'returns 422 when content is missing' do expect { post_snippet({ social_media_snippet: { social_media_type: 'LinkedIn', content: '' } })