Skip to content

Commit

Permalink
Merge pull request #400 from michaelherold/rails-logger
Browse files Browse the repository at this point in the history
Fix loading of Hashie.logger and set it to Rails.logger when in Rails
  • Loading branch information
michaelherold committed Feb 10, 2017
2 parents 61813e6 + f4d4bec commit c36f67e
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Expand Up @@ -4,6 +4,8 @@ coverage
rdoc
pkg
*.gem
*.log
.bundle
.rvmrc
Gemfile.lock
Gemfile.lock
log/
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions 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'
Expand Down
9 changes: 8 additions & 1 deletion lib/hashie/logger.rb
@@ -1,9 +1,16 @@
require 'logger'

module Hashie
# The logger that Hashie uses for reporting errors.
#
# @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.
Expand Down
3 changes: 3 additions & 0 deletions spec/integration/rails/.rspec
@@ -0,0 +1,3 @@
--colour
--format=documentation
--pattern=*_spec.rb
5 changes: 5 additions & 0 deletions spec/integration/rails/Gemfile
@@ -0,0 +1,5 @@
source 'http://rubygems.org'

gem 'hashie', path: '../../..'
gem 'rails'
gem 'rspec', '~> 3.5.0'
66 changes: 66 additions & 0 deletions 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
<!DOCTYPE html>
<html>
<head>
<title>TestApp</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
HTML

INDEX = <<-HTML
<h1>Hello, world!</h1>
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

0 comments on commit c36f67e

Please sign in to comment.