Skip to content

Commit 4275a01

Browse files
author
zhandao
committed
Add code snippets Current
1 parent 4a8b76d commit 4275a01

File tree

5 files changed

+14
-9
lines changed

5 files changed

+14
-9
lines changed

config/code_snippets.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,7 @@ home_controller:
66
basic_auth:
77
prompt: "BasicAuth controller concern"
88
description: "Allow app to be secured with ENV-based basic auth credentials"
9+
10+
current:
11+
prompt: "Current.user"
12+
description: "Intro thread-isolated attributes singleton `Current`, which resets automatically before and after each request"

lib/nextgen/commands/create.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ def initialize(app_path, _options)
2222
@app_path = File.expand_path(app_path)
2323
@app_name = File.basename(@app_path).gsub(/\W/, "_").squeeze("_").camelize
2424
@rails_opts = RailsOptions.new
25-
@generators = {basic: Generators.compatible_with(rails_opts: rails_opts, scope: "basic")}
2625
end
2726

2827
def run # rubocop:disable Metrics/MethodLength Metrics/PerceivedComplexity
@@ -40,7 +39,7 @@ def run # rubocop:disable Metrics/MethodLength Metrics/PerceivedComplexity
4039
ask_system_testing if rails_opts.frontend? && rails_opts.test_framework?
4140
say
4241

43-
if prompt.yes?("More detailed configuration? [ cache, job and gems ] ↵")
42+
if prompt.yes?("More detailed configuration? [ job, code snippets, gems ... ] ↵")
4443
ask_job_backend if rails_opts.active_job?
4544
ask_workflows
4645
ask_checkers
@@ -99,6 +98,7 @@ def ask_full_stack_or_api
9998
"API only" => true
10099
)
101100
rails_opts.api! if api
101+
@generators = {basic: Generators.compatible_with(rails_opts: rails_opts, scope: "basic")}
102102
end
103103

104104
def ask_frontend_management

lib/nextgen/generators.rb

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Nextgen
44
class Generators
55
def self.compatible_with(rails_opts:, scope:)
66
yaml_path = File.expand_path("../../config/#{scope}.yml", __dir__)
7-
new(scope).tap do |generators|
7+
new(scope, api: rails_opts.api?).tap do |generators|
88
YAML.load_file(yaml_path).each do |name, options|
99
options ||= {}
1010
requirements = Array(options["requires"])
@@ -19,16 +19,13 @@ def self.compatible_with(rails_opts:, scope:)
1919
)
2020
end
2121

22-
generators.variables[:api] = rails_opts.api?
2322
generators.deactivate_node unless rails_opts.requires_node?
2423
end
2524
end
2625

27-
attr_accessor :variables
28-
29-
def initialize(scope)
26+
def initialize(scope, **vars)
3027
@generators = {}
31-
@variables = {}
28+
@variables = vars
3229
@scope = scope
3330
end
3431

@@ -113,6 +110,6 @@ def to_ruby_script
113110

114111
private
115112

116-
attr_reader :generators, :scope
113+
attr_reader :generators, :variables, :scope
117114
end
118115
end
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
copy_file "app/models/current.rb"

template/app/models/current.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Current < ActiveSupport::CurrentAttributes
2+
attribute :user
3+
end

0 commit comments

Comments
 (0)