Thursday, May 12, 2011

Paperclip: How to delete an attachment?

Paperclip (https://github.com/thoughtbot/paperclip) is a great gem. I use it all the time in my rails projects.

One constant need I have is to be able to remove a picture I added to one of my model objects while editing it. Say I uploaded a profile picture and now I want to remove it...

While browsing for a solution, I ran into this: http://railsforum.com/viewtopic.php?id=30349.
The solution is nice but I'd hate to repeat it in every model object where I use an attachment. Some models have more than one attachment too...

So I converted the proposed solution into a module, saved it as 'image_clearer.rb' under the 'lib' directory in my rails project:


module ImageClearer
  def self.included(host_class)
    host_class.extend(ClassMethods)
  end
  module ClassMethods
    def clear_image_for(field)
      attr_accessor "clear_#{field}"
      before_validation "check_for_clear_#{field}"
      define_method("clear_#{field}=") do |val|
        instance_variable_set("@clear_#{field}", !val.to_i.zero?)
      end
      define_method("clear_#{field}") do
        !!instance_variable_get("@clear_#{field}")
      end
      alias_method "clear_#{field}?", "clear_#{field}"
      class_eval <<-"END"
        def check_for_clear_#{field}
          if clear_#{field}?
            self.#{field} = nil
          end
        end
      END
   
    end
  end
end


Then in the model where I have an attachment:


require 'image_clearer'
class Product < ActiveRecord::Base
 include ImageClearer
      has_attached_file :photo,
        :url => "/uploaded/:class/:attachment/:id/:style_:basename.:extension"
  end
  clear_image_for :photo

and in my edit view, all I have to do is:

...
 <% if @product.photo.file? -%>
      <%= f.check_box :clear_photo -%>Delete photo
  <% end -%>


If you have more than one attachment in your model, you can use it as:
 clear_image_for  :attachment1
 clear_image_for  :attachment2

Monday, March 21, 2011

Happy Pepe is now happier

Pepe is very happy today. It has climbed to Number 2 spot in the 'Educational Games' category in iTunes Turkish store.

Saturday, March 19, 2011

Happy Pepe: An iPhone Game designed to teach math and improve memory

I have a son (Emir) in 4th grade and a daughter (Selin) in 3rd. Neither one of them enjoy doing homework. This is not very surprising or upsetting for me as I still remember how much I hated it myself.

I sometimes try to help them out with their math homework and usually get frustrated quite easily. Just recently I found myself telling my daughter one needs to memorize the multiplication table. That insanity didn't last for too long fortunately as I realized the absurdity of what I was asking. I realize one needs to know how to multiply at least single digit numbers but with which method a child needs to get there?

One can start with say 2s and recite outloud 2x1 is two, 2x2 is four, and so on..Up until 5s, that works but it gets exponentially harder once you hit 6s. You then resort to rimes, etc.

I thought another and more fun way would be to embed multiplication and addition questions inside an iPhone game application. And I decided to write one...

I am a software engineer so I know how to code but my experience has been with client/server systems, financial systems, web applications, etc. I had never written a single game before. But the game I wanted to write didn't need to be a 3D extravaganza either.

I had heard and read about Cocos2D (http://www.cocos2d-iphone.org/) game framework before. But then when I was googling for other frameworks I ran into Jonathan Beebe's blog post (http://jonbeebe.tumblr.com/post/1119939987/corona-sdk-review) about Corona SDK by AnscaMobile (http://www.anscamobile.com/corona/). I started reading about it and it immediately felt right which is a feeling coders are familiar with.

I had to learn a new language, Lua, in order to start coding with Corona. That didn't turn out to be a problem as I enjoy experimenting with new languages and it's not a cryptic language with a steep learning curve.

Corona download comes with great samples, and those helped me out a lot. Both in learning Lua and understanding the components of a game.

I first consulted with my kids about the story of the game. We picked up the hero (a penguin) and decided that the aim of the game would be for Pepe the penguin to try to catch fish falling from the sky. Some fish would get Pepe points, and some others, like a shark, a killer whale, would cause him points.

For the 'teaching math' part of the game, we decided to display for a short period multiplication and addition questions such as '3x9=?', '14+9=?', etc in the middle of the screen and then fade the question so that the player would need to calculate the answer and remember it. Then along with fish, we would start dropping numbers from the sky and Pepe would get points if he were to catch the correct answer number. Catching the wrong numbers would cause him big points...

I started writing the code in the evening on March 3rd. My first challenge was to create multiple screens: one to display the menu, another one where the actual game happens, another one for the settings, etc. Corona samples really helped a lot with this. I read lots of their code and settled on an approach.

The next issue was to figure out how to drop objects from the sky in a real-life manner. It turns out Corona comes bundled with a physics engine: Box2d (http://www.box2d.org/). And it's funny how straightforward it's to use it:

physics.addBody(hero, "dynamic", {density=3.0, friction = 0.5, bounce=0.0})

There is a comfort point for a coder when reached makes him able to achieve almost anything using a framework. Getting to that point is very quick with Corona. Corona is to game programming what Ruby on Rails is to web development.

Once I got to that 'comfort' point, I was able to advance quickly and finished the skeleton of the game. I consulted with my kids at all times about the look and rules of the game and they gave me plenty of valuable feedback.

Then came the hard part: graphics! Man do I suck at graphics! Even trying to come up with a background and foreground color combination is a task enough to give me nightmares at night. And I didn't want to spend a fortune nor waste time trying to find a graphics guy to come up with icons, images, etc. So I started searching for the images on the net. Of course I had to watch out for the licenses as well.

Next came the sound problem. Playing sounds inside the program couldn't be easier:

local sound = audio.loadSound( "your sound file")
audio.play(sound)


but finding background music, or drop sounds were the tough ones.

At the very end, we decided to name the game HAPPY PEPE. Here are some of the screenshots taken during game play:






On March 10th, I was done. Emir and Selin tested the app thoroughly and then we submitted to Apple for approval.

And finally on March 18th, the approval came from Apple.

You can find Happy Pepe at:




And of course, big thanks to Corona...

Mobile Development Showcase

Monday, February 28, 2011

Follow Twitter Trends in Turkey and the World

My latest iPhone app is Trendler ('Trends'). If you want to see what the latest trends are on Twitter for Turkey and the world, this is the app for it...



Tuesday, February 22, 2011

Share your location in case of Emergency: Acil PRO

I've been meaning to do this for a while but didn't have time to get around to it. Finally here it is:


Thursday, February 17, 2011

Hızlı ('Quick') SMS - Productivity iPhone Application

I send quite a bit of SMS messages during the day and they are usually same kind of messages: "I'm stuck in traffic", "shall we meet for lunch", "I'm in a meeting, will call back later", etc.
I wanted to automate this process and make it a couple of clicks affair. Also I really think I'm going to get into an accident if I continue typing sms messages while I drive...So here's the app:


Tuesday, June 15, 2010

Aile - Utility iPhone Application

I like to have easy access to some data such as frequent flier numbers, health insurance numbers, passport numbers, etc.. I had it in mind to write a small iPhone app to do just that for the whole family. Finally I found some time to do it.
You can get it here: