Marco Tapia

my way to blog about development

September 19, 2011
by firebait
0 comments

Edit on the Fly with Simply_Edit

Hello all,

I just uploaded a gem that lets you updated fields on the fly, is called simply_edit. Is the sister gem of simply_load that you can find on rubygems and github.

Simply edit transforms labels into editable fields like textfield, textareas and datefield(using jQuery date picker).

Why did I make a gem out of this little thing? I felt it was a waste of time to got to a form if you only needed to update a title, content or a date.

Here is the gem at rubygems and github.

Anyway, I hope you guys like it and comment about it.

July 14, 2011
by firebait
0 comments

A Gem Called “simply_load”

Hello all,

Just yesterday, I released a gem called “simply_load”.

Simply load is really just a short way to write a JQuery load method to populate a page.

Why did I make a gem out of this little thing? I just thought it was fun to load pieces of html at random or have a little form widget without having to actually instantiate the object on a completely unrelated controller method.

Here is the gem at rubygems and github.

Anyway, I hope you guys like it and comment about it.

June 30, 2011
by firebait
0 comments

Do not create methods from table records with Ruby Metaprogramming

I recently got the opportunity to integrate the Devise gem with Cancan for authentication and authorization.
These gems are a real time saver and one really good thing about these is the ability to customize them especially Cancan. Cancan doesn’t enforce a model on you or a data structure for it to work, you can define the role table as you will and plug it in to your app.

But this blog is not intended to be a blog about authorization and authentication is more about using metaprogramming as a tool for generating methods from table records.
In my particular case I used a Roles table with a name field, pretty standard. My first instinct was to loop thru the records in the Roles table and generate a method called “is_role_name?”. This was my implementation.

Role.all.each do |role|
   define_method("is_#{role.name.downcase}?") do
      !roles.find_by_name(role).nil?
   end
end

This worked really well as I could query a user and just ask for user.is_admin? and that would be it. The problem happened when I wanted to test it, because the database get’s deleted, all of the seed data for the default roles get deleted as well. If I tried to create them after the fact, the User model was already loaded so the methods never get created. Even worst if I went and drop the database and tried to create it, it would blow up saying that there is no Roles table when it’s loading the User model (there is a clever way around this, just put this piece of code after the Roles.all.each loop and the methods won’t get created if there is no Roles table: if ActiveRecord::Base.connection.tables.include?('roles'). So, no good.

I had to go back to the traditional way of querying roles and use user.has_role?(admin) which if fine if you want to keep thing simple. But my goal was to call the roles using the role name inside the method so method_missing to the rescue.

  def has_role?(role)
    !roles.find_by_name(role).nil?
  end

  def method_missing(method, *args)
    return has_role?($1.to_s, *args) if method =~ /^is_+(.*)\?/
    super
  end

This way you can still user the format user.is_admin? to call your roles and not have the issue of creating methods with seed data or table records.

Hope this helps.

September 15, 2010
by firebait
1 Comment

Prototype Ajax.Response with Rails Json

Json is just way faster than updating partials with Rails, so why didn’t I use it before?
Well, basically I was just new to the Rails thing and it’s just easy to update with a partial. But I was presented with a choice recently, I had to update a resource intensive Google Map part of the page with an textfield keyup event.
At first glance it was cake, just observe the keyup event and send an Ajax call to update the partial. That got really slow since I had to create the map object everytime.
The other answer that you might be thinking is why didn’t he just update it with the page element in rails from the controller.
Is not that simple, I wanted to write this as separated from the backend as possible as I want to share this as an API in the future, so having view updates in the backend or in a partial was not going to cut it.
The task was simple, it was just moving the map location and setting a marker, it’s literally two lines of code so why not just pass a Json response from the rails backend and update the map.
Seems easy until I found that I couldn’t do the following:

				new Ajax.Request(url, {
					parameters: {query: this.getValue()},
					method: 'get',
					onSuccess: function(transport,){
						var response = transport.responseText;
						alert(response.somevalue);
					}
				});

I will always get the somevalue undefined. I also found that it was because even though I would send the response as json, for some reason it was in json format but not an actual json object.

This was my rendering line:

     render :json => "{somevalue:#{my_value.to_json}}"

I found that I only needed to change this line:

     var response = transport.responseText.evalJSON();

And that was how I got my Json working after an hour of googling. I found it on the prototype api documentation.
Read the manual kids!!!
Firebait

September 7, 2010
by firebait
0 comments

Facebooker on a Tab

Hello all,

As you know I’ve been playing with facebooker for the past few days and I want my app to work on both canvas and tab.
This is the first thing you have to do in order to get your app on a tab.
Apply these before filters:

      before_filter :check_authentication
      before_filter :check_installation
      before_filter :get_user

      private

      def check_authentication
        ensure_authenticated_to_facebook unless request_is_facebook_tab?
      end

      def check_installation
        ensure_application_is_installed_by_facebook_user unless request_is_facebook_tab?
      end

      def get_user
        if request_is_facebook_tab?
          @facebook_user = Facebooker::User.new(params['fb_sig_profile_id'],Facebooker::Session.create)
        else
          @facebook_user = facebook_session.user
        end
        @user = User.find_or_create_by_uid(@facebook_user.uid)
      end

As you can see I had to manually retrieve the fb_sig_profile_id from the request in order to make this work. If someone found what is the equivalent in facebooker I will really appreciate it.

Firebait

September 7, 2010
by firebait
0 comments

Facebooker: Filter chain halted as [:ensure_authenticated_to_facebook]

Hello everyone, this is my first post, and I will write a very short one. I’ve been playing with the facebooker plugin for Rails and I followed this tutorial and got it up and running pretty fast. The only issue I had was I kept getting this error:

Filter chain halted as [:ensure_authenticated_to_facebook] rendered_or_redirected.

When I used this before_filters:

ensure_authenticated_to_facebook
ensure_application_is_installed_by_facebook_user

I spend a day running around just to find that I needed to add this piece of code to my application_controller file or configure your “Post-Authorize Redirect” with out trailing “/”.

def after_facebook_login_url
     request.request_uri
end

Hope this helps all of you that are trying to write rails apps for facebook
Firebait out!

March 3, 2010
by firebait
0 comments

Bienvenidos a mi Blog

This is my First post. Just to let you know that I’m going to start blogging about the different stuff I’m coding so expect some updates soon.
I’m currently working on a Facebook app so my first posts are about Facebooker, a really cool framework for developing Facebook apps with Ruby on Rails.
Besides that, I really like writing CMS systems, and I hope to sometime have the time to write and open source version.
By for now.
Firebait!