Rails Page Specific Maintenance Mode - ruby-on-rails

I have a rails 4.1 application that is in production. There have been one or two instances recently where it would have been beneficial to have one page on maintenance mode while the rest of the site is live. I would like to toggle maintenance page on foobar.com/sign_in by changing the value of an environmental variable or by running a rake task.
I'm open to either using a gem or writing my own tool to handle this. How are other people solving this problem?

You can use turnout gem for this:
https://rubygems.org/gems/turnout

Related

Revert the results of rails scaffolding in production

When a model is generated in rails - let's say to keep records for users, then we also get a route/controller/view for handling these (CRUD). Therefore visiting "root_url/users" would list all the users, "root_url/users/1" would display the first user etc.
While this is handy in a dev environment, it's not inappropriate for production (currently production for me is Heroku).
I could just remove the extra controllers, views etc. but I was wondering whether there is a standard way of approaching this issue (like a flag in a config file) so that there isn't a mismatch between dev and production.
Yes there is a standard way of approaching this issue, it's called testing, code review, and generellay just doing what is required for your application to work.
Scaffolding code is a good thing, but you just have to use it when it's needed. If you commit often enough, you can scaffold and easily reset what you've done if it does not meet your need.
It is your responsibility to not put any code in production :)

Optimize a backend rails app

I have a rails app which serves as a backend for Bootstrap based frontend. My application is fully Rest based. Which tools can I use to find the unused joins or queries from database in code? Also, any tools/gems that can help in finding the code that can be refactored?
There are gems like Bullet or Rack-mini-profiler that work if Rails app is used to render view also. But how do I use gems like those to optimize and tune my queries and code if only way to get a response is from rest client?
Depending on the configuration of Bullet, I believe by default Bullet logs all optimizable queries in (Rails.root/log/bullet.log), I guess the others have similar configurations/logs.
You could setup bullet for your test environment, since if you're shooting for a good coverage you'd most likely be testing all the AR methods in the first place.
Additional enhancers are New Relic and tools like Rubocop for some best practice.
You should check this out as well.

Can the delayed_job gem be used outside of Rails?

Is it possible to use the delayed_job gem outside of Rails? Or put another way, can it be used in a pure Ruby project?
If so, how?
I don't know why this person never posted this.
It's easy. Two steps:
http://brkrd.com/post/45269754283/delayed-job-without-rails
UPDATE Looks like the link is broken.
You will need Active Record, but not Rails. You will have to mock a Rails object, and load your database information, environment, and root into the Rails object so that DelayedJob thinks it's in the Rails environment.
Delayed Job looks heavily rails dependent. https://github.com/defunkt/resque resque is a very good gem that is not rails dependent that accomplishes the same goal. You do however have to have redis on your machine which is very lightweight. Resque also has a cool Sinatra web console to see what is going on.

How can I profile a request in Ruby on Rails?

How can I profile a controller action?
One of my views is taking quite some time to render, and I'd like to break it down. I see script/performance/profiler, but that seems to only have access to the global scope.
ruby-prof is the way to go. Here's a howto, How to profile your Rails and Ruby applications with ruby-prof.
If you use it in combination with a visualisation tool such as kcachegrind, you can easily separate the code that is your application code from the framework code.
I gave a talk on these tools at my local Ruby-users group a while back: Making your rails app kick ass with ruby-prof and kcachegrind.
The Ruby on Rails console should show you which partials are taking a while to render. If that's not enough, you could install the new relic gem (https://github.com/newrelic/rpm) and use that in developer mode which will give you an overview of what's going on.
If you're deploying on Heroku you can add the free version of rpm which will give you the same statistics on your running application.
Ruby has a profiler (http://ruby-prof.rubyforge.org/) which will show you memory usage, call times, etc., but it might be hard to disseminate your code from the framework code.
I've had some success with https://github.com/bhb/rack-perftools_profiler
It'll let you profile a single or multiple requests for any of cputime, methods, object allocations or walltime, and integrate nicely via Rack middleware.

What rails plugins are good, stable and *really* enhance your code?

Anyone have a list of rails plugins that are both stable and give you enough functionality to be worth the extra effort of supporting?
Edit:
I am mostly interested in the best, most complete list of plugins so I can use it the next I'm starting a rails app. I don't currently need a particular plugin.
You can use bort as reference
Plugins Installed
Bort comes with a few commonly used
plugins installed and already setup.
RESTful Authentication
RESTful Authentication is already
setup. The routes are setup, along
with the mailers and observers.
Forgotten password comes setup, so you
don’t have to mess around setting it
up with every project.
The AASM plugin comes pre-installed.
RESTful Authentication is also setup
to use user activation.
User Roles
Bort now comes with Role Requirement
by Tim Harper. A default admin role is
predefined along with a default admin
user. See the migrations for the admin
login details.
Open ID Authentication
Bort, as of 0.3, has Open ID
integrated with RESTful
Authentication. Rejoice!
Will Paginate
We use will_paginate in pretty much
every project we use, so Bort comes
with it pre-installed.
Rspec & Rspec-rails
You should be testing your code, so
Bort comes with Rspec and Rspec-rails
already installed so you’re ready to
roll.
Exception Notifier
You don’t want your applications to
crash and burn so Exception Notifier
is already installed to let you know
when everything goes to shit.
Asset Packager
Packages up your css/javascript so
you’re not sending 143 files down to
the user at the same time. Reduces
load times and saves you bandwidth.
p/s: agree with #eric, specifics
restful_authentication for sign in, sign out, sign up.
paperclip for file uploads.
rspec and shoulda for testing.
Could you be more specific in what you are looking for? There are so many great plugins for so many different tasks, it's hard to guess the right ones for you.
Try resource_controller. http://jamesgolick.com/2007/10/19/introducing-resource_controller-focus-on-what-makes-your-controller-special
It seriously dries up your RESTful controllers. And is the only plausible way of implementing polymorphic actions that I've come across.
Loads of other good stuff too. Give it a try.
I can imagine why you are asking that. I used to work in a project with more than 20 plugins in use. Sure, it speeded up the development early on, but later debugging became difficult. Also, updating to a new version of Rails was a lengthy process.
My advice is that don't start using a plugin before have a reasonable understanding of how it works and of the trade-offs involved. For small plugins you should probably read the source code. For larger ones, see what other people are saying about them, when the plugin was updated the last time, etc.
For scanning popular plugins, see the most popular github projects. Quite a few of them are Rails plugins.
For me, Haml is excellent. It's not for everyone but if it clicks with you you'll love it. Set aside 30 min and give it a shot. It reduces the clutter in my views by about 50%.
It's easy to install using Rails 2.1+'s gem dependencies :
# environment.rb
config.gem 'haml'
Then:
rake gems:install
Ruby Trends is a good place to check what the most popular plugins/gems/books/practices are. It's like StackOverflow (i.e., voting plugins up/down) but is more fine-grained and has the ability to search/filter.
I my rails time I used http://github.com/mbleigh/acts-as-taggable-on/tree/master with success.

Resources