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
Proxies to file_attacher.kithe_create_derivatives to create kithe managed derivatives.
-
#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, **options) ⇒ 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
Like #update_derivative, but can update multiple at once.
Methods inherited from Model
#friendlier_id, kithe_earlier_after_commit, #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.
152 153 154 155 156 157 158 159 |
# File 'app/models/kithe/asset.rb', line 152 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
Proxies to file_attacher.kithe_create_derivatives to create kithe managed derivatives.
This will include any derivatives you created with in the uploader with kithe Attacher.define_derivative, as well as any derivative processors you opted into kithe management with ‘kithe_include_derivatives_processors` in uploader.
This is safe for concurrent updates to the underlying model, the derivatives will be consistent and not left orphaned.
By default it will create all derivatives configured for default generation, but you can customize by listing derivative keys with :only and :except options, and with :lazy option which, if true, only executes derivative creation if a given derivative doesn’t already exist.
See more in docs for kithe at Shrine::Plugins::KitheDerivativeDefinitions::AttacherMethods#kithe_create_derivatives
62 63 64 |
# File 'app/models/kithe/asset.rb', line 62 def create_derivatives(only: nil, except: nil, lazy: false) file_attacher.kithe_create_derivatives(only: only, except: except, lazy: lazy) 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.
131 132 133 134 135 136 137 138 139 140 |
# File 'app/models/kithe/asset.rb', line 131 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, **options) ⇒ Object
just a convenience for kithe remove_persisted_derivatives
117 118 119 |
# File 'app/models/kithe/asset.rb', line 117 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
143 144 145 |
# File 'app/models/kithe/asset.rb', line 143 def representative self end |
#representative_id ⇒ Object Also known as: leaf_representative_id
147 148 149 |
# File 'app/models/kithe/asset.rb', line 147 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" })
99 100 101 102 |
# File 'app/models/kithe/asset.rb', line 99 def update_derivative(key, io, **) result = update_derivatives({ key => io }, **) result && result.values.first end |
#update_derivatives(deriv_hash, **options) ⇒ Object
Like #update_derivative, but can update multiple at once.
asset.update_derivatives({ "big_thumb" => big_thumb_io, "small_thumb" => small_thumb_io })
Options from kithe ‘add_persisted_derivatives`/shrine `add_derivative` supported.
asset.update_derivatives({ "big_thumb" => big_thumb_io, "small_thumb" => small_thumb_io }, delete_false)
112 113 114 |
# File 'app/models/kithe/asset.rb', line 112 def update_derivatives(deriv_hash, **) file_attacher.add_persisted_derivatives(deriv_hash, **) end |