Skip to content

Commit

Permalink
Merge pull request #3066 from lazebny/fix-for-caching-field-with-error
Browse files Browse the repository at this point in the history
Fix field is reseted to default if error happened
  • Loading branch information
mshibuya committed Sep 24, 2018
2 parents 9d06364 + 19f2f9b commit 875dcb4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
29 changes: 18 additions & 11 deletions lib/rails_admin/config/configurable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ def register_deprecated_instance_option(option_name, replacement_option_name = n
self.class.register_deprecated_instance_option(option_name, replacement_option_name, scope, &custom_error)
end

private

def with_recurring(option_name, value_proc, default_proc)
# Track recursive invocation with an instance variable. This prevents run-away recursion
# and allows configurations such as
# label { "#{label}".upcase }
# This will use the default definition when called recursively.
if instance_variable_get("@#{option_name}_recurring")
instance_eval(&default_proc)
else
instance_variable_set("@#{option_name}_recurring", true)
instance_eval(&value_proc)
end
ensure
instance_variable_set("@#{option_name}_recurring", false)
end

module ClassMethods
# Register an instance option. Instance option is a configuration
# option that stores its value within an instance variable and is
Expand Down Expand Up @@ -51,17 +68,7 @@ def register_instance_option(option_name, scope = self, &default)
value = instance_variable_get("@#{option_name}_registered")
case value
when Proc
# Track recursive invocation with an instance variable. This prevents run-away recursion
# and allows configurations such as
# label { "#{label}".upcase }
# This will use the default definition when called recursively.
if instance_variable_get("@#{option_name}_recurring")
value = instance_eval(&default)
else
instance_variable_set("@#{option_name}_recurring", true)
value = instance_eval(&value)
instance_variable_set("@#{option_name}_recurring", false)
end
value = with_recurring(option_name, value, default)
when nil
value = instance_eval(&default)
end
Expand Down
14 changes: 14 additions & 0 deletions spec/integration/config/edit/rails_admin_config_edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@
visit new_path(model_name: 'team')
expect(find_field('team[color]').value).to eq('black')
end

it 'renders custom value next time if error happend' do
RailsAdmin.config(Team) do
field :name do
render do
bindings[:object].persisted? ? 'Custom Name' : raise(ZeroDivisionError)
end
end
end
expect { visit new_path(model_name: 'team') }.to raise_error(/ZeroDivisionError/)
record = FactoryBot.create(:team)
visit edit_path(model_name: 'team', id: record.id)
expect(page).to have_content('Custom Name')
end
end

describe 'css hooks' do
Expand Down

0 comments on commit 875dcb4

Please sign in to comment.