boring_generators 0.3.0 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/.github/FUNDING.yml +12 -0
  3. data/.github/workflows/ci.yml +23 -0
  4. data/CHANGELOG.md +31 -0
  5. data/Gemfile +6 -0
  6. data/Gemfile.lock +150 -0
  7. data/README.md +47 -7
  8. data/Rakefile +1 -0
  9. data/boring_generators.gemspec +2 -0
  10. data/exe/boring +5 -0
  11. data/lib/boring_generators.rb +1 -0
  12. data/lib/boring_generators/cli.rb +26 -0
  13. data/lib/boring_generators/version.rb +1 -1
  14. data/lib/generators/boring/bootstrap/install/install_generator.rb +2 -2
  15. data/lib/generators/boring/ci/circleci/install/install_generator.rb +31 -0
  16. data/lib/generators/boring/ci/circleci/install/templates/config.psql.yml.tt +48 -0
  17. data/lib/generators/boring/ci/circleci/install/templates/config.sql.yml.tt +48 -0
  18. data/lib/generators/boring/ci/circleci/install/templates/database.yml.ci.tt +14 -0
  19. data/lib/generators/boring/ci/github_action/install/install_generator.rb +45 -0
  20. data/lib/generators/boring/ci/github_action/install/templates/ci.yml.tt +49 -0
  21. data/lib/generators/boring/ci/travisci/install/install_generator.rb +23 -0
  22. data/lib/generators/boring/ci/travisci/install/templates/.travis.yml.tt +35 -0
  23. data/lib/generators/boring/devise/install/install_generator.rb +75 -0
  24. data/lib/generators/boring/favicon/build/build_generator.rb +120 -0
  25. data/lib/generators/boring/favicon/build/templates/favicon.html.erb.tt +19 -0
  26. data/lib/generators/boring/graphql/install/install_generator.rb +51 -0
  27. data/lib/generators/boring/graphql/install/templates/base_resolver.rb +2 -0
  28. data/lib/generators/boring/graphql/install/templates/hello_world_resolver.rb +11 -0
  29. data/lib/generators/boring/oauth/base_generator.rb +63 -0
  30. data/lib/generators/boring/oauth/facebook/install/install_generator.rb +42 -0
  31. data/lib/generators/boring/oauth/facebook/install/templates/README +20 -0
  32. data/lib/generators/boring/oauth/facebook/install/templates/omniauth.rb +3 -0
  33. data/lib/generators/boring/oauth/facebook/install/templates/omniauth_callbacks_controller.rb +21 -0
  34. data/lib/generators/boring/oauth/github/install/install_generator.rb +42 -0
  35. data/lib/generators/boring/oauth/github/install/templates/README +20 -0
  36. data/lib/generators/boring/oauth/github/install/templates/omniauth_callbacks_controller.rb +21 -0
  37. data/lib/generators/boring/oauth/google/install/install_generator.rb +36 -0
  38. data/lib/generators/boring/oauth/google/install/templates/README +20 -0
  39. data/lib/generators/boring/oauth/google/install/templates/omniauth_callbacks_controller.rb +21 -0
  40. data/lib/generators/boring/oauth/twitter/install/install_generator.rb +36 -0
  41. data/lib/generators/boring/oauth/twitter/install/templates/README +20 -0
  42. data/lib/generators/boring/oauth/twitter/install/templates/omniauth_callbacks_controller.rb +21 -0
  43. data/lib/generators/boring/pundit/install/install_generator.rb +85 -0
  44. data/lib/generators/boring/rubocop/install/install_generator.rb +37 -0
  45. data/lib/generators/boring/rubocop/install/templates/.rubocop.yml.tt +262 -0
  46. data/lib/generators/boring/simple_form/install/install_generator.rb +44 -0
  47. data/lib/generators/boring/tailwind/install/install_generator.rb +13 -12
  48. data/lib/generators/boring/tailwind/install/templates/README +13 -0
  49. data/lib/generators/boring/twilio/install/install_generator.rb +30 -0
  50. data/lib/generators/boring/twilio/install/templates/README +11 -0
  51. data/lib/generators/boring/twilio/install/templates/twilio.rb +4 -0
  52. metadata +2525 -5
  53. data/.travis.yml +0 -6
@@ -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 twitter omniauth APP_ID and APP_SECRET in "config/initializers/devise.rb"
15
+ after registering the Rails application on: https://developer.twitter.com/en/apps
16
+ For example:
17
+
18
+ config.omniauth :twitter, "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: :twitter
4
+
5
+ def twitter
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: "Twitter") if is_navigational_format?
12
+ else
13
+ session["devise.twitter_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,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boring
4
+ module Pundit
5
+ class InstallGenerator < Rails::Generators::Base
6
+ desc "Adds pundit to the application"
7
+
8
+ class_option :skip_ensuring_policies, type: :boolean, aliases: "-s",
9
+ desc: "Skip before_action to ensure user is authorized"
10
+ class_option :skip_rescue, type: :boolean, aliases: "-sr",
11
+ desc: "Skip adding rescue for Pundit::NotAuthorizedError"
12
+ class_option :skip_generator, type: :boolean, aliases: "-sg",
13
+ desc: "Skip running Pundit install generator"
14
+
15
+ def add_pundit_gem
16
+ say "Adding Pundit gem", :green
17
+ pundit_gem = <<~RUBY
18
+ \n
19
+ # for authorization
20
+ gem 'pundit', '~> 2.1'
21
+ RUBY
22
+ append_to_file "Gemfile", pundit_gem
23
+ Bundler.with_unbundled_env do
24
+ run "bundle install"
25
+ end
26
+ end
27
+
28
+ def run_pundit_generator
29
+ return if options[:skip_generator]
30
+
31
+ say "Running Pundit Generator", :green
32
+ run "DISABLE_SPRING=1 rails generate pundit:install"
33
+ end
34
+
35
+ def inject_pundit_to_controller
36
+ say "Adding Pundit module into ApplicationController", :green
37
+ inject_into_file 'app/controllers/application_controller.rb', after: "class ApplicationController < ActionController::Base\n" do
38
+ " include Pundit\n"
39
+ end
40
+ end
41
+
42
+ def ensure_policies
43
+ return if options[:skip_ensuring_policies]
44
+
45
+ say "Force ensuring policies", :green
46
+ inject_into_file 'app/controllers/application_controller.rb', after: "include Pundit\n" do
47
+ " after_action :verify_authorized\n"
48
+ end
49
+ end
50
+
51
+ def rescue_from_not_authorized
52
+ return if options[:skip_rescue]
53
+
54
+ say "Adding rescue from Pundit::NotAuthorizedError", :green
55
+
56
+ after = if File.read('app/controllers/application_controller.rb') =~ (/:verify_authorized/)
57
+ "after_action :verify_authorized\n"
58
+ else
59
+ "include Pundit\n"
60
+ end
61
+
62
+ inject_into_file 'app/controllers/application_controller.rb', after: after do
63
+ <<~RUBY
64
+ \trescue_from Pundit::NotAuthorizedError, with: :user_not_authorized
65
+
66
+ \tprivate
67
+
68
+ \tdef user_not_authorized
69
+ \t flash[:alert] = "You are not authorized to perform this action."
70
+ \t redirect_to(request.referrer || root_path)
71
+ \tend
72
+ RUBY
73
+ end
74
+ end
75
+
76
+ def after_run
77
+ unless options[:skip_rescue]
78
+ say "\nPlease check the `application_controller.rb` file and fix any potential issues"
79
+ end
80
+
81
+ say "\nDon't forget, that you can generate policies with \nrails g pundit:policy Model\n"
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boring
4
+ module Rubocop
5
+ class InstallGenerator < Rails::Generators::Base
6
+ desc "Adds rubocop to the application"
7
+ source_root File.expand_path("templates", __dir__)
8
+
9
+ DEFAULT_RUBY_VERSION = "2.7.1"
10
+
11
+ class_option :skip_adding_rubocop_rules, type: :boolean, aliases: "-s",
12
+ desc: "Skip adding rubocop rules and add empty file."
13
+ class_option :ruby_version, type: :string, aliases: "-v",
14
+ desc: "Tell us the ruby version which you use for the application. Default to Ruby #{DEFAULT_RUBY_VERSION}"
15
+
16
+ def add_rubocop_gems
17
+ say "Adding rubocop gems", :green
18
+ bullet_gem_content = <<~RUBY
19
+ \n
20
+ \t# A Ruby static code analyzer, based on the community Ruby style guide
21
+ \tgem "rubocop", require: false
22
+ \tgem "rubocop-rails", require: false
23
+ \tgem "rubocop-performance", require: false
24
+ RUBY
25
+ insert_into_file "Gemfile", bullet_gem_content, after: /group :development do/
26
+ run "bundle install"
27
+ end
28
+
29
+ def add_rails_prefered_rubocop_rules
30
+ say "Adding rubocop style guides", :green
31
+ @skip_adding_rules = options[:skip_adding_rubocop_rules]
32
+ @target_ruby_version = options[:ruby_version] ? options[:ruby_version] : DEFAULT_RUBY_VERSION
33
+ template(".rubocop.yml", ".rubocop.yml")
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,262 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rails
4
+
5
+ <%- unless @skip_adding_rules %>
6
+ AllCops:
7
+ TargetRubyVersion: <%= @target_ruby_version %>
8
+ # RuboCop has a bunch of cops enabled by default. This setting tells RuboCop
9
+ # to ignore them, so only the ones explicitly set in this file are enabled.
10
+ DisabledByDefault: true
11
+ Exclude:
12
+ - bin/*
13
+ - db/**/*
14
+ - script/**/*
15
+ - vendor/**/*
16
+ - node_modules/**/*
17
+ - node-server/**/*
18
+ - tmp/**/*
19
+ - config/**/*
20
+ - Gemfile
21
+
22
+ # Prefer assert_not over assert !
23
+ Rails/AssertNot:
24
+ Include:
25
+ - '**/test/**/*'
26
+
27
+ # Prefer assert_not_x over refute_x
28
+ Rails/RefuteMethods:
29
+ Include:
30
+ - '**/test/**/*'
31
+
32
+ Rails/IndexBy:
33
+ Enabled: true
34
+
35
+ Rails/IndexWith:
36
+ Enabled: true
37
+
38
+ # Prefer &&/|| over and/or.
39
+ Style/AndOr:
40
+ Enabled: true
41
+
42
+ # Align `when` with `case`.
43
+ Layout/CaseIndentation:
44
+ Enabled: true
45
+
46
+ Layout/ClosingHeredocIndentation:
47
+ Enabled: true
48
+
49
+ # Align comments with method definitions.
50
+ Layout/CommentIndentation:
51
+ Enabled: true
52
+
53
+ Layout/ElseAlignment:
54
+ Enabled: true
55
+
56
+ # Align `end` with the matching keyword or starting expression except for
57
+ # assignments, where it should be aligned with the LHS.
58
+ Layout/EndAlignment:
59
+ Enabled: true
60
+ EnforcedStyleAlignWith: variable
61
+ AutoCorrect: true
62
+
63
+ Layout/EmptyLineAfterMagicComment:
64
+ Enabled: true
65
+
66
+ Layout/EmptyLinesAroundAccessModifier:
67
+ Enabled: true
68
+ EnforcedStyle: only_before
69
+
70
+ Layout/EmptyLinesAroundBlockBody:
71
+ Enabled: true
72
+
73
+ # In a regular class definition, no empty lines around the body.
74
+ Layout/EmptyLinesAroundClassBody:
75
+ Enabled: true
76
+
77
+ # In a regular method definition, no empty lines around the body.
78
+ Layout/EmptyLinesAroundMethodBody:
79
+ Enabled: true
80
+
81
+ # In a regular module definition, no empty lines around the body.
82
+ Layout/EmptyLinesAroundModuleBody:
83
+ Enabled: true
84
+
85
+ # Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
86
+ Style/HashSyntax:
87
+ Enabled: true
88
+
89
+ Layout/FirstArgumentIndentation:
90
+ Enabled: true
91
+
92
+ # Method definitions after `private` or `protected` isolated calls need one
93
+ # extra level of indentation.
94
+ Layout/IndentationConsistency:
95
+ Enabled: true
96
+ EnforcedStyle: indented_internal_methods
97
+
98
+ # Two spaces, no tabs (for indentation).
99
+ Layout/IndentationWidth:
100
+ Enabled: true
101
+
102
+ Layout/LeadingCommentSpace:
103
+ Enabled: true
104
+
105
+ Layout/SpaceAfterColon:
106
+ Enabled: true
107
+
108
+ Layout/SpaceAfterComma:
109
+ Enabled: true
110
+
111
+ Layout/SpaceAfterSemicolon:
112
+ Enabled: true
113
+
114
+ Layout/SpaceAroundEqualsInParameterDefault:
115
+ Enabled: true
116
+
117
+ Layout/SpaceAroundKeyword:
118
+ Enabled: true
119
+
120
+ Layout/SpaceBeforeComma:
121
+ Enabled: true
122
+
123
+ Layout/SpaceBeforeComment:
124
+ Enabled: true
125
+
126
+ Layout/SpaceBeforeFirstArg:
127
+ Enabled: true
128
+
129
+ Style/DefWithParentheses:
130
+ Enabled: true
131
+
132
+ # Defining a method with parameters needs parentheses.
133
+ Style/MethodDefParentheses:
134
+ Enabled: true
135
+
136
+ Style/FrozenStringLiteralComment:
137
+ Enabled: true
138
+ EnforcedStyle: always
139
+
140
+ Style/RedundantFreeze:
141
+ Enabled: true
142
+
143
+ # Use `foo {}` not `foo{}`.
144
+ Layout/SpaceBeforeBlockBraces:
145
+ Enabled: true
146
+
147
+ # Use `foo { bar }` not `foo {bar}`.
148
+ Layout/SpaceInsideBlockBraces:
149
+ Enabled: true
150
+ EnforcedStyleForEmptyBraces: space
151
+
152
+ # Use `{ a: 1 }` not `{a:1}`.
153
+ Layout/SpaceInsideHashLiteralBraces:
154
+ Enabled: true
155
+
156
+ Layout/SpaceInsideParens:
157
+ Enabled: true
158
+
159
+ # Check quotes usage according to lint rule below.
160
+ Style/StringLiterals:
161
+ Enabled: true
162
+ EnforcedStyle: double_quotes
163
+
164
+ # Detect hard tabs, no hard tabs.
165
+ Layout/IndentationStyle:
166
+ Enabled: true
167
+
168
+ # Empty lines should not have any spaces.
169
+ Layout/TrailingEmptyLines:
170
+ Enabled: true
171
+
172
+ # No trailing whitespace.
173
+ Layout/TrailingWhitespace:
174
+ Enabled: true
175
+
176
+ # Use quotes for string literals when they are enough.
177
+ Style/RedundantPercentQ:
178
+ Enabled: true
179
+
180
+ Lint/AmbiguousOperator:
181
+ Enabled: true
182
+
183
+ Lint/AmbiguousRegexpLiteral:
184
+ Enabled: true
185
+
186
+ Lint/ErbNewArguments:
187
+ Enabled: true
188
+
189
+ # Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
190
+ Lint/RequireParentheses:
191
+ Enabled: true
192
+
193
+ Lint/ShadowingOuterLocalVariable:
194
+ Enabled: true
195
+
196
+ Lint/RedundantStringCoercion:
197
+ Enabled: true
198
+
199
+ Lint/UriEscapeUnescape:
200
+ Enabled: true
201
+
202
+ Lint/UselessAssignment:
203
+ Enabled: true
204
+
205
+ Lint/DeprecatedClassMethods:
206
+ Enabled: true
207
+
208
+ Style/ParenthesesAroundCondition:
209
+ Enabled: true
210
+
211
+ Style/HashTransformKeys:
212
+ Enabled: true
213
+
214
+ Style/HashTransformValues:
215
+ Enabled: true
216
+
217
+ Style/RedundantBegin:
218
+ Enabled: true
219
+
220
+ Style/RedundantReturn:
221
+ Enabled: true
222
+ AllowMultipleReturnValues: true
223
+
224
+ Style/Semicolon:
225
+ Enabled: true
226
+ AllowAsExpressionSeparator: true
227
+
228
+ # Prefer Foo.method over Foo::method
229
+ Style/ColonMethodCall:
230
+ Enabled: true
231
+
232
+ Style/TrivialAccessors:
233
+ Enabled: true
234
+
235
+ Performance/FlatMap:
236
+ Enabled: true
237
+
238
+ Performance/RedundantMerge:
239
+ Enabled: true
240
+
241
+ Performance/StartWith:
242
+ Enabled: true
243
+
244
+ Performance/EndWith:
245
+ Enabled: true
246
+
247
+ Performance/RegexpMatch:
248
+ Enabled: true
249
+
250
+ Performance/ReverseEach:
251
+ Enabled: true
252
+
253
+ Performance/UnfreezeString:
254
+ Enabled: true
255
+
256
+ Performance/DeletePrefix:
257
+ Enabled: true
258
+
259
+ Performance/DeleteSuffix:
260
+ Enabled: true
261
+
262
+ <% end %>
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Boring
4
+ module SimpleForm
5
+ class InstallGenerator < Rails::Generators::Base
6
+ desc "Adds SimpleForm to the application"
7
+ source_root File.expand_path("templates", __dir__)
8
+
9
+ class_option :css_framework, type: :string, aliases: "-css",
10
+ desc: "Skip before_action to ensure user is authorized"
11
+ class_option :skip_generator, type: :boolean, aliases: "-sg",
12
+ desc: "Skip running Pundit install generator"
13
+
14
+ ALLOWED_CSS_FRAMEWORK = %w(bootstrap foundation)
15
+
16
+ def add_bullet_gem
17
+ say "Adding SimpleForm gem", :green
18
+ simple_form_content = <<~RUBY
19
+ \n
20
+ # for simple forms
21
+ gem 'simple_form', '~> 5.0'
22
+ RUBY
23
+ append_to_file "Gemfile", simple_form_content
24
+ run "bundle install"
25
+ end
26
+
27
+ def run_simple_form_generator
28
+ return if options[:skip_generator]
29
+
30
+ say "Running SimpleForm Generator", :green
31
+ if options[:css_framework].present? && ALLOWED_CSS_FRAMEWORK.include?(options[:css_framework])
32
+ run "rails generate simple_form:install --#{options[:css_framework]}"
33
+ elsif options[:css_framework].present?
34
+ say <<~WARNING, :red
35
+ ERROR: Invalid option css_framework: #{options[:css_framework]}. Generator allows css_framework: #{ALLOWED_CSS_FRAMEWORK.join(", ")}
36
+ WARNING
37
+ else
38
+ run "rails generate simple_form:install"
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+