Class: Boring::Pundit::InstallGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- Boring::Pundit::InstallGenerator
- Defined in:
- lib/generators/boring/pundit/install/install_generator.rb
Instance Method Summary collapse
- #add_pundit_gem ⇒ Object
- #after_run ⇒ Object
- #ensure_policies ⇒ Object
- #inject_pundit_to_controller ⇒ Object
- #rescue_from_not_authorized ⇒ Object
- #run_pundit_generator ⇒ Object
Instance Method Details
#add_pundit_gem ⇒ Object
15 16 17 18 19 20 |
# File 'lib/generators/boring/pundit/install/install_generator.rb', line 15 def add_pundit_gem say "Adding Pundit gem", :green Bundler.with_unbundled_env do run "bundle add pundit" end end |
#after_run ⇒ Object
70 71 72 73 74 75 76 |
# File 'lib/generators/boring/pundit/install/install_generator.rb', line 70 def after_run unless [:skip_rescue] say "\nPlease check the `application_controller.rb` file and fix any potential issues" end say "\nDon't forget, that you can generate policies with \nrails g pundit:policy Model\n" end |
#ensure_policies ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/generators/boring/pundit/install/install_generator.rb', line 36 def ensure_policies return if [:skip_ensuring_policies] say "Force ensuring policies", :green inject_into_file 'app/controllers/application_controller.rb', after: "include Pundit\n" do " after_action :verify_authorized\n" end end |
#inject_pundit_to_controller ⇒ Object
29 30 31 32 33 34 |
# File 'lib/generators/boring/pundit/install/install_generator.rb', line 29 def inject_pundit_to_controller say "Adding Pundit module into ApplicationController", :green inject_into_file 'app/controllers/application_controller.rb', after: "class ApplicationController < ActionController::Base\n" do " include Pundit\n" end end |
#rescue_from_not_authorized ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/generators/boring/pundit/install/install_generator.rb', line 45 def return if [:skip_rescue] say "Adding rescue from Pundit::NotAuthorizedError", :green after = if File.read('app/controllers/application_controller.rb') =~ (/:verify_authorized/) "after_action :verify_authorized\n" else "include Pundit\n" end inject_into_file 'app/controllers/application_controller.rb', after: after do <<~RUBY \trescue_from Pundit::NotAuthorizedError, with: :user_not_authorized \tprivate \tdef user_not_authorized \t flash[:alert] = "You are not authorized to perform this action." \t redirect_to(request.referrer || root_path) \tend RUBY end end |
#run_pundit_generator ⇒ Object
22 23 24 25 26 27 |
# File 'lib/generators/boring/pundit/install/install_generator.rb', line 22 def run_pundit_generator return if [:skip_generator] say "Running Pundit Generator", :green run "DISABLE_SPRING=1 rails generate pundit:install" end |