class Tokenable::Generators::InstallGenerator

Public Instance Methods

install_config() click to toggle source
# 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_strategy() click to toggle source
# 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

Private Instance Methods

list_of_strategies() click to toggle source
# File lib/generators/tokenable/install_generator.rb, line 50
def list_of_strategies
  Dir.entries(File.expand_path('../../tokenable/strategies', __dir__))
     .reject { |f| File.directory?(f) }
     .map { |f| File.basename(f, File.extname(f)) }
     .compact
end
model_exists?() click to toggle source
# File lib/generators/tokenable/install_generator.rb, line 42
def model_exists?
  File.exist?(File.join(destination_root, "app/models/#{file_name}.rb"))
end
routes_file_exists?() click to toggle source
# File lib/generators/tokenable/install_generator.rb, line 46
def routes_file_exists?
  File.exist?(File.join(destination_root, 'config/routes.rb'))
end