Module: SocialConstruct::Controller
- Extended by:
- ActiveSupport::Concern
- Included in:
- PreviewsController
- Defined in:
- app/controllers/concerns/social_construct/controller.rb
Instance Method Summary collapse
-
#render(*args) ⇒ Object
Allow using render with social cards.
-
#send_social_card(card, cache_key: nil, expires_in: 7.days, cache_in_development: false) ⇒ Object
Render a social card as PNG with caching support.
Instance Method Details
#render(*args) ⇒ Object
Allow using render with social cards
36 37 38 39 40 41 42 43 44 45 46 |
# File 'app/controllers/concerns/social_construct/controller.rb', line 36 def render(*args) return super unless args.first.is_a?(SocialConstruct::BaseCard) card = args.first = args.second || {} respond_to do |format| format.png { (card, **) } format.html { render(html: card.render.html_safe, layout: false) } end end |
#send_social_card(card, cache_key: nil, expires_in: 7.days, cache_in_development: false) ⇒ Object
Render a social card as PNG with caching support
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'app/controllers/concerns/social_construct/controller.rb', line 10 def (card, cache_key: nil, expires_in: 7.days, cache_in_development: false) # Build cache key if provided if cache_key && (!Rails.env.development? || cache_in_development) cache_key = Array(cache_key).join("-") if cache_key.is_a?(Array) png_data = Rails.cache.fetch(cache_key, expires_in: expires_in) do card.to_png end else png_data = card.to_png end # Set caching headers expires_in(1.day, public: true) unless Rails.env.development? send_data( png_data, type: "image/png", disposition: "inline", filename: "#{controller_name.singularize}-social-card.png" ) rescue => e (e) end |