1. The Rails side!
Edwin Vlieg - BlueTools
Twente.rb - november 2008
2. MoneyBird
• Simpele facturatie voor kleine bedrijven
• Facturen aanmaken, versturen en beheren
• Simpel contactbeheer
• Binnenkort periodieke facturen
• Innovatieve Ajax interface voor factuur
bewerken
Edwin Vlieg — MoneyBird 2008 2
3. MoneyBird
• April 2008: eerste idee
• Juni 2008: prototypes van applicatie gereed
(HTML mockups met JavaScript
functionaliteit)
• September 2008: test versie gereed
• Oktober 2008: live!
Edwin Vlieg — MoneyBird 2008 3
11. Testing: Cucumber
Feature: invoice
An user should be able to manage his invoices
Scenario: Create a new invoice
Given I'm logged in as edwin at bluetools
When I follow quot;New invoicequot;
And I fill in quot;Name or Companynamequot; with quot;Test companyquot;
And I fill in quot;Addressquot; with quot;Hengelosestraat 534quot;
And I fill in quot;invoice_zipcodequot; with quot;7500AGquot;
And I fill in quot;invoice_cityquot; with quot;Enschedequot;
And I fill in quot;invoice_new_detail_attributes__descriptionquot; with quot;Test descriptionquot;
And I fill in quot;invoice_new_detail_attributes__pricequot; with quot;123quot;
And I press quot;Savequot;
Then an invoice with invoice id quot;2008-0001quot; should exist
And I should see quot;2008-0001quot;
And I should see quot;Test companyquot;
And I should see quot;Test descriptionquot;
Scenario: Create a new invoice with no invoice details
Given I'm logged in as edwin at bluetools
When I follow quot;New invoicequot;
And I fill in quot;Name or Companynamequot; with quot;Test companyquot;
And I fill in quot;Addressquot; with quot;Hengelosestraat 534quot;
And I fill in quot;invoice_zipcodequot; with quot;7500AGquot;
And I fill in quot;invoice_cityquot; with quot;Enschedequot;
And I press quot;Savequot;
Then no invoice with name quot;Test companyquot; should exist
And I should see quot;Details should contain at least one rowquot;
Edwin Vlieg — MoneyBird 2008 11
12. Testing: Cucumber
• Only ~30 steps defined!
• Example resource definition and assertion:
Given /^I've created (a|an) ([A-z].*) with ([A-z ].* quot;.*quot;( and )?)+$/ do |_, resource, fields, _| #'
fields = fields.split(quot; and quot;).collect { |f| r = f.strip.match(/^([A-z ].*) quot;(.*)quot;$/); [r[1].tr(quot; quot;,
quot;_quot;).underscore, r[2]] }.flatten
Factory(resource.tr(quot; quot;, quot;_quot;).to_sym, Hash[*fields])
end
# Then a company with name quot;foobarquot; should exist
Then /^(a|an|no) ([A-z].*) with ([A-z ].*) quot;(.*)quot; should exist$/ do |condition, resource, field, value|
if resource.to_class.respond_to?(:exists?)
condition = condition == 'no' ? false : true
resource.to_class.exists?({ field.tr(quot; quot;, quot;_quot;).underscore => value }).should == condition
else
raise quot;Resource #{resource.classify} isn't a modelquot;
end
end
Edwin Vlieg — MoneyBird 2008 12
13. PDF generation
• PDF writer:
• API.... :-(
• PNG file errors (‘PNG uses more than
8 bits’, ‘Invalid filter algorithm 61’)
Edwin Vlieg — MoneyBird 2008 13
14. PDF generation
• Prawn
• API... :-)
• Better PNG support
• Only the Euro sign doesn’t show up
Edwin Vlieg — MoneyBird 2008 14
16. Concerned_with
• Problem: Invoice model has 450 LOC
• Other models have on average 200 LOC
• Make the model less fat with
concerned_with
Edwin Vlieg — MoneyBird 2008 16
17. Concerned_with
# In config/initializers
class << ActiveRecord::Base
def concerned_with(*concerns)
concerns.each do |concern|
require_dependency quot;#{name.underscore}/#{concern}quot;
end
end
end
# In models/invoice.rb
class Invoice < ActiveRecord::Base
concerned_with :validations, :states, :policies, :scopes
...
end
# In models/invoice/validations.rb
class Invoice
validates_presence_of ...
end
Edwin Vlieg — MoneyBird 2008 17
18. Invoice edit interface
• Difference between new and edit action:
• ‘edit’ is performed on an persistant
instance
• ‘new’ does not exist after the request has
finished
Edwin Vlieg — MoneyBird 2008 18
19. Invoice edit interface
• Solution: always load the complete form on
the background
• Switch between display and form with JS
• Just plain old submitting the complete form
for creating a new record
Edwin Vlieg — MoneyBird 2008 19