disable explain in Rails 3.2 - sql

Is it possible to disable the new the explain functionality in Rails 3.2 globally via configuration? I'm using activerecord-sqlserver-adapter 3.2.1 and there appear to be some bugs with the explain (show plan) portion of the gem.

To cite from http://weblog.rubyonrails.org/2011/12/6/what-s-new-in-edge-rails-explain/
New applications get
config.active_record.auto_explain_threshold_in_seconds = 0.5
in config/environments/development.rb. Active Record monitors queries and if they take more than that threshold their query plan will be logged using warn.
[...]
By default the threshold is nil in the test and production environments, which means the feature is disabled.
so just set
config.active_record.auto_explain_threshold_in_seconds = nil

You can disable auto-explain by setting config.active_record.auto_explain_threshold_in_seconds = nil in your config/environments/development.rb

You should note that config.auto_explain_threshold_in_seconds has been completely removed in Rails 4.0 & 4.1. In 4.1 it will throw a full error.
See https://github.com/rails/rails/pull/9400/files
If you upgrade, comment that line out.

Related

How to enable a Dexterity behavior on tests?

I' writing a behavior for a Dexterity-based content type; it is working but I don't know what's the right way to enable it on tests.
I was using the following:
def _enable_background_image_behavior(self):
fti = queryUtility(IDexterityFTI, name='collective.cover.content')
behaviors = list(fti.behaviors)
behaviors.append(self.name)
fti.behaviors = tuple(behaviors)
def _disable_background_image_behavior(self):
fti = queryUtility(IDexterityFTI, name='collective.cover.content')
behaviors = list(fti.behaviors)
behaviors.remove(self.name)
fti.behaviors = tuple(behaviors)
but the behavior seems not be be disabled or enabled in certain Plone versions (it behaves differently in Plone 4.2 and Plone 4.3, pobably because of Dexterity moving from 1.x to 2.x).
the full code for the tests is in: https://github.com/collective/collective.cover/blob/background-image-behavior/src/collective/cover/tests/test_behaviors.py
the result of tests in Plone 4.2 in: https://travis-ci.org/collective/collective.cover/jobs/33327495
what should be the right way of enabling and disabling the behavior on Integration tests?
Thanks, Asko, for pointing me in the right direction: I ended up invalidating the schema cache the following way:
from plone.dexterity.schema import SchemaInvalidatedEvent
from zope.event import notify
# invalidate schema cache
notify(SchemaInvalidatedEvent('collective.cover.content'))
I believe that you are doing it correctly, but the issue is related to caching fixes between dx 1.x and 2.x. I have managed to clear dx caches in test setups with:
def testSetUp(self):
import plone.dexterity.schema
for name in dir(plone.dexterity.schema.generated):
if name.startswith("plone"):
delattr(plone.dexterity.schema.generated, name)
plone.dexterity.schema.SCHEMA_CACHE.clear()

How can I run a Controller Action from Rails Console, but setting the request.shot?

I'm working on a multi-tenant application, so pretty much everything throughout (starting with routing) expects to have a "request.host" that looks like "tenant_id.myapp.com".
I'm trying to run a controller action from the Rails console, but I haven't been able to figure this one out.
The easiest thing to do seems to be "app.get", but I can't figure out how to set the host.
I also saw an answer that mentions using "ActionController::TestProcess", which as far as I understand has been removed from Rails (or if not, I haven't found how to include it)
Any other options?
Thanks!
Daniel
I just verified it in my console(Pry)
# Send request with fake HTTP_HOST
>>app.get(app.root_path, nil, {"HTTP_HOST" => "123.myapp.com"})
>>#=>200
# Then check if it works
>>app.request.env
>>#...
>># "HTTP_HOST" : "123.myapp.com"

Constant 500 Errors on Heroku

I recently switched from Heroku's Bamboo stack to the Cedar one (Rails 3.1.4, Ruby 1.9.2, Thin gem for web server). Since then I keep getting 500 errors such as this, where it seems that the query is not acting right:
207 <13>1 2012-05-06T16:10:51+00:00 d. app web.1 - - ActiveRecord::StatementInvalid (Mysql::Error: : SELECT `foos`.* FROM `foos` WHERE `foos`.`id` = ? LIMIT 1)
It's not an error in the code though because the page eventually renders successfully (ie status 200) when I refresh the page. Sometimes it is 1 refresh, but can get up to 4 refreshes before I get a 200.
I thought it was the database because I was on ClearDB's free plan, but I upgraded to ClearDB's next plan with better I/O performance and it still happens
this never happened when I was on Bamboo
it happens on just about every page that does queries on the DB
it doesn't always happen, but I'd say it happens on at least 1 in 5 pages views
the model/query doesn't matter, the same error occurs (just indicating a different model/fields then the example above)
Do you get the same errors if you are in console heroku run console ? I've never seen this before. Try upgrading your Mysql gem, which one are you using http://api.rubyonrails.org/classes/ActiveRecord/StatementInvalid.html i think the correct one is mysql2 https://rubygems.org/gems/mysql2

Capybara-webkit tries to open example.com

I'm using capybara, capybara-webkit, capybara-screenshot together with cucumber. (Ruby 1.9.3, Rails 3.1.3) and Capybara.javascript_driver = :webkit is also set env.rb
Unfortunately running a cucumber spec with #javascript will never succeed for some reason and the error screenshots just capture example.com.
The URL which I actually try to open is generated with a rails router result for one of my models e.g. with visit products_url
So how can I avoid that it ends up querying example.com?
Any input is very welcome.
Just because the comment is messed up - here's what I found was the solution:
Capybara.run_server = true
Capybara.server_port = 7787
Before '#javascript' do
Capybara.app_host = "http://127.0.0.1:#{Capybara.server_port}"
end
Try using visit products_path instead. They do not recommend using absolute URLs in "Gotchas" section of README.
For me, there was a much sneaker "gotcha" (and I was using Capybara with Rspec). Originally in a spec I had:
visit "foos/5"
This worked fine with Rack::Test but when I wanted to switch to the webkit driver to test js interactions, I got that exception (Unable to load URL: file:///products (Capybara::Driver::Webkit::WebkitInvalidResponseError)).
What I had to do was change the path I passed to visit, like so:
visit "/foos/5"
Succes!!
Here's another potential gotcha, posted for others that might have this issue. I was testing action caching, and the key Rails generates looks like "views/www.example.com/products". This happens even if you use products_path as the url. This can lead to the need to set your server name so you can know in advance what cache key to expect.

How to avoid a rails 3 app from saving in development.log?

I want to keep my rails 3 app from saving all the development logs in the development.log because its making my file bigger.
You can either do this in your:
application.rb
Or if you only want to disable for specific environments do this in the specific env config, in your case:
config/environments/development.rb
You're probably looking for some or all of the following settings:
config.logger
config.active_record.logger
config.action_controller.logger
config.action_view.logger
config.action_mailer.logger
config.active_resource.logger
If you set any of them to nil e.g.:
config.active_record.logger = nil
It should disable logging for that specific component. You can experiment with these to disable logging for specific components or set all of them to nil to get no logging.
Switching it entirely off is a very bad idea. You can make it put less transactions in it by specifying a log level like:
config.log_level = :info
in the config/environments/development.rb file. For the various options look here:
http://guides.rubyonrails.org/configuring.html
Normally, in a rails-app, there is a configuration-file, where you can set development-, production-, staging - specific configuration-data for logging, database-binding, bootstraps,...
Maybe you check these file, if there are no attributes defined for logging, check out, if you´re logging-level is set to the appropriate value.
Link: Click
greets