Thursday, September 12, 2013

Deploying Rails APP resources

The end of internal combustion engines

I was exploring new Tesla model S videos, resources, reading about the company goals and future. It is now safe to say that internal combustion engines are going away and electric cars are replacing them big time.

Why is the Tesla Motors the one who will lead this revolution?

Apart from Tesla Motors we have couple of other attempts of exploring electric cars from Nissan with Lief model and Volt from Chevrolet. And of course we also have hybrid cars primarily from Toyota, being in production for a while now. But that's what it is, exploring the market, technology etc. Non of them really committed themselves to a electric car direction or at least department. All they wanted to do is try to prepare themselves if at some point electric cars replace existing ones. And that is fine too.

Tesla Motors made a bold move by throwing away everything we have built so far in terms of engines and started a new direction solely focused on electric motors. And while they were doing it, they picked a perfect name for it - Tesla.

OK, so we have a committed company, determined to make electric cars who won't play by rules of others and let  oil companies in the way. But how are they going to do it? Well, steps they are doing so far make perfect sense to me.

  1. Build a usable prototype to show the world that this can be done - Tesla Roadster - check.
  2. Build large sedan that can be mass produces at reasonable price and compeete with other luxury sedans in all ways possible including travel range. Sell it to high end customers to show off. Tesla Model S - check.
  3. Create a smaller family car for masses - in progress and looking forward to it!

How do people react? And how do oil companies react?

Timing for something like this is perfect. When you look at the Tesla car you won't see any significant technology changes that we haven't had before. Car runs with a electric motor and supplies electricity from batteries similar to the ones we have in our laptops. So, we had this technology for a while so why anyone didn't built this earlier? They did, I know about few smaller attempts either by individuals or small companies but they couldn't place this in the correct way to the eyes of public as Tesla Motors did.

Now when it's out there how do people react? I have to admit I am a fan of cars and love the sound of high performance engines. But what I love more that that is the future and being able to live in it, myself and my kids. So anyone with average intelligence will embrace this at least because of  the environment. I can't imagine how my city would look like of none of the cars would produce noise and CO2. Really looking forward to that. The second reason why people like myself and other people from YouTube videos love this car is the fact it's better than any other internal combustion car period. Here's why
  1. Faster than Maserati and BMW M from 0 - 60 mph
  2. Instant acceleration available at anytime 
  3. Lighter, more space inside anywhere
  4. Better aerodynamics
  5. And on top of all that, reasonable price and you drive it for free (no oil, no spare parts etc)
The only concern about electric cars we all had is the oil companies preventing this from happening. Now these are all the things kept in secret since money is involved but we all know that they won't just give up on the oil empire they made. After all some countries depend on it since that's all they have/do. 
The good news is, today we live in the time where there's so much competition everywhere and people are open minded and aware that they have all the power in their hands, not the individuals with the money. 
I'm sure that building a charging stations is being prevented here and there but that's just short term. 


Car as it should be

Model S is finally a sample of a technology really put to use and now when I look at it the only things that's crossing my mind is "this is how the car should be built". We've lived so long in the era of combustion engines and really pushed that to the limits. We have extraordinary results with cars topping amazing speeds and accelerations. That took us many many years to master. Now, put any latest high brand sport car next to the Model S drag race. Well, bye bye internal combustion. Tesla did it with few years of work.

So, let's talk about the car. The essence of this car is electric motor, batteries and parts necessary for actually driving it. Electric engine is the size of watermelon and batteries at the car bottom. There's no big engine, filters, pumps, gears, oil tank etc.  Great! Now when the car essence is stripped to the bare minimum this opens a whole new set of options for us in terms of design and usability.

What is Tesla doing wrong?

As Tesla states they had to build a car that will people relate to in terms of looks and everything. I agree with that since if they built something extreme people would reject it. The usual car looks is so deeply embedded in our minds that we couldn't accept anything different. It's a step by step process and we'll get there. Here's what should Tesla do:
  • Play with the car representation. Cars we have today are completely built around engine and that one single part basically defined everything. Now when it's gone you can do anything with it. Car doesn't have to look like a - car.
  • Improve batteries. Easier to be said than do you'd say. But when I heard that batteries are just Lithium Ions ones I thought that's wrong. Experiments with new materials like Graphene show exceptional characteristics of batteries. One small battery could power Model S and not to be charged for months. 
  • Finally, as you are wearing the name of Tesla, you should listen to the man. World power supply should be taken from the ground. Not home or station power plugs. Ok, this is extreme, at least for us right now but it makes sense. Let's narrow it down only to the cars. Anywhere you go you'r on the road. Well, let's make road our power supply. 



Sunday, February 12, 2012

Dynamically create and populate select boxes for awesome_nested_set gem rails 3


Here is quick overview how I made dynamically creating and populating select fields, instead of using one select to display all levels. I am using it for selecting one category when creating a new user. gem awesome_nested_set

View code (new user)

<div id="user_category_select"> 
<span id="loading" style="display:none;">Loading...</span>
<%= f.select :user_category_id, category_children(@categories), { :include_blank => true } %>
</div>


Helper

def category_children(objects)
    if(objects)
      output = Array.new
      objects.each do |o|
        output << [o.name, o.id]
      end
      output
    end
  end


controller

def new
      @categories = UserCategory.roots
end
def get_nested_categories
    if(params[:id])
      @categories = UserCategory.find(params[:id]).children
      if(!@categories.empty?)
        respond_to do |format|
          format.js
        end
      else
        render :nothing => true
      end
    else
      render :nothing => true
    end
  end


Javascript

// get actual values from first select
    var real_id = $('#user_category_select select').attr("id");
    var real_name = $('#user_category_select select').attr("name");

    $('#user_category_select select').live('change', function(){
        remove_select_after($(this).next("select"));
        if($(this).val()) {
            manage_select_class($("#user_category_select select"), $(this), real_id, real_name);
            $("#user_category_select #loading").show();
            $.get("get_nested_categories",
                    { id: $(this).val() }, function() { $("#user_category_select #loading").hide(); }, "script");
        } else {
            manage_select_class($("#user_category_select select"), $(this).prev("select"), real_id, real_name);
        }
    });

    // remove all next selects to generate new
    function remove_select_after(object) {
        if(object.length) {
            var next = object.next("select");
            object.remove();
            if(next.length) remove_select_after(next);
        }
    }

    // remove all ids and names and add to current one
    // since there is just one category value to send
    function manage_select_class(remove, assign, id, name) {
        remove.attr("name", ""); remove.attr("id", "");
        assign.attr("name", name); assign.attr("id", id);
    }

get_nested_categories.js

$("#user_category_select").append("<%= escape_javascript(select :not, :selected, category_children(@categories), { :include_blank => true }) %> ");

I am sure that someone could re-factor some code better, especially JS, so fell free to post it in comments