I always forget to truncate my development logs, and when I need to debug or dig into the logs, they are dead slow to work with.
Use this simple gist to truncate your logs automagically. It will only touch your development.log, as I do note like when initializers pull production or staging logs into the mix, as that might be a security risk when you just needed to test a part of your app on your production environment.
Showing posts with label rails. Show all posts
Showing posts with label rails. Show all posts
Monday, March 10, 2014
Monday, October 14, 2013
Get the dot notation of a embedded Mongoid element
I needed to get the dot notation for embedded documents to perform queries using the given dot notation.
I could not find a "standard" way of doing just that, so here are some helper methods I use in a class. (Part of soon-to-come BoxCMS)
This Gist uses Mongoid metadata and reflect_on_all_associations, so it might break for newer versions of mongoid at some point.
Using dot notation queries in mongoid, makes it easy to dig for even the deepest "weird" embedded structures you might have.
Example of query:
» Box::Page.where("rows.boxes.box_items.is_template_object" => false).first
Dot notations in mongodb are a powerful tool, but use it wisely unless you are indexing "more than you should".
Basic usage: Call dot_notation_for_element(embedded_element) to get the dot notation all the way up to the parent document, excluding the parent documents name/embedded name.
I could not find a "standard" way of doing just that, so here are some helper methods I use in a class. (Part of soon-to-come BoxCMS)
This Gist uses Mongoid metadata and reflect_on_all_associations, so it might break for newer versions of mongoid at some point.
Using dot notation queries in mongoid, makes it easy to dig for even the deepest "weird" embedded structures you might have.
Example of query:
» Box::Page.where("rows.boxes.box_items.is_template_object" => false).first
Dot notations in mongodb are a powerful tool, but use it wisely unless you are indexing "more than you should".
Basic usage: Call dot_notation_for_element(embedded_element) to get the dot notation all the way up to the parent document, excluding the parent documents name/embedded name.
Thursday, August 8, 2013
wicked_pdf and stylesheets
There are several articles showing how to use wicked_pdf on a Ruby on Rails application, so I am not going to exlain that here. Setup is quite simple, and the gem is just awesome.
There are however a few things to notice:
After writing this blog post, I found this repository:
https://github.com/jwo/railsdotpdf
It is a fully working example of generating pdf with linking to the assets instead of inline css. It is a fairly basic example, but it shows how to setup wicked_pdf with linked stylesheets.
There are however a few things to notice:
Outputs on osx and ubuntu can be inconsistent
If you are using wkhtmltopdf-binary-11 on osx in development and the same binary in production on a ubuntu system the output of the pdf is most likely to be quite different. Solution: Develop on your local computer, fine-tune using an Ubuntu machine in Virtualbox.Stylesheets don't 'behave'
You might get mixed results when using linked stylesheets. The simple solution is to inline your pdf or print stylesheet. Put this in your pdf layout file:<style type="text/css">
<%= Rails.application.assets.find_asset('pdf_print.css').to_s.html_safe %>
</style>
This way your stylesheet will be inline, and all linking issues with wkhtmltopdf is resolved. If you have full paths for your assets, that will sometimes work as well. (e.g. if they are hosted on a cdn), but to my experience, inline CSS is more consistant when using wkhtmltopdf.After writing this blog post, I found this repository:
https://github.com/jwo/railsdotpdf
It is a fully working example of generating pdf with linking to the assets instead of inline css. It is a fairly basic example, but it shows how to setup wicked_pdf with linked stylesheets.
Thursday, February 28, 2013
Rake task for rspec:features
I'm using Capybara, rspec and poltergeist to test features/requests in Ruby on Rails
I wanted a simple raketask to check the features without typing:
I wanted a simple raketask to check the features without typing:
$ rake spec SPEC=spec/features/**/*_spec.rb
I wanted to type:
$ rake spec:features
Creating the new raketask is quite simple:
desc "Run the code examples in spec/features" task "spec:features" do Rails.env = ENV['RAILS_ENV'] = 'test' ENV['SPEC'] = "spec/features/**/*_spec.rb" Rake::Task['spec'].invoke endNote that the spec is set as an ENV variable and not passed as an argument to the invoke command.
Thursday, October 25, 2012
Rails 3.2 and virtual attributes
I was fighting with virtual attributes for a few hours, and I'll share what I did wrong:
Here's a model with one virtual attribute as well as a text field
I would expect the following to work:
To summarize and remember for the future:
Here's a model with one virtual attribute as well as a text field
class User < ActiveRecord::Base attr_accessible :sanity_level attr_accessor :sanity_level validates_presence_of :sanity_level, :on => :update end
I would expect the following to work:
user = User.create(:name => "Leif") user.save(:sanity_level => 5) user.valid? # => false # What? user.errors.inspect # => Errors show: Sanity Level cannot be blank... but it is 5? user.update_attributes(:sanity_level => 5) user.valid? # => trueI expected both to work, but it doesn't by design. After looking at activerecord and mysql2 source, it is easly spotted that save calls create_or_update without forwarding the hash, so of course it will fail!
To summarize and remember for the future:
- Use update_attributes when updating with a hash for the attributes to update
- Use save after setting attributes manually
- Use create when creating new records
- Use build when building new records with a hash and saving later
Subscribe to:
Posts (Atom)