Class: Kithe::Asset
- Includes:
- SetShrineUploader
- Defined in:
- app/models/kithe/asset.rb
Defined Under Namespace
Modules: SetShrineUploader Classes: DerivativeCreator, DerivativeDefinition
Instance Method Summary collapse
-
#create_derivatives(only: nil, except: nil, lazy: false) ⇒ Object
A convenience to call file_attacher.create_persisted_derivatives (from :kithe_derivatives) to create derivatives with conncurrent access safety, with the :kithe_derivatives processor argument, to create derivatives defined using kithe_derivative_definitions.
-
#initialize(*args) ⇒ Asset
constructor
A new instance of Asset.
-
#promote(action: :store, **context) ⇒ Object
Runs the shrine promotion step, that we normally have in backgrounding, manually and in foreground.
-
#remove_derivatives(*keys) ⇒ Object
just a convenience for kithe remove_persisted_derivatives.
-
#representative ⇒ Object
(also: #leaf_representative)
An Asset is it’s own representative.
- #representative_id ⇒ Object (also: #leaf_representative_id)
-
#update_derivative(key, io, **options) ⇒ Shrine::UploadedFile
Just a convennience for file_attacher.add_persisted_derivatives (from :kithe_derivatives), feel free to use that if you want to add more than one etc.
- #update_derivatives(deriv_hash, **options) ⇒ Object
Methods inherited from Model
#friendlier_id, #set_leaf_representative, #to_param
Methods included from Indexable
auto_callbacks?, index_with, #update_index
Constructor Details
#initialize(*args) ⇒ Asset
Returns a new instance of Asset.
187 188 189 190 191 192 193 194 |
# File 'app/models/kithe/asset.rb', line 187 def initialize(*args) super # copy class-level global promotion directives as initial instance value if self.class.promotion_directives.present? self.set_promotion_directives(self.class.promotion_directives) end end |
Instance Method Details
#create_derivatives(only: nil, except: nil, lazy: false) ⇒ Object
A convenience to call file_attacher.create_persisted_derivatives (from :kithe_derivatives) to create derivatives with conncurrent access safety, with the :kithe_derivatives processor argument, to create derivatives defined using kithe_derivative_definitions.
This is designed for use with kithe_derivatives processor, and has options only relevant to it, although could be expanded to take a processor argument in the future if needed.
Create derivatives for every definition added to uploader/attacher with kithe_derivatives ‘define_derivative`. 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`.
create_derivatives should be idempotent. If it has failed having only created some derivatives, you can always just run it again.
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.
## Avoiding eager-download
Additionally, this has a feature to ‘trick’ shrine into not eager downloading the original before our kithe_derivatives processor has a chance to decide if it needs to create any derivatives (based on ‘lazy` arg), making no-op create_derivatives so much faster and more efficient, which matters when doing bulk operations say with our rake task.
kithe_derivatives processor must then do a Shrine.with_file to make sure it’s a local file, when needed.
80 81 82 83 84 85 86 87 88 |
# File 'app/models/kithe/asset.rb', line 80 def create_derivatives(only: nil, except: nil, lazy: false) source = file return false unless source #local_files = file_attacher.process_derivatives(:kithe_derivatives, only: only, except: except, lazy: lazy) local_files = _process_kithe_derivatives_without_download(source, only: only, except: except, lazy: lazy) file_attacher.add_persisted_derivatives(local_files) end |
#promote(action: :store, **context) ⇒ Object
Runs the shrine promotion step, that we normally have in backgrounding, manually and in foreground. You might use this if a promotion failed and you need to re-run it, perhaps in bulk. It’s also useful in tests. Also persists, using shrine ‘atomic_promote`.
This will no-op unless the attached file is stored in cache – that is, it will no-op if the file has already been promoted. In this way it matches ordinary shrine promotion. (Do we need an option to force promotion anyway?)
Note that calling ‘file_attacher.promote` or `atomic_promote` on it’s own won’t do quite the same things.
166 167 168 169 170 171 172 173 174 175 |
# File 'app/models/kithe/asset.rb', line 166 def promote(action: :store, **context) return unless file_attacher.cached? context = { action: action, record: self }.merge(context) file_attacher.atomic_promote(**context) end |
#remove_derivatives(*keys) ⇒ Object
just a convenience for kithe remove_persisted_derivatives
152 153 154 |
# File 'app/models/kithe/asset.rb', line 152 def remove_derivatives(*keys) file_attacher.remove_persisted_derivatives(*keys) end |
#representative ⇒ Object Also known as: leaf_representative
An Asset is it’s own representative
178 179 180 |
# File 'app/models/kithe/asset.rb', line 178 def representative self end |
#representative_id ⇒ Object Also known as: leaf_representative_id
182 183 184 |
# File 'app/models/kithe/asset.rb', line 182 def representative_id id end |
#update_derivative(key, io, **options) ⇒ Shrine::UploadedFile
Just a convennience for file_attacher.add_persisted_derivatives (from :kithe_derivatives), feel free to use that if you want to add more than one etc. By default stores to :kithe_derivatives, just as ‘add_persisted_derivatives` does.
Note that just like shrine’s own usual ‘add_derivative(s)`, it assumes any files you pass it are meant to be temporary and will delete them, unless you pass `delete: false`.
Adds an associated derivative with key and io bytestream specified, doing so in a way that is safe from race conditions under multi-process concurrency.
Normally you don’t use this with derivatives defined with ‘define_derivative`, this is used by higher-level API. But if you’d like to add a derivative not defined with ‘define_derivative`, or for any other reason would like to manually add a derivative.
Can specify any options normally allowed for kithe ‘add_persisted_derivatives`, which are also generally any allowed for shrine `add_derivative`.
asset.update_derivative("big_thumb", File.open(something))
asset.update_derivative("something", File.open(something), delete: false)
asset.update_derivative("something", File.open(something), storage_key: :registered_storage, metadata: { "foo": "bar" })
142 143 144 145 |
# File 'app/models/kithe/asset.rb', line 142 def update_derivative(key, io, **) result = update_derivatives({ key => io }, **) result && result.values.first end |
#update_derivatives(deriv_hash, **options) ⇒ Object
147 148 149 |
# File 'app/models/kithe/asset.rb', line 147 def update_derivatives(deriv_hash, **) file_attacher.add_persisted_derivatives(deriv_hash, **) end |