Skip to content

Commit

Permalink
code_reload
Browse files Browse the repository at this point in the history
  • Loading branch information
AnatolyShirykalov committed Jul 5, 2018
1 parent da0584a commit 15a665e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/rails_admin/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,14 @@ def reset_model(model)
@registry.delete(key)
end

# Reset all models configuration
# Used to clear all configurations when reloading code in development.
# @see RailsAdmin::Engine
# @see RailsAdmin::Config.registry
def reset_all_models
@registry = {}
end

# Get all models that are configured as visible sorted by their weight and label.
#
# @see RailsAdmin::Config::Hideable
Expand Down
8 changes: 8 additions & 0 deletions lib/rails_admin/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ class Engine < Rails::Engine
app.config.middleware.use Rack::Pjax
end

initializer 'RailsAdmin reload config in development' do
unless Rails.application.config.cache_classes
ActiveSupport::Reloader.before_class_unload do |cl|
RailsAdmin::Config.reset_all_models
end
end
end

rake_tasks do
Dir[File.join(File.dirname(__FILE__), '../tasks/*.rake')].each { |f| load f }
end
Expand Down
26 changes: 26 additions & 0 deletions spec/rails_admin/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,12 @@ class RecursivelyEmbedsMany
end
end

let(:team_config3) do
proc do
field :wins
end
end

it "allows code reloading" do
Team.send(:rails_admin, &team_config)

Expand All @@ -364,6 +370,26 @@ class Toggle < RailsAdmin::Config::Fields::Base
Team.send(:rails_admin, &team_config2)
expect(fields.map(&:name)).to match_array %i(id wins)
end

it "updates model config when reloading code" do
Team.send(:rails_admin, &team_config)

# this simulates rails code reloading
Rails.application.config.cache_classes = false
RailsAdmin::Engine.initializers.select do |i|
i.name == "RailsAdmin reload config in development"
end.first.block.call
ActiveSupport::Reloader.new.tap do |instance|
instance.class_unload!
end
# /end

Team.send(:rails_admin, &team_config3)
expect(fields.map(&:name)).to match_array %i(wins)

# restore setting to previous value
Rails.application.config.cache_classes = true
end
end
end

Expand Down

0 comments on commit 15a665e

Please sign in to comment.