Module: JSONAPI::ActiveStorage::Detection
- Defined in:
- lib/json_api/active_storage/detection.rb
Class Method Summary collapse
- .attachment?(association_name, model_class) ⇒ Boolean
- .blob_type?(type) ⇒ Boolean
- .filter_from_includes(includes_hash, current_model_class) ⇒ Object
- .filter_include_value(value, association) ⇒ Object
- .filter_polymorphic_from_includes(includes_hash, current_model_class) ⇒ Object
- .process_include_entry(key, value, current_model_class, filtered) ⇒ Object
- .process_polymorphic_include(key, value, model_class, filtered) ⇒ Object
- .resolve_polymorphic_value(value, association) ⇒ Object
Class Method Details
.attachment?(association_name, model_class) ⇒ Boolean
8 9 10 11 12 13 |
# File 'lib/json_api/active_storage/detection.rb', line 8 def (association_name, model_class) return false unless defined?(::ActiveStorage) model_class.respond_to?(:reflect_on_attachment) && model_class.(association_name.to_sym).present? end |
.blob_type?(type) ⇒ Boolean
15 16 17 18 19 |
# File 'lib/json_api/active_storage/detection.rb', line 15 def blob_type?(type) return false unless defined?(::ActiveStorage) type.to_s == "active_storage_blobs" end |
.filter_from_includes(includes_hash, current_model_class) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/json_api/active_storage/detection.rb', line 21 def filter_from_includes(includes_hash, current_model_class) return {} unless defined?(::ActiveStorage) includes_hash.each_with_object({}) do |(key, value), filtered| process_include_entry(key, value, current_model_class, filtered) end end |
.filter_include_value(value, association) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/json_api/active_storage/detection.rb', line 39 def filter_include_value(value, association) return value unless value.is_a?(Hash) && value.any? return value if association.polymorphic? nested_filtered = filter_from_includes(value, association.klass) nested_filtered.any? ? nested_filtered : nil end |
.filter_polymorphic_from_includes(includes_hash, current_model_class) ⇒ Object
47 48 49 50 51 |
# File 'lib/json_api/active_storage/detection.rb', line 47 def filter_polymorphic_from_includes(includes_hash, current_model_class) includes_hash.each_with_object({}) do |(key, value), filtered| process_polymorphic_include(key, value, current_model_class, filtered) end end |
.process_include_entry(key, value, current_model_class, filtered) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/json_api/active_storage/detection.rb', line 29 def process_include_entry(key, value, current_model_class, filtered) return if current_model_class.(key).present? association = current_model_class.reflect_on_association(key) return if association.nil? result = filter_include_value(value, association) filtered[key] = result unless result.nil? end |
.process_polymorphic_include(key, value, model_class, filtered) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/json_api/active_storage/detection.rb', line 53 def process_polymorphic_include(key, value, model_class, filtered) association = model_class.reflect_on_association(key) return unless association && !association.polymorphic? result = resolve_polymorphic_value(value, association) filtered[key] = result if result end |
.resolve_polymorphic_value(value, association) ⇒ Object
61 62 63 64 65 66 |
# File 'lib/json_api/active_storage/detection.rb', line 61 def resolve_polymorphic_value(value, association) return value unless value.is_a?(Hash) && value.any? nested = filter_polymorphic_from_includes(value, association.klass) nested.any? ? nested : nil end |