Class: BridgetownDirectus::Configuration::CollectionConfig
- Inherits:
-
Object
- Object
- BridgetownDirectus::Configuration::CollectionConfig
- Defined in:
- lib/bridgetown_directus/configuration.rb
Overview
Collection configuration class
Instance Attribute Summary collapse
-
#default_query ⇒ Object
Set up accessors for collection configuration properties.
-
#endpoint ⇒ Object
Set up accessors for collection configuration properties.
-
#fields ⇒ Object
Set up accessors for collection configuration properties.
-
#layout ⇒ Object
Set up accessors for collection configuration properties.
-
#m2m_flattenings ⇒ Object
readonly
Returns the value of attribute m2m_flattenings.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#resource_type ⇒ Object
Set up accessors for collection configuration properties.
-
#singleton ⇒ Object
Set up accessors for collection configuration properties.
-
#translatable_fields ⇒ Object
Set up accessors for collection configuration properties.
-
#translations_enabled ⇒ Object
Set up accessors for collection configuration properties.
Instance Method Summary collapse
-
#data? ⇒ Boolean
Check if this collection is a data-only collection.
-
#enable_translations(fields = []) ⇒ void
Enable translations for this collection.
-
#field(bridgetown_field, directus_field, &converter) ⇒ void
Define a field mapping with optional converter.
-
#flatten_m2m(path, key:) ⇒ void
Register a many-to-many junction to flatten after fetching.
-
#initialize(name) ⇒ CollectionConfig
constructor
Initialize a new collection configuration.
-
#path(item) ⇒ String
Generate the resource path for a given item.
Constructor Details
#initialize(name) ⇒ CollectionConfig
Initialize a new collection configuration
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/bridgetown_directus/configuration.rb', line 37 def initialize(name) @name = name @fields = {} @default_query = {} @resource_type = :posts @layout = "post" @translations_enabled = false @translatable_fields = [] @endpoint = nil @singleton = false @m2m_flattenings = [] end |
Instance Attribute Details
#default_query ⇒ Object
Set up accessors for collection configuration properties
51 52 53 |
# File 'lib/bridgetown_directus/configuration.rb', line 51 def default_query @default_query end |
#endpoint ⇒ Object
Set up accessors for collection configuration properties
51 52 53 |
# File 'lib/bridgetown_directus/configuration.rb', line 51 def endpoint @endpoint end |
#fields ⇒ Object
Set up accessors for collection configuration properties
51 52 53 |
# File 'lib/bridgetown_directus/configuration.rb', line 51 def fields @fields end |
#layout ⇒ Object
Set up accessors for collection configuration properties
51 52 53 |
# File 'lib/bridgetown_directus/configuration.rb', line 51 def layout @layout end |
#m2m_flattenings ⇒ Object (readonly)
Returns the value of attribute m2m_flattenings.
53 54 55 |
# File 'lib/bridgetown_directus/configuration.rb', line 53 def m2m_flattenings @m2m_flattenings end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
33 34 35 |
# File 'lib/bridgetown_directus/configuration.rb', line 33 def name @name end |
#resource_type ⇒ Object
Set up accessors for collection configuration properties
51 52 53 |
# File 'lib/bridgetown_directus/configuration.rb', line 51 def resource_type @resource_type end |
#singleton ⇒ Object
Set up accessors for collection configuration properties
51 52 53 |
# File 'lib/bridgetown_directus/configuration.rb', line 51 def singleton @singleton end |
#translatable_fields ⇒ Object
Set up accessors for collection configuration properties
51 52 53 |
# File 'lib/bridgetown_directus/configuration.rb', line 51 def translatable_fields @translatable_fields end |
#translations_enabled ⇒ Object
Set up accessors for collection configuration properties
51 52 53 |
# File 'lib/bridgetown_directus/configuration.rb', line 51 def translations_enabled @translations_enabled end |
Instance Method Details
#data? ⇒ Boolean
Check if this collection is a data-only collection
70 71 72 |
# File 'lib/bridgetown_directus/configuration.rb', line 70 def data? @resource_type == :data end |
#enable_translations(fields = []) ⇒ void
This method returns an undefined value.
Enable translations for this collection
89 90 91 92 |
# File 'lib/bridgetown_directus/configuration.rb', line 89 def enable_translations(fields = []) @translations_enabled = true @translatable_fields = fields end |
#field(bridgetown_field, directus_field, &converter) ⇒ void
This method returns an undefined value.
Define a field mapping with optional converter
79 80 81 82 83 84 |
# File 'lib/bridgetown_directus/configuration.rb', line 79 def field(bridgetown_field, directus_field, &converter) @fields[bridgetown_field] = { directus_field: directus_field.to_s, converter: converter, } end |
#flatten_m2m(path, key:) ⇒ void
This method returns an undefined value.
Register a many-to-many junction to flatten after fetching. Directus returns M2M data wrapped in junction objects like:
[{"raus_stats_id": {"id": 1, "value": "8+"}}]
This unwraps them to:
[{"id": 1, "value": "8+"}]
64 65 66 |
# File 'lib/bridgetown_directus/configuration.rb', line 64 def flatten_m2m(path, key:) @m2m_flattenings << { path: path, key: key } end |
#path(item) ⇒ String
Generate the resource path for a given item
97 98 99 100 101 |
# File 'lib/bridgetown_directus/configuration.rb', line 97 def path(item) # Default: /:resource_type/:slug/index.html slug = item["slug"] || item[:slug] || item["id"] || item[:id] "/#{resource_type}/#{slug}/index.html" end |