I have just started as a full time Frontiersman after a long 7 years of finishing off a Computer Science degree(I hope). I've been through a week and its going really well; the people are awesome and I'm learning a lot.
Something I told myself was that once I started full time I really wanted to push myself as a developer. I figured one good way to help consolidate things that I am learning is to write a weekly blog post about things I found of particular interest in the previous week. So here are what I hope are the first of many posts to come.
Bogus Gateway in Active Merchant
The first thing I want to talk about is the BogusGateway that ActiveMerchant provides. I had done some work previously with Active Merchant but this was the first time I had come across it. Basically, it gives you a gateway which allows you to test with without having to actually hit a banks api with a test account or stub out all the calls. Generally you'd set it up something like this:
module Example
class Application < Rails::Application
...
@@CreditCardGateway = ActiveMerchant::Billing::Gateway.new
end
end
Example::Application.configure do
...
Gimme::CreditCardGateway = ActiveMerchant::Billing::ActualGateway.new(:login => 'RealMerchant', :password => 'password')
end
Example::Application.configure do
...
Gimme::CreditCardGateway = ActiveMerchant::Billing::BogusGateway.new(:login => 'TestMerchant', :password => 'password')
end
You then pass it a ActiveMerchant::Billing::CreditCard object with a number of 1 for valid or 2 for invalid (anything else throws an error). You can then make calls on the gateway like you normally would. The only small issue with this is that 1 and 2 aren't valid credit card numbers so if in your method you check that the card is valid (as you should), you will need to stub the call to valid? out.
This is something that caught me last week. I had the following associations.
class Airplane < ActiveRecord::Base
has_many :airplane_remotes
has_many :remotes, :through => :airplane_remotes
end
class AirplaneRemote < ActiveRecord::Base
belongs_to :airplane
belongs_to :remotes
end
class Remote < ActiveRecord::Base
has_many :airplane_remotes
has_many :airplanes, :through => :airplane_remotes
end
class Battery < ActiveRecord::Base
belongs_to :remotes
has_many :airplanes, :through => :remotes
end
If you call battery.remote.airplanes you will get the list of airplanes that are associated with its remote. If you call battery.airplanes you get an empty collection. In anything below Rails 3.1 you can't have a has many through through relationship.
I also had the chance to play around with ActiveAdmin a bit last week and was very impressed with it. One thing that I was really impressed with was the nice little addition to the Formtastic form builder it has. What it allows you to do is specify a has many relationship in the following way.
ActiveAdmin.register Example do
...
form do |f|
f.inputs do
f.input :name
f.has_many :programs
end
f.buttons
end
end
This will use ajax to create new program associations without having to refresh or write any custom javascript. You need to make sure that Example accepts_nested_attributes_for :programs otherwise it won't work One slight drawback is whilst it will allow you to destroy any newly created associations, it won't allow you to destroy any pre-existing ones. Even so, I found this really neat.
As I think I've said before a lot of the topics on this blog aren't ground breaking stuff but I figure it might be useful to people who are learning like I am.
Stay tuned for more updates.