From 1c26be3bd6efcf5bb25215e31b06750ebdeeb65d Mon Sep 17 00:00:00 2001 From: Juan Manuel Ramallo Date: Mon, 25 Mar 2024 14:57:38 -0300 Subject: [PATCH] Using after_initialize in engine Allows users to execute ruby code that consumes classes from app/models or any other portion of their app. This is particularly helpful for creating smart columns for AR enums. i.e.: ```ruby # app/models/account.rb class Account < ApplicationRecord enum state: [:pending, :setup, :succeded, :failed] end # config/blazer.yml data_sources: main: smart_columns: account_state: <%= Account.states.to_h {|k,v| [v, k.titleize]}.to_json %> ``` Without this change, initialization fails with error "uninitialized constant Account (NameError)". --- lib/blazer/engine.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/blazer/engine.rb b/lib/blazer/engine.rb index 4034d4ff7..6b2194569 100644 --- a/lib/blazer/engine.rb +++ b/lib/blazer/engine.rb @@ -20,7 +20,9 @@ class Engine < ::Rails::Engine app.config.assets.precompile << proc { |path| path =~ /\Ablazer\/.+\.(eot|svg|ttf|woff|woff2)\z/ } app.config.assets.precompile << proc { |path| path == "blazer/favicon.png" } end + end + config.after_initialize do Blazer.time_zone ||= Blazer.settings["time_zone"] || Time.zone Blazer.audit = Blazer.settings.key?("audit") ? Blazer.settings["audit"] : true Blazer.user_name = Blazer.settings["user_name"] if Blazer.settings["user_name"]