When using markup like HTML, there is a distinction between structure and layout. HTML provides the structure and CSS provides the layout. You should be able to separate out the two. The HTML should still make sense and be organized even without the CSS. This idea is very well demonstrated with CSS Zen Garden.
Sunday, February 27, 2011
Why Haml?
When things are modular, simplicity and understanding have a chance to precipitate. Working with something that deals with a multitude of responsibilities is mentally tiring.
When using markup like HTML, there is a distinction between structure and layout. HTML provides the structure and CSS provides the layout. You should be able to separate out the two. The HTML should still make sense and be organized even without the CSS. This idea is very well demonstrated with CSS Zen Garden.
When using markup like HTML, there is a distinction between structure and layout. HTML provides the structure and CSS provides the layout. You should be able to separate out the two. The HTML should still make sense and be organized even without the CSS. This idea is very well demonstrated with CSS Zen Garden.
Thursday, February 10, 2011
Rails 3.0: Cucumber + Mocking
I know. Cucumber is used for integration tests. Integration tests and mocking shouldn't even be in the same sentence. However, there are times when practicality trumps theory.
For instance, emails. How do you ensure that emails have been sent/received?
Another example is MailChimp. When a user signs up, you add them to a MailChimp list. Now, whenever you need to create a user, your app hits MailChimp. Since you are suppose to start with a clean slate, you now need to clean out the MailChimp list. This gets tedious and time consuming.
To speed things up, you may want to mock certain tasks like these.
Cucumber supports RSpec mocking (aka doubles) pretty easily.
Open up features/support/local_env.rb (or your custom config file for Cucumber)
Done! Enjoy!
(Just as a note, I would test MailChimp without mocking when testing signup. I would mock it out in all other cases. Unfortunately, I haven't figured out how to test emails for real. If you have any suggestions, please leave a comment. I would love to hear it!)
For instance, emails. How do you ensure that emails have been sent/received?
Another example is MailChimp. When a user signs up, you add them to a MailChimp list. Now, whenever you need to create a user, your app hits MailChimp. Since you are suppose to start with a clean slate, you now need to clean out the MailChimp list. This gets tedious and time consuming.
To speed things up, you may want to mock certain tasks like these.
Cucumber supports RSpec mocking (aka doubles) pretty easily.
Open up features/support/local_env.rb (or your custom config file for Cucumber)
require 'cucumber/rspec/doubles'
Done! Enjoy!
(Just as a note, I would test MailChimp without mocking when testing signup. I would mock it out in all other cases. Unfortunately, I haven't figured out how to test emails for real. If you have any suggestions, please leave a comment. I would love to hear it!)
Friday, February 4, 2011
Rails 3.0: Devise + Mongoid + RSpec
Devise removes the mundane job of writing all the code required to handle a user account. Plus, it supports Mongoid.
The following is a non-exhaustive list of tasks Devise takes care of for you:
All this is available out of the box. Along with the goodies that are pre-packaged, Devise is fully customizable.
Install
You will need to setup your mailer if it isn't already setup. Devise depends on the default host being set, so ensure the following is set
Don't worry! Devise is smart and has already detected that you are using Mongoid at this point and not ActiveRecord. It has also created config/initializers/devise.rb. This is where you can configure Devise. Take some time to familiarize yourself with all the options and uncomment any that you would like to use. I'll wait.
Now you have access to things like
This generates a user model at app/models/user.rb with some Devise specific lines of code. Include the modules that you want and remove the ones you don't.
Believe it or not, Devise is now properly hooked up and ready!
To see all the routes
You will notice a bunch of user routes. Fire up your server and navigate to localhost:3000/users/sign_up. Voila!
As a quick reference, here are some useful routes:
https://github.com/plataformatec/devise
The following is a non-exhaustive list of tasks Devise takes care of for you:
- signup
- forgot/reset password (including emails)
- confirmation emails
- password encryption
- login
- logout
- editing account info
- locking accounts
- session expiration
All this is available out of the box. Along with the goodies that are pre-packaged, Devise is fully customizable.
Installation
Open Gemfilegem 'devise'
Install
bundle install rails generate devise:install
You will need to setup your mailer if it isn't already setup. Devise depends on the default host being set, so ensure the following is set
# ... other mailer settings
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
Don't worry! Devise is smart and has already detected that you are using Mongoid at this point and not ActiveRecord. It has also created config/initializers/devise.rb. This is where you can configure Devise. Take some time to familiarize yourself with all the options and uncomment any that you would like to use. I'll wait.
Test Helpers
If you are using RSpec, create spec/support/devise.rb withRSpec.configure do |config| config.include Devise::TestHelpers, :type => :controller end
Now you have access to things like
sign_in @user sign_out @user
Create a User model
It is time to create a User!rails generate devise user
This generates a user model at app/models/user.rb with some Devise specific lines of code. Include the modules that you want and remove the ones you don't.
Believe it or not, Devise is now properly hooked up and ready!
Routes
Devise automagically generates all the user routes required for handling the user account. If you look in config/routes.rb, you'll noticedevise_for :users
To see all the routes
rake routes
You will notice a bunch of user routes. Fire up your server and navigate to localhost:3000/users/sign_up. Voila!
As a quick reference, here are some useful routes:
- signup: new_user_registration_url
- edit account: edit_user_registration_url
- login: new_user_session_url
- logout: destroy_new_user_session_url
References
I encourage you to take a look at the Devise home page as there is a wealth of information there.https://github.com/plataformatec/devise
Wednesday, January 12, 2011
Ubuntu: Installing Fonts
It's very easy to install fonts in Ubuntu. Fonts usually come in a zip file. There may be several different versions of the same font with different extensions. I've currently tried this with ttf and otf fonts.
System-wide fonts are located at /usr/share/fonts. However, I prefer to keep things to myself, so I will install it in my home directory.
Create a .fonts directory in your home if it doesn't exist already.
Extract your font to the .fonts directory. You only need to extract one of the font versions. For example, if the zip file contains both myfont.ttf and myfont.otf, you only need to pick one of them, like myfont.ttf.
Refresh the font cache.
If you had an application open, now would be a great time to restart it. You should now see the new font.
System-wide fonts are located at /usr/share/fonts. However, I prefer to keep things to myself, so I will install it in my home directory.
Create a .fonts directory in your home if it doesn't exist already.
mkdir ~/.fonts
Extract your font to the .fonts directory. You only need to extract one of the font versions. For example, if the zip file contains both myfont.ttf and myfont.otf, you only need to pick one of them, like myfont.ttf.
Refresh the font cache.
sudo fc-cache -f -v
If you had an application open, now would be a great time to restart it. You should now see the new font.
Sunday, December 12, 2010
Mongoid: autocreate_indexes
Indexes help make your database reads faster, but has the downside of making your database writes slower. Either way, you will come across the need for indexes at some point.
Mongoid has a flag called autocreate_indexes. This flag tells MongoDB to create the indexes every time a class is loaded. This is set to false by default. However, when you add an index to a model, your application will complain since there is no index in MongoDB yet.
To get around this, you can set autocreate_indexes to true. You only want to do this for development and test since this can be slow. For staging and production, you will need to log into MongoDB and create an index manually before pushing your code.
Mongoid has a flag called autocreate_indexes. This flag tells MongoDB to create the indexes every time a class is loaded. This is set to false by default. However, when you add an index to a model, your application will complain since there is no index in MongoDB yet.
To get around this, you can set autocreate_indexes to true. You only want to do this for development and test since this can be slow. For staging and production, you will need to log into MongoDB and create an index manually before pushing your code.
Thursday, November 18, 2010
RSpec 2: #raise_error
I was writing a spec and attempted to use #raise_error. To my surprise, it wasn't working. Here's what I did and how I solved it.
Code to be tested:
Spec:
To my surprise, the error wasn't caught and it failed with a raised error. There was no syntax error and the way I wrote it felt very natural, but it wasn't behaving the way I thought it should. However, upon further investigation, I realized the usage of #raise_error is on a Proc or lambda.
The spec should have been:
And if you really want it to read well:
Code to be tested:
class TestClass def run; raise "Error"; end end
Spec:
it 'should raise' do TestClass.new.run.should raise_error end
To my surprise, the error wasn't caught and it failed with a raised error. There was no syntax error and the way I wrote it felt very natural, but it wasn't behaving the way I thought it should. However, upon further investigation, I realized the usage of #raise_error is on a Proc or lambda.
The spec should have been:
it 'should raise' do
Proc.new { TestCase.new.run }.should raise_error
lambda { TestCase.new.run }.should raise_error
end
And if you really want it to read well:
it 'should raise' do
expect { TestCase.new.run }.to raise_error
end
Thursday, November 11, 2010
Fixing Ruby on Ubuntu
Before using RVM, I was using the default Ruby installation on Ubuntu. Then, I needed different versions of Ruby and started messing around with installing other Rubies. Apparently, I messed up my default Ruby install without knowing it. Then, I moved off to RVM and never looked back.
Recently, I needed my system Ruby again. This is when I found out it was completely messed. It kept giving me this error when I tried to use RubyGems.
This was supposed to be impossible since thread is in the core library of Ruby. Then, I tried other things like requiring pp and date. Both gave me the same error.
I attempted to reinstall Ruby.
Unfortunately, this didn't help at all. I tried this several times to no avail. I then tried installing RubyGems through the source, but I couldn't even run setup.rb since it requires core libraries.
I decided to look through Synaptic Package Manager for anything that matched Ruby. That's when I realized I hadn't uninstalled all of Ruby. I had missed the Ruby libraries! Duh! I had messed up my Ruby libraries and wasn't reinstalling it.
This fixed my require problems and all was right again.
Recently, I needed my system Ruby again. This is when I found out it was completely messed. It kept giving me this error when I tried to use RubyGems.
'require': no such file to load -- thread (LoadError)
This was supposed to be impossible since thread is in the core library of Ruby. Then, I tried other things like requiring pp and date. Both gave me the same error.
I attempted to reinstall Ruby.
sudo apt-get purge ruby ruby-dev ruby1.8 ruby1.8-dev rubygems rubygems1.8 sudo apt-get install ruby ruby-dev rubygems
Unfortunately, this didn't help at all. I tried this several times to no avail. I then tried installing RubyGems through the source, but I couldn't even run setup.rb since it requires core libraries.
I decided to look through Synaptic Package Manager for anything that matched Ruby. That's when I realized I hadn't uninstalled all of Ruby. I had missed the Ruby libraries! Duh! I had messed up my Ruby libraries and wasn't reinstalling it.
sudo apt-get purge libruby libruby1.8 sudo apt-get install libruby
This fixed my require problems and all was right again.
Subscribe to:
Posts (Atom)