• Update: there’s a new version of this post covering Devise 4.0.0: Show a devise log in or sign up forms in another page

    Devise create various forms, among them one for signing up and one for logging in of course. These are the forms as they are generated in Devise 1.0.8:

    <h2>Sign up</h2>
    
    <%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
      <%= f.error_messages %>
    
      <div>
        <%= f.label :email %><br>
        <%= f.text_field :email %>
      </div>
    
      <div>
        <%= f.label :password %><br>
        <%= f.password_field :password %>
      </div>
    
      <div>
        <%= f.label :password_confirmation %><br>
        <%= f.password_field :password_confirmation %>
      </div>
    
      <div>
        <%= f.submit "Sign up" %>
      </div>
    <% end %>
    
    <%= render partial: "shared/devise_links" %>

    and

    <h2>Sign in</h2>
    
    <%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
      <div>
        <%= f.label :email %><br>
        <%= f.text_field :email %>
      </div>
    
      <div>
        <%= f.label :password %><br>
        <%= f.password_field :password %>
      </div>
    
      <% if devise_mapping.rememberable? %>
        <div>
          <%= f.check_box :remember_me %>
          <%= f.label :remember_me %>
        </div>
      <% end %>
    
      <div>
        <%= f.submit "Sign in" %>
      </div>
    <% end %>
    
    <%= render partial: "shared/devise_links" %>

    If you try to put them somewhere else you’ll run into some problem. There are some variables/methods those forms use that you’ll be lacking, specifically: resource_name, resource and for logging in also devise_mapping. I’ve recently tried to put them both in the homepage for an upcoming project of mine and this is how I’ve solved it:

    module ContentHelper
      def resource_name
        :user
      end
    
      def resource
        @resource ||= User.new
      end
    
      def devise_mapping
        @devise_mapping ||= Devise.mappings[:user]
      end
    end
  • When I write software, bug reports and feature requests are a blessing. It shows me that people is using it and want more. Silence is death. It’s nice to see an software project show appreciation for bug reports and I’m honestly impressed:

    Dear NetBeans User,

    In the past you have taken the time to report issues that you encountered while using NetBeans software. A new version (NetBeans 6.9) has just been released,and we’d like to inform you that the following issue(s) you reported have been addressed in the new release:

    181509 Dot paths not available when adding new platform in Ruby Platform Manager
    181766 Gem directories picked wrongly
    181844 Search for RVM installations

    Please visit the netbeans.org website to download NetBeans 6.9 and to learn more about the new release.

    We appreciate your contribution to our efforts to make NetBeans software and features better for all users. And as always, we look forward to feedback from you about ways we can continue to improve.Thank you.

    The NetBeans Team

  • When I start coding a Ruby on Rails project, I find myself modifying the migration files over and over. I know this is not the way they were intended to use, but to avoid upfront design, I only ad fields when I need them. If I respected the way migrations were intended I would end up with hundred of migrations the first day and I would waste half my day just creating migrations.

    After a project is deployed or a second developer is working on it, I revert to the way migrations are intended and I create a new one every time there’s a change I need in the database.

    As migrations are intended to run only once, if you modify them, they won’t get run; and if you force them to run them, they’ll fail, because the database already contains such a table. So I’ve found myself doing this quite often:

    rake db:drop && rake db:create && rake db:migrate && rake db:seed && rake db:data

    db:data is a task I created to generate some sample data. Good known data that I can use to test the site locally. I’m using Factory Girl to create it, which I also use for the tests so I can re-use as much data creating logic as possible. It’s very good to get to a known state of the project you are developing and to get other developers started right away. I really recommend everyone doing it.

    The problem is that I also need to reset my test data, so I end up having this other command and it gets horrible:

    RAILS_ENV=test rake db:drop && RAILS_ENV=test rake db:create && RAILS_ENV=test rake db:migrate && RAILS_ENV=test rake db:seed

    Note: no db:data this time.

    I’ve got tired of re-writing these commands or trying to find them in my bash history, so I decided to write a Ruby task that will do it for me and here it is in case you want to use it too:

    namespace :db do
      desc "Crush and burn the database"
      task :hard_reset => :environment do
        File.delete("db/schema.rb")
        Rake::Task["db:drop"].execute
        Rake::Task["db:create"].execute
        Rake::Task["db:migrate"].execute
        Rake::Task["db:seed"].execute
        if !Rails.env.test?
          Rake::Task["db:data"].execute
        end
      end
    
      desc "Generate sample data for developing"
      task :data => :environment do
        # Create the sample data in here
      end
    end

    Enjoy!

    Update: To delete all records without resetting the whole database, check my post Deleting all records in a Rails project.

  • I consider the iPad a very negative force. Apple is taking the close system of iPhone and moving it up to tablets. What’s the next move? Desktops? That’s very scary! Imagine a world where Steve Jobs is the final word on which programs people can and can’t run. If that happens, the dark ages of computers will have started, and who knows how long they’ll last.

    Now there’s a very good side effect to the iPad. People is starting to spend a lot of time on non-Windows computers. No, this is not a rant against Microsoft.

    There was a time when we had many operating systems and people use to choose. Different machines came with different operating systems. Then Microsoft dominated the market and there was one and only one operating system. For a time there was even only one browser. That hinders innovation.

    Microsoft Windows obviously failed on phones and tablets. Apple, first with the iPhone and now with the iPad, opened those markets. Now people is talking about how great WebOS (Palm’s operating system) would be on a tablet (made by HP). Nobody would have though of a non-Windows tablet if Apple haven’t done it before. Also Android tablets are coming. Microsoft is rebooting its phone efforts, without Windows this time; and maybe they’ll reboot their tablet efforts too. At any rate Microsoft won’t dominate that market and certainly not Windows.

    The only company capable of dominating phones and tablets is Apple; and they are not set to dominate it. Apple makes one and only one product, with no variations. If you want something different (a USB port, a memory card reader, a camera, porn, a cheaper product, whatever) you go to a competitor. Apple makes a lot of money by making the luxury products, not by selling it to everybody.

    I think it’s going to be a lot of fun to watch a very competitive market where four players are so will be constantly innovating trying to win users over.

  • Paul Graham says that startup founders should be relentlessly resourceful. Well, the other day I needed a charger for my Sony PRS505, which was so empty that the USB plug wouldn’t charge it.

    We went through a big box of charges and none matched the voltage of the ebook reader. Well, there was one that matched it, but I needed a car to plug it in.

    In the box there was also a 12V charger. I grew up with McGyver, so a couple of wires and some tape latter I got it charging.

    Relentlessly resourceful!

  • I’m reading the book Refactoring and one of the refactorings it shows is called “Consolidate Duplicate Conditional Fragments” and it shows an example in Java:

    if (isSpecialDeal()) {
      total = price * 0.95;
      send();
    } else {
      total = price * 0.98;
      send();
    }

    is refactored into

    if (isSpecialDeal()) {
      total = price * 0.95;
    } else {
      total = price * 0.98;
    }
    send();

    If you do it in Python it’s actually quite similar:

    if isSpecialDeal():
      total = price * 0.95
      send()
    else:
      total = price * 0.98
      send()

    is refactored into

    if isSpecialDeal():
      total = price * 0.95
    else:
      total = price * 0.98
    send()

    But in Ruby it’s different. In Ruby, like in Lisp, everything is an expression, everything has a value (maybe there are exceptions, I haven’t found them). Let’s look at it in Ruby:

    if isSpecialDeal()
      total = price * 0.95
      send()
    else
      total = price * 0.98
      send()
    end

    is refactored into

    total = if isSpecialDeal()
      price * 0.95
    else
      price * 0.98
    end
    send()

    Or if you want it indented in another way:

    total = if isSpecialDeal()
                  price * 0.95
                else
                  price * 0.98
                end
    send()

    We can push it one step further:

    total = price * if isSpecialDeal()
                              0.95
                            else
                              0.98
                            end
    send()

    Of these three languages, only Ruby can manage to have inside each branch of the if only what changes depending on the condition and nothing else. In this simple case you could use the ternary operator, :?, but if the case wasn’t simple, Ruby would be at an advantage.

    I’m reading Refactoring: Ruby Edition next.

  • I’ve created a blog called:

    Science is cool

    It is my first try at a blog with a subject. The subject is, of course, science and coolness. Well, cool science. I find things related to science, every day, that are cool and I want to share them. Videos of experiments, conferences that are funny or inspiring. This is my way to share them with everybody.

    It’s also a statement in an age where myth and legend are jumping at us everywhere, we have to remember that science and technology brought us where we are and that science is cool.

    My web site was born around 1998 or 1999. Since then it has been many things, but for a while it has been a blog. My blog, my personal blog, where nothing I write is off-topic. Science is cool is my first try at a blog with a subject and that a blog that I intend to grow beyond a handful of friends.

    Of course, linking, sharing, re-tweeting and so on is very welcome.

  • I’ve just seen Avatar. I liked it, except for one thing.

    In Avatar there are two societies, one is technologically advanced and believes in science; the other is religious. Of course they gave some consistency to the religion, but it remains a religion. The technological society, the humans, are warmongers; while the spiritual society is peaceful. They go to war and the religious society wins. I don’t think that’s the right message.

    I’m a geek. I believe in reason. I believe in science. I believe in technology. I believe the human race will only survive if it stops taking myth and legend seriously and start seeking proof, learning, studying, researching, building. Look at medicine, people were dying of very simple deases a hundred years ago. Today we conquered a lot of them!

    The life expentansy is growing at the rate of one year every two years. If today the life expectancy is 80 years old, by the time I’m 80, it’ll be 106 years old. And that’s consider the growth of the life expectancy linear, it’s actually accelerating.

    The previous generation of science fiction authors dreamed of supercomputers in our pockets, being able to pick up a microphone and talk with anyone on the planet. We are living that and it’s great.

    Back to Avatar, for me a story that is much more worthy of being told is the one of Rama. In Rama there’s an alien civilization, extremely advanced and technological, and at the same time very pacific. They inhabit part of a huge ship while the humans inhabit another part. One day the stupid humans decide they want the whole ship. Maybe they were procreating too much and were overpopulated, go figure!

    Stop reading know if you intend to read Rama, spoilers ahead.

    They start invading the technological civilization. A selected group of the technological civilization gathers to save their race, they develop a virus that would kill adult human males; the group that was actually attacking them. In a couple of hours, the war is over, every human adult male is dead and peace returns.

    The individuals of the advanced civilization who participated in the extermination, all commit suicide. It’s part of their law: those that participate in war must kill themselves at the end, even the leaders. Nobody that causes the death of other beings is fit to return to the society.

    How many soldiers would enlist if they knew that after returning from a tour, what awaits them is suicide? Very few. How many wars would we have in the world if those declaring them would have to blow their brains out at the end of it? None.

  • It seems iPad lack of Flash support is the debate of the moment. On one camp: “You can’t use the web without Flash”, on the other camp: “We don’t need no stinking Flash”. Although I do realize how a technology like Flash is sometimes needed, I’m more on the second camp. The less flash, the better.

    I think iPad’s lack of Flash cause two things to happen:

    • Slow down the adoption of the iPad: surely someone will say “No Flash, No iPad“.
    • Speed up the adoption of HTML5: surely someone will consider using HTML5 to support the tablet.

    Giving that the iPad is a closed device, probably the closedest non-phone computer consumers ever had access to and that HTML5 is good progress for the web, I consider both results of the iPad not having Flash positive. If I have to say anything about it, it’d be: please, stop trying to wake Steve Jobs up regarding this, you’ll ruin it.