Class: LcpRuby::Generators::ApiTokensGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
FormatSupport, Prerequisites
Defined in:
lib/generators/lcp_ruby/api_tokens_generator.rb

Constant Summary

Constants included from FormatSupport

FormatSupport::VALID_FORMATS

Instance Method Summary collapse

Methods included from FormatSupport

included, #validate_format

Methods included from Prerequisites

included, #lcp_format_missing_prereqs

Instance Method Details

#append_menu_entryObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/generators/lcp_ruby/api_tokens_generator.rb', line 51

def append_menu_entry
  menu_path = File.join(destination_root, "config/lcp_ruby/menu.yml")
  unless File.exist?(menu_path)
    say "Skipped menu append — config/lcp_ruby/menu.yml not found. Add the entry manually if desired.", :yellow
    return
  end

  raw = YAML.safe_load(File.read(menu_path), permitted_classes: [ Symbol ]) || {}
  # menu.yml accepts either `top_menu:` at root, or nested under `menu:`.
  # Detect which shape is in use and modify in place.
  nested = raw.key?("menu")
  container = nested ? (raw["menu"] ||= {}) : raw
  top = container["top_menu"] || []

  url = "/my-api-tokens"
  if top.any? { |entry| entry.is_a?(Hash) && entry["url"] == url }
    say "Skipped menu append — entry for #{url} already present.", :yellow
    return
  end

  top << {
    "label" => "My API Tokens",
    "icon"  => "key",
    "url"   => url,
    "position" => "right"
  }
  container["top_menu"] = top
  File.write(menu_path, raw.to_yaml)
  say "Appended menu entry for #{url} to config/lcp_ruby/menu.yml", :green
end

#copy_create_dialog_presenterObject



31
32
33
34
35
# File 'lib/generators/lcp_ruby/api_tokens_generator.rb', line 31

def copy_create_dialog_presenter
  copy_dsl_or_yaml "create_dialog.rb",
    dsl_target:  "config/lcp_ruby/presenters/my_api_tokens_create_dialog.rb",
    yaml_target: "config/lcp_ruby/presenters/my_api_tokens_create_dialog.yml"
end

#copy_localeObject



47
48
49
# File 'lib/generators/lcp_ruby/api_tokens_generator.rb', line 47

def copy_locale
  template "locales.en.yml", "config/locales/lcp_ruby/api_tokens.en.yml"
end

#copy_modelObject



19
20
21
22
23
# File 'lib/generators/lcp_ruby/api_tokens_generator.rb', line 19

def copy_model
  copy_dsl_or_yaml "model.rb",
    dsl_target:  "config/lcp_ruby/models/api_token.rb",
    yaml_target: "config/lcp_ruby/models/api_token.yml"
end

#copy_permissionsObject



43
44
45
# File 'lib/generators/lcp_ruby/api_tokens_generator.rb', line 43

def copy_permissions
  template "permissions.yml", "config/lcp_ruby/permissions/api_token.yml"
end

#copy_presenterObject



25
26
27
28
29
# File 'lib/generators/lcp_ruby/api_tokens_generator.rb', line 25

def copy_presenter
  copy_dsl_or_yaml "presenter.rb",
    dsl_target:  "config/lcp_ruby/presenters/my_api_tokens.rb",
    yaml_target: "config/lcp_ruby/presenters/my_api_tokens.yml"
end

#copy_view_groupObject



37
38
39
40
41
# File 'lib/generators/lcp_ruby/api_tokens_generator.rb', line 37

def copy_view_group
  copy_dsl_or_yaml "view_group.rb",
    dsl_target:  "config/lcp_ruby/views/my_api_tokens.rb",
    yaml_target: "config/lcp_ruby/views/my_api_tokens.yml"
end

#show_post_install_messageObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/generators/lcp_ruby/api_tokens_generator.rb', line 82

def show_post_install_message
  say ""
  say "LCP Ruby API tokens installed!", :green
  say ""
  say "Generated files:"
  say "  - config/lcp_ruby/models/api_token.#{format_extension}"
  say "  - config/lcp_ruby/presenters/my_api_tokens.#{format_extension}"
  say "  - config/lcp_ruby/presenters/my_api_tokens_create_dialog.#{format_extension}"
  say "  - config/lcp_ruby/views/my_api_tokens.#{format_extension}"
  say "  - config/lcp_ruby/permissions/api_token.yml"
  say "  - config/locales/lcp_ruby/api_tokens.en.yml"
  say ""
  say "Next steps:"
  say "  1. bundle exec rails db:prepare    # creates lcp_ruby_api_tokens table"
  say "  2. Start server and visit /my-api-tokens to mint a token."
  say "  3. Use the token via 'Authorization: Bearer <token>' or HTTP Basic (password = token)."
  say ""
end