diff --git a/app/controllers/social_previews_controller.rb b/app/controllers/social_previews_controller.rb index 15751d8bb..fc25d074e 100644 --- a/app/controllers/social_previews_controller.rb +++ b/app/controllers/social_previews_controller.rb @@ -9,7 +9,8 @@ class SocialPreviewsController < ApplicationController not_found unless @article.published template = @article.tags. - where("tags.social_preview_template IS NOT NULL AND tags.social_preview_template != ?", "article"). + where.not(social_preview_template: nil). + where.not(social_preview_template: "article"). select(:social_preview_template).first&.social_preview_template # make sure that the template exists @@ -25,8 +26,7 @@ class SocialPreviewsController < ApplicationController end def listing - @listing = ClassifiedListing.find(params[:id]) - define_categories + @listing = ClassifiedListing.find(params[:id]).decorate set_respond end @@ -51,25 +51,6 @@ class SocialPreviewsController < ApplicationController private - # TODO: [thepracticaldev/oss] don't hardcode this - def define_categories - cat_info = { - "collabs": ["Collaborators Wanted", "#5AE8D9"], - "cfp": ["Call For Proposal", "#f58f8d"], - "forhire": ["Available For Hire", "#b78cf4"], - "education": ["Education", "#5AABE8"], - "jobs": ["Now Hiring", "#53c3ad"], - "mentors": ["Offering Mentorship", "#A69EE8"], - "mentees": ["Looking For Mentorship", "#88aedb"], - "forsale": ["Stuff For Sale", "#d0adfb"], - "events": ["Upcoming Event", "#f8b3d0"], - "misc": ["Miscellaneous", "#6393FF"], - "products": ["Products & Tools", "#5AE8D9"] - } - @category = cat_info[@listing.category.to_sym][0] - @cat_color = cat_info[@listing.category.to_sym][1] - end - def set_respond(template = nil) respond_to do |format| format.html do diff --git a/app/dashboards/classified_listing_category_dashboard.rb b/app/dashboards/classified_listing_category_dashboard.rb index ec436426c..901b3d287 100644 --- a/app/dashboards/classified_listing_category_dashboard.rb +++ b/app/dashboards/classified_listing_category_dashboard.rb @@ -15,6 +15,8 @@ class ClassifiedListingCategoryDashboard < Administrate::BaseDashboard name: Field::String, rules: Field::String, slug: Field::String, + social_preview_description: Field::String, + social_preview_color: Field::String, updated_at: Field::DateTime }.freeze diff --git a/app/decorators/classified_listing_decorator.rb b/app/decorators/classified_listing_decorator.rb new file mode 100644 index 000000000..983c84f77 --- /dev/null +++ b/app/decorators/classified_listing_decorator.rb @@ -0,0 +1,14 @@ +class ClassifiedListingDecorator < ApplicationDecorator + DEFAULT_COLOR = "#000000".freeze + + def social_preview_category + category = object.classified_listing_category + category.social_preview_description.presence || category.name + end + + def social_preview_color(brightness: 1.0) + category = object.classified_listing_category + color = category.social_preview_color.presence || DEFAULT_COLOR + HexComparer.new([color]).brightness(brightness) + end +end diff --git a/app/models/classified_listing_category.rb b/app/models/classified_listing_category.rb index 417246e82..1554b5e34 100644 --- a/app/models/classified_listing_category.rb +++ b/app/models/classified_listing_category.rb @@ -1,6 +1,21 @@ class ClassifiedListingCategory < ApplicationRecord has_many :classified_listings + before_validation :normalize_social_preview_color + validates :name, :cost, :rules, :slug, presence: true validates :name, :slug, uniqueness: true + + # This needs to be a hex color of format "#CCC" or "#A1B2C3" + validates :social_preview_color, + format: /\A#(?:[a-f0-9]{3}){1,2}\z/, + allow_blank: true + + private + + def normalize_social_preview_color + return unless social_preview_color + + self.social_preview_color = social_preview_color.downcase + end end diff --git a/app/views/social_previews/listing.html.erb b/app/views/social_previews/listing.html.erb index 6f485d788..4233aa43f 100644 --- a/app/views/social_previews/listing.html.erb +++ b/app/views/social_previews/listing.html.erb @@ -35,7 +35,7 @@ } h1 { - color: <%= HexComparer.new([@cat_color]).brightness(0.70) %>; + color: <%= @listing.social_preview_color(brightness: 0.70) %>; width: 92%; margin: 0; padding: 1vw; @@ -46,7 +46,7 @@ font-size: 3.1vw; padding: 0 4vw; font-family: "Helvetica Neue", Helvetica, Arial, "Lucida Grande", "Roboto", sans-serif; - color: <%= HexComparer.new([@cat_color]).brightness(0.77) %>; + color: <%= @listing.social_preview_color(brightness: 0.77) %>; } .preview-category { @@ -78,7 +78,7 @@ <%= sanitize_rendered_markdown(@listing.processed_html) %>
- <%= @category %> + <%= @listing.social_preview_category %>