Module: Shrine::Plugins::KithePromotionHooks::AttacherMethods

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

Instance Method Summary collapse

Instance Method Details

#dumpObject

Overridden so our context is serialized for backgrounding.



99
100
101
102
103
104
105
# File 'lib/shrine/plugins/kithe_promotion_hooks.rb', line 99

def dump
  super.tap do |hash|
    if context[:promotion_directives]
      hash["promotion_directives"] = context[:promotion_directives]
    end
  end
end

#promote(uploaded_file = get, **options) ⇒ Object

Overridden to: a) refresh metadata as part of promotion (adds ‘promoting: true` to context for such) b) call promotion callbacks on Asset model, unless `promotion_directives`

has been set.


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/shrine/plugins/kithe_promotion_hooks.rb', line 111

def promote(uploaded_file = get, **options)
  # insist on a metadata extraction, add a new key `promoting: true` in case
  # anyone is interested.

  uploaded_file.refresh_metadata!(context.merge(options).merge(promoting: true))

  # Now run ordinary promotion with activemodel callbacks from
  # the Asset, which will automatically allow them to cancel promotion using
  # ordinary activemodel callbacck technique of `throw :abort`.
  if ( !promotion_directives[:skip_callbacks] &&
       context[:record] &&
       context[:record].class.respond_to?(:_promotion_callbacks) )
    context[:record].run_callbacks(:promotion) do
      super(uploaded_file, **options)
    end
  else
    super(uploaded_file, **options)
  end
end

#promotion_directivesObject

context, lazily initializing to hash for convenience.



93
94
95
# File 'lib/shrine/plugins/kithe_promotion_hooks.rb', line 93

def promotion_directives
  context[:promotion_directives] ||= {}
end

#set_promotion_directives(hash) ⇒ Object

Set one or more promotion directives, in context, that will be serialized and restored to context for bg promotion. The values are intended to be simple strings or other json-serializable primitives.

set_promotion_directives will merge it’s results into existing promotion directives, existing keys will remain. So you can set multiple directives with multiple calls to set_promotion_directives, or pass multiple keys to one calls.

Examples:

some_model.file_attacher.set_promotion_directives(skip_callbacks: true)
some_model.save!


83
84
85
86
87
88
89
90
# File 'lib/shrine/plugins/kithe_promotion_hooks.rb', line 83

def set_promotion_directives(hash)
  unrecognized = hash.keys.collect(&:to_sym) - KithePromotionHooks.allowed_promotion_directives
  unless unrecognized.length == 0
    raise ArgumentError.new("Unrecognized promotion directive key: #{unrecognized.join('')}")
  end

  promotion_directives.merge!(hash)
end