summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby McDonald <BobbyMcWho@users.noreply.github.com>2019-10-02 10:48:19 -0400
committerDaniel Doubrovkine (dB.) @dblockdotorg <dblock@dblock.org>2019-10-02 10:48:19 -0400
commitca3604516b5725b30a290482c219ca55fca5de49 (patch)
treed8c190ab769cba11a430c5c4b92162547f305fde
parent04cd30af1362574a1adea53aae0008e5732ea994 (diff)
downloadhashie-ca3604516b5725b30a290482c219ca55fca5de49.tar.gz
Make Hashie play nice with Rails 6 Hash#except method (#479)
-rw-r--r--.rubocop.yml3
-rw-r--r--.rubocop_todo.yml5
-rw-r--r--CHANGELOG.md1
-rw-r--r--lib/hashie/extensions/active_support/core_ext/hash.rb14
-rw-r--r--lib/hashie/railtie.rb7
-rw-r--r--spec/integration/elasticsearch/Gemfile2
-rw-r--r--spec/integration/elasticsearch/integration_spec.rb2
-rw-r--r--spec/integration/rails/Gemfile2
-rw-r--r--spec/integration/rails/app.rb17
-rw-r--r--spec/integration/rails/integration_spec.rb18
10 files changed, 51 insertions, 20 deletions
diff --git a/.rubocop.yml b/.rubocop.yml
index 8d0274b..30141c9 100644
--- a/.rubocop.yml
+++ b/.rubocop.yml
@@ -12,6 +12,9 @@ inherit_from: .rubocop_todo.yml
Layout/IndentHeredoc:
Enabled: false
+Metrics/ClassLength:
+ Enabled: false
+
Metrics/BlockLength:
Exclude:
- 'spec/**/*.rb'
diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml
index bc15a46..b3fa0d3 100644
--- a/.rubocop_todo.yml
+++ b/.rubocop_todo.yml
@@ -10,11 +10,6 @@
Metrics/AbcSize:
Max: 23
-# Offense count: 2
-# Configuration parameters: CountComments.
-Metrics/ClassLength:
- Max: 266
-
# Offense count: 7
Metrics/CyclomaticComplexity:
Max: 11
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fd227bd..56ac655 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -35,6 +35,7 @@ scheme are considered to be bugs.
* [#459](https://github.com/intridea/hashie/pull/459): Fixed a regression in `Mash.load` that disallowed aliases - [@arekt](https://github.com/arekt) and [@michaelherold](https://github.com/michaelherold).
* [#465](https://github.com/intridea/hashie/pull/465): Fixed `deep_update` to call any readers when a key exists - [@laertispappas](https://github.com/laertispappas).
+* [#479](https://github.com/intridea/hashie/pull/479): Fixed an issue with `Hash#except` not returning a `Mash` in Rails 6 - [@bobbymcwho](https://github.com/bobbymcwho).
* Your contribution here.
### Security
diff --git a/lib/hashie/extensions/active_support/core_ext/hash.rb b/lib/hashie/extensions/active_support/core_ext/hash.rb
new file mode 100644
index 0000000..b8d71f7
--- /dev/null
+++ b/lib/hashie/extensions/active_support/core_ext/hash.rb
@@ -0,0 +1,14 @@
+module Hashie
+ module Extensions
+ module ActiveSupport
+ module CoreExt
+ module Hash
+ def except(*keys)
+ string_keys = keys.map { |key| convert_key(key) }
+ super(*string_keys)
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/hashie/railtie.rb b/lib/hashie/railtie.rb
index 65cd007..c5f05b7 100644
--- a/lib/hashie/railtie.rb
+++ b/lib/hashie/railtie.rb
@@ -7,6 +7,13 @@ begin
initializer 'hashie.configure_logger', after: 'initialize_logger' do
Hashie.logger = Rails.logger
end
+
+ initializer 'hashie.patch_hash_except', after: 'load_active_support' do
+ if Rails::VERSION::MAJOR >= 6
+ require 'hashie/extensions/active_support/core_ext/hash'
+ Hashie::Mash.send(:include, Hashie::Extensions::ActiveSupport::CoreExt::Hash)
+ end
+ end
end
end
rescue LoadError => e
diff --git a/spec/integration/elasticsearch/Gemfile b/spec/integration/elasticsearch/Gemfile
index 318242a..15dc04f 100644
--- a/spec/integration/elasticsearch/Gemfile
+++ b/spec/integration/elasticsearch/Gemfile
@@ -1,6 +1,6 @@
source 'http://rubygems.org'
-gem 'elasticsearch-model'
+gem 'elasticsearch-model', '~> 7.0'
gem 'hashie', path: '../../..'
gem 'rake'
gem 'rspec', '~> 3.5.0'
diff --git a/spec/integration/elasticsearch/integration_spec.rb b/spec/integration/elasticsearch/integration_spec.rb
index 2c4e220..70689fb 100644
--- a/spec/integration/elasticsearch/integration_spec.rb
+++ b/spec/integration/elasticsearch/integration_spec.rb
@@ -23,7 +23,7 @@ RSpec.describe 'elaasticsearch-model' do
object = MyModel.new
stub_elasticsearch_client
- expect { object.__elasticsearch__.index_document }.to raise_error(NoMethodError)
+ expect { object.__elasticsearch__.index_document }.to raise_error(NameError)
end
it 'does not raise an error when the model has an id' do
diff --git a/spec/integration/rails/Gemfile b/spec/integration/rails/Gemfile
index 32e1749..da94f2f 100644
--- a/spec/integration/rails/Gemfile
+++ b/spec/integration/rails/Gemfile
@@ -1,6 +1,6 @@
source 'http://rubygems.org'
gem 'hashie', path: '../../..'
-gem 'rails', '~> 5.0.1'
+gem 'rails', '~> 6.0.0'
gem 'rspec', '~> 3.5.0'
gem 'rspec-rails'
diff --git a/spec/integration/rails/app.rb b/spec/integration/rails/app.rb
index dee7fe1..1065688 100644
--- a/spec/integration/rails/app.rb
+++ b/spec/integration/rails/app.rb
@@ -14,7 +14,7 @@ module RailsApp
end
end
-LAYOUT = <<-HTML.freeze
+PAGE = <<-HTML.freeze
<!DOCTYPE html>
<html>
<head>
@@ -22,24 +22,17 @@ LAYOUT = <<-HTML.freeze
<%= csrf_meta_tags %>
</head>
<body>
-<%= yield %>
+ <h1>Hello, world!</h1>
</body>
</html>
HTML
-INDEX = '<h1>Hello, world!</h1>'.freeze
-
class ApplicationController < ActionController::Base
include Rails.application.routes.url_helpers
- layout 'application'
-
- self.view_paths = [ActionView::FixtureResolver.new(
- 'layouts/application.html.erb' => LAYOUT,
- 'application/index.html.erb' => INDEX
- )]
-
- def index; end
+ def index
+ render inline: PAGE
+ end
end
Bundler.require(:default, Rails.env)
diff --git a/spec/integration/rails/integration_spec.rb b/spec/integration/rails/integration_spec.rb
index 9b150a2..dc1d670 100644
--- a/spec/integration/rails/integration_spec.rb
+++ b/spec/integration/rails/integration_spec.rb
@@ -19,6 +19,24 @@ RSpec.describe 'rails', type: :request do
expect(Hashie.logger).to eq(Rails.logger)
end
+ context '#except' do
+ subject { Hashie::Mash.new(x: 1, y: 2) }
+
+ it 'returns an instance of the class it was called on' do
+ class HashieKlass < Hashie::Mash; end
+ hashie_klass = HashieKlass.new(subject)
+ expect(hashie_klass.except('x')).to be_a HashieKlass
+ end
+
+ it 'works with string keys' do
+ expect(subject.except('x')).to eq Hashie::Mash.new(y: 2)
+ end
+
+ it 'works with symbol keys' do
+ expect(subject.except(:x)).to eq Hashie::Mash.new(y: 2)
+ end
+ end
+
it 'works' do
get '/'
assert_select 'h1', 'Hello, world!'