Sha256: 8d6be89a72a027ed1b3b28d8cd929d799d56e192fe774269f8c9044fc49ca67b
Contents?: true
Size: 1.6 KB
Versions: 5
Compression:
Stored size: 1.6 KB
Contents
# frozen_string_literal: true module Paperweight # Adds the `paperweight` hook in ActiveRecord models. class Railtie < Rails::Railtie # Extensions that need be generated from input. class DynamicExtension < Module def initialize(styles) define_method(:image_styles) { styles } end end # Extensions that are the same for each kind of attachment. module StaticExtension def clear_image_url @image_url = nil end def image Image.new(self) end def image? image_uuid end def image_url @image_url end def image_url=(url) assign_attributes( image_processing: url ? true : false, image_uuid: url ? SecureRandom.uuid : nil ) @image_url = url end def self.included(base) base.after_commit do PurgeJob.perform_later_for(self, prev_image_uuid) if prev_image_uuid ThumbnailsJob.perform_later(self, image_url) if image_url end base.before_destroy if: :image? do PurgeJob.perform_later_for(self) end end private def prev_image_uuid previous_changes.fetch('image_uuid', [])[0] end end # Provides the `has_image` method to attach to models. module Hook def has_image(styles = {}) # rubocop:disable Naming/PredicateName extend DynamicExtension.new({ original: '' }.merge!(styles)) include StaticExtension end end initializer 'paperweight' do ActiveSupport.on_load(:active_record) { extend Hook } end end end
Version data entries
5 entries across 5 versions & 1 rubygems