Skip to content

Commit

Permalink
Merge pull request #82 from FundingCircle/rails50-support
Browse files Browse the repository at this point in the history
Rails 5 Support & Co
  • Loading branch information
timstott authored Sep 13, 2016
2 parents 729f94d + 297f1fe commit ae9fd05
Show file tree
Hide file tree
Showing 51 changed files with 874 additions and 19 deletions.
4 changes: 4 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ appraise 'sinatra14' do
gem 'sinatra', '~> 1.4.0'
end

appraise 'rails50' do
gem 'rails', '~> 5.0.0'
end

appraise 'unit' do
end
11 changes: 11 additions & 0 deletions gemfiles/rails50.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# This file was generated by Appraisal

source "https://rubygems.org"

gem "rails", "~> 5.0.0"

group :test do
gem "codeclimate-test-reporter", :require => false
end

gemspec :path => "../"
File renamed without changes.
1 change: 1 addition & 0 deletions lib/loga/rack/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def set_data
data['request_id'] = request.uuid
data['request_ip'] = request.ip
data['user_agent'] = request.user_agent
data['controller'] = request.action_controller if request.action_controller_instance
data['duration'] = duration_in_ms(started_at, Time.now)
end

Expand Down
11 changes: 11 additions & 0 deletions lib/loga/rack/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Loga
module Rack
class Request < ::Rack::Request
ACTION_DISPATCH_REQUEST_ID = 'action_dispatch.request_id'.freeze
ACTION_CONTROLLER_INSTANCE = 'action_controller.instance'.freeze

def initialize(env)
super
Expand All @@ -15,6 +16,16 @@ def uuid
@uuid ||= env[ACTION_DISPATCH_REQUEST_ID]
end

alias request_id uuid

def action_controller
"#{action_controller_instance.class}##{action_controller_instance.action_name}"
end

def action_controller_instance
@action_controller_instance ||= env[ACTION_CONTROLLER_INSTANCE]
end

def original_path
env['loga.request.original_path']
end
Expand Down
15 changes: 12 additions & 3 deletions lib/loga/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ def self.included(base) #:nodoc:

private

def render_exception_with_loga(env, exception)
def render_exception_with_loga(arg, exception)
env = arg.is_a?(ActionDispatch::Request) ? arg.env : arg
env['loga.exception'] = exception
render_exception_without_loga(env, exception)
render_exception_without_loga(arg, exception)
end
end

Expand Down Expand Up @@ -85,7 +86,8 @@ def disable_rails_rack_logger

case Rails::VERSION::MAJOR
when 3 then require 'loga/ext/rails/rack/logger3.rb'
when 4 then require 'loga/ext/rails/rack/logger4.rb'
else
require 'loga/ext/rails/rack/logger.rb'
end
end

Expand Down Expand Up @@ -113,14 +115,21 @@ def call

private

# rubocop:disable Metrics/CyclomaticComplexity
def remove_existing_log_subscriptions
ActionView::Base if defined?(ActionView::Base)
ActionController::Base if defined?(ActionController::Base)

ActiveSupport::LogSubscriber.log_subscribers.each do |subscriber|
case subscriber
when defined?(ActionView::LogSubscriber) && ActionView::LogSubscriber
unsubscribe(:action_view, subscriber)
when defined?(ActionController::LogSubscriber) && ActionController::LogSubscriber
unsubscribe(:action_controller, subscriber)
end
end
end
# rubocop:enable Metrics/CyclomaticComplexity

def unsubscribe(component, subscriber)
events = subscriber.public_methods(false).reject { |method| method.to_s == 'call' }
Expand Down
2 changes: 1 addition & 1 deletion lib/loga/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Loga
VERSION = '1.3.0'.freeze
VERSION = '1.4.0'.freeze
end
7 changes: 5 additions & 2 deletions spec/fixtures/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Generating fixture Apps

## Rails 3.2
`appraisal rails_3.2 rails new spec/fixtures/rails32 --skip-gemfile --skip-git --skip-active-record --skip-sprockets --skip-javascript --skip-test-unit`
`appraisal rails32 rails new spec/fixtures/rails32 --skip-gemfile --skip-git --skip-active-record --skip-sprockets --skip-javascript --skip-test-unit`


## Rails 4.0
`appraisal rails_4.0 rails new spec/fixtures/rails40 --skip-gemfile --skip-git --skip-keeps --skip-active-record --skip-action-view --skip-sprockets --skip-spring --skip-javascript --skip-test-unit`
`appraisal rails40 rails new spec/fixtures/rails40 --skip-gemfile --skip-git --skip-keeps --skip-active-record --skip-action-view --skip-sprockets --skip-spring --skip-javascript --skip-test-unit`

## Rails 5.0
`appraisal rails50 rails new spec/fixtures/rails50 --skip-gemfile --skip-git --skip-keeps --skip-active-record --skip-action-view --skip-sprockets --skip-spring --skip-javascript --skip-test-unit`
6 changes: 6 additions & 0 deletions spec/fixtures/rails50/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require_relative 'config/application'

Rails.application.load_tasks
28 changes: 28 additions & 0 deletions spec/fixtures/rails50/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :null_session

def ok
render text: 'Hello Rails'
end

def error
nil.name
end

def show
render json: params
end

def create
render json: params
end

def new
redirect_to :ok
end

def update
@id = params[:id]
render '/user'
end
end
2 changes: 2 additions & 0 deletions spec/fixtures/rails50/app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ApplicationHelper
end
13 changes: 13 additions & 0 deletions spec/fixtures/rails50/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<title>Rails50</title>
<%= csrf_meta_tags %>

<%= stylesheet_link_tag 'application', media: 'all' %>
</head>

<body>
<%= yield %>
</body>
</html>
1 change: 1 addition & 0 deletions spec/fixtures/rails50/app/views/user.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>User <%= @id %></p>
3 changes: 3 additions & 0 deletions spec/fixtures/rails50/bin/bundle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env ruby
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
load Gem.bin_path('bundler', 'bundle')
4 changes: 4 additions & 0 deletions spec/fixtures/rails50/bin/rails
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env ruby
APP_PATH = File.expand_path('../config/application', __dir__)
require_relative '../config/boot'
require 'rails/commands'
4 changes: 4 additions & 0 deletions spec/fixtures/rails50/bin/rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env ruby
require_relative '../config/boot'
require 'rake'
Rake.application.run
34 changes: 34 additions & 0 deletions spec/fixtures/rails50/bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env ruby
require 'pathname'
require 'fileutils'
include FileUtils

# path to your application root.
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)

def system!(*args)
system(*args) || abort("\n== Command #{args} failed ==")
end

chdir APP_ROOT do
# This script is a starting point to setup your application.
# Add necessary setup steps to this file.

puts '== Installing dependencies =='
system! 'gem install bundler --conservative'
system('bundle check') || system!('bundle install')

# puts "\n== Copying sample files =="
# unless File.exist?('config/database.yml')
# cp 'config/database.yml.sample', 'config/database.yml'
# end

puts "\n== Preparing database =="
system! 'bin/rails db:setup'

puts "\n== Removing old logs and tempfiles =="
system! 'bin/rails log:clear tmp:clear'

puts "\n== Restarting application server =="
system! 'bin/rails restart'
end
29 changes: 29 additions & 0 deletions spec/fixtures/rails50/bin/update
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env ruby
require 'pathname'
require 'fileutils'
include FileUtils

# path to your application root.
APP_ROOT = Pathname.new File.expand_path('../../', __FILE__)

def system!(*args)
system(*args) || abort("\n== Command #{args} failed ==")
end

chdir APP_ROOT do
# This script is a way to update your development environment automatically.
# Add necessary update steps to this file.

puts '== Installing dependencies =='
system! 'gem install bundler --conservative'
system('bundle check') || system!('bundle install')

puts "\n== Updating database =="
system! 'bin/rails db:migrate'

puts "\n== Removing old logs and tempfiles =="
system! 'bin/rails log:clear tmp:clear'

puts "\n== Restarting application server =="
system! 'bin/rails restart'
end
5 changes: 5 additions & 0 deletions spec/fixtures/rails50/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This file is used by Rack-based servers to start the application.

require_relative 'config/environment'

run Rails.application
34 changes: 34 additions & 0 deletions spec/fixtures/rails50/config/application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require_relative 'boot'

require "rails"
# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
# require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "action_cable/engine"
# require "sprockets/railtie"
# require "rails/test_unit/railtie"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

STREAM = StringIO.new unless defined?(STREAM)

module Rails50
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
config.log_tags = [ :request_id, 'TEST_TAG' ]
config.loga.configure do |loga|
loga.service_name = 'hello_world_app'
loga.service_version = '1.0'
loga.host = 'bird.example.com'
loga.device = STREAM
end
end
end
3 changes: 3 additions & 0 deletions spec/fixtures/rails50/config/boot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)

require 'bundler/setup' # Set up gems listed in the Gemfile.
9 changes: 9 additions & 0 deletions spec/fixtures/rails50/config/cable.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
development:
adapter: async

test:
adapter: async

production:
adapter: redis
url: redis://localhost:6379/1
5 changes: 5 additions & 0 deletions spec/fixtures/rails50/config/environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Load the Rails application.
require_relative 'application'

# Initialize the Rails application.
Rails.application.initialize!
44 changes: 44 additions & 0 deletions spec/fixtures/rails50/config/environments/development.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false

# Do not eager load code on boot.
config.eager_load = false

# Show full error reports.
config.consider_all_requests_local = true

# Enable/disable caching. By default caching is disabled.
if Rails.root.join('tmp/caching-dev.txt').exist?
config.action_controller.perform_caching = true

config.cache_store = :memory_store
config.public_file_server.headers = {
'Cache-Control' => 'public, max-age=172800'
}
else
config.action_controller.perform_caching = false

config.cache_store = :null_store
end

# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false

config.action_mailer.perform_caching = false

# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log


# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true

# Use an evented file watcher to asynchronously detect changes in source code,
# routes, locales, etc. This feature depends on the listen gem.
# config.file_watcher = ActiveSupport::EventedFileUpdateChecker
end
Loading

0 comments on commit ae9fd05

Please sign in to comment.