Skip to content

Commit 02538e4

Browse files
authored
Add generator for initializer generation (#2286)
1 parent 0b318c0 commit 02538e4

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## Unreleased
2+
3+
### Features
4+
5+
- Add generator for initializer generation ([#2286](https://github.com/getsentry/sentry-ruby/pull/2286))
6+
7+
Rails users will be able to use `bin/rails generate sentry` to generate their `config/initializers/sentry.rb` file.
8+
19
## 5.17.3
210

311
### Features
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require "rails/generators/base"
2+
3+
class SentryGenerator < ::Rails::Generators::Base
4+
class_option :dsn, type: :string, desc: "Sentry DSN"
5+
6+
def copy_initializer_file
7+
dsn = options[:dsn] ? "'#{options[:dsn]}'" : "ENV['SENTRY_DSN']"
8+
9+
create_file "config/initializers/sentry.rb", <<~RUBY
10+
# frozen_string_literal: true
11+
12+
Sentry.init do |config|
13+
config.breadcrumbs_logger = [:active_support_logger]
14+
config.dsn = #{dsn}
15+
config.enable_tracing = true
16+
end
17+
RUBY
18+
end
19+
end
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# frozen_string_literal: true
2+
3+
require "fileutils"
4+
require "rails/generators/test_case"
5+
require "generators/sentry_generator"
6+
7+
RSpec.describe SentryGenerator do
8+
include ::Rails::Generators::Testing::Behaviour
9+
include FileUtils
10+
self.destination File.expand_path('../../tmp', __dir__)
11+
self.generator_class = described_class
12+
13+
before do
14+
prepare_destination
15+
end
16+
17+
it "creates a initializer file" do
18+
run_generator
19+
20+
file = File.join(destination_root, "config/initializers/sentry.rb")
21+
expect(File).to exist(file)
22+
content = File.read(file)
23+
expect(content).to include(<<~RUBY)
24+
Sentry.init do |config|
25+
config.breadcrumbs_logger = [:active_support_logger]
26+
config.dsn = ENV['SENTRY_DSN']
27+
config.enable_tracing = true
28+
end
29+
RUBY
30+
end
31+
32+
context "with a DSN option" do
33+
it "creates a initializer file with the DSN" do
34+
run_generator %w[--dsn foobarbaz]
35+
36+
file = File.join(destination_root, "config/initializers/sentry.rb")
37+
expect(File).to exist(file)
38+
content = File.read(file)
39+
expect(content).to include(<<~RUBY)
40+
Sentry.init do |config|
41+
config.breadcrumbs_logger = [:active_support_logger]
42+
config.dsn = 'foobarbaz'
43+
config.enable_tracing = true
44+
end
45+
RUBY
46+
end
47+
end
48+
end

0 commit comments

Comments
 (0)