Skip to content

Commit

Permalink
Add code snippets Current
Browse files Browse the repository at this point in the history
  • Loading branch information
zhandao committed Mar 12, 2024
1 parent 4a8b76d commit 4275a01
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 9 deletions.
4 changes: 4 additions & 0 deletions config/code_snippets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ home_controller:
basic_auth:
prompt: "BasicAuth controller concern"
description: "Allow app to be secured with ENV-based basic auth credentials"

current:
prompt: "Current.user"
description: "Intro thread-isolated attributes singleton `Current`, which resets automatically before and after each request"
4 changes: 2 additions & 2 deletions lib/nextgen/commands/create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ def initialize(app_path, _options)
@app_path = File.expand_path(app_path)
@app_name = File.basename(@app_path).gsub(/\W/, "_").squeeze("_").camelize
@rails_opts = RailsOptions.new
@generators = {basic: Generators.compatible_with(rails_opts: rails_opts, scope: "basic")}
end

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

if prompt.yes?("More detailed configuration? [ cache, job and gems ] ↵")
if prompt.yes?("More detailed configuration? [ job, code snippets, gems ... ] ↵")
ask_job_backend if rails_opts.active_job?
ask_workflows
ask_checkers
Expand Down Expand Up @@ -99,6 +98,7 @@ def ask_full_stack_or_api
"API only" => true
)
rails_opts.api! if api
@generators = {basic: Generators.compatible_with(rails_opts: rails_opts, scope: "basic")}
end

def ask_frontend_management
Expand Down
11 changes: 4 additions & 7 deletions lib/nextgen/generators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Nextgen
class Generators
def self.compatible_with(rails_opts:, scope:)
yaml_path = File.expand_path("../../config/#{scope}.yml", __dir__)
new(scope).tap do |generators|
new(scope, api: rails_opts.api?).tap do |generators|
YAML.load_file(yaml_path).each do |name, options|
options ||= {}
requirements = Array(options["requires"])
Expand All @@ -19,16 +19,13 @@ def self.compatible_with(rails_opts:, scope:)
)
end

generators.variables[:api] = rails_opts.api?
generators.deactivate_node unless rails_opts.requires_node?
end
end

attr_accessor :variables

def initialize(scope)
def initialize(scope, **vars)
@generators = {}
@variables = {}
@variables = vars
@scope = scope
end

Expand Down Expand Up @@ -113,6 +110,6 @@ def to_ruby_script

private

attr_reader :generators, :scope
attr_reader :generators, :variables, :scope
end
end
1 change: 1 addition & 0 deletions lib/nextgen/generators/code_snippets/current.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
copy_file "app/models/current.rb"
3 changes: 3 additions & 0 deletions template/app/models/current.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Current < ActiveSupport::CurrentAttributes
attribute :user
end

0 comments on commit 4275a01

Please sign in to comment.