DO Spaces with administrate gem and Rails


What Is Administrate?

Administrate is a library for Rails apps that automatically generates admin dashboards.

Administrate’s admin dashboards give non-technical users clean interfaces that allow them to create, edit, search, and delete records for any model in the application.

Administrate solves the same problem as Rails Admin and ActiveAdmin, but aims to provide a better user experience for site admins, and to be easier for developers to customize.

To accomplish these goals, Administrate follows a few guiding principles:

Support the simplest use cases, and let the user override defaults with standard tools such as plain Rails controllers and views.

Break up the library into core components and plugins, so each component stays small and easy to maintain.

image

Website:

https://github.com/thoughtbot/administrate

DigitalOcean Spaces

What Is DigitalOcean Spaces?

DigitalOcean Spaces offers Amazon S3 compatible object storage for a low cost and with a built-in CDN.

Especially if you use the DigitalOcean services, Spaces offers a great way to store backup files (when used as a private repository) or even to host a static site using the CDN capabilities.

  • $5 per month
  • 250 GB of storage space
  • 1 TB of outbound transfer
  • Unlimited uploads
  • Creation of unlimited number of spaces
  • In this article, we are going to explore how to get started with DigitalOcean Spaces and some common operations that would be used.

Read more:

https://www.digitalocean.com/products/spaces/

First steps

Let’s create new Space and set name:

image

Coding part

Let’s start from bundle:

gem 'administrate'
gem 'administrate-field-active_storage'
gem 'image_processing'
gem 'aws-sdk-s3', require: false

Workout model attachment:

class Workout < ApplicationRecord
  has_one_attached :video
end

Next step - our dashboard for Workout model:

require "administrate/base_dashboard"

class WorkoutDashboard < Administrate::BaseDashboard

  ATTRIBUTE_TYPES = {
    id: Field::Number,
    title: Field::String,
    # ACTIVE STORAGE FIELD TYPE
    video: Field::ActiveStorage,
    created_at: Field::DateTime,
    updated_at: Field::DateTime
  }.freeze

  COLLECTION_ATTRIBUTES = %i[
    id
    title
  ].freeze

  SHOW_PAGE_ATTRIBUTES = %i[
    id
    title
    video
  ].freeze

  FORM_ATTRIBUTES = %i[
    title
    video
  ].freeze

  COLLECTION_FILTERS = {}.freeze
end

Configuration

Let’s adjust app credentials:

digitalocean:
  access_key: YOUR_API_ACCESS_KEY
  secret: YOUR_API_ACCESS_SECRET

And add new entry in storage.yml as a next step:

digitalocean:
  service: S3
  endpoint: https://your-spaces-endpoint
  access_key_id: <%= Rails.application.credentials.dig(:digitalocean, :access_key) %>
  secret_access_key: <%= Rails.application.credentials.dig(:digitalocean, :secret) %>
  bucket: your-space-name
  region: unused

Let’s set default active_storage service: config/environments/development.rb:

config.active_storage.service = :digitalocean

Results

Let’s upload new file from our dashboard:

image

And finally our uploaded file in DigitalOcean Spaces page:

image

Done!