Class: Tokenable::Generators::InstallGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/tokenable/install_generator.rb

Instance Method Summary collapse

Instance Method Details

#install_configObject



11
12
13
14
15
# File 'lib/generators/tokenable/install_generator.rb', line 11

def install_config
  template 'tokenable.rb.erb', 'config/initializers/tokenable.rb'
  template 'routes.rb.erb', 'config/routes.rb' unless routes_file_exists?
  route "mount Tokenable::Engine => '/api/auth'"
end

#setup_strategyObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/generators/tokenable/install_generator.rb', line 17

def setup_strategy
  unless options.strategy
    say_status :skip, 'strategy (none provided)', :yellow
    return
  end

  if options.strategy.in?(list_of_strategies)
    invoke 'active_record:model', [name], migration: false unless model_exists?

    strategy_class = options.strategy.classify
    model_path = "app/models/#{file_name}.rb"
    already_injected = File.open(File.join(destination_root, model_path)).grep(/Tokenable::Strategies/).any?

    if already_injected
      say_status :skip, 'a strategy is already in this model', :yellow
    else
      inject_into_file model_path, "  include Tokenable::Strategies::#{strategy_class}\n", after: " < ApplicationRecord\n"
    end
  else
    say_status :failure, "stargery not found (#{options.strategy}). Available: #{list_of_strategies.join(", ")}", :red
  end
end