Skip to content

Commit 807fe15

Browse files
committed
Allow cover and card images to defer to category cover/card image for categorizable records if category has cover image but categorizable record does not
1 parent d70387d commit 807fe15

File tree

26 files changed

+358
-179
lines changed

26 files changed

+358
-179
lines changed

app/controllers/better_together/categories_controller.rb

Lines changed: 6 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,75 +2,17 @@
22

33
module BetterTogether
44
class CategoriesController < FriendlyResourceController # rubocop:todo Style/Documentation
5-
before_action :set_model_instance, only: %i[show edit update destroy]
6-
before_action :authorize_category, only: %i[show edit update destroy]
7-
after_action :verify_authorized, except: :index
8-
9-
# GET /categories
10-
def index
11-
authorize resource_class
12-
@categories = resource_class.all
13-
end
14-
15-
# GET /categories/1
16-
def show; end
17-
18-
# GET /categories/new
19-
def new
20-
@category = resource_class.new
21-
authorize_category
22-
end
23-
24-
# GET /categories/1/edit
25-
def edit; end
26-
27-
# POST /categories
28-
def create
29-
@category = resource_class.new(category_params)
30-
authorize_category
31-
32-
if @category.save
33-
redirect_to @category, notice: 'Category was successfully created.'
34-
else
35-
render :new, status: :unprocessable_entity
36-
end
37-
end
38-
39-
# PATCH/PUT /categories/1
40-
def update
41-
if @category.update(category_params)
42-
redirect_to @category, notice: 'Category was successfully updated.', status: :see_other
43-
else
44-
render :edit, status: :unprocessable_entity
45-
end
5+
before_action only: %i[new edit index], if: -> { Rails.env.development? } do
6+
# Make sure that all subclasses are loaded in dev to generate type selector
7+
Rails.application.eager_load!
468
end
479

48-
# DELETE /categories/1
49-
def destroy
50-
@category.destroy!
51-
redirect_to categories_url, notice: 'Category was successfully destroyed.', status: :see_other
52-
end
10+
# def index
11+
# raise 'test'
12+
# end
5313

5414
protected
5515

56-
# Adds a policy check for the category
57-
def authorize_category
58-
authorize @category
59-
end
60-
61-
def set_model_instance
62-
@category = set_resource_instance
63-
end
64-
65-
# Only allow a list of trusted parameters through.
66-
def category_params
67-
permitted = [
68-
*resource_class.extra_permitted_attributes
69-
]
70-
71-
params.require(resource_class.name.demodulize.underscore.to_sym).permit(permitted)
72-
end
73-
7416
def resource_class
7517
::BetterTogether::Category
7618
end

app/controllers/better_together/events_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
module BetterTogether
44
# CRUD for BetterTogether::Event
55
class EventsController < FriendlyResourceController
6+
before_action if: -> { Rails.env.development? } do
7+
# Make sure that all subclasses are loaded in dev to generate type selector
8+
Rails.application.eager_load!
9+
end
10+
611
def index
712
@draft_events = @events.draft
813
@upcoming_events = @events.upcoming

app/helpers/better_together/badges_helper.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,27 @@
33
module BetterTogether
44
# Helpers for Badges
55
module BadgesHelper
6+
def categories_badge(entity, rounded: true, style: 'info')
7+
return unless entity.respond_to?(:categories) && entity.categories.any?
8+
9+
entity.categories.map do |category|
10+
create_badge(category.name, rounded: rounded, style: style)
11+
end.join(' ').html_safe
12+
end
13+
614
def privacy_badge(entity, rounded: true, style: 'primary')
715
return unless entity.respond_to? :privacy
816

9-
badge_label = entity.privacy.humanize.capitalize
17+
create_badge(entity.privacy.humanize.capitalize, rounded: rounded, style: style)
18+
end
19+
20+
private
21+
22+
def create_badge(label, rounded: true, style: 'primary')
1023
rounded_class = rounded ? 'rounded-pill' : ''
1124
style_class = "text-bg-#{style}"
1225

13-
content_tag :span, badge_label, class: "badge #{rounded_class} #{style_class}"
26+
content_tag :span, label, class: "badge #{rounded_class} #{style_class}"
1427
end
1528
end
1629
end

app/helpers/better_together/content/blocks_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Content
66
module BlocksHelper
77
# Returns an array of acceptable image file types
88
def acceptable_image_file_types
9-
BetterTogether::Content::Image::CONTENT_TYPES
9+
BetterTogether::Attachments::Images::VALID_IMAGE_CONTENT_TYPES
1010
end
1111

1212
# Helper to generate a unique temp_id for a model

app/helpers/better_together/image_helper.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,16 @@ def cover_image_tag(entity, options = {}) # rubocop:todo Metrics/MethodLength, M
2929
entity.cover_image_variant(image_width, image_height)
3030
end
3131

32-
image_tag(attachment.url, **image_tag_attributes)
32+
image_tag(rails_storage_proxy_url(attachment), **image_tag_attributes)
33+
elsif entity.respond_to?(:categories) && entity.categories.with_cover_images.any?
34+
category = entity.categories.with_cover_images.first
35+
36+
attachment = if category.respond_to?(:optimized_cover_image)
37+
category.optimized_cover_image
38+
else
39+
category.cover_image_variant(image_width, image_height)
40+
end
41+
image_tag(rails_storage_proxy_url(attachment), **image_tag_attributes)
3342
else
3443
# Use a default image based on the entity type
3544
default_image = default_cover_image(entity, image_format)
@@ -61,6 +70,15 @@ def card_image_tag(entity, options = {}) # rubocop:todo Metrics/MethodLength, Me
6170
end
6271

6372
image_tag(rails_storage_proxy_url(attachment), **image_tag_attributes)
73+
elsif entity.respond_to?(:categories) && entity.categories.with_cover_images.any?
74+
category = entity.categories.with_cover_images.first
75+
76+
attachment = if category.respond_to?(:optimized_cover_image)
77+
category.optimized_cover_image
78+
else
79+
category.cover_image_variant(image_width, image_height)
80+
end
81+
image_tag(rails_storage_proxy_url(attachment), **image_tag_attributes)
6482
else
6583
# Use a default image based on the entity type
6684
default_image = default_card_image(entity, image_format)

app/models/better_together/category.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,35 @@
22

33
module BetterTogether
44
class Category < ApplicationRecord # rubocop:todo Style/Documentation
5+
include Attachments::Images
56
include Identifier
67
include Positioned
78
include Protected
89
include Translatable
910

11+
attachable_cover_image
12+
1013
has_many :categorizations, dependent: :destroy
1114
has_many :pages, through: :categorizations, source: :categorizable, source_type: 'BetterTogether::Page'
1215

1316
translates :name, type: :string
1417
translates :description, backend: :action_text
1518

19+
slugged :name
20+
1621
validates :name, presence: true
1722
validates :type, presence: true
1823

19-
def to_s
20-
name
24+
def self.permitted_attributes(id: false, destroy: false)
25+
super + [
26+
:type, :icon
27+
]
2128
end
29+
30+
def as_category
31+
becomes(self.class.base_class)
32+
end
33+
34+
configure_attachment_cleanup
2235
end
2336
end

app/models/better_together/event.rb

Lines changed: 8 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
module BetterTogether
44
# A Schedulable Event
55
class Event < ApplicationRecord
6+
include Attachments::Images
7+
include Categorizable
68
include Creatable
79
include FriendlySlug
810
include Geography::Geospatial::One
@@ -11,6 +13,10 @@ class Event < ApplicationRecord
1113
include Privacy
1214
include Viewable
1315

16+
attachable_cover_image
17+
18+
categorizable(class_name: 'BetterTogether::EventCategory')
19+
1420
# belongs_to :address, -> { where(physical: true, primary_flag: true) }
1521
# accepts_nested_attributes_for :address, allow_destroy: true, reject_if: :blank?
1622
# delegate :geocoding_string, to: :address, allow_nil: true
@@ -21,33 +27,7 @@ class Event < ApplicationRecord
2127
translates :name
2228
translates :description, backend: :action_text
2329

24-
has_one_attached :cover_image do |attachable|
25-
attachable.variant :optimized_jpeg, resize_to_limit: [2400, 1200],
26-
# rubocop:todo Layout/LineLength
27-
saver: { strip: true, quality: 85, interlace: true, optimize_coding: true, trellis_quant: true, quant_table: 3 }, format: 'jpg'
28-
# rubocop:enable Layout/LineLength
29-
attachable.variant :optimized_png, resize_to_limit: [2400, 1200],
30-
saver: { strip: true, quality: 85, optimize_coding: true }, format: 'png'
31-
32-
attachable.variant :optimized_card_jpeg, resize_to_limit: [1200, 300],
33-
# rubocop:todo Layout/LineLength
34-
saver: { strip: true, quality: 90, interlace: true, optimize_coding: true, trellis_quant: true, quant_table: 3 }, format: 'jpg'
35-
# rubocop:enable Layout/LineLength
36-
attachable.variant :optimized_card_png, resize_to_limit: [1200, 300],
37-
saver: { strip: true, quality: 90, optimize_coding: true }, format: 'png'
38-
end
39-
40-
CONTENT_TYPES = %w[image/jpeg image/png image/gif image/webp image/svg+xml].freeze
41-
42-
validates :cover_image,
43-
content_type: CONTENT_TYPES,
44-
size: { less_than: 100.megabytes, message: 'is too large' }
45-
46-
alias card_image cover_image
47-
48-
attr_accessor :remove_cover_image
49-
50-
before_save :purge_cover_image, if: -> { remove_cover_image == '1' }
30+
validates :name, presence: true
5131

5232
scope :draft, lambda {
5333
start_query = arel_table[:starts_at].eq(nil)
@@ -74,36 +54,6 @@ def self.permitted_attributes(id: false, destroy: false)
7454
]
7555
end
7656

77-
def optimized_card_image
78-
if card_image.content_type == 'image/svg+xml'
79-
# If SVG, return the original without transformation
80-
card_image
81-
82-
# For other formats, analyze to determine transparency
83-
elsif card_image.content_type == 'image/png'
84-
# If PNG with transparency, return the optimized PNG variant
85-
card_image.variant(:optimized_card_png).processed
86-
else
87-
# Otherwise, use the optimized JPG variant
88-
card_image.variant(:optimized_card_jpeg).processed
89-
end
90-
end
91-
92-
def optimized_cover_image
93-
if cover_image.content_type == 'image/svg+xml'
94-
# If SVG, return the original without transformation
95-
cover_image
96-
97-
# For other formats, analyze to determine transparency
98-
elsif cover_image.content_type == 'image/png'
99-
# If PNG with transparency, return the optimized PNG variant
100-
cover_image.variant(:optimized_png).processed
101-
else
102-
# Otherwise, use the optimized JPG variant
103-
cover_image.variant(:optimized_jpeg).processed
104-
end
105-
end
106-
10757
def schedule_address_geocoding
10858
return unless should_geocode?
10959

@@ -122,6 +72,6 @@ def to_s
12272
name
12373
end
12474

125-
include ::BetterTogether::RemoveableAttachment
75+
configure_attachment_cleanup
12676
end
12777
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module BetterTogether
2+
class EventCategory < Category
3+
has_many :events, through: :categorizations, source: :categorizable, source_type: 'BetterTogether::Event'
4+
end
5+
end

app/models/better_together/page.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class Page < ApplicationRecord
1010
include Privacy
1111
include Searchable
1212

13+
categorizable
14+
1315
PAGE_LAYOUTS = [
1416
'layouts/better_together/page',
1517
'layouts/better_together/page_with_nav',

app/models/better_together/post.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class Post < ApplicationRecord
1010
include Privacy
1111
include Publishable
1212

13+
categorizable
14+
1315
translates :title
1416
translates :content, type: :text
1517
# translates :content_html, type: :action_text

0 commit comments

Comments
 (0)