OOZOU
與我們聯繫
Back to TIL
ruby on rails

Add new types to Rails 5 attributes API

October 13, 2020

The Rails 5 attributes API allows us to build form objects very easily.

I had previously been using an after_initialize callback to downcase emails, with the following code I can define an :email date type and values are automatically cast when set.

This has the advantage of casting subsequent calls to set the email value.

class EmailType < ActiveModel::Type::String
  def cast(value)
    return super unless value.is_a?(String)
    super(value.downcase)
  end
end

ActiveModel::Type.register(:email, EmailType)
class Foo
  include ActiveModel::Model
  include ActiveModel::Attributes

  attribute :email, :email
end
> Foo.new(email: 'EMAIL@MAIL.com').email
=> 'email@mail.com'

More on ruby on rails

ruby on railsFebruary 10, 2022

Use Pundit Policies to manage permitted parameters, defaults and per action

To manage permitted params you can add permitted\attributes and permitted\attributes\for\#{action} in your policies e.g.

ruby on railsJanuary 31, 2022

Use `requestSubmit` for form submits with Turbo

Turbo listens to submit events on form.  Hence, always use requestSubmit to submit  a form using JS.

ruby on railsJanuary 19, 2022

Ignore bundled gems for Rubocop and ESLint in GitHub Actions

Rails projects will often install gems into the vendor directory in GitHub actions so we can cache them for the consecutive runs.

有專案構想嗎?

我們很樂意聆聽您正在打造的產品。

開始對話hello@oozou.com
OOZOU Logo

曼谷 · 新加坡 · 香港

X
Facebook
Instagram
LinkedIn

服務

  • Web 開發
  • AI 與生成式 AI 解決方案
  • 行動應用程式開發
  • 資料分析與工程
  • UI/UX 設計與產品設計
  • 數位轉型

公司

  • 關於我們
  • 職涯
  • 部落格
  • 白皮書
  • 案例研究
  • Today I Learned
  • 聯繫我們

更多

  • 產業
  • 索取報價
  • 合作夥伴網絡
© 2026 OOZOU. 版權所有。
隱私政策行為準則反賄賂反貪腐政策