How to Patch the Console?

46 views
Skip to first unread message

hartmut bischoff

unread,
Feb 6, 2017, 4:41:49 AM2/6/17
to Ruby on Rails: Talk
Hi everbody,

I am working on a Rails Project using OrientDB as Database ( https://github.com/topofocus/active-orient )
This gem initializes any available database-class and assigns it to model-classes.

For me its convenient to  recognize, which database-classes are found, when starting the console.

Thus I patched the source in railties/rails/lib/commands/console.rb 

Obviously, I am not happy with this dirty approach. 

How is this done properly? 


in detail:
I want to execute some ruby-code after any initialization of the core and before the first command can be entered.
This code prints out some status information.

Any help is appreciated

greetings from Stuttgart
hartmut 

Marco Antonio Almeida

unread,
Feb 6, 2017, 9:28:14 AM2/6/17
to Ruby on Rails: Talk
Hi Hartmut,

My suggestion would be to add your code in the initialisers folder, something like config/initializers/console.rb and this will run after Rails has initialised its environment.

However this will run every time you run a rails or rake command, so to avoid that you could add something like this to your console initialiser: 

if Rails.const_defined?("Console") 
   # your code here
end

And this will run only if you are on a Rails console.

When I tried the code myself I had some problems with the binaries generated by spring, so you might need to update them if it doesn't work. rake rails:update:bin or rails app:update:bin if you're on Rails 5.

Hope it works.

Kind regards,
/ Marco

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0ea5a569-6d70-490e-b1f1-b2bca5150065%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Frederick Cheung

unread,
Feb 6, 2017, 9:53:45 AM2/6/17
to Ruby on Rails: Talk
In application.rb you can put

console do
  #only executed when booting console
end

We use this to inject some helper methods into console sessions

Fred
 

hartmut bischoff

unread,
Feb 7, 2017, 1:58:02 AM2/7/17
to Ruby on Rails: Talk
This works perfectly if inserted in the application-block. 

Thanks alot.

hartmut bischoff

unread,
Feb 7, 2017, 4:26:41 AM2/7/17
to Ruby on Rails: Talk
I added your Input to the documentation in  https://github.com/topofocus/active-orient/blob/master/rails.md
Reply all
Reply to author
Forward
0 new messages