0

I'd like to view Devise::RegistrationsController (out of curiosity, as I'll inherit from it as shown here)

I tried the obvious rails c then puts Devise::RegistrationsController and a few variations (e.g. .to_s) but no luck..

Is it available somewhere for viewing, and can I print it to the rails console to view it?

stevec
  • 41,291
  • 27
  • 223
  • 311
  • It's open source -- look right at the code...https://github.com/heartcombo/devise/blob/master/test/rails_app/app/controllers/custom/registrations_controller.rb – dbugger Aug 19 '20 at 17:33
  • You can view it in the `devise` gem repository. e.g. https://github.com/heartcombo/devise/blob/master/app/controllers/devise/registrations_controller.rb – Salil Aug 19 '20 at 17:35
  • @dbugger random question. Is the code somehow searchable (and editable) inside a rails app or is the only place to view it on github? – stevec Aug 19 '20 at 17:38
  • 1
    Not to my knowledge. Nor do I think you'd want to edit it if you could, it is very customizable and ruby lets you monkey patch what is not easy to customize. Take a look at the devise repo for customizing controllers, views, etc. We have about five variations of devise, all done just by customizing various bits. – dbugger Aug 19 '20 at 17:43
  • There are some possibilities it seems for viewing the code. YMMV https://stackoverflow.com/questions/3393096/how-can-i-get-source-code-of-a-method-dynamically-and-also-which-file-is-this-me – dbugger Aug 19 '20 at 17:45
  • @dbugger thanks very much! I’ll read up on devise customisation – stevec Aug 19 '20 at 17:46
  • 2
    you can always open it up in an editor too using `bundle open devise` – Rockwell Rice Aug 19 '20 at 18:03

1 Answers1

2

Pry which is a replacement console for IRB has built in source browsing.

Install the pry-rails gem and start the console with rails c and it will start up Pry instead of IRB.

You can then use show-source to view the source code of the gem right from the console:

max@pop-os ~/p/sandbox> rails c
Running via Spring preloader in process 29286
Loading development environment (Rails 6.0.2.1)
[1] pry(main)> show-source Devise::RegistrationsController

From: /home/linuxbrew/.linuxbrew/lib/ruby/gems/2.7.0/gems/devise-4.7.2/app/controllers/devise/registrations_controller.rb @ line 3:
Class name: Devise::RegistrationsController
Number of monkeypatches: 4. Use the `-a` option to display all available monkeypatches
Number of lines: 166

class Devise::RegistrationsController < DeviseController
  prepend_before_action :require_no_authentication, only: [:new, :create, :cancel]
  prepend_before_action :authenticate_scope!, only: [:edit, :update, :destroy]
  prepend_before_action :set_minimum_password_length, only: [:new, :edit]

  # GET /resource/sign_up
  def new
    build_resource
    yield resource if block_given?
    respond_with resource
  end

  # POST /resource
  def create
    build_resource(sign_up_params)

    resource.save
    yield resource if block_given?
    if resource.persisted?
      if resource.active_for_authentication?
        set_flash_message! :notice, :signed_up
        sign_up(resource_name, resource)
        respond_with resource, location: after_sign_up_path_for(resource)
      else
        set_flash_message! :notice, :"signed_up_but_#{resource.inactive_message}"
        expire_data_after_sign_in!
        respond_with resource, location: after_inactive_sign_up_path_for(resource)
      end
    else
      clean_up_passwords resource
      set_minimum_password_length
      respond_with resource
    end
  end
:

If you don't want to depend on Pry and stick with IRB you can cobble something together with Method#source_location and IO.readlines. But IMHO it seems like a waste of time

max
  • 96,212
  • 14
  • 104
  • 165