Skip to content

Custom file placements (multiple)

Petr Marek edited this page Jan 14, 2019 · 1 revision

Let's create maps placements for MyProject::City.

Create a model

Use folio_image_placement for Folio::Image placements and folio_document_placement for Folio::Document placements.

# app/models/my_project/file_placement/map.rb

class MyProject::FilePlacement::Map < Folio::FilePlacement::Base
  folio_image_placement :map_placements
end

Update model

# app/models/my_project/city.rb

class MyProject::City < ApplicationRecord
  include Folio::HasAttachments

  has_many_placements(:maps,
                      placements_key: :map_placements,
                      placement: 'MyProject::FilePlacement::Map')
end

has_many_placements creates two relations - has_many :map_placements and has_many :maps, through: :map_placements

Add the key to console base controller decorator

# app/decorators/controllers/folio/console/base_controller_decorator.rb

Folio::Console::BaseController.class_eval do
  private

    def additional_file_placements_strong_params_keys
      %i[
        map_placements_attributes
      ]
    end
end

Add attribute translation

# config/locales/activerecord.cs.yml

---
cs:
  activerecord:
    attributes:
      my_project/city:
        map_placement: Mapy

Add the form field to view

Use the react_images/react_documents helper.

/ app/views/folio/console/my_project/cities/_form.slim

= tabs [:content, :maps]

= f.error_notification

.tab-content
  = tab :content
    = f.input :title

  = tab :maps
    = react_images(f.object.map_placements, attachmentable: 'my_project_city', \
                                            type: :map_placements)

Clone this wiki locally