From ac6193c9eab02c94b2c079068f6019ebd08a4d30 Mon Sep 17 00:00:00 2001 From: Michael Herold Date: Fri, 3 Feb 2017 17:35:06 -0600 Subject: [PATCH 1/3] Remove auto-load for `Hashie.logger` The auto-load is never triggered because there is no `Hashie::Logger` constant to trigger it. We need to `require` the file in the top level `hashie.rb` and anywhere else that uses it (in case someone just loads a single file). At the moment, this is only used in Mash, so we're covered. --- lib/hashie.rb | 3 +-- lib/hashie/logger.rb | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/hashie.rb b/lib/hashie.rb index 6941c32a..c0f2caad 100644 --- a/lib/hashie.rb +++ b/lib/hashie.rb @@ -1,8 +1,7 @@ -require 'logger' +require 'hashie/logger' require 'hashie/version' module Hashie - autoload :Logger, 'hashie/logger' autoload :Clash, 'hashie/clash' autoload :Dash, 'hashie/dash' autoload :Hash, 'hashie/hash' diff --git a/lib/hashie/logger.rb b/lib/hashie/logger.rb index 4731fb90..065738f4 100644 --- a/lib/hashie/logger.rb +++ b/lib/hashie/logger.rb @@ -1,3 +1,5 @@ +require 'logger' + module Hashie # The logger that Hashie uses for reporting errors. # From ca34d28a8404437a2fad962a9493e6fa57bbd5ba Mon Sep 17 00:00:00 2001 From: Michael Herold Date: Fri, 10 Feb 2017 08:23:51 -0600 Subject: [PATCH 2/3] In Rails, set `Hashie.logger` to `Rails.logger` --- .gitignore | 4 +- lib/hashie/logger.rb | 7 ++- spec/integration/rails/.rspec | 3 + spec/integration/rails/Gemfile | 5 ++ spec/integration/rails/integration_spec.rb | 66 ++++++++++++++++++++++ 5 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 spec/integration/rails/.rspec create mode 100644 spec/integration/rails/Gemfile create mode 100644 spec/integration/rails/integration_spec.rb diff --git a/.gitignore b/.gitignore index 42bfa645..3563ce05 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,8 @@ coverage rdoc pkg *.gem +*.log .bundle .rvmrc -Gemfile.lock \ No newline at end of file +Gemfile.lock +log/ diff --git a/lib/hashie/logger.rb b/lib/hashie/logger.rb index 065738f4..4f8fe5ae 100644 --- a/lib/hashie/logger.rb +++ b/lib/hashie/logger.rb @@ -5,7 +5,12 @@ module Hashie # # @return [Logger] def self.logger - @logger ||= Logger.new(STDOUT) + @logger ||= + if defined?(::Rails) + Rails.logger + else + Logger.new(STDOUT) + end end # Sets the logger that Hashie uses for reporting errors. diff --git a/spec/integration/rails/.rspec b/spec/integration/rails/.rspec new file mode 100644 index 00000000..a4c51e72 --- /dev/null +++ b/spec/integration/rails/.rspec @@ -0,0 +1,3 @@ +--colour +--format=documentation +--pattern=*_spec.rb diff --git a/spec/integration/rails/Gemfile b/spec/integration/rails/Gemfile new file mode 100644 index 00000000..3478982a --- /dev/null +++ b/spec/integration/rails/Gemfile @@ -0,0 +1,5 @@ +source 'http://rubygems.org' + +gem 'hashie', path: '../../..' +gem 'rails' +gem 'rspec', '~> 3.5.0' diff --git a/spec/integration/rails/integration_spec.rb b/spec/integration/rails/integration_spec.rb new file mode 100644 index 00000000..87ff1fca --- /dev/null +++ b/spec/integration/rails/integration_spec.rb @@ -0,0 +1,66 @@ +ENV['RACK_ENV'] = 'test' + +require 'rspec/core' +require 'rails' +require 'rails/all' +require 'action_view/testing/resolvers' + +module RailsApp + class Application < ::Rails::Application + config.action_dispatch.show_exceptions = false + config.active_support.deprecation = :stderr + config.eager_load = false + config.root = __dir__ + config.secret_key_base = 'hashieintegrationtest' + + routes.append do + get '/' => 'application#index' + end + + config.assets.paths << File.join(__dir__, 'assets/javascripts') + config.assets.debug = true + end +end + +LAYOUT = <<-HTML + + + + TestApp + <%= stylesheet_link_tag "application", :media => "all" %> + <%= javascript_include_tag "application" %> + <%= csrf_meta_tags %> + + +<%= yield %> + + +HTML + +INDEX = <<-HTML +

Hello, world!

+HTML + +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 +end + +RailsApp::Application.initialize! + +require 'hashie' + +RSpec.describe 'the Hashie logger' do + it 'is set to the Rails logger' do + expect(Hashie.logger).to eq(Rails.logger) + end +end From f4d4becdaae05787f7d450e84e372b7c350c98d7 Mon Sep 17 00:00:00 2001 From: Michael Herold Date: Fri, 10 Feb 2017 08:29:11 -0600 Subject: [PATCH 3/3] Add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7986d131..a9ccdc7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ scheme are considered to be bugs. ### Added * [#395](https://github.com/intridea/hashie/pull/395): Add the ability to disable warnings in Mash subclasses - [@michaelherold](https://github.com/michaelherold). +* [#400](https://github.com/intridea/hashie/pull/400): Fix Hashie.logger load and set the Hashie logger to the Rails logger in a Rails environment - [@michaelherold](https://github.com/michaelherold). ### Changed