Module: MobilityActiveStorage::BackendMethods::ClassMethods
- Defined in:
- lib/mobility_active_storage/backend_methods.rb
Overview
Class-level configuration shared by both backends.
Instance Method Summary collapse
-
#[](name, _locale) ⇒ Object
Mobility's query plugin asks the backend for an Arel node so it can build a predicate.
-
#attachment_name_for(attribute, locale) ⇒ Object
Locale-suffixed attachment name, rejecting locales this attribute was not configured for.
-
#attachment_names(attribute) ⇒ Object
Every configured locale's attachment name.
- #configure(options) ⇒ Object
- #configured_locale?(locale) ⇒ Boolean
- #normalized_locales ⇒ Object
- #valid_keys ⇒ Object
Instance Method Details
#[](name, _locale) ⇒ Object
Mobility's query plugin asks the backend for an Arel node so it can build a predicate.
An attachment is a row in active_storage_attachments, not a comparable column value, so
there is nothing meaningful to compare. Fail with an explanation rather than the
NoMethodError a missing [] would otherwise produce.
64 65 66 67 68 |
# File 'lib/mobility_active_storage/backend_methods.rb', line 64 def [](name, _locale) raise Error, "cannot query translated attachment #{name} -- attachments are not " \ "comparable values. Query ActiveStorage::Attachment directly, filtering " \ "on name (for example \"#{name}_#{Mobility.normalize_locale}\")." end |
#attachment_name_for(attribute, locale) ⇒ Object
Locale-suffixed attachment name, rejecting locales this attribute was not configured for. Shared by the read path and by the eager-loading scopes, so both fail the same way rather than the scope leaking an ActiveRecord::AssociationNotFoundError about a generated association name.
40 41 42 43 44 45 46 47 |
# File 'lib/mobility_active_storage/backend_methods.rb', line 40 def (attribute, locale) normalized = Mobility.normalize_locale(locale) return "#{attribute}_#{normalized}" if normalized_locales.include?(normalized) raise Error, "#{model_class.name} has no translated #{attribute} attachment for " \ "locale #{locale.inspect}. Configured locales: " \ "#{[:locales].map(&:to_s).join(", ")}." end |
#attachment_names(attribute) ⇒ Object
Every configured locale's attachment name. Memoized per attribute: the locale set is fixed when the class body runs.
55 56 57 58 |
# File 'lib/mobility_active_storage/backend_methods.rb', line 55 def (attribute) @attachment_names ||= {} @attachment_names[attribute] ||= normalized_locales.map { |locale| "#{attribute}_#{locale}" } end |
#configure(options) ⇒ Object
30 31 32 33 34 |
# File 'lib/mobility_active_storage/backend_methods.rb', line 30 def configure() [:locales] = Array([:locales] || Mobility.available_locales).map(&:to_sym) [:attached_options] ||= {} [:attachment_fallbacks] = build_fallbacks([:attachment_fallbacks]) end |
#configured_locale?(locale) ⇒ Boolean
49 50 51 |
# File 'lib/mobility_active_storage/backend_methods.rb', line 49 def configured_locale?(locale) normalized_locales.include?(Mobility.normalize_locale(locale)) end |
#normalized_locales ⇒ Object
70 71 72 |
# File 'lib/mobility_active_storage/backend_methods.rb', line 70 def normalized_locales @normalized_locales ||= [:locales].map { |locale| Mobility.normalize_locale(locale) } end |
#valid_keys ⇒ Object
26 27 28 |
# File 'lib/mobility_active_storage/backend_methods.rb', line 26 def valid_keys %i[locales attachment_fallbacks attached_options] end |