Skip to content

Commit

Permalink
Merge pull request #3041 from AnatolyShirykalov/code_reload
Browse files Browse the repository at this point in the history
code_reload
  • Loading branch information
mshibuya committed Jul 17, 2018
2 parents 87ac7e7 + 347f145 commit 175d8ac
Show file tree
Hide file tree
Showing 3 changed files with 54 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
12 changes: 12 additions & 0 deletions lib/rails_admin/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ class Engine < Rails::Engine
app.config.middleware.use Rack::Pjax
end

initializer 'RailsAdmin reload config in development' do
if Rails.application.config.cache_classes
if defined?(ActiveSupport::Reloader)
ActiveSupport::Reloader.before_class_unload do
RailsAdmin::Config.reset_all_models
end
# else
# For Rails 4 not implemented
end
end
end

rake_tasks do
Dir[File.join(File.dirname(__FILE__), '../tasks/*.rake')].each { |f| load f }
end
Expand Down
34 changes: 34 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,34 @@ 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 for rails 5" do
if defined?(ActiveSupport::Reloader)
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
if defined?(ActiveSupport::Reloader)
Rails.application.executor.wrap do
ActiveSupport::Reloader.new.tap(&:class_unload!).complete!
end
# else
# for Rails 4 not imlemented yet
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
# else
# pending "for Rails 4 not implemented"
end
end
end
end

Expand Down

0 comments on commit 175d8ac

Please sign in to comment.