Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code_reload #3041

Merged
merged 3 commits into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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