Class: Boring::Devise::Doorkeeper::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
BoringGenerators::GeneratorHelper
Defined in:
lib/generators/boring/devise/doorkeeper/install/install_generator.rb

Instance Method Summary collapse

Methods included from BoringGenerators::GeneratorHelper

#app_ruby_version, #bundle_install, #check_and_install_gem, #gem_installed?, #inject_into_file_if_new

Instance Method Details

#add_doorkeeper_gemObject



43
44
45
46
47
# File 'lib/generators/boring/devise/doorkeeper/install/install_generator.rb', line 43

def add_doorkeeper_gem
  say "Adding doorkeeper gem", :green
  
  check_and_install_gem "doorkeeper"
end


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/generators/boring/devise/doorkeeper/install/install_generator.rb', line 58

def add_doorkeeper_related_association_to_model
  model_name = options[:model_name].underscore
  say "Adding doorkeeper related associations to the model file app/models/#{model_name}.rb",
      :green
  model_content = <<~RUBY
    has_many :access_grants,
             class_name: 'Doorkeeper::AccessGrant',
             foreign_key: :resource_owner_id,
             dependent: :delete_all # or :destroy if you need callbacks

    has_many :access_tokens,
             class_name: 'Doorkeeper::AccessToken',
             foreign_key: :resource_owner_id,
             dependent: :delete_all # or :destroy if you need callbacks
  RUBY

  inject_into_file "app/models/#{model_name}.rb",
                   optimize_indentation(model_content, 2),
                   after: "ApplicationRecord\n"
end

#run_doorkeeper_generatorsObject



49
50
51
52
53
54
55
56
# File 'lib/generators/boring/devise/doorkeeper/install/install_generator.rb', line 49

def run_doorkeeper_generators
  say "Running doorkeeper generators", :green

  Bundler.with_unbundled_env do
    run "bundle exec rails generate doorkeeper:install"
    run "bundle exec rails generate doorkeeper:migration"
  end
end

#show_messageObject



132
133
134
135
136
137
138
139
140
# File 'lib/generators/boring/devise/doorkeeper/install/install_generator.rb', line 132

def show_message
  return if options[:api_only] || options[:skip_applications_routes]

  model_name = options[:model_name].underscore
  admin_authenticator_content = "current_#{model_name} || warden.authenticate!(scope: :#{model_name})"

  say "\nWe've implemented `#{admin_authenticator_content}` in the admin_authenticator block of config/initializers/doorkeeper.rb to manage access to application routes. Please adjust it as necessary to suit your requirements.",
      :yellow
end

#update_doorkeeper_initializerObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/generators/boring/devise/doorkeeper/install/install_generator.rb', line 79

def update_doorkeeper_initializer
  say "Updating doorkeeper initializer", :green

  configure_resource_owner_authenticator if options[:grant_flows].include?("authorization_code")
  configure_admin_authenticator unless options[:api_only] || options[:skip_applications_routes]
  configure_resource_owner_from_credentials if options[:grant_flows].include?("password")

  gsub_file "config/initializers/doorkeeper.rb",
            /# grant_flows %w\[authorization_code client_credentials\]/,
            "grant_flows %w[#{options[:grant_flows].uniq.join(' ')}]"

  if options[:api_only]
    gsub_file "config/initializers/doorkeeper.rb",
              /# api_only/,
              "api_only"
  end

  if options[:skip_applications_routes]
    doorkeeper_routes_content = <<~RUBY
      use_doorkeeper do
        skip_controllers :applications, :authorized_applications
      end
    RUBY
    
    gsub_file "config/routes.rb",
              /.*use_doorkeeper/,
              optimize_indentation(doorkeeper_routes_content, 2)
  end

  if options[:use_refresh_token]
    uncomment_lines "config/initializers/doorkeeper.rb",
                    /use_refresh_token/
  end
end

#update_doorkeeper_migrationObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/generators/boring/devise/doorkeeper/install/install_generator.rb', line 114

def update_doorkeeper_migration
  say "Updating doorkeeper migration", :green
  model_name = options[:model_name].underscore

  uncomment_lines Dir["db/migrate/*_create_doorkeeper_tables.rb"].first,
                  /add_foreign_key :oauth/

  gsub_file Dir["db/migrate/*_create_doorkeeper_tables.rb"].first,
            /<model>/,
            ":#{model_name.pluralize}"

  return unless (%w[password client_credentials] & options[:grant_flows]).any?

  gsub_file Dir["db/migrate/*_create_doorkeeper_tables.rb"].first,
            /t.text    :redirect_uri, null: false/,
            "t.text    :redirect_uri"
end

#verify_presence_of_devise_gemObject



25
26
27
28
29
30
31
32
# File 'lib/generators/boring/devise/doorkeeper/install/install_generator.rb', line 25

def verify_presence_of_devise_gem
  return if gem_installed?("devise")

  say "We couldn't find devise gem. Please configure devise gem and rerun the generator. Consider running `rails generate boring:devise:install` to set up Devise.",
      :red

  abort
end

#verify_presence_of_devise_modelObject



34
35
36
37
38
39
40
41
# File 'lib/generators/boring/devise/doorkeeper/install/install_generator.rb', line 34

def verify_presence_of_devise_model
  return if File.exist?("app/models/#{options[:model_name].underscore}.rb")

  say "We couldn't find the #{options[:model_name]} model. Maybe there is a typo? Please provide the correct model name and run the generator again.",
      :red

  abort
end