Making CRUD less "Cruddy", one step at a time
One of the great “new” features of Rails (as of 2.3) is accepts_nested_attributes_for, allowing you to build cross-model CRUD forms without “cruddying” your controller. There are some great examples out there about how to do this, but I’d like to walk thorough a particular use case — managing the “join” records in a has_many :through relationship.
Consider the following database schema:
class Villain < ActiveRecord::Base
has_many :gifts
has_many :super_powers, :through => :gifts
end
class Gift < ActiveRecord::Base
belongs_to :villain
belongs_to :super_power
validates_uniqueness_of :super_power, :scope => :villain_id
end
class SuperPower < ActiveRecord::Base
has_many :gifts
has_many :villains, :through => :gifts
endIn our dataset, there are a relatively small number of super powers which we wish to present as a list of checkboxes on the villain management form. Checking/unchecking the boxes will manage the gift records for that villain, effectively managing the list of super powers available to the baddy.
To get started, we need to add accepts_nested_attributes_for :gifts to the Villain class — piece of cake. To complete the implementation, we need to change the params hash that our form generates. Let’s review the cases that we need to support and the associated params hash format needed to implement the correct functionality.
The first case is a super power record that is not currently associated with the villain. Here, the UI should display an unchecked checkbox. If we check it and submit the form, a gift record should be created linking the villain with the super power, making this bad guy that much badder. Here is an example of the params hash we should be sending to accomplish this:
{
'villain' => {
'name' => 'Lex Luthor',
...
'gifts_attributes' => {
1 => { 'super_power_id' => 5 },
2 => { 'super_power_id' => 7 },
...
}
}
}The alternate case is a super power this villain already possesses. In this instance, the UI should display a checked checkbox, and if we uncheck it, the existing gift record should be deleted, diminishing the villain’s capacity for evil. And our params hash needs to look like:
{
'villain' => {
'name' => 'Two-Face',
...
'gifts_attributes' => {
1 => { 'id' => 101, '_delete' => true },
...
}
}
}Note that the keys for the gifts_attributes hash are arbitrary; we can use any scheme to generate unique keys for the hash.
So how can we craft a form that sends the params hash that Rails wants to see? Here’s my implementation:
<%- SuperPower.all.each_with_index do |super_power, index| -%>
<label>
<%- if gift = @villain.gifts.find_by_super_power_id(super_power.id) -%>
<%= hidden_field_tag "villain[gifts_attributes][#{ index }][id]", gift.id %>
<%= check_box_tag "villain[gifts_attributes][#{ index }][_delete]", false, true %>
<%= hidden_field_tag "villain[gifts_attributes][#{ index }][_delete]", true %>
<%- else -%>
<%= check_box_tag "villain[gifts_attributes][#{ index }][super_power_id]", super_power.id %>
<%- end -%>
<%= super_power.name %>
</label><br />
<%- end -%>If the gift is detected, the villain has the super power, and we handle our second case from above, using the checkbox / hidden field hack Rails employs in the check_box helper method to make sure a value is sent whether or not the checkbox is checked. The else block handles the other case, setting up our params hash to create the gift if the checkbox is checked.
This works, but we probably don’t want to copy and paste that code everywhere we use this pattern. How can we reuse this in a DRY fashion? Here’s a helper method that encapsulates the logic:
def has_join_relationship(model, join_collection_name, related_item, collection_index, options={})
returning "" do |output|
relationship_name = options[:relationship_name] || related_item.class.table_name.singularize + "_id"
tag_prefix = "#{ model.class.class_name.underscore }[#{ join_collection_name }_attributes][#{ collection_index }]"
if join_item = model.send(join_collection_name).find(:first, :conditions => { relationship_name => related_item.id })
output << hidden_field_tag("#{ tag_prefix }[id]", related_item.id)
output << check_box_tag("#{ tag_prefix }[_delete]", false, true)
output << hidden_field_tag("#{ tag_prefix }[_delete]", true)
else
output << check_box_tag("#{ tag_prefix }[#{ relationship_name }]", related_item.id, false)
end
end
endDrop that in a helper, and then your form code becomes:
<%- SuperPower.all.each_with_index do |super_power, index| -%>
<label>
<%= has_join_relationship(@villain, :gifts, super_power, index) %>
<%= super_power.name %>
</label><br />
<%- end -%>Much nicer … although I’m not sold on the name has_join_relationship. Any suggestions?
Shutting down Usage Report!
Oops, I did it again. Abandoning an idea after I invested quite some time on it even before it was released. This morning I had a good talk with my wife on where I should go with this project. It’s always hard to call it quit after spending quite some time on something but I changed once too many direction in the last two month that I don’t have a product to put on the market yet and to reach a point where I could sell something would take me a few more months (working on Mondays only) to get there…So I think I will call it a day on this project. Now that doesn’t make me feel great as I start having a track record of starting things and not finishing them…at least for project I want to commercialize. I seem to have an easier time to create stuff that I give away for free for some reasons. Having three kids and only limited time I was hoping to turn my hobby of trying to create products into some revenue generating activity so I can easier justify the time I spend on it.
Now there are several lessons I’m learning the hard way here. First I shouldn’t get stuck in having so much fun trying new stuff or solving technical issues without knowing if there is even a market for it. I’m for sure not a great business person but I should really focus on finding something people would be interested before spending too much time in one direction. Based on the fact that I work only Monday’s on this, something that can be created in three weeks full time takes month and month when working part time. I was pretty sure the UsageReport tool was a short term project but of course it ended up being more complex and more time consuming than expected. I was trying to see if there is any side product I could create from this last effort but to create something useful from my code base would still be more work than I can/I’m willing to spare at this time.
In the same vein, I’m not too troubled of spending all this time on these projects as I see it more like practice which makes me a better programmer in the long run and often helps out tremendously on my consulting gigs, and it’s fun to do. And the fact that I’m not troubled by this is an issue in it self as I could spend a fraction of the time learning the same things while working on these projects if the goal was just to learn.
The main issue is still that I never went life with any of these projects. MySpyder.net was awesome, but we never opened it up to the public or even a private beta. SiteExtractor.com was a cool concept but it was too rough to release, before I changed focus and worked WatchThatSite.com. All along the way I used ec2, s3, and Co so I’ve create a tool to visualize the pricing breakdown…and thought I could make a product from it. So over the last few years, just when I had to release something I jumped to the next thing.
Now I want to write about this publicly as it helps me think about why I ended up in the same place again, and I want to find out a way to do it differently next time….Yea, I know some people never learn ;-). So let’s see where I will go from here.
I had several consulting opportunities over the last few month I didn’t follow as I currently only have 20% of my week still available and I really thought I could nail the Usage Report project over a 6 month period and didn’t want to take on any extra work. Well I still worked on the Vault (http://vault.ncaa.com) with Cameron, and that was really fun! I’m right at 5 month with no end in view.
My wife recommends taking on new gigs as she doesn’t believe that a product can be created working part time…At least by me, and I think I’m proving her point pretty well. But I’m not ready to give up yet on creating a product. So what can I do avoid repeating history? I think I loose quickly faith in the ideas I have, or rather I’m getting quickly excited about new ideas without even proving that something works or releasing the project. So instead of tackling 15 days project (that are really 60 days) projects, I should start with some mini projects where I can create something within 3 to 5 days, then release it. At least this will force me to focus really hard on one idea and implement it, get real user feedback and go from there. If I could pull this of, I could maybe create several smaller apps and see which one takes off. Hey let me dream of that at least :-).
Gatelys had the same approach with their eCommerce store. They had a main store where selling many different type of products and based on best sellers where opening custom store for that type of item. In the same way, instead of guessing what type of application could work, I could experiment with smaller apps and see which gains more tractions.
So what are the new ideas…Well, first I need to see if I really cannot create a version of the Usage Report within 3 to 5 days based on the code base I now have. That would be a great first app to add to http://appsden.com. Another idea would be a pro version of the WebSnapshot tool http://myspyder.net/tools/websnapshot/ I wrote several years ago and which was downloaded over 5000 times and received good reviews. I have a few good ideas on that front. Another tool I would love to write is something that provides the functionality of iAwsManager but by showing a visual representation of a data center, more like a 2.5D game…Well that sounds bigger than a few days of work. I would love to create a twitter visualization tools that shows discussion threads and retweet flows. And there are many more ideas poping every days…If only I can pick one and stick with it, I wouldn’t have to write such blog entries.
If you read so far, thank you for sticking with me during this long thought process. For me at least it was useful ;-)
Cheers,
Daniel
UsageReport Downloader for Amazon Web ServicesTM. A simple tool to download all you usage reports with one click. ec2, s3, sns, sqs and more
UsageReport Downloader for Amazon Web ServicesTM is a simple tool to download all you usage reports with one click.
Install UsageReport Downloader

The files are download in your documents folder. You can change the default folder. You selection is kept for the next time.
Click the Download XML or Download CSV button to choose which format the report should be downloaded from and off you go…

All you files are download to the select download folder (here /Users/daniel/Documents/usagereport/downloads/Current\ Billing\ Period)

When the application start it checks if you already logged in and you will see the following message.
If you need to login just enter your email and amazon password as usual for https://aws.amazon.com.

If you use the authentication tokens for signing in you will be presented this additional screen:

Et voila…Happy downloading!
Please contact me at daniel@appsden.com for any bugs, issues, questions.
Enjoy!
Daniel Wanja
Cucumber, meet Routes
I’ve been loving Rails BDD with Cucumber for the past year or so — it helps me focus on the next required step to build a feature in my application, and better focus equals better development velocity. However, one thing I found tedious in starting with Cucumber was defining route matchers in paths.rb.
The stock path_to method is implemented as a case statement, allowing you to add a case for each path you want to recognize. This works, but leaves you feeling a bit un-DRY since you’re basically duplicating information in your routes.rb file.
Here’s a quick hack to paths.rb that lets you leverage your existing routes:
change
else
raise "Can't find mapping from \"#{page_name}\" to a path.\n" +
"Now, go and add a mapping in #{__FILE__}"
endto
else
begin
page_name =~ /the (.*) page/
path_components = $1.split(/\s+/)
self.send(path_components.push('path').join('_').to_sym)
rescue Object => e
raise "Can't find mapping from \"#{page_name}\" to a path.\n" +
"Now, go and add a mapping in #{__FILE__}"
end
endIn your Cucumber steps, you can now use any named route that does not require a parameter. For example, users_path would become “the users page”, and new_product_path would become “the new product page”. As you add new resources, at least the index and new options should work out of the box — no further edits to paths.rb required!
UPDATE:
w00t!
This is now baked into cucumber-rails!
Call for help for a Amazon Web Services Usage Report visualizer tool
I’ve cross posted this call for help on the Amazon Web Service Discussion Forum.
I need your help understanding your usage reports logs for a visualization tool I’m currently developing.
I’ve been reading this forum for a while and found great information on it. The tool in question is for all Amazon services not just ec2, but I thought the ec2 forum was the most appropriate. Let me know if that’s not the case.
I’ve been working for a while on a desktop application to visualize your amazon web services usage logs, and it’s far from ready, however I should have reached out way earlier an figured out what others would like in such a tool and also I need help to make sure that I can visualize data for larger account and for accounts that use different services.
As a disclaimer, the tool will not be a free tool, but it will be very affordable but I didn’t figure the pricing out yet as I’m not exactly sure what functionality I can make work. Currently I’m having difficulties figuring out how I will make the pricing calculator work correctly after the recent announcement of the Combined AWS Data Transfer Pricing. So the first version of the tool will focus more on visualizing usage rather than calculating price.
So my call for help is to find out more information about your usage report logs. I’m looking for people that have larger accounts than my current usage. I currently have a few servers and pay a little more than $200 a month. I use ec2, sdb, sqs, rds, s3, but not cf. So I’m looking for a couple of different scenarios with people that pay $2000/month or even way larger usage would be great.
The best help would be if I can have a copy of to the monthly usage logs by hour in XML format. I would understand if you don’t want to give out that information. Please contact me if you are hesitant or need more information so I make clear what I’m trying to achieve and how I will proceed. If I don’t find anyone out there to share this information I would be able to create a tool that just extracts anonymous statistic from your logs that would help answer the different questions I have. Let me know if that would be something you are willing to share.
You can also just help by letting me know what size each of you log files is for each of the services when downloading it in XML format. And how many lines each of the files contains. That would verify some of the assumptions I’m currently making.
To access your logs you must login to your aws.amazon.com account go to the Account page and select Usage Reports from the right menu. Select one of the service from the dropdown, the list includes the following:
- Amazon CloudFront, Amazon Elastic Compute Cloud
- Amazon Simple Storage Service
- Amazon RDS Service
- Amazon SimpleDB
- Amazon Simple Queue Service
- Amazon Virtual Private Cloud
Then your are presented with the Download Usage Report form. For the Time Period field select “Last Month”. Then press the “Download report (XML)” button. This downloads the file my tool will analyze. Download it for each of the services.
My tool, tentatively named Usage Report for Amazon Web Services™, is a desktop application that doesn’t require any server side installation that with one click lets you download directly from Amazon website all the usage reports and give a summary of the usage and allows to view each service individually by providing usage charts and time series of what was used when.
You can find my not so interesting notes on the development progress of my tool at http://awsusageanalyzr.tumblr.com/. The tool will be made available via http://appsden.com. My other blog is http://onrails.org, I’m a Flex and Ruby on Rails developer in Denver.
Any help is appreciated.
Daniel Wanja
d@n-so.com