Module: ActiveStorage::Attached::Model
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
::ActiveSupport::Concern
|
|
Defined in: | activestorage/lib/active_storage/attached/model.rb |
Overview
Provides the class-level DSL for declaring an Active Record model’s attachments.
Class Method Summary
- .validate_global_service_configuration(model_class) private
- .validate_service_configuration(service_name, model_class, association_name) Internal use only
::ActiveSupport::Concern
- Extended
class_methods | Define class methods from given block. |
included | Evaluate given block in context of base class, so that you can write class macros here. |
prepended | Evaluate given block in context of base class, so that you can write class macros here. |
append_features, prepend_features |
Instance Attribute Summary
- #changed_for_autosave? ⇒ Boolean readonly Internal use only
Instance Method Summary
-
#*_attachment
Returns the attachment for the
has_one_attached
. -
#*_attachments
Returns the attachments for the
has_many_attached
. -
#*_blob
Returns the blob for the
has_one_attached
attachment. -
#*_blobs
Returns the blobs for the
has_many_attached
attachments. -
#with_attached_*
Includes the attached blobs in your query to avoid N+1 queries.
- #attachment_changes Internal use only
- #initialize_dup Internal use only
- #reload Internal use only
Class Method Details
.validate_global_service_configuration(model_class) (private)
[ GitHub ]# File 'activestorage/lib/active_storage/attached/model.rb', line 274
def validate_global_service_configuration(model_class) if model_class.connected? && ActiveStorage::Blob.table_exists? && Rails.configuration.active_storage.service.nil? raise RuntimeError, "Missing Active Storage service name. Specify Active Storage service name for config.active_storage.service in config/environments/#{Rails.env}.rb" end end
.validate_service_configuration(service_name, model_class, association_name)
# File 'activestorage/lib/active_storage/attached/model.rb', line 263
def validate_service_configuration(service_name, model_class, association_name) # :nodoc: if service_name ActiveStorage::Blob.services.fetch(service_name) do raise ArgumentError, "Cannot configure service #{service_name.inspect} for #{model_class}##{association_name}" end else validate_global_service_configuration(model_class) end end
Instance Attribute Details
#changed_for_autosave? ⇒ Boolean
(readonly)
# File 'activestorage/lib/active_storage/attached/model.rb', line 285
def changed_for_autosave? # :nodoc: super || .any? end
Instance Method Details
#*_attachment
Returns the attachment for the has_one_attached
.
User.last.
# File 'activestorage/lib/active_storage/attached/model.rb', line 13
rdoc_method :method: *
#*_attachments
Returns the attachments for the has_many_attached
.
Gallery.last.
# File 'activestorage/lib/active_storage/attached/model.rb', line 20
rdoc_method :method: *
#*_blob
Returns the blob for the has_one_attached
attachment.
User.last.avatar_blob
# File 'activestorage/lib/active_storage/attached/model.rb', line 27
rdoc_method :method: *_blob
#*_blobs
Returns the blobs for the has_many_attached
attachments.
Gallery.last.photos_blobs
# File 'activestorage/lib/active_storage/attached/model.rb', line 34
rdoc_method :method: *_blobs
#attachment_changes
# File 'activestorage/lib/active_storage/attached/model.rb', line 281
def # :nodoc: @attachment_changes ||= {} end
#initialize_dup
# File 'activestorage/lib/active_storage/attached/model.rb', line 289
def initialize_dup(*) # :nodoc: super @active_storage_attached = nil @attachment_changes = nil end
#reload
# File 'activestorage/lib/active_storage/attached/model.rb', line 295
def reload(*) # :nodoc: super.tap { @attachment_changes = nil } end
#with_attached_*
Includes the attached blobs in your query to avoid N+1 queries.
If ActiveStorage.track_variants is enabled, it will also include the variants record and their attached blobs.
User.with_attached_avatar
Use the plural form for has_many_attached
:
Gallery.with_attached_photos
# File 'activestorage/lib/active_storage/attached/model.rb', line 55
rdoc_method :method: with_attached_*