Saturday, July 30, 2011

Reducing Slug Size (Heroku)

If you check out http://devcenter.heroku.com/articles/slug-size, three tips are offered to reduce slug size:
  1. Move large assets like PDFs or audio files to asset storage
  2. Ignore files which are unnecessary to run the app. For example, unit tests, PSDs, or large design documents.
  3. When possible, reference a released gem by name in your Gemfile rather than loading it from source using the :git option.
Let's first look at #3.

I originally had the following in my Gemfile:
gem 'rails', :git => 'git://github.com/rails/rails.git', :branch => '3-1-stable'
gem 'sprockets', :git => 'git://github.com/sstephenson/sprockets.git'

When I changed this to
gem 'rails', '3.1.0.rc4' # :git => 'git://github.com/rails/rails.git' ...
gem 'sprockets', '2.0.0.beta.10' # :git => 'git://github.com/sstephenson/sprockets.git'
my slug size changed from 63 MB to 61 MB (specifically to 61.6 MB). But as Heroku recommends,
The maximum slug size is 100MB. Most apps should be far below this size. Anything under 10MB is good. If you exceed 40MB, you should think about trying to reduce the size of the slug.
...so I better keep going.

I moved on to #2. I created a .slugignore file with the following contents:
features
test
spec

In my case, just ignoring these folders in my app reduced my slug size from 61.6 MB to .... 61.6 MB. Negligible difference, unfortunately. I'm suspicious that adding the .slugignore didn't make a difference because when I set up this same app again at a different URL on heroku, the slug size was only 20.5 MB.

After filing a request to Heroku (https://support.heroku.com/requests/27593), I learned that, as the support staff member said, "There is currently a bug in bundler that is not cleaning old versions or unused gems out properly. We are working on upstreaming a fix for this, so hopefully sometime soon your slug size will lessen automatically with a push."

Guess it's back to waiting then. Nonetheless, I hope you found my little investigation above interesting if not helpful.

Tuesday, July 26, 2011

Trick to tell when a website was last updated

Some of you might find this useful... I certainly do when I want to check how up-to-date a website is.

Put this in the URL bar at the top, and hit enter:
javascript:alert(document.lastModified)
This will tell you the date and time (Greenwich Mean Time) the site was last updated.

This is nice when you're citing online sources and you need the date the site content was last changed, or if you simply want to know if a site's content is likely to be relevant today.

Comment if you find this helpful :]

Thursday, July 21, 2011

Using PostgreSQL as your database for Ruby on Rails

The following blogpost does an outstanding job of explaining how to set up PostgreSQL for Ruby on Rails development on OS X (for Rails 3).

https://willj.net/2011/05/31/setting-up-postgresql-for-ruby-on-rails-development-on-os-x/

However, there is one part I want to draw attention to:
$ createuser shawsome
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) n
Shall the new role be allowed to create more new roles? (y/n) n

He says that it's okay to specify "No" for each of these, but you'll face a testing problem later:
$ bundle exec rake db:test:prepare
PGError: ERROR: permission denied to create database

Instead of replying "n" to "Shall the new role be allowed to create databases?" you should have replied "y" (yes) because using Rails' testing frameworks inherently requires that the test db is destroyed and regenerated frequently, as is shown by the existence of the rake command "rake db:test:prepare".

So what can you do now to fix the role? Instead of retracing your steps and making a new user with proper permissions, just change it via postgres:

1) Connect to the standard "postgres" db of your postgres installation (replace "your_username" with your username):
psql -U your_username postgres

If you're not sure what your_username is, run the following; it's the name under "Owner" across from "postgres":
psql -l

Note that not specifying your username might let you connect, too:
psql postgres

2) Run the postgres command:
ALTER ROLE that_username CREATEDB
...where that_username is the username of your Rails application.

That should do it. If interested, read on about useful postgres commands.

Wednesday, July 20, 2011

A social network convo: Google+, Facebook, Twitter

I had this post on Google+ a while back that led to some interesting discussion on social networks.
The original, on Google+, is right here if you want to check it out.

The key parts went like this:

Me: The more I think about it, the more it seems like Google+ will be comfortably ensconced between Facebook and Twitter. Facebook is heavier; you're less likely to babble about random things on there - each post you tend to put more thought into. It also has that exclusive and private feel to it - just me and my friends. This is relative to Twitter, where you tweet whatever the !$@# you want; it's like a shouting affair. Plus the fact you can follow nearly anyone...

G+ puts all this together with the ability to follow and group into circles. The lack of a wall makes you a little more comfortable to post whatever, whenever, without considering the "other people's posts on my wall vs. my posts" ratio.

Ah and I think Instant Upload is a big deal... and the (so I hear) greater privacy controls over photos. The easier privacy controls actually is a huge.... plus.


Friend (male): Interesting - I would actually rearrange that to say that Facebook sits between Plus and Twitter, in that status posts and such are seen by all "friends," regardless of if they're a "friend of 10 years and counting" or a "friend that I saw once in class." For me that exclusive and private feel to it is more readily manifested by Plus's control via circles, than Facebook's control via friending. Ie., you can mimick Facebook in Plus by putting everyone into the same circle, but it's a lot harder to mimick Plus in Facebook, and going down a step you can mimick Twitter in both by again putting everyone in the same circle in Plus and barfing your brains out in discrete little chunks of 140 characters, or doing the same while accepting any and all friend invites in Facebook, but you can't go back up the ladder from Twitter to either of the two.

Friend (female): In Facebook, you can filter your friends into groups so certain people see certain things, while others don't...which is kind of similar to the circles idea, IMO.

What I would like to see in + is the trending stuff in twitter.....because twitter is cool like that.

Friend (male): Granted, but because Plus is entirely built around the concept the interface is a lot smoother and intuitive - a merely aesthetic point, perhaps, but it makes a difference. The concept is made salient in Plus, while I would say that very few people in Facebook go to the trouble of compartmentalizing to such a degree. At the moment I see one of Facebook's largest advantages is its enormous user-base (world's third largest "nation" :P) - this difference may be what will allow Plus to siphon people away.

Also, more generally, I see Google's expertise in search being very easily implemented, while Facebook's search functions really aren't much to speak of.

Me: +[insert name] though Facebook's list organization is far more difficult to use than Google's Circles;
+[insert name] I see what you mean. I feel that the ability to draw from both types of social networks gives me the image of something like this:

Facebook <===> G+ <===> Twitter
More formal; flexible formality; informal
Many people, loosely categorized; categorized people through and through; not necessarily ppl
Meaningful posts; any kind of post; %$#%^ posts (tweets)

... eh, it's all relative anyway. It probably ties more to how I personally use social networks.

Friend (male): Yeah, I think Facebook isn't nearly as "formal" to a lot of other people as it is to you - though one interesting perspective is to consider the GUI. Question: between Windows XP and Mac OS X, which seems more "formal" or "professionsal" or "technical?" For me, based purely on presentation, I would say XP, if only for the reason that XP looks more linear and squareish, while Mac OS X is softer and more elegant. Similarly between Facebook and Plus, do you think there could be an influence of presentation? Facebook is familiar, structured, and linear, while Plus is open and sparse, and graphically less rigid.

Me: YES. I was actually about to mention design. Facebook's design has that blue bar at the top, which lends to a "rigid" or "restricted" feel (at least for me, probably for a good number of others deep down). Also, while not terrible, the constant ads on the right sidebar, or actually both sidebars combined contribute to the barred feeling of the site. After visiting Facebook for years, I admit the comfortable feeling of the site wears away with these subtleties... albeit only for brief moments when I'm surfing.

Google+ has such a clean, smooth, minimalist layout that the feeling of flexibility really comes through. It can morph into anything = how I think of G+. In fact, google.com has always been my home page, and part of the reason is the absolute emptiness of the page. It just, for me at least, clears your mind. Helps you relax a bit, even. (You know, with all the clutter on the web and in life.)
Between Windows and Mac, I feel Windows is slightly more formal - partly based on its philosophy behind maximizing windows, partly based on my experiences visiting software companies showcasing their software - there'll all on windows. Windows also gives me a "laborious" feeling that's difficult to describe. Oh - and the key thing that undermines the formality of Mac (which isn't a bad thing) is the philosophy behind having many windows open at once with corners and bits of sides peaking out. (Goes back to the philosophies behind maximizing: http://www.forevergeek.com/2006/09/mac_vs_windows_its_all_about_the_maximize_button/)

Technical, though, I wouldn't push too much to the Windows side - the Unix OS within Mac is actually a large reason I switched. I needed it for RoR programming, and I know all [good] Rails devs say Mac rules and Windows.. doesn't.


Friend (male): Personally I don't really mind the ads on Facebook - they're there, and they take up space, sure, but not an inordinate amount, so they become just another part of the framework that I don't need to pay attention to.

And yeah, I've always had google as my homepage as well, until I switched to Chrome haha (now my "homepage" is just that "new tab" page).

Haha, I won't get into Windows vs. Mac - that's a debate that never ends. And really - in the context of programming, given that you can use even notepad to program, albeit more slowly, it really doesn't matter what OS you're on. In other contexts such as gaming or graphic design, there are much clearer advantages. I intended it as purely a visual comparison - though your article on "maximization" as an indication of the workflow philosophies is interesting. I'm not sure how much I believe about how strong of an indicator it is, but it's an interesting perspective.

Tuesday, July 19, 2011

That's not what I said!

Ever get in one of those arguments in which you (or the other person) is like, "That's not what I said!" or "I never said that!" in a tone of rising fury?

The retaliation tends to be, "Yes you did! You definitely said [some word here]!"

From experience and... well.... musing, I believe that in 99% of these arguments, the listener is right. Why?

Think about when you are speaking to someone. All you want to do is convey some meaning to this other person, so you're focused primarily on the meaning. More often than not, the actual words you say aren't registered in your own mind nearly as much as they are to the listener, who must absorb the words first and then extract meaning.  I see it similar to...

Speaker ==> Meaning he wants to convey ==> words he tries to use to convey it ==> Listener

The listener is closer / more focused on the words because he is trying to grasp them entirely so he can get the speaker's meaning. Think about it. The speaker is often so entrenched in the meaning that he may "let things slip" from his mind quite easily. This all goes to explain why I feel the listener is correct in most of these arguments.

Quick example from my life. My five-year-old sister is talking about trophies - she has a little trophy from kids' gymnastics - and in a quick talk she says that she wants her own place to put all her trophies (cute little ambitious kid, right?). From the depths of my heart, I dislike the extrinsic and superficial values of trophies, so I mutter that trophies are stupid. Yep. Slipped right out of my mouth, and I didn't even realize it. My sister says, "Noooo..." and grabs her little trophy and says something about how pretty it is, and I, then more engaged, say that trophies are "superficial" and don't really matter.

Then it happens.  My sister says they're not stupid; I say I didn't say they were stupid; she says yes I did; I say no I didn't; she insists; I insist... the petty argument ends with my simply turning away and going back to work, telling her to be off with some activity.  I reflect on the event later, though, and I realize that... who was right?

The five-year-old.  That is, the listener.

Monday, July 18, 2011

From Windows to Linux to Mac

For over a decade I've used Windows. Then, sometime during junior and senior year of high school, I started using Linux - specifically, the Ubuntu 10.x distribution. Finally, the summer before I enter college, I made the long journey to Mac.

Why did I switch? What are my experiences and thoughts with each platform?

Initially, I needed a Unix-based machine for programming work - i.e. Linux or Mac. I thought going to Mac would be too large a jump, so I installed an Ubuntu distribution of Linux on an old Windows laptop. Experimenting with that and doing work on it for a good year was nice, but the laptop was just sooo old, and I eventually had the opportunity to get an entirely new laptop. After looking into Mac - particularly learning the keyboard shortcuts and philosophies behind it - I decided it was the right choice. I didn't want an unstable, "risky" machine like Linux, and I had since realized how unelegant Windows was. I got a Macbook, and I've never looked back since.

That's the quick synopsis. There are lots of miniscule details that played an enormous part with each system. Since Linux never really got a big hold, particularly because I felt there was just too much freedom with what you could do to it (so using it could be dangerous), I'll distinguish Windows and Mac.

1) Maximizing:

I used Windows ever since I was little - so that's for ~10 years. Its philosophy behind maximizing is simple: click the maximize button in the top right, and the window's widths and lengths stretch until they reach the edge of the screen.

Mac's philosophy is different. On computer screens, it actually looks ugly to maximize a window all the way as Windows does because more often than not, excessive white space shows, and the window consequently looks unappealing. So Mac toggles between two sizes: (1) the "best" size of the window to display as much of its content as possible, and (2) the smaller size so you may place other windows side-by-side. This actually makes more sense, and it makes the screen far more elegant to look at. For a more thorough explanation of this, check out http://www.forevergeek.com/2006/09/mac_vs_windows_its_all_about_the_maximize_button/.

2) Minimizing

Windows basically lets you store open windows at the bottom bar through minimizing. Alt + Tab lets you toggle between all active windows, regardless of whether the window is minimized or not.

Mac, en revanche, considers minimized windows as an isolated group. Once minimized, windows can not be switched to via Command + Tab (the Mac equivalent to Alt + Tab). Minimized windows on Mac are "stored away" - often so you can reopen them in different spaces or just deal with them later. Personally, I like to reactive minimized windows by doing the four-finger swipe down on the trackpad (or through whatever means to activate exposé) and then selecting the minimized window (it'll always be at the bottom, in the minimized windows section).

That said, Mac also provides the hide option, which hides all windows associated with the active program. So if you have various Google Chrome browsers open, Command + H will hide all of them. They don't appear in the minimized windows section, and activating exposé won't show them; they're hidden. To reactivate them, I just Command + Tab to the program. This hiding feature is nice when you just want to hide from your mind all windows related to an application without exiting the program.

3) Usage of clicking

Perhaps one of the biggest difficulties with switching from Windows to Mac was the difference in using clicking. In this post, I describe how I disliked the extent to which so many Mac users tediously used the mouse to navigate all the way to whatever they wanted to click when they could just use a shortcut to get there a million times faster.

First, I realize now it's not that bad to use the mouse like so; in fact, using the mouse for things actually helps users' stamina when using a computer. That might sound weird, but think about the times your hands get cramped from using Windows doing all this intense work for hours... if the mouse had been used, something comfortable albeit a bit slower, you'd be more relaxed. It really helps keep users in a calmer, more comfortable state while working on a computer. Second, if you check out the rest of that post, there are actually many, many shortcuts that you can set on Mac. It's just a matter of having the knowledge to use them.

4) Design

Undoubtedly, I find the Mac design more comfortable. In spite of nearly a decade's worth of using Windows, after barely half a season, I can feel the beauty of Mac each moment I use it. It helps me feel inspired and comfortable in doing my work. So there's a reason - a worthwhile reason - Mac is more expensive; it truly is far more elegant and user-friendly.

Specifically, let's break down the situation in which an application is active. On Windows, an active application is simply the window that it's in. You could have many Chrome applications open right at this moment. On Mac, when an application is active, you see its name in the menu bar at the top next to the Apple logo. On Mac, an application could be active and still have zero windows. This definitely changes things around a bit. On Windows, when you close the one Chrome window you have open, you are effectively exiting the Chrome application. Not so on Mac. The menu bar still shows Chrome. Command + q is necessary to exit the program. I, however, find it nice to leave an application active although no windows for it are open so you can quickly open a window when it's suddenly needed.

5) Resizing

One complaint some Windows - and sometimes Mac - users have when working on a Mac is the fact that you can only resize a window by clicking on the bottom right corner of it. I've thought about this, and indeed, some people definitely dislike this (http://forums.macrumors.com/showthread.php?t=203227). But let's pause for a second. Look closely at any window on a Mac, and you'll realize that it has shadows. It leaves shadows in the background, which is simple yet ingenious. Further, look at the edges. There's no thin border like there is on Windows. In sum, Mac sacrifices a little functionality for further elegance. Some might think it's not a worthy tradeoff; others, the contrary.

Alright. Pause. I guess you could still have the ability to drag windows from any side even with the kind of window design Macs have, but there's a silent benefit to lacking such functionality. I've noticed that on Mac, I spend time resizing / adjusting window sizes far less - partly because of the maximizing philosophy of Mac (see 1. Maximizing), and partly because of this only-the-bottom-right-corner thing. I think this is good - seriously. Users aren't as distracted or fidgety about adjusting window sizes, and productivity rises. I know, it may seem insignificant, but I really feel far more comfortable. I rarely ever feel the urge to resize a window now, and when I do, I pause and ask myself if I should bother going all the way to the bottom right corner and dragging or if things are elegant as is. Usually, it's the latter.

6) Folders, Applications

Apple likes to have a library of files all bunched together, put in one inexplicable but convenient file. This is true for iTunes (with its iTunes library) and iPhoto (with its Events). Windows users, including me previously, are probably more accustomed to complete control over files and their locations in folders. While you can do the same on Mac, albeit with slightly fewer common keyboard shortcuts (does anyone know how to cut and paste in Finder with keyboard shortcuts? Seriously.... do we really have to do stuff like this? http://lifehacker.com/5622046/cut-and-paste-files-in-os-xs-finder-with-automator-services), this other concept exists, and it took me a while to get used to: To add a file to iTunes, it must be in the iTunes library. Similarly, to add a file to iPhoto, it must be in Events.

Basically, the iTunes library and Events for iPhoto are like "general storage areas" that have to store everything before you can organize it or do whatever you want. Wrapping my head around this was odd as I came from a decade spent on Windows, but once I did, the whole deal with file management was much easier. I also find everything much more elegant.

So.

These are just a few highlights of the differences mixed with my thoughts on usage. I hope you find some of them beneficial.

If you have notions regarding Mac and Windows (or Linux), feel free to Leave a Comment Below :).

Saturday, July 16, 2011

Trick to tell the number of links, images, or forms on a webpage

I have no idea WHY you might want to do this, but you can. :]

For each of these, put the following in the URL bar at the top, and hit enter:

To find the number of links...
javascript:alert(document.links.length)

To find the number of images...
javascript:alert(document.images.length)

To find the number of forms...
javascript:alert(document.forms.length)

Friday, July 15, 2011

link_to with image_tag

How do you use Rails' link_to helper with the image_tag helper in your views?

Easy:
<%= link_to (image_tag "my_logo.png", :size => "150x50"), '/' %>

Just surround the image_tag part with parentheses and place it where you'd typically put the text to display in your link_to.

Mac shortcuts

After making my switch from Windows to Linux to Mac, I realized how helpful it could be to others to highlight a few key Mac features that I personally find essential. It always bothered me how most other Mac users I see use the mouse just too much. They move it all the way to the menu bar, click, and then move back down the list to select something... when they could just use a keyboard shortcut to get it done instantly. I guess it's a productivity thing, and unproductivity has always been one of my pet peeves.

On Windows, many users like to use the keyboard to tab from button to button and then hit enter to select whatever they want. Mac users seem to enjoy moving the mouse all the way to the button and clicking, and I have to admit this was one of my biggest turnoffs with using a Mac. But I knew there just must be a shortcut, and when I found it, boy was I glad:

*If a button or icon in a form or pop-up menu is highlighted with a blue border, hitting the spacebar is equivalent to clicking that button.

*If a button is entirely blue, hitting enter is equivalent to clicking that button.

*If you want a shortcut to "cancel" in a pop-up dialog, hitting the escape key will do the trick.

*If you want to tab between all controls, open System Preferences -> Keyboard -> Keyboard Shortcuts and look for the words "Full Keyboard Access" located at the bottom of the menu.  Normally, "Text boxes and lists only" is selected, but selecting "All controls" lets you tab among all controls.

These were just a few key things that made me more productively use my mac and helped me transition from Windows and Linux.

More info: http://support.apple.com/kb/ht1343.

Monday, July 11, 2011

Sass partials with Rails 3.1 - use .css.scss

I tried importing partials in Sass for a Rails 3.1.rc4 app today, but for some reason, variables just wouldn't be recognized although they definitely existed in the partial.

The key thing is that your partials have to have the .css.scss extension - not the .scss extension as is shown on the Sass website (http://sass-lang.com/tutorial.html) explaining partial usage.

Here's an example of Sass used in my app:
assets/stylesheets/admin/meals.css.scss:
// Place all the styles related to the admin/meals controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
 
@import "../partials/standard_list";
 
body {
 #meals_index {
  #meal_new_area {
 
  }
  ul#meal_list {
   @include standard_list(meal);
  }
 
 }
}

assets/stylesheets/partials/_standard_list.css.scss:
@mixin standard_list($resource) {
 $light_green: #52D017;
 
 list-style-type: none;

 li {
  ul li {
   display: inline-block;
  }

  ul li.name {
   position: absolute;
  }
  ul li.controls {
   position: relative;
   margin-left: 300px;
   width: 200px;
  }

  ul##{$resource}_list_headers {
   li { 
    font-weight: bold;
   }
  }
  ul.items_within_#{$resource} {
   li.name {}
   li.controls {}
  }

  ul:hover { background: $light_green; }
 }
 
}

Sunday, July 3, 2011

Running multiple tags for Cucumber

If you want to run multiple tags at once, run something like this:
cucumber -t @a,@b
Note that there should be no space after the comma.  The sole separator is the comma.

See this site for more info.

Saturday, July 2, 2011

"Missing template" error with RSpec... Solved

If you ever get an error like...
Failure/Error: post :destroy, :id => organization.id
     ActionView::MissingTemplate:
       Missing template superadmin/organizations/destroy, application/destroy with {:handlers=>[:erb, :builder], :formats=>[:html], :locale=>[:en, :en]}. Searched in:
         * "#"
...when you run a spec test for a controller, and your routes are definitely fine and there shouldn't be a template for the action, you just need to specify a redirect_to or render.