Submit any form with a keyboard shortcut

Say you have a simple form like “Add comment” similar to this image

Add comment form

Now, this is a text-area, so the ENTER key will break rows and will not automatically submit the form to the server.

I want to create a flexible way to send the form to the server with a keyboard shortcut like CTRL+ENTER.

Thanks to an awesome library by Thomas Fuchs called KeyMaster it turned out to be quite simple.

By default, Keymaster ignores all keyboard shortcuts if it’s inside an input or textarea or even a select box, obviously that is not what I wanted or needed.

Luckily, you can override that behavior with a filter function, like so

 

Now, all you need to do is listen to the event, and when it’s fired, submit the parent form. like so:

 

That’s it, now every text-input, input or select box can submit the form with this shortcut, it’s super useful and your users will certainly like it.

Remote Pair Programming – Remote pair programming resources, articles, and thoughts.

Link

I have been a remote programmer for a while now (over 3 years), working across time-zones (10 hours difference in many cases).

I used this blog a lot as an inspiration and took many great tips.

If you are into remote programming and you have remote workers, you should read the posts in this blog.

Remote Pair Programming – Remote pair programming resources, articles, and thoughts..

14 Ways to Contribute to Open Source without Being a Programming Genius or a Rock Star

Link

Contribution to open source has a myth around it.

People think that “contributing” means changing huge chunks of the code, and being a genius.

It’s actually not the case, documentation for example is a huge contribution to open source, sometimes scratching your own itch with changing a single method is also contribution.

Find something you like and work with every day, and when you encounter a bug, or a need for something, try and implement it.

14 Ways to Contribute to Open Source without Being a Programming Genius or a Rock Star.

Pull Request #5340: Added wrapper tag for simple format, added test by KensoDev · rails/rails

Link

Following my latest post about the simple_format, I think that it’s broken to assume only “p” tags are valid wrappers.

So, I opened a pull request to Rails itself.

Basically, it changes simple_format so you can pass in the wrapper tag you want, I found myself wanting a “div” or a “span” too many times.

Pull Request #5340: Added wrapper tag for simple format, added test by KensoDev · rails/rails.

Enjoy!

Do your own Agile…

There’s a lot of buzz going on about software development methodologies, people talking about Agile, Kanban, Pair, XP and more and more.

There’s not much people are talking about when it comes to a single developer.

I have been a consultant/developer for the past ~3 years or so, working from home for several clients both in Israel and abroad (US mainly).

During this time I gained tons of experience in knowing what works and what doesn’t, this may be specific to me, but I don’t think so, so I decided to share this knowledge. So… what am I doing to keep productive during the day?

Pomodoro

The pomodoro technique I am using is simple, I have a small application running on my Mac constantly (there’s windows versions as well).

When I start work, I simply choose start pomodoro, input what I am going to work on and dive in.

Now, all that’s pretty standard, but I am using it with get-shit-done, so once I start a pomodoro, get-shit-done executes work mode.

This work mode shuts down all outer traffic to time-sinks like Facebook, Twitter, news sites, YouTube and many more.

The script is customizable so I keep changing what I want/need. Along side with shutting down the time sinks, I execute a “busy” mode on Skype and any IM I am using at the moment. This is completely automatic, you can see it in the following screenshots

Pomodoro scripts

IM status change by Pomodoro

As you can see, any pomodoro I do is going into “Things” which I can then look at my day and see where my time went, what did I accomplish during the day and so on (it has OmniFocus integration as well).

If you want to get the app, you can get it here

Stay Hydrated

This may seem stupid so up high here, but it’s REALLY important. I found that in days where I drink and stay hydrated, I am much more productive, less tired and get a lot more done during the day.

Water break

Another benefit is that I need to visit the restroom quite often :) so I don’t slack around in the chair too much. I use an application for that as well, it sits in my top bar and reminds me to take 5 minute water breaks every hour. if you are wired in, you can just ignore that, but don’t ignore it too much, it’s not the point :-)

If you want to get the app, you can get it here

Things To Do

Things To Do

I open every day with a “things to do” list.

I write down anything I need to do, and then I forget about it, I don’t keep my mind occupied with all I need to do, I look at the list during the day.

One important thing is that I set times for stuff

  • 1-2pm => working on the cashier module
  • 2:30-3:30pm => working on the master/slave @dirty variable

While of course, these 2 tasks don’t actually take an hour, it’s here just for an example :-) .

Anything you need to remember during the day, write it down, than simply let it go, don’t keep you mind occupied about that.

I keep any blog post idea, stuff that come to mind on other project that are not currently in work order and more…

Knowing I have it written down saves me the trouble of having it in my brain, and I dedicate all my brain resources (which are slim to begin with) dedicated to my code.

I must say, I have tried any TODO app out there, the one that works best for me is a pen and a paper, I use Things as a history for my pomodoros and nothing more (usually)

TDD

Another possible controversial title in a “be productive” blog post :-)

When I am TDD’ing my mind goes into a “small piece” mode, I think only about the next 2-3 steps in what I need to accomplish.

People (wrongly) think that TDD is time consuming and they get less done, I have proven it numerous times in the past that this is incorrect, people that TDD are often 2-3X more productive then others.

Just think about every time you open a browser to “test” something, it can be done in TDD while you save your file, and you can instantly see if you broke something else in the code you are not thinking about.

The right side code is test, left side is actual controller code :-)

TDD your ass off

When you are not productive…

There are simply days when you are not productive on the actual things you need to get done, in these days I don’t try and force myself into it, I simply move to other exciting stuff for 20-30 minutes (sometimes more)

I do a TON of open source, I help people in groups, forums etc..

Sometimes, answering a question, gets me back into code mode and I can find myself wired in for 2-3 hours after it.

To sum up

I touched but a fraction of my work methodology.

The thing is, you need to find YOUR groove, you need to find your pace, the hours where you are productive, the things that excite you.

You may take some of what I said here, you may take all and you may take nothing, all I am saying is that regardless of where/how you work in the team, you can have your own thing going on.

You may work with Pivotal Tracker or Trajectory, but still maintain your TODO list for the next few hours (like I do).

Share your thoughts…

I would love to hear what you think in the comments and have a discussion.

Better simple_format for Rails 3.X projects

Since Rails 3.0 came out, we Rails devs deal with the auto escaping of HTML.

Now, in most cases it comes in SUPER handy, but not always.

Rails has an answer to that, it’s called ‘simple_format’, and it escapes the HTML and formats the string to be friendly, with line breaks, etc…

Now, the problem with simple_format is that it wraps everything with a paragraph tag, which doens’t always fit your design, your markup or just your sense of aesthetics.

So, I wrote a REALLY simple helper, not even going to explain it because it is so simple

  def simple_format_no_tags(text, html_options = {}, options = {})
    text = '' if text.nil?
    text = smart_truncate(text, options[:truncate]) if options[:truncate].present?
    text = sanitize(text) unless options[:sanitize] == false
    text = text.to_str
    text.gsub!(/\r\n?/, "\n")                    # \r\n and \r -> \n
    text.gsub!(/([^\n]\n)(?=[^\n])/, '\1<br />') # 1 newline   -> br
    text.html_safe
  end

As you can see, it does not wrap with any tag, also it adds an option to truncate the string.

Setup your laptop for Ruby and Rails development

Setting up a new machine has never been an easy task, I remember back in the days where I was a windows user, setting up a new machine was a real pain in the ass.

I used to write everything I had installed on a piece of paper and then go one by one and install everything, if I needed to download it could take even days to get a new machine up to speed.

Today I am a Mac user (Thank god), so installing a new machine for myself is the easiest, I simply restore it from a time machine, and I am ready to go in a matter of minutes (well, maybe more, because the data can take quite some GB’s)

At Gogobot, we have new developers coming in (Yeah, we are growing), they all get a brand new Mac machine and they need to get it up and running, this is a common thing, since many developer buy new machines and they want the environment for Ruby, Rails, RVM and the rest of the stack be ready for them, no hassle.

When I came in to Gogobot, we were still running on rails 2 with no bundler, so this was a bigger pain, but today, we are on rails 3 and we have bundler which certainly makes the process easier, but still, not as easy as I wanted it to be.

Our development environment uses almost across the stack technologies, we use mysql of course, we use Redis, memcached, Resque, SOLR and many more, so installing everything can take a long time.

We wanted to take this pain away, enabling new developers (and of course all of the community) to get their machines up and running in a matter of minutes with no interruption for the developer. So, while the developer is doing his tour around the system, his machine will be ready for him when he gets back.

(B.T.W, I keep saying him, but I am referring to female developers as well, don’t get me wrong here ha…)

Last week, I got me a brand new machine, and I could not access my time machine (I was out of my house).

So, I said this is a good opportunity to do what I wanted to do in ages now and get this script up and running.

I cheated

I started with a script Thoughbot had released, I tweaked it and twisted it to fit our needs.

I got my new machine up and running with our project in about 20 minutes, that was amazing and it certainly breaks a Gogobot record.

So, as I always do when I have something half decent, I released it as open source.

You can find it here

Please feel free to use it, if you are having any issues, open an issue in the repo and I will try to address it.

Keep in mind the script is provided AS IS.

Kill all resque workers with a single command

At Gogobot we are running background processes with Resque.

We are using God to start workers on all servers, both front end and back end (with different queues).

God sometimes has an issue with leaving some stale workers laying around which cache old code, so if you change your code, this is your enemy since you start seeing failed jobs that should not fail what so ever.

Even when you stop the rakes (Workers run by rake) with god, it will still leave those lying around.

In one word, Frustrating…

So, I was IM’ng with Chiu-Ki and we were talking about killing workers, and I was telling here how to kill those.

She told me she has a shell script for it.

There you go…

and if you just want to test before doing some killing, you can run this:

Of course after this GOD will restart the workers with the new code and that’s it, you have fresh workers, with new code, ready to be up and running.

Rails uninitialized constant Resque::Server

One of the most significant changes in my migration of a rails app from Rails 2.3.10 to rails 3.0.9 was the move from delayed_jobs to resque.

Resque is a Queue system that’s based on Redis, which is much faster and reduces the load on your database. Also we were experiencing some nasty deadlocks with delayed jobs that really broke our transactions and our backs.

I installed the gem and installed Redis on my development machine, mounted the Sinatra app on my rails app and navigated to the web view of resque.

Then, I saw this error:

RuntimeError at /resque/overview
ERR unknown command 'server'
file: client.rb location: call line: 47

Which looked like that:

After doing some digging into the code (as I always do when I encountered a problem with a gem), I found that the version of the gem (resque) I was using called the “server” method on the redis instance which was not there any more.

Looking at the code for the new gem showed that the app is now using the property “redis_id” which looks like this:

</span>
<pre>    def redis_id
      # support 1.x versions of redis-rb
      if redis.respond_to?(:server)
        redis.server
        elsif redis.respond_to?(:nodes) # distributed
          redis.nodes.map { |n| n.id }.join(', ')
        else
          redis.client.id
      end
    end

Upgrading the gem to the newest version solved my problem and everything got back to normal.
So, if you encounter this issue, just upgrade the resque gem and you are all good.

The gem version I was using was “resque (1.8.2)”, now I am using “resque (1.18.2)”

As always, if this post was helpful, drop me a comment, I would love to hear.