boring_generators 0.2.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/FUNDING.yml +12 -0
- data/.github/workflows/ci.yml +17 -0
- data/CHANGELOG.md +35 -1
- data/Gemfile +6 -0
- data/Gemfile.lock +148 -0
- data/README.md +45 -3
- data/boring_generators.gemspec +2 -0
- data/exe/boring +5 -0
- data/lib/boring_generators.rb +1 -0
- data/lib/boring_generators/cli.rb +26 -0
- data/lib/boring_generators/version.rb +1 -1
- data/lib/generators/boring/active_storage/aws/install/install_generator.rb +48 -0
- data/lib/generators/boring/active_storage/azure/install/install_generator.rb +48 -0
- data/lib/generators/boring/active_storage/google/install/install_generator.rb +48 -0
- data/lib/generators/boring/audit/install/install_generator.rb +22 -0
- data/lib/generators/boring/bootstrap/install/install_generator.rb +2 -2
- data/lib/generators/boring/bullet/install/install_generator.rb +31 -0
- data/lib/generators/boring/bullet/install/templates/bullet.rb +8 -0
- data/lib/generators/boring/ci/circleci/install/install_generator.rb +31 -0
- data/lib/generators/boring/ci/circleci/install/templates/config.psql.yml.tt +48 -0
- data/lib/generators/boring/ci/circleci/install/templates/config.sql.yml.tt +48 -0
- data/lib/generators/boring/ci/circleci/install/templates/database.yml.ci.tt +14 -0
- data/lib/generators/boring/ci/github_action/install/install_generator.rb +45 -0
- data/lib/generators/boring/ci/github_action/install/templates/ci.yml.tt +49 -0
- data/lib/generators/boring/ci/travisci/install/install_generator.rb +23 -0
- data/lib/generators/boring/ci/travisci/install/templates/.travis.yml.tt +35 -0
- data/lib/generators/boring/devise/install/install_generator.rb +75 -0
- data/lib/generators/boring/favicon/build/build_generator.rb +120 -0
- data/lib/generators/boring/favicon/build/templates/favicon.html.erb.tt +19 -0
- data/lib/generators/boring/graphql/install/install_generator.rb +51 -0
- data/lib/generators/boring/graphql/install/templates/base_resolver.rb +2 -0
- data/lib/generators/boring/graphql/install/templates/hello_world_resolver.rb +11 -0
- data/lib/generators/boring/oauth/base_generator.rb +63 -0
- data/lib/generators/boring/oauth/facebook/install/install_generator.rb +42 -0
- data/lib/generators/boring/oauth/facebook/install/templates/README +20 -0
- data/lib/generators/boring/oauth/facebook/install/templates/omniauth.rb +3 -0
- data/lib/generators/boring/oauth/facebook/install/templates/omniauth_callbacks_controller.rb +21 -0
- data/lib/generators/boring/oauth/github/install/install_generator.rb +42 -0
- data/lib/generators/boring/oauth/github/install/templates/README +20 -0
- data/lib/generators/boring/oauth/github/install/templates/omniauth_callbacks_controller.rb +21 -0
- data/lib/generators/boring/pry/install/install_generator.rb +29 -0
- data/lib/generators/boring/pry/install/templates/pryrc +24 -0
- data/lib/generators/boring/pundit/install/install_generator.rb +85 -0
- data/lib/generators/boring/rubocop/install/install_generator.rb +37 -0
- data/lib/generators/boring/rubocop/install/templates/.rubocop.yml.tt +262 -0
- data/lib/generators/boring/simple_form/install/install_generator.rb +44 -0
- data/lib/generators/boring/tailwind/install/install_generator.rb +13 -12
- data/lib/generators/boring/tailwind/install/templates/README +13 -0
- metadata +2524 -5
- data/.travis.yml +0 -6
@@ -0,0 +1,120 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Boring
|
4
|
+
module Favicon
|
5
|
+
class BuildGenerator < Rails::Generators::Base
|
6
|
+
desc "Build favicons for all platforms"
|
7
|
+
source_root File.expand_path("templates", __dir__)
|
8
|
+
|
9
|
+
ICO_SIZES = %w(16 32)
|
10
|
+
APPLE_SIZES = %w(57 60 72 76 114 120 129 144 152)
|
11
|
+
APPLE_PRECOMPOSED_SIZES = %w(120 129 152)
|
12
|
+
SMALL_BREAK = 50
|
13
|
+
LARGE_BREAK = 150
|
14
|
+
MS_TILE_SIZES = %w(144)
|
15
|
+
FAVICON_DIR = "favicons"
|
16
|
+
FILE_FAVICO_DIR = "app/assets/images/#{FAVICON_DIR}"
|
17
|
+
DEFAULT_PRIMARY_COLOR = "#082472"
|
18
|
+
DEFAULT_FAVICON_LETTER = "B" # B for boring_generators :)
|
19
|
+
DEFAULT_IMAGE_PATH = "#{FILE_FAVICO_DIR}/template.png"
|
20
|
+
|
21
|
+
class_option :primary_color, type: :string, aliases: "-color",
|
22
|
+
desc: "Tell us the primary color to build favicon. Default to primary: #{DEFAULT_PRIMARY_COLOR}"
|
23
|
+
class_option :favico_letter, type: :string, aliases: "-letter",
|
24
|
+
desc: "Tell us the favicon letter to build favicon. Default to primary: #{DEFAULT_FAVICON_LETTER}"
|
25
|
+
class_option :font_file_path, type: :string, aliases: "-fp",
|
26
|
+
desc: "Tell us the font to be used generate favicon."
|
27
|
+
class_option :application_name, type: :string, aliases: "-app_name",
|
28
|
+
desc: "Tell us the application name to be used in favicon partial."
|
29
|
+
|
30
|
+
def build_favicon
|
31
|
+
@application_name = options[:application_name]
|
32
|
+
@primary_color = options[:primary_color]
|
33
|
+
|
34
|
+
unless /Version/m =~ (`convert -version`)
|
35
|
+
say <<~WARNING, :red
|
36
|
+
ERROR: You do not have ImageMagick installed.
|
37
|
+
WARNING
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def create_favicon_directory
|
42
|
+
unless File.exists?(FILE_FAVICO_DIR)
|
43
|
+
Dir.mkdir FILE_FAVICO_DIR
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def build_favicon_for_existing_template_image
|
48
|
+
return unless File.exist?(DEFAULT_IMAGE_PATH)
|
49
|
+
|
50
|
+
say "Converting template image to favicons..."
|
51
|
+
template_name = "#{FILE_FAVICO_DIR}/template.png"
|
52
|
+
template_small_name = "#{FILE_FAVICO_DIR}/template-small.png"
|
53
|
+
template_large_name = "#{FILE_FAVICO_DIR}/template-large.png"
|
54
|
+
template_small_name = template_name unless File.file?(template_small_name)
|
55
|
+
template_large_name = template_name unless File.file?(template_large_name)
|
56
|
+
ICO_SIZES.each do |size|
|
57
|
+
ico_template = template_name
|
58
|
+
ico_template = template_small_name if size.to_i <= SMALL_BREAK
|
59
|
+
ico_template = template_small_name if size.to_i >= LARGE_BREAK
|
60
|
+
(`convert #{ico_template} -resize #{size}x#{size} #{FILE_FAVICO_DIR}/favicon-#{size}x#{size}.ico`)
|
61
|
+
end
|
62
|
+
APPLE_SIZES.each do |size|
|
63
|
+
ico_template = template_name
|
64
|
+
ico_template = template_small_name if size.to_i <= SMALL_BREAK
|
65
|
+
ico_template = template_small_name if size.to_i >= LARGE_BREAK
|
66
|
+
(`convert #{ico_template} -resize #{size}x#{size} #{FILE_FAVICO_DIR}/apple-touch-icon-#{size}x#{size}.png`)
|
67
|
+
end
|
68
|
+
APPLE_PRECOMPOSED_SIZES.each do |size|
|
69
|
+
ico_template = template_name
|
70
|
+
ico_template = template_small_name if size.to_i <= SMALL_BREAK
|
71
|
+
ico_template = template_small_name if size.to_i >= LARGE_BREAK
|
72
|
+
(`convert #{ico_template} -resize #{size}x#{size} #{FILE_FAVICO_DIR}/apple-touch-icon-#{size}x#{size}-precomposed.png`)
|
73
|
+
end
|
74
|
+
MS_TILE_SIZES.each do |size|
|
75
|
+
ico_template = template_name
|
76
|
+
ico_template = template_small_name if size.to_i <= SMALL_BREAK
|
77
|
+
ico_template = template_small_name if size.to_i >= LARGE_BREAK
|
78
|
+
(`convert #{ico_template} -resize #{size}x#{size} #{FILE_FAVICO_DIR}/mstile-#{size}x#{size}.png`)
|
79
|
+
end
|
80
|
+
ico_template = template_name
|
81
|
+
ico_template = template_small_name if 152 <= SMALL_BREAK
|
82
|
+
ico_template = template_small_name if 152 >= LARGE_BREAK
|
83
|
+
(`convert #{ico_template} -resize 152x152 #{FILE_FAVICO_DIR}/apple-touch-icon.png`)
|
84
|
+
(`convert #{ico_template} -resize 152x152 #{FILE_FAVICO_DIR}/apple-touch-icon-precomposed.png`)
|
85
|
+
end
|
86
|
+
|
87
|
+
def generate_new_favicon_using_favico_letter
|
88
|
+
return if File.exist?(DEFAULT_IMAGE_PATH)
|
89
|
+
say "Creating favicons from application...", :green
|
90
|
+
|
91
|
+
favico_letter = options[:favico_letter] || @application_name.try(:first) || DEFAULT_FAVICON_LETTER
|
92
|
+
font_file_path = options[:font_file_path]
|
93
|
+
favicon_color = options[:primary_color] || DEFAULT_PRIMARY_COLOR
|
94
|
+
|
95
|
+
ICO_SIZES.each do |size|
|
96
|
+
(`convert -background "#{favicon_color}" -fill white -size #{size}x#{size} -gravity center #{font_file_path ? "-font #{font_file_path}" : ""} label:#{favico_letter} #{FILE_FAVICO_DIR}/favicon-#{size}x#{size}.ico`)
|
97
|
+
end
|
98
|
+
APPLE_SIZES.each do |size|
|
99
|
+
(`convert -background "#{favicon_color}" -fill white -size #{size}x#{size} -gravity center #{font_file_path ? "-font #{font_file_path}" : ""} label:#{favico_letter} #{FILE_FAVICO_DIR}/apple-touch-icon-#{size}x#{size}.png`)
|
100
|
+
end
|
101
|
+
APPLE_PRECOMPOSED_SIZES.each do |size|
|
102
|
+
(`convert -background "#{favicon_color}" -fill white -size #{size}x#{size} -gravity center #{font_file_path ? "-font #{font_file_path}" : ""} label:#{favico_letter} #{FILE_FAVICO_DIR}/apple-touch-icon-#{size}x#{size}-precomposed.png`)
|
103
|
+
end
|
104
|
+
MS_TILE_SIZES.each do |size|
|
105
|
+
(`convert -background "#{favicon_color}" -fill white -size #{size}x#{size} -gravity center #{font_file_path ? "-font #{font_file_path}" : ""} label:#{favico_letter} #{FILE_FAVICO_DIR}/mstile-#{size}x#{size}.png`)
|
106
|
+
end
|
107
|
+
(`convert -background "#{favicon_color}" -fill white -size 152x152 -gravity center #{font_file_path ? "-font #{font_file_path}" : ""} label:#{favico_letter} #{FILE_FAVICO_DIR}/apple-touch-icon.png`)
|
108
|
+
(`convert -background "#{favicon_color}" -fill white -size 152x152 -gravity center #{font_file_path ? "-font #{font_file_path}" : ""} label:#{favico_letter} #{FILE_FAVICO_DIR}/apple-touch-icon-precomposed.png`)
|
109
|
+
end
|
110
|
+
|
111
|
+
def add_favicon_partial
|
112
|
+
say "Copying favicon layout partial", :green
|
113
|
+
template("favicon.html.erb", "app/views/layouts/shared/_favicon.html.erb")
|
114
|
+
insert_into_file "app/views/layouts/application.html.erb", <<~RUBY, after: /head.*\n/
|
115
|
+
\t\t<%= render 'layouts/shared/favicon' %>
|
116
|
+
RUBY
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
<%%= favicon_link_tag "favicons/favicon-16x16.ico", rel: 'shortcut icon', type: 'image/x-icon' %>
|
2
|
+
<%%= favicon_link_tag "favicons/favicon-32x32.ico", rel: 'shortcut icon', type: 'image/x-icon' %>
|
3
|
+
<%%= favicon_link_tag "favicons/apple-touch-icon-57x57.png", rel: 'apple-touch-icon', type: 'image/png', sizes: "57x57" %>
|
4
|
+
<%%= favicon_link_tag "favicons/apple-touch-icon-60x60.png", rel: 'apple-touch-icon', type: 'image/png', sizes: "60x60" %>
|
5
|
+
<%%= favicon_link_tag "favicons/apple-touch-icon-72x72.png", rel: 'apple-touch-icon', type: 'image/png', sizes: "72x72" %>
|
6
|
+
<%%= favicon_link_tag "favicons/apple-touch-icon-76x76.png", rel: 'apple-touch-icon', type: 'image/png', sizes: "76x76" %>
|
7
|
+
<%%= favicon_link_tag "favicons/apple-touch-icon-114x114.png", rel: 'apple-touch-icon', type: 'image/png', sizes: "114x114" %>
|
8
|
+
<%%= favicon_link_tag "favicons/apple-touch-icon-120x120.png", rel: 'apple-touch-icon', type: 'image/png', sizes: "120x120" %>
|
9
|
+
<%%= favicon_link_tag "favicons/apple-touch-icon-129x129.png", rel: 'apple-touch-icon', type: 'image/png', sizes: "129x129" %>
|
10
|
+
<%%= favicon_link_tag "favicons/apple-touch-icon-144x144.png", rel: 'apple-touch-icon', type: 'image/png', sizes: "144x144" %>
|
11
|
+
<%%= favicon_link_tag "favicons/apple-touch-icon-152x152.png", rel: 'apple-touch-icon', type: 'image/png', sizes: "152x152" %>
|
12
|
+
<%%= favicon_link_tag "favicons/apple-touch-icon.png", rel: 'apple-touch-icon', type: 'image/png' %>
|
13
|
+
<%%= favicon_link_tag "favicons/apple-touch-icon-120x120-precomposed.png", rel: 'apple-touch-icon-precomposed', type: 'image/png', sizes: "120x120" %>
|
14
|
+
<%%= favicon_link_tag "favicons/apple-touch-icon-129x129-precomposed.png", rel: 'apple-touch-icon-precomposed', type: 'image/png', sizes: "129x129" %>
|
15
|
+
<%%= favicon_link_tag "favicons/apple-touch-icon-152x152-precomposed.png", rel: 'apple-touch-icon-precomposed', type: 'image/png', sizes: "152x152" %>
|
16
|
+
<%%= favicon_link_tag "favicons/apple-touch-icon-precomposed.png", rel: 'apple-touch-icon-precomposed', type: 'image/png' %>
|
17
|
+
<meta name="msapplication-TileImage" content="<%%= image_path("favicons/mstile-144x144.png") %>" />
|
18
|
+
<meta name="msapplication-TileColor" content="<%= @primary_color %>" />
|
19
|
+
<meta name="application-name" content="<%= @application_name %>">
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Boring
|
4
|
+
module Graphql
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
6
|
+
desc "Adds GraphQL to the application"
|
7
|
+
source_root File.expand_path("templates", __dir__)
|
8
|
+
|
9
|
+
OPTIONS = %w(relay batch playground no-graphiql schema)
|
10
|
+
|
11
|
+
class_option :skip_resolver_setup, type: :boolean, aliases: "-s",
|
12
|
+
desc: "Skips adding GraphQL resolver setup to the application"
|
13
|
+
|
14
|
+
def add_graphql_gem
|
15
|
+
say "Adding graphql gem", :green
|
16
|
+
graphql_gem = <<~RUBY
|
17
|
+
\n
|
18
|
+
# for building APIs
|
19
|
+
gem 'graphql', '~> 1.11'
|
20
|
+
RUBY
|
21
|
+
append_to_file "Gemfile", graphql_gem
|
22
|
+
run "bundle install"
|
23
|
+
end
|
24
|
+
|
25
|
+
def run_graphql_generator
|
26
|
+
say "Running GraphQL default generator", :green
|
27
|
+
run "bundle exec rails generate graphql:install"
|
28
|
+
run "bundle install"
|
29
|
+
|
30
|
+
graphiql_precompile_assets = <<~RUBY
|
31
|
+
\n
|
32
|
+
if Rails.env.development?
|
33
|
+
Rails.application.config.assets.precompile += %w[graphiql/rails/application.js graphiql/rails/application.css]
|
34
|
+
end
|
35
|
+
RUBY
|
36
|
+
append_to_file "config/initializers/assets.rb", graphiql_precompile_assets
|
37
|
+
end
|
38
|
+
|
39
|
+
def adds_graphql_resolver
|
40
|
+
return if options[:skip_resolver_setup]
|
41
|
+
template("base_resolver.rb", "app/graphql/resolvers/base_resolver.rb")
|
42
|
+
template("hello_world_resolver.rb", "app/graphql/resolvers/hello_world_resolver.rb")
|
43
|
+
|
44
|
+
insert_into_file "app/graphql/types/query_type.rb", <<~RUBY, after: /class QueryType < Types::BaseObject\n/
|
45
|
+
\t\t# TODO: remove me
|
46
|
+
\t\tfield :hello, resolver: Resolvers::HelloWorldResolver
|
47
|
+
RUBY
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler'
|
4
|
+
|
5
|
+
module Boring
|
6
|
+
module Oauth
|
7
|
+
module BaseGenerator
|
8
|
+
def add_provider_and_uuid_user_details
|
9
|
+
say "Adding migration to add provider and uuid columns to users", :green
|
10
|
+
Bundler.with_unbundled_env do
|
11
|
+
run "DISABLE_SPRING=1 bundle exec rails generate migration AddOmniauthToUsers provider:string uid:string"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def configure_devise_omniauth
|
16
|
+
say "Adding omniauth devise configuration", :green
|
17
|
+
if File.exist?("config/initializers/devise.rb")
|
18
|
+
insert_into_file "config/initializers/devise.rb", <<~RUBY, after: /Devise.setup do \|config\|/
|
19
|
+
\n
|
20
|
+
\tconfig.omniauth :#{@oauth_name}, "APP_ID", "APP_SECRET"
|
21
|
+
RUBY
|
22
|
+
else
|
23
|
+
raise MissingDeviseConfigurationError, <<~ERROR
|
24
|
+
Looks like the devise installation is incomplete. Could not find devise.rb in config/initializers.
|
25
|
+
ERROR
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def add_omniauth_callback_routes
|
30
|
+
devise_route = '# devise_for :users, controllers: { omniauth_callbacks: "users/omniauth_callbacks" }'.dup
|
31
|
+
route devise_route
|
32
|
+
end
|
33
|
+
|
34
|
+
def add_omniauth_callback_controller
|
35
|
+
say "Copying omniauth_callbacks_controller.rb", :green
|
36
|
+
template("omniauth_callbacks_controller.rb", "app/controllers/users/omniauth_callbacks_controller.rb")
|
37
|
+
end
|
38
|
+
|
39
|
+
def configure_and_add_devise_setting_in_user_model
|
40
|
+
say "Configuring #{@oauth_name.to_s} omniauth for user model", :green
|
41
|
+
insert_into_file "app/models/user.rb", <<~RUBY, after: /class User < ApplicationRecord/
|
42
|
+
\n\tdevise :omniauthable, omniauth_providers: %i[#{@oauth_name}]
|
43
|
+
|
44
|
+
\tdef self.from_omniauth(auth)
|
45
|
+
\twhere(provider: auth.provider, uid: auth.uid).first_or_create do |user|
|
46
|
+
\tuser.email = auth.info.email
|
47
|
+
\tuser.password = Devise.friendly_token[0, 20]
|
48
|
+
\tuser.name = auth.info.name # assuming the user model has a name
|
49
|
+
\t# user.image = auth.info.image # assuming the user model has an image
|
50
|
+
\t# If you are using confirmable and the provider(s) you use validate emails,
|
51
|
+
\t# uncomment the line below to skip the confirmation emails.
|
52
|
+
\t# user.skip_confirmation!
|
53
|
+
\tend
|
54
|
+
\tend
|
55
|
+
RUBY
|
56
|
+
end
|
57
|
+
|
58
|
+
def show_readme
|
59
|
+
readme "README"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler'
|
4
|
+
require 'generators/boring/oauth/base_generator'
|
5
|
+
|
6
|
+
module Boring
|
7
|
+
module Oauth
|
8
|
+
module Facebook
|
9
|
+
class InstallGenerator < Rails::Generators::Base
|
10
|
+
include Boring::Oauth::BaseGenerator
|
11
|
+
|
12
|
+
class MissingDeviseConfigurationError < StandardError; end
|
13
|
+
|
14
|
+
desc "Adds facebook OmniAuth to the application"
|
15
|
+
source_root File.expand_path("templates", __dir__)
|
16
|
+
|
17
|
+
def add_facebook_omniauth_gem
|
18
|
+
say "Adding Facebook OmniAuth gem", :green
|
19
|
+
facebook_omniauth_gem = <<~RUBY
|
20
|
+
\n
|
21
|
+
# for omniauth facebook
|
22
|
+
gem 'omniauth-facebook', '~> 8.0'
|
23
|
+
RUBY
|
24
|
+
append_to_file "Gemfile", facebook_omniauth_gem
|
25
|
+
Bundler.with_unbundled_env do
|
26
|
+
run "bundle install"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def invoke_common_generator_methods
|
31
|
+
@oauth_name = :facebook
|
32
|
+
add_provider_and_uuid_user_details
|
33
|
+
configure_devise_omniauth
|
34
|
+
add_omniauth_callback_routes
|
35
|
+
add_omniauth_callback_controller
|
36
|
+
configure_and_add_devise_setting_in_user_model
|
37
|
+
show_readme
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
===============================================================================
|
2
|
+
|
3
|
+
Some setup you must do manually if you haven't yet:
|
4
|
+
|
5
|
+
1. Ensure you have overridden or uncommented the routes for generated omniauth callback controllers in your routes.rb.
|
6
|
+
For example:
|
7
|
+
|
8
|
+
Rails.application.routes.draw do
|
9
|
+
devise_for :users, controllers: {
|
10
|
+
omniauth_callbacks: "users/omniauth_callbacks"
|
11
|
+
}
|
12
|
+
end
|
13
|
+
|
14
|
+
2. Update the devise facebook omniauth APP_ID and APP_SECRET in "config/initializers/devise.rb"
|
15
|
+
after registering the Rails application on: https://developers.facebook.com/apps/
|
16
|
+
For example:
|
17
|
+
|
18
|
+
config.omniauth :facebook, "APP_ID", "APP_SECRET"
|
19
|
+
|
20
|
+
===============================================================================
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
|
2
|
+
# See https://github.com/omniauth/omniauth/wiki/FAQ#rails-session-is-clobbered-after-callback-on-developer-strategy
|
3
|
+
skip_before_action :verify_authenticity_token, only: :facebook
|
4
|
+
|
5
|
+
def facebook
|
6
|
+
# You need to implement the method below in your model (e.g. app/models/user.rb)
|
7
|
+
@user = User.from_omniauth(request.env["omniauth.auth"])
|
8
|
+
|
9
|
+
if @user.persisted?
|
10
|
+
sign_in_and_redirect @user, event: :authentication #this will throw if @user is not activated
|
11
|
+
set_flash_message(:notice, :success, kind: "Facebook") if is_navigational_format?
|
12
|
+
else
|
13
|
+
session["devise.facebook_data"] = request.env["omniauth.auth"].except(:extra) # Removing extra as it can overflow some session stores
|
14
|
+
redirect_to new_user_registration_url
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def failure
|
19
|
+
redirect_to root_path
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'bundler'
|
4
|
+
require 'generators/boring/oauth/base_generator'
|
5
|
+
|
6
|
+
module Boring
|
7
|
+
module Oauth
|
8
|
+
module Github
|
9
|
+
class InstallGenerator < Rails::Generators::Base
|
10
|
+
include Boring::Oauth::BaseGenerator
|
11
|
+
|
12
|
+
class MissingDeviseConfigurationError < StandardError; end
|
13
|
+
|
14
|
+
desc "Adds GitHub OmniAuth to the application"
|
15
|
+
source_root File.expand_path("templates", __dir__)
|
16
|
+
|
17
|
+
def add_github_omniauth_gem
|
18
|
+
say "Adding GitHub OmniAuth gem", :green
|
19
|
+
github_omniauth_gem = <<~RUBY
|
20
|
+
\n
|
21
|
+
# for omniauth github
|
22
|
+
gem 'omniauth-github', '~> 1.4.0'
|
23
|
+
RUBY
|
24
|
+
append_to_file "Gemfile", github_omniauth_gem
|
25
|
+
Bundler.with_unbundled_env do
|
26
|
+
run "bundle install"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def invoke_common_generator_methods
|
31
|
+
@oauth_name = :github
|
32
|
+
add_provider_and_uuid_user_details
|
33
|
+
configure_devise_omniauth
|
34
|
+
add_omniauth_callback_routes
|
35
|
+
add_omniauth_callback_controller
|
36
|
+
configure_and_add_devise_setting_in_user_model
|
37
|
+
show_readme
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
===============================================================================
|
2
|
+
|
3
|
+
Some setup you must do manually if you haven't yet:
|
4
|
+
|
5
|
+
1. Ensure you have overridden or uncommented the routes for generated omniauth callback controllers in your routes.rb.
|
6
|
+
For example:
|
7
|
+
|
8
|
+
Rails.application.routes.draw do
|
9
|
+
devise_for :users, controllers: {
|
10
|
+
omniauth_callbacks: "users/omniauth_callbacks"
|
11
|
+
}
|
12
|
+
end
|
13
|
+
|
14
|
+
2. Update the devise github omniauth APP_ID and APP_SECRET in "config/initializers/devise.rb"
|
15
|
+
after registering the Rails application on: https://github.com/settings/applications/new
|
16
|
+
For example:
|
17
|
+
|
18
|
+
config.omniauth :github, "APP_ID", "APP_SECRET"
|
19
|
+
|
20
|
+
===============================================================================
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
|
2
|
+
# See https://github.com/omniauth/omniauth/wiki/FAQ#rails-session-is-clobbered-after-callback-on-developer-strategy
|
3
|
+
skip_before_action :verify_authenticity_token, only: :github
|
4
|
+
|
5
|
+
def github
|
6
|
+
# You need to implement the method below in your model (e.g. app/models/user.rb)
|
7
|
+
@user = User.from_omniauth(request.env["omniauth.auth"])
|
8
|
+
|
9
|
+
if @user.persisted?
|
10
|
+
sign_in_and_redirect @user, event: :authentication #this will throw if @user is not activated
|
11
|
+
set_flash_message(:notice, :success, kind: "GitHub") if is_navigational_format?
|
12
|
+
else
|
13
|
+
session["devise.github_data"] = request.env["omniauth.auth"].except(:extra) # Removing extra as it can overflow some session stores
|
14
|
+
redirect_to new_user_registration_url
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def failure
|
19
|
+
redirect_to root_path
|
20
|
+
end
|
21
|
+
end
|