Global prerocessor for ActiveStorage

Today I want to tell you how to do global preprocessing of uploaded files in ActiveStorage. Unfortunately ActiveStorage design doesn’t provide this feature out of the box, or I haven’t found any information about it. But sometimes you may need to process attachments for all models in a project. To achieve this goal, we need to override several methods of singleton class of ActiveStorage::Blob class, in my version (rails 6.1) they are build_after_upload, build_after_unfurling and upload....

June 2, 2024 · 2 min

Get Extension by Mimetype in Rails

Sometimes we need to get an extension by content mime type. There are many ways to manage this but in Rails (or rack-based app) you can use this trick: Rack::Mime::MIME_TYPES.invert['image/jpeg'] # => '.jpg'

February 9, 2023 · 1 min

A simple way to stream JSON with Rails Live API

A few weeks ago I faced with a requirement to optimize a huge JSON response from Rails-based API. For example, we want to respond large set of data as JSON collection. In our controller, we have some action for that, but rails Live API couldn’t stream JSON or XML directly. But we can use a little trick. class PostsController def index @data = Post.order(:created_at) # Or other scope or service object....

May 18, 2021 · 1 min

POST request to API with Kotlin and Spring RestClient (RestTemplate)

I am new in Kotlin. So maybe this post is just a bookmark for future. For example we want request some API for some data by POST to it an json array of ids from our Spring service. First of all we need Jackson to parse API’s response. So check out your build.gradle file implementation "com.fasterxml.jackson.module:jackson-module-kotlin" And the example code for POST json request. import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue import org.springframework.http.HttpEntity import org....

February 9, 2021 · 1 min

Let's begin...

This is another attempt to create my own standalone blog. Other versions were lost, but it is not a problem. I decided make it with Hugo. So let’s begin…

December 16, 2020 · 1 min