2

我现在在玩 RailsAdmin。

RailsAdmin DSL 配置存储在初始化程序中。有一种方法可以将它们保留在模型中,但我认为这是个坏主意(将视图相关信息存储在模型中)。

因此,每次我必须更改它时,我都必须重新启动 rails 服务器,这很烦人。

有没有办法为初始化程序启用自动重载?

4

2 回答 2

1

如此处文档中所述:

在你的/config/initializers/rails_admin.rb你应该添加config.parent_controller = ApplicationController.to_s

class ApplicationController < ActionController::Base
  protect_from_forgery

  before_filter :reload_rails_admin if :rails_admin_path?

  private

  def reload_rails_admin
    models = %W(User UserProfile)

    models.each do |m|
      RailsAdmin::Config.reset_model(m)
    end

    RailsAdmin::Config::Actions.reset

    load("#{Rails.root}/config/initializers/rails_admin.rb")
  end

  def rails_admin_path?
    controller_path =~ /rails_admin/ && Rails.env == "development"
  end

end

它确实感觉非常hacky,我很想听到更好的选择。

于 2013-08-29T00:56:08.557 回答
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"

    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

于 2020-06-18T12:47:59.603 回答