概要
rails_admin
の設定ファイル rails_admin.rb
を編集すると自動で rails_admin.rb
を再読み込みし、変更を反映させる。
環境
Rails (5.1.5)
rails_admin (1.3.0)
方法
config/initializers/rails_admin_reload.rb
ファイルを追加
# See also: https://github.com/sferik/rails_admin/wiki/How-to:-Reloading-RailsAdmin-Config-Automatically#alternative-method-for-the-railsadmin-versions-that-does-not-extend-applicationcontroller
Rails.application.config.to_prepare do
RailsAdmin::ApplicationController.class_eval do
before_action :reload_rails_admin, if: :reload_rails_admin? # Reloading RailsAdmin Config Automatically
RAILS_ADMIN_FILE_PATH = "#{Rails.root}/config/initializers/rails_admin.rb" unless defined?(RAILS_ADMIN_FILE_PATH)
class << self
def fetch_rails_admin_file_updated_time
File::Stat.new(RAILS_ADMIN_FILE_PATH).mtime
end
attr_accessor :__file_timestamp
end
@__file_timestamp = fetch_rails_admin_file_updated_time
def reload_rails_admin
RailsAdmin::Config.reset
load(RAILS_ADMIN_FILE_PATH)
end
def reload_rails_admin?
return false unless Rails.env.development?
begin
file_updated_at = RailsAdmin::ApplicationController.fetch_rails_admin_file_updated_time
has_changed = file_updated_at != self.class.__file_timestamp
return has_changed
ensure
self.class.__file_timestamp = file_updated_at
end
end
end
end