-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from TruemarkDev/generator-for-honeybadger
Generator for honeybadger
- Loading branch information
Showing
6 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
lib/generators/boring/honeybadger/install/install_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
module Boring | ||
module Honeybadger | ||
class InstallGenerator < Rails::Generators::Base | ||
source_root File.expand_path("templates", __dir__) | ||
desc 'Adds honeybadger to the app' | ||
|
||
class_option :use_env_variable, type: :boolean, aliases: "-ev", | ||
desc: 'Use ENV variable for devise_jwt_secret_key. By default Rails credentials will be used.' | ||
|
||
def add_honeybadger_gem | ||
say 'Adding Honeybadger gem', :green | ||
|
||
Bundler.with_unbundled_env do | ||
run 'bundle add honeybadger' | ||
end | ||
end | ||
|
||
def configure_honeybadger_gem | ||
say 'Setting up Honeybadger', :green | ||
|
||
@api_key = honeybadger_api_key | ||
|
||
template 'honeybadger.yml', 'config/honeybadger.yml' | ||
|
||
show_readme | ||
end | ||
|
||
private | ||
|
||
def show_readme | ||
readme_template = File.read(File.join(self.class.source_root, 'README')) | ||
readme_content = ERB.new(readme_template).result(binding) | ||
say readme_content | ||
end | ||
|
||
def honeybadger_api_key | ||
if options[:use_env_variable] | ||
"ENV['HONEYBADGER_API_KEY']" | ||
else | ||
"Rails.application.credentials.dig(:honeybadger, :api_key)" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
=============================================================================== | ||
|
||
The API key for Honeybadger will be used from <%= @api_key %>. | ||
You can change these values if they don't match with your app. | ||
|
||
=============================================================================== |
26 changes: 26 additions & 0 deletions
26
lib/generators/boring/honeybadger/install/templates/honeybadger.yml.tt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
--- | ||
# For more options, see https://docs.honeybadger.io/lib/ruby/gem-reference/configuration | ||
|
||
api_key: <%= @api_key %> | ||
|
||
# The environment your app is running in. | ||
env: "<%= Rails.env %>" | ||
|
||
# The absolute path to your project folder. | ||
root: "<%= Rails.root.to_s %>" | ||
|
||
# Honeybadger won't report errors in these environments. | ||
development_environments: | ||
- test | ||
- development | ||
- cucumber | ||
|
||
# By default, Honeybadger won't report errors in the development_environments. | ||
# You can override this by explicitly setting report_data to true or false. | ||
# report_data: true | ||
|
||
# The current Git revision of your project. Defaults to the last commit hash. | ||
# revision: null | ||
|
||
# Enable verbose debug logging (useful for troubleshooting). | ||
debug: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# fronzen_string_literal: true | ||
|
||
require 'test_helper' | ||
require 'generators/boring/honeybadger/install/install_generator' | ||
|
||
class HoneybadgerInstallGeneratorTest < Rails::Generators::TestCase | ||
tests Boring::Honeybadger::InstallGenerator | ||
setup :build_app | ||
teardown :teardown_app | ||
|
||
include GeneratorHelper | ||
|
||
def destination_root | ||
app_path | ||
end | ||
|
||
def test_should_configure_honeybadger | ||
Dir.chdir(app_path) do | ||
quietly { run_generator } | ||
|
||
assert_gem 'honeybadger' | ||
|
||
assert_file 'config/honeybadger.yml' do |content| | ||
assert_match(/\napi_key: Rails.application.credentials.dig\(:honeybadger, :api_key\)\n/, content) | ||
end | ||
end | ||
end | ||
|
||
def test_should_use_env_variable_for_api_key | ||
Dir.chdir(app_path) do | ||
quietly { run_generator([destination_root, '--use_env_variable']) } | ||
|
||
assert_gem 'honeybadger' | ||
|
||
assert_file 'config/honeybadger.yml' do |content| | ||
assert_match(/api_key: ENV\['HONEYBADGER_API_KEY'\]\n/, content) | ||
end | ||
end | ||
end | ||
end |