From 80ad7b34cb49b9fda5c7a57326d3b540dc8db74e Mon Sep 17 00:00:00 2001 From: Timo Schilling Date: Tue, 19 Aug 2014 12:47:26 +0200 Subject: [PATCH] refactor DependencyError --- lib/active_admin/dependency.rb | 13 +++++-------- lib/active_admin/error.rb | 4 ++++ .../active_admin/devise/devise_generator.rb | 2 +- spec/unit/dependency_spec.rb | 12 ++++++------ 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/active_admin/dependency.rb b/lib/active_admin/dependency.rb index a05875bd77e..587da24918c 100644 --- a/lib/active_admin/dependency.rb +++ b/lib/active_admin/dependency.rb @@ -23,10 +23,10 @@ module Dependency # => true # # ActiveAdmin::Dependency.rails! '2' - # -> ActiveAdmin::Dependency::Error: You provided rails 3.2.18 but we need: 2. + # -> ActiveAdmin::DependencyError: You provided rails 3.2.18 but we need: 2. # # ActiveAdmin::Dependency.devise! - # -> ActiveAdmin::Dependency::Error: To use devise you need to specify it in your Gemfile. + # -> ActiveAdmin::DependencyError: To use devise you need to specify it in your Gemfile. # # # All but the pessimistic operator (~>) can also be run using Ruby's comparison syntax. @@ -64,11 +64,11 @@ def match?(*reqs) def match!(*reqs) unless @spec - raise Error, "To use #{@name} you need to specify it in your Gemfile." + raise DependencyError, "To use #{@name} you need to specify it in your Gemfile." end unless match? reqs - raise Error, "You provided #{@spec.name} #{@spec.version} but we need: #{reqs.join ', '}." + raise DependencyError, "You provided #{@spec.name} #{@spec.version} but we need: #{reqs.join ', '}." end end @@ -80,7 +80,7 @@ def <=>(other) else # you'd otherwise get an unhelpful error message: # ArgumentError: comparison of ActiveAdmin::Dependency::Matcher with 2 failed - raise Error, "To use #{@name} you need to specify it in your Gemfile." + raise DependencyError, "To use #{@name} you need to specify it in your Gemfile." end end @@ -89,8 +89,5 @@ def inspect "" end end - - class Error < ::ActiveAdmin::ErrorLoading; end - end end diff --git a/lib/active_admin/error.rb b/lib/active_admin/error.rb index dbd805c5973..e9530657d41 100644 --- a/lib/active_admin/error.rb +++ b/lib/active_admin/error.rb @@ -51,6 +51,10 @@ def self.database_error_classes end end + class DependencyError < ErrorLoading + end + class NoMenuError < KeyError end + end diff --git a/lib/generators/active_admin/devise/devise_generator.rb b/lib/generators/active_admin/devise/devise_generator.rb index d3c9cbb68e6..7616516318e 100644 --- a/lib/generators/active_admin/devise/devise_generator.rb +++ b/lib/generators/active_admin/devise/devise_generator.rb @@ -18,7 +18,7 @@ class DeviseGenerator < Rails::Generators::NamedBase def install_devise begin Dependency.devise! Dependency::DEVISE - rescue Dependency::Error => e + rescue DependencyError => e raise Error, "#{e.message} If you don't want to use devise, run the generator with --skip-users." end diff --git a/spec/unit/dependency_spec.rb b/spec/unit/dependency_spec.rb index ee6ad663244..c0a798ebb84 100644 --- a/spec/unit/dependency_spec.rb +++ b/spec/unit/dependency_spec.rb @@ -52,15 +52,15 @@ describe '`!`' do it 'raises an error if requirement not met' do - expect{ k.foo! '5' }.to raise_error ActiveAdmin::Dependency::Error, + expect{ k.foo! '5' }.to raise_error ActiveAdmin::DependencyError, 'You provided foo 1.2.3 but we need: 5.' end it 'accepts multiple arguments' do - expect{ k.foo! '> 1', '< 1.2' }.to raise_error ActiveAdmin::Dependency::Error, + expect{ k.foo! '> 1', '< 1.2' }.to raise_error ActiveAdmin::DependencyError, 'You provided foo 1.2.3 but we need: > 1, < 1.2.' end it 'raises an error if not provided' do - expect{ k.bar! }.to raise_error ActiveAdmin::Dependency::Error, + expect{ k.bar! }.to raise_error ActiveAdmin::DependencyError, 'To use bar you need to specify it in your Gemfile.' end end @@ -90,10 +90,10 @@ expect(k['a-b'].match! ).to eq nil expect(k['a-b'].match! '1.2.3').to eq nil - expect{ k['a-b'].match! '2.5' }.to raise_error ActiveAdmin::Dependency::Error, + expect{ k['a-b'].match! '2.5' }.to raise_error ActiveAdmin::DependencyError, 'You provided a-b 1.2.3 but we need: 2.5.' - expect{ k['b-c'].match! }.to raise_error ActiveAdmin::Dependency::Error, + expect{ k['b-c'].match! }.to raise_error ActiveAdmin::DependencyError, 'To use b-c you need to specify it in your Gemfile.' end @@ -126,7 +126,7 @@ end it 'throws a custom error if the gem is missing' do - expect{ k['b-c'] < 23 }.to raise_error ActiveAdmin::Dependency::Error, + expect{ k['b-c'] < 23 }.to raise_error ActiveAdmin::DependencyError, 'To use b-c you need to specify it in your Gemfile.' end end