0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

RailsAdmin設定ファイル変更後に自動再読み込み 

Last updated at Posted at 2020-06-18

概要

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

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?