Module: MobilityActiveStorage::Model

Defined in:
lib/mobility_active_storage/model.rb,
sig/mobility_active_storage.rbs

Overview

Class macros mixed into ActiveRecord::Base.

Instance Method Summary collapse

Instance Method Details

#has_many_translated_attached(name, fallbacks: false, locales: nil, **attached_options) ⇒ Object

Declares a translated collection of attachments. Options match #has_one_translated_attached; attached_options is forwarded to has_many_attached.



26
27
28
# File 'lib/mobility_active_storage/model.rb', line 26

def has_many_translated_attached(name, fallbacks: false, locales: nil, **attached_options)
  translated_attachment(name, :active_storage_many, fallbacks, locales, attached_options)
end

#has_one_translated_attached(name, fallbacks: false, locales: nil, **attached_options) ⇒ Object

Declares a single translated attachment.

class Product < ApplicationRecord
has_one_translated_attached :document
has_one_translated_attached :manual, fallbacks: true, service: :s3
end

Parameters:

  • name (Symbol)

    attribute name

  • fallbacks (Boolean, Hash) (defaults to: false)

    true to fall back through I18n's chain, or an explicit hash such as { fr: :en }. Defaults to no fallback.

  • locales (Array<Symbol>, nil) (defaults to: nil)

    locales to declare attachments for. Defaults to Mobility.available_locales at the time the class body runs.

  • attached_options (Hash)

    forwarded to has_one_attached (+:service+, :strict_loading, :dependent).



20
21
22
# File 'lib/mobility_active_storage/model.rb', line 20

def has_one_translated_attached(name, fallbacks: false, locales: nil, **attached_options)
  translated_attachment(name, :active_storage, fallbacks, locales, attached_options)
end

#translated_attachment_attribute_namesObject

Attribute names declared through the attachment macros, including any inherited from a superclass. Used to keep attachments out of the hashes Mobility's attribute_methods plugin builds; see AttributeMethodsExclusion.



33
34
35
36
37
38
39
40
# File 'lib/mobility_active_storage/model.rb', line 33

def translated_attachment_attribute_names
  @translated_attachment_attribute_names ||=
    if superclass.respond_to?(:translated_attachment_attribute_names)
      superclass.translated_attachment_attribute_names.dup
    else
      []
    end
end