0

Ruby on Rails and Forms

Posted by jason on Feb 19, 2010 in Uncategorized

Just found a cool form tool I’m going to try out for Rails:

http://github.com/justinfrench/formtastic

He built the output to be based on a presentation given by Aaron Gustafson

http://github.com/justinfrench/formtastic

I’ve been doing web development for a LONG time so it’s easy to fall into the trap of thinking I know everything there is to know about something as basic as forms.  I’ve learned some really good stuff by Aaron Gustafson’s presentation that I’m going to immediately incorporate into my applications.

 
0

Financial Consequences of Being Self Employed

Posted by jason on Feb 13, 2010 in Uncategorized

I discovered a few interesting things while preparing for my quarterly taxes. I don’t have to pay them until April 15th. Also, I don’t base it on the total taxes I will owe. I only have to pre pay 1/4 of last year’s total federal tax. So I can ignore the medicare and social security if I want to owe a ton of money at the end of the year.  I think what I’ll actually do is save the difference between what I will owe and what I have to pre pay into a savings account.  That way at the end of the year, I won’t have to scramble to pay a huge tax bill.

What I can tell so far, there are four major financial differences between being an employee and being a self employed individual. These four are health care, retirement, taxes (social security/medicare), and time.  I’m speaking from the perspective of someone who bills by the hour.

Health care is really dependent upon what your employer provides. I can get similar coverage to my old job for roughly the same as what my former employer was providing.  I wasn’t getting fantastic health coverage before though.  Some of the plans with low deductibles are quite expensive to get on your own.  I’m settling on a plan with a $10,000 family max out of pocket plan for around $350/mo for my family of five through Blue Cross.  On the plus side, it is an HSA.  So I can contribute to this account tax free money.  If I don’t use it on health care, it stays in the account.

Retirement is interesting. As an employee, you are limited to a maximum contribution of $16,500. With a SEP, the max you can contribute is 20% of business income up to $45,000. With a solo-401k, you can contribute more than 20% but still with a limit of $45,000.  I wouldn’t reach these maximum contributions in either case, but it is a nice benefit if you are earning lots of money.

A third area is taxes.  I knew that the employer paid the other half of the employment tax. I never really thought about the implications of it.  I didn’t see the actual dollar amount, and it was never reported on any of my W2s.  Employees pay this tax in the form of reduced wages so it is a hidden tax.  Employees only see 7.65% taken out for social security and medicare.  Self-Employed individuals pay 15.3%.  In order to compare your wages as an employee vs your income as a self employed individual, you need to make adjustments.  What this means practically is that you either need to work an extra 3 hours per a 40/hr week at the same pay rate as an employee, or you need to increase your rate by $0.83 for every $10 you were getting paid as an employee.

The fourth area is time.  As a self employed individual, time does indeed equal money.  If I’m not working, I’m not getting paid.  What this means practically is that holidays have a different feel to me.  I am not able to work or bill during a holiday.  It also means that when I take vacations, I am not getting paid.  It means that if a client doesn’t need me for any reason, I’m not getting paid.  If I’m actively seeking out new contracts, I’m not getting paid for this time.  You need to factor in this down time into your bill rate otherwise, you will find yourself making a lot less than you were previously.  The upside to this time factor is that if you are a hard worker, you can work a lot more than 40/hrs a week and can increase your income that way.

The biggest change for me in being self employed is that I love what I do.  I get to work on different and interesting projects.  This is strange, but I no longer am sad it’s monday or happy it’s friday.  This really doesn’t have anything to do with working for myself.  It is entirely dependent on liking what I do.  I am pretty sure I could be just as happy working as an employee if the work was interesting.  I am getting to work on the Blackberry, the iPhone, Windows Mobile.  I am using Java and C#.  The Blackberry project is especially fulfilling.  Because of the Blackberry, the sales team are getting many more opportunities to sell.  I will have directly contributed to increasing the revenue of this company.  I love it!

 
0

iPhone web apps

Posted by jason on Feb 13, 2010 in iPhone

The iPhone has some really cool features for web apps.

One feature that I particularly like is called Web Clips.  These are pages that can be installed on your home screen.  You can specify an icon for use on the home page just like you would for a native app.  If you don’t supply one, then it uses a screen capture of your page.  You can also supply a splash screen png that will display while your web page is loading.  You can hide the address bar to make it look even more like a native app.  There is one small gotcha with this though.  If you click on any link inside your webpage, it will open up Safari and close your nice page.  The trick then is to make the entire application a one page app.  You accomplish this through the use of AJAX.

Another feature that is very cool is new to HTML 5.  You can specify a cache manifest file that tells the iPhone which files to cache locally.  The cool thing about this is that if you have an application that is static, you can run the app while the iPhone is in airplane mode.  This is very cool.  You can even specify fallback files that are used when the app is offline.  When I tested this out, the application was VERY snappy.  Currently only Firefox 3 and Safari 4 support this.

A final feature that I have not explored very much is client side storage.  This is also a new HTML 5 feature.  All the newest browsers except Chrome support this feature.  This is a necessary feature if you want to have some sort of database driven offline capable application for the mobile device.

 
0

Simulating a slow network on a Mac

Posted by jason on Feb 12, 2010 in Mac, Ruby

I ran across these very handy commands to simulate a connection of only 4kbps.  This is helpful when developing mobile applications in particular because some cell connections are horrible.

sudo ipfw pipe 1 config bw 4KByte/s
sudo ipfw add 100 pipe 1 tcp from any to me 3000

There are several interesting parts to these commands.

The 4KByte/s which can be whatever speed you want.  This is the bandwidth, measured in [K|M]{bit/s|Byte/s}.

The other interesting part is 3000.  This is the port that will be slowed.  Port 3000 is the default port on which Ruby on Rails listens.  If you are doing Java, you would probably want to use 8080.

The 100 is the rule number.  Useful when it comes time to delete this rule.

To clear out this pipe, simply type the following when finished:

sudo ipfw delete 100

To clear all custom rules, you could type:

sudo ipfw flush

 
0

Controls for Windows Mobile Smart Phones

Posted by jason on Feb 5, 2010 in Windows Mobile

I ran into Smart Phone not supporting another control.  It doesn’t support button controls.  The Smart Phone has a menu at the bottom that behaves as a button.

I seem to have troubles finding this article when I need it, so here is a link to help me figure out the user interface options I have for the SmartPhone:

http://msdn.microsoft.com/en-us/library/aa446513.aspx#smartphoneuserinterface_topic3

 
0

css @font-face

Posted by jason on Jan 15, 2010 in Uncategorized

Embedded fonts are almost a reality in web development.  All the modern browsers support the @font-face attribute.  Microsoft gets a lot of criticism for innovation, but they have had this ability since 1997 in Internet Explorer 4.0.  Unfortunately, they do not simply let you use a TTF font or an OTF font.  It uses an EOT file.

The key to embedding fonts is to use the @font-face  css rule.

@font-face {
  font-family: "myfontname";
  src: url(myfont.ttf) format("truetype");
}

Once you’ve added this, you can simply use your font as you would any other font.

<p style="font-family:myfontname">My font name looks like this</p>

It is really simple…
EXCEPT

If you happen to choose a free font that is similarly named, you will run into troubles using Google Chrome.  I chose a font called Dakota that is almost identical to Handwriting Dakota on my Mac.  When I installed this font to try it out, my Mac warned me that it was a duplicate font, except the menu tool to remove duplicates was disabled.  No matter what I tried, I could not get it to work in Chrome.  Safari and Firefox both worked just fine.  The only way I could fix it was to edit the font and change the name.  I used the free tool Font Forge to do this.  While I was in the font, I had to fix a bunch of issues with the font and I changed the name to Dakkota.  Once I did that, I got it working in all three browsers on my Mac.

Tomorrow, I’m going to tackle IE and the EOT file format…

 
2

Ruby on Rails Nested Resources, Namespaces, and RESTful

Posted by admin on Jan 11, 2010 in Ruby

I am working on a website where I have an admin section to add data and public read only sections.  I like to have a clean hierarchy of URLs.  I first implemented it using the standard generator code.  I had a /restaurants and a /locations url for both.  Locations doesn’t make sense as a top level URL because Restaurants have locations.  I’d rather see things like /restaurants/1/locations.  In fact, that isn’t quite right either.  I only want to show the restaurants for the logged in user.  I really want it to look like /admin/restaurants/1/locations.

Nested Resources

So the first thing I did was to make locations be a resource of restaurants. In my config/routes.rb file, I did the following:

map.resources :restaurants, :has_many => [ :locations ]

In the app/controllers/locations_controller.rb, I created a before_filter to set the @restaurant reference.  Every place where I used Location.find, I changed it to @restaurants.locations.find.  This only gets the locations for the restaurant from the restaurant’s locations attribute.

class LocationsController < ApplicationController
  before_filter :get_restaurant      

  def index
    @locations = @restaurant.locations
    ...
  end

private
  def get_restaurant
    @restaurant = Restaurant.find(params[:restaurant_id])
  end

end

Now my URLs look like /restaurants/1/locations.

You will need to change the form_for on your new.html.erb and edit.html.erb.  If you are using partials for both of these (you should be) then edit that file.  Change the form_for tag to

<% form_for([@restaurant, @location]) do |f| %>

Namespace

Now, I’d like to move these into the /admin url.  I do this by modifying the config.routes.rb file again.

  map.namespace :admin do |admin|
    admin.resources :restaurants, :has_many => [ :locations ]
  end

This creates the admin namespace with URLs that look like /admin/restaurants/1/locations.  When you add this namespace, Rails will then look for your controllers inside the module Admin.  So you will need to create the app/controllers/admin directory and move the restaurants_controller.rb and locations_controller.rb inside it.  Do the same for the app/views/admin directory.  Move the locations and restaurants folder inside it.  You will need to modify the /app/controllers/admin/restaurants_controller.rb and the /app/controllers/admin/locations_controller.rb files.  Add Admin:: in front of the class name like follows:

class Admin::LocationsController < ApplicationController

When I opened up /admin/restaurants/1/locations in my browser, I got errors saying things like restaurant_location_path is invalid.  To correct this, I had to modify the views to prepend “admin_” to all the path functions and add the location parameter.  A safe guess is that Ruby on Rails dynamically generates these functions.

<td><%= link_to 'Show', admin_restaurant_location_path(@restaurant, location) %></td>
<td><%= link_to 'Edit', edit_admin_restaurant_location_path(@restaurant, location) %></td>
<td><%= link_to 'Destroy', admin_restaurant_location_path(@restaurant, location), :confirm => 'Are you sure?', :method => :delete %></td>

I really don’t like embedding the “admin” into the code because I could change my mind and want it all to live inside /administration.  I don’t want to have to change all my code just because of where it lives.  Too tightly coupled.  so, I am not going to use the function created for me.  Instead, I’ll build the URL myself.  If you do not specify the controller or namespace, it uses the current one. Change

<td><%= link_to 'Show', url_for(:action => 'show', :id => location) %></td>
<td><%= link_to 'Edit', url_for(:action => 'edit', :id => location) %></td>
<td><%= link_to 'Destroy', url_for(:action => 'destroy',:id => location), :confirm => 'Are you sure?', :method => :delete %></td>

This will build the same URLs as using the “admin_” functions but without hardcoding the admin path.  You need to do the same thing inside the controllers.  For the link back to the parent, you will need to add the controller as a parameter.

<%= link_to 'Back', admin_restaurant_path(@restaurant) %>

Change to

<%= link_to 'Back', url_for(:controller => 'restaurants', :action => 'show', :id => @restaurant) %>

The next thing you will need to change is the form edit.

<% form_for( :location, @location, :url => { :action => "create"} ) do |f| %>

 
1

Ruby on Rails shell commands

Posted by jason on Jan 6, 2010 in Ruby

This is just a reminder to me about commands that I use.

rails appname
rake --tasks
rake db:create
rake db:drop
rake db:migrate
script/generate controller myobject
script/generate model Person name:string age:integer
script/generate scaffold Person name:string age:integer

 
0

Ruby on Rails ORM through ActiveRecord

Posted by jason on Jan 6, 2010 in Ruby

Rails offers an ORM layer.  This layer is not a very strict layer.  To filter or sort objects, you are actually writing SQL code.  There doesn’t seem to be much complaining in the community about this mis-feature.  In fact in the Agile Web Development with Rails book, the author basically makes snide comments about people who think that the object hierarchy and the database model should not be tied tightly together.  This may just be a cultural thing.  Ruby on Rails seems to be used mostly on brand new projects.  Given this situation, it is understandable why Rails developers don’t understand the importance of uncoupling the database from the object graph.

One of my first consulting jobs was with AMC Theaters.  I was on a tiny project that needed to read from a database that we had absolutely no control over.  Unfortunately for us, the tables and fields often had entirely different purposes than what they were labeled as.  These corporate databases can last decades and changing them is not possible.  Many different groups have written code around these names and changing a field in a table has ramifications way beyond our little group.  It would cause applications to break and there is no funding to fix all the applications just because we don’t like the field name.  Consequently, you are left with the task of mapping a field in a table that has a non-sense name to a field in the object that actually makes sense when you read the code.  This is where Ruby fails to deliver with it’s implementation of ORM through ActiveRecord.

Since I’m learning Rails so I can create new ideas quickly, I will happily use the current ORM.  But this is one area where Rails needs to improve.

 
0

Ruby on Rails Layouts

Posted by jason on Jan 6, 2010 in Ruby

Site wide layout

One common method of creating common look and feels in web development is to split up all the common code into a header file and a footer file and include those files in every single interface page.  This is better than embedding all that html code inside each page, but it is still clunky.  You can not easily see where your tags open and close because they can be opened in one file and closed in another file.  More advanced mechanisms put the overall code in one file but have middle sections where code specific html goes.  So, your html template file would look something like:

<html>
  <body>
    <%= yield %>
  </body>
</html>

It is easy to see the structure of an html page this way.  It also cleans up the code for your view.  Ruby on Rails implements this style of templating and calls it layouts.  Because of Rails’ convention over configuration, to implement a site wide template, you merely need to create /app/views/layouts/application.html.erb and put your site wide html code there.

Override default layout

You can override the layout that will be used by adding a layout declaration inside your controller class like this:

class HelloController < ApplicationController

  layout "mylayout"

  def index
  end
end

Create /app/views/layouts/mylayout.html.erb

Two different layouts, same view

You may have different layouts depending on whether the user is logged in or not.  This is easy to accomplish by using a symbol for the layout.

class HelloController < ApplicationController

  layout :isLoggedInLayout

  def index
  end

private
  def isLoggedInLayout
    "loggedIn"
  else
    "notLoggedIn"
  end
end

You would have two templates, one /app/views/layouts/loggedIn.html.erb, the other /app/views/layouts/notLoggedIn.html.erb.

Copyright © 2010 programming with passion All rights reserved. Theme by Laptop Geek.