Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow organisation to use FeaturedImageData #8399

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/models/featured_image_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class FeaturedImageData < ApplicationRecord
validates :file, presence: true, if: :image_changed?
validates_with ImageValidator, size: [960, 640], if: :image_changed?

has_many :assets,
as: :assetable,
inverse_of: :assetable

private

def image_changed?
Expand Down
1 change: 1 addition & 0 deletions app/models/organisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Organisation < ApplicationRecord
FEATURED_DOCUMENTS_DISPLAY_LIMIT = 6

belongs_to :default_news_image, class_name: "DefaultNewsOrganisationImageData", foreign_key: :default_news_organisation_image_data_id
belongs_to :default_news_image_new, class_name: "FeaturedImageData", foreign_key: :featured_image_data_id

has_many :assets,
as: :assetable,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddFeaturedImageDataRefToOrganisations < ActiveRecord::Migration[7.0]
def change
add_reference :organisations, :featured_image_data, index: true
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_10_23_110956) do
ActiveRecord::Schema[7.0].define(version: 2023_10_23_142600) do
create_table "assets", charset: "utf8mb3", force: :cascade do |t|
t.string "asset_manager_id", null: false
t.string "variant", null: false
Expand Down Expand Up @@ -737,8 +737,10 @@
t.string "homepage_type", default: "news"
t.boolean "political", default: false
t.integer "ministerial_ordering"
t.bigint "featured_image_data_id"
t.index ["content_id"], name: "index_organisations_on_content_id", unique: true
t.index ["default_news_organisation_image_data_id"], name: "index_organisations_on_default_news_organisation_image_data_id"
t.index ["featured_image_data_id"], name: "index_organisations_on_featured_image_data_id"
t.index ["organisation_logo_type_id"], name: "index_organisations_on_organisation_logo_type_id"
t.index ["organisation_type_key"], name: "index_organisations_on_organisation_type_key"
t.index ["slug"], name: "index_organisations_on_slug", unique: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Admin::Organisations::Show::SummaryListComponentTest < ViewComponent::Test
end

test "renders default news image row if the organisation has a default news image" do
news_image = build_stubbed(:default_news_organisation_image_data)
news_image = build_stubbed(:featured_image_data)
organisation = build_stubbed(:ministerial_department, default_news_image: news_image)

render_inline(Admin::Organisations::Show::SummaryListComponent.new(organisation:))
Expand Down
2 changes: 1 addition & 1 deletion test/unit/app/models/edition/lead_image_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Edition::LeadImageTest < ActiveSupport::TestCase

test "#lead_image_has_all_assets? returns true if the lead image data doesn't implement all_asset_variants_uploaded?" do
# e.g. DefaultNewsOrganisationImageData doesn't yet implement all_asset_variants_uploaded?
image = build(:default_news_organisation_image_data)
image = build(:featured_image_data)
organisation = build(:organisation, default_news_image: image)
model = stub("Target", { images: [], lead_organisations: [], organisations: [organisation] }).extend(Edition::LeadImage)

Expand Down
28 changes: 26 additions & 2 deletions test/unit/app/models/organisation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,14 @@ class OrganisationTest < ActiveSupport::TestCase
assert organisation.errors[:logo].present?
end

test "can have a default news article image" do
test "can have a default news article image legacy" do
image = build(:default_news_organisation_image_data)
organisation = build(:organisation, default_news_image_legacy: image)
assert_equal image, organisation.default_news_image_legacy
end

test "can have a default news article image" do
image = build(:featured_image_data)
organisation = build(:organisation, default_news_image: image)
assert_equal image, organisation.default_news_image
end
Expand Down Expand Up @@ -1069,7 +1075,25 @@ class OrganisationTest < ActiveSupport::TestCase
end

organisation.update!(
default_news_image: create(:default_news_organisation_image_data),
default_news_image: create(:featured_image_data),
)
end

test "#save triggers organisation with a changed default news organisation image legacy to republish news articles" do
organisation = create(:organisation)

documents = NewsArticle
.in_organisation(organisation)
.includes(:images)
.where(images: { id: nil })
.map(&:document)

documents.each do |d|
Whitehall::PublishingApi.expects(:republish_document_async).with(d)
end

organisation.update!(
default_news_image_legacy: create(:default_news_organisation_image_data),
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def present(edition)
end

test "falls back to the organisation's default news image when there is no image" do
organisation_image = DefaultNewsOrganisationImageData.new(file: image_fixture_file)
organisation_image = build(:featured_image_data)
organisation = create(:organisation, default_news_image: organisation_image)

case_study = create(:published_case_study, lead_organisations: [organisation])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def govspeak_to_html(govspeak)

test "presents an Organisation ready for adding to the publishing API" do
parent_organisation = create(:organisation, name: "Department for Stuff")
news_image = create(:default_news_organisation_image_data)
news_image = create(:featured_image_data)
organisation = create(
:organisation,
name: "Organisation of Things",
Expand Down
Loading