Module: Shrine::Plugins::KitheDerivativeDefinitions::AttacherMethods

Defined in:
lib/shrine/plugins/kithe_derivative_definitions.rb

Instance Method Summary collapse

Instance Method Details

#kithe_create_derivatives(only: nil, except: nil, lazy: false) ⇒ Object

Similar to shrine create_derivatives, but with kithe standards:

  • Will call the :kithe_derivatives processor (that handles any define_derivative definitions), plus any processors you’ve configured with kithe_include_derivatives_processors

  • Uses the methods added by :kithe_persisted_derivatives to add derivatives completely concurrency-safely, if the model had it’s attachment changed concurrently, you won’t get derivatives attached that belong to old version of original attachment, and won’t get any leftover “orphaned” derivatives either.

The :kithe_derivatives processor has additional logic and options for determining which derivative definitions – created with ‘define_deriative` will be executed:

  • Ordinarily will create a definition for every definition that has not been marked

`default_create: false`.
  • But you can also pass ‘only` and/or `except` to customize the list of definitions to be created, possibly including some that are `default_create: false`.

  • Will normally re-create derivatives (per existing definitions) even if they already exist, but pass ‘lazy: false` to skip creating if a derivative with a given key already exists. This will use the asset `derivatives` association, so if you are doing this in bulk for several assets, you should eager-load the derivatives association for efficiency.

If you’ve added any custom processors with ‘kithe_include_derivatives_processors`, it’s your responsibility to make them respect those options. See #process_kithe_derivative? helper method.

create_derivatives should be idempotent. If it has failed having only created some derivatives, you can always just run it again.



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/shrine/plugins/kithe_derivative_definitions.rb', line 156

def kithe_create_derivatives(only: nil, except: nil, lazy: false)
  return false unless file

  local_files = self.process_derivatives(:kithe_derivatives, only: only, except: except, lazy: lazy)

  # include any other configured processors
  self.kithe_include_derivatives_processors.each do |processor|
    local_files.merge!(
      self.process_derivatives(processor.to_sym, only: only, except: except, lazy: lazy)
    )
  end

  self.add_persisted_derivatives(local_files)
end

#process_any_kithe_derivative?(keys, **options) ⇒ Boolean

Convenience to check #process_kithe_derivative? for multiple keys at once,

Examples:

process_any_kithe_derivative?([:thumb_mini, :thumb_large], only: [:thumb_large, :thumb_mega], lazy: true)

Returns:

  • (Boolean)

    true if any key returns true



197
198
199
# File 'lib/shrine/plugins/kithe_derivative_definitions.rb', line 197

def process_any_kithe_derivative?(keys, **options)
  keys.any? { |k| process_kithe_derivative?(k, **options) }
end

#process_kithe_derivative?(key, **options) ⇒ Boolean

a helper method that you can use in your own shrine processors to handle only/except/lazy guarding logic.

Parameters:

  • key (Symbol)

    derivative key to check for guarded processing

  • only (Array<Symbol>)

    If present, method will only return true if ‘key` is included in `only`

  • except (Array<Symbol] If present, method will only return true if `key` is NOT included in `except`)

    xcept [Array<Symbol] If present, method will only return true if ‘key` is NOT included in `except`

  • lazy (Boolean)

    If true, method will only return true if derivative key is not already present in attacher with a value.

Returns:

  • (Boolean)

    should the ‘key` be processed based on only/except/lazy conditions?



182
183
184
185
186
187
188
189
190
191
# File 'lib/shrine/plugins/kithe_derivative_definitions.rb', line 182

def process_kithe_derivative?(key, **options)
  key = key.to_sym
  only = options[:only] && Array(options[:only]).map(&:to_sym)
  except = options[:except] && Array(options[:except]).map(&:to_sym)
  lazy = !! options[:lazy]

  (only.nil? ? true : only.include?(key)) &&
  (except.nil? || ! except.include?(key)) &&
  (!lazy || !derivatives.keys.include?(key))
end