No Pressure

May 22

Mongoid recursively embedding

This is a note for myself for a project I am actually working on that I thought could be useful to share :)

More info here.

CYCLIC RELATIONS on MONGODB WITH MONGOID

A document can recursively embed itself using recursively_embeds_many, which provides accessors for the parent and children.

class Tag
  include Mongoid::Document
  recursively_embeds_many
  field :name
end

programming = Tag.new(:name => 'programming')
ruby = programming.child_tags.build(:name => 'ruby')
python = programming.child_tags.build(:name => 'python')

programming.child_tags  # [ruby, python]
ruby.parent_tag         # [programming]
python.parent_tag       # [programming]

Updating embedded child objects when the parent object is saved.

Mongoid has added a tag which automatically makes callbacks for embedded child objects when the parent object is saved.

You can find it at the end of the mongoid documentation on embedding.

class Post
  include
Mongoid::Document
  include
Mongoid::Timestamps
  include
Mongoid::Paranoia

  embeds_many
:comments, cascade_callbacks: true
  accepts_nested_attributes_for
:comments
end

May 18

“I am truly free only when all human beings, men and women, are equally free. The freedom of other men, far from negating or limiting my freedom, is, on the contrary, its necessary premise and confirmation. It is the slavery of other men that sets up a barrier to my freedom, or what amounts to the same thing, it is their bestiality which is the negation of my humanity. For my dignity as a man, my human right which consists of refusing to obey any other man, and to determine my own acts in conformity with my convictions is reflected by the equally free conscience of all and confirmed by the consent of all humanity. My personal freedom, confirmed by the liberty of all, extends to infinity.” — Mikhail Bakunin - Bakunin on Anarchy

May 17

“Do I contradict myself? Very well, then I contradict myself, I am large, I contain multitudes.” — Whitman

May 16

“I find fascinating how people protect the right of the richest for fear of loosing what they don’t have but are told they might get someday…” — my self :)

May 14

“If you go where everybody else is you might just end up doing the same thing… if you think about it Bob Marley didn’t go to London or New York to play Rock music, he stayed in Jamaica to invent Reggae…” — myself :)

Nested hash in ruby

Ever wondered how to create nested hash in ruby?

Here it is:

1.9.3p125 :001 > a = Hash.new{|h,k| h[k]=Hash.new(&h.default_proc) }

 => {} 

1.9.3p125 :002 > a[2][1] = 2

 => 2 

1.9.3p125 :003 > a[2][2][3] = 2

 => 2 

1.9.3p125 :004 > a[2][2][4] = 5

 => 5 

1.9.3p125 :005 > a[3][1][1][1] = 7

 => 7 

1.9.3p125 :006 > a

 => {2=>{1=>2, 2=>{3=>2, 4=>5}}, 3=>{1=>{1=>{1=>7}}}} 

:) sweet!

Mar 26

Devise + Mongoid + Cancan

I have thought a while about making a summary post about configuring devise + mongoid + cancan for creating accounts and implementing authentication in a rails app.

First of all there are various guides that you can use to get you started. All of them can give you a different insight on the technology and a different perspective and what is needed to accomplish whatever you have in mind :).

So feel free to google for devise + cancan and you end up reading Tony Amoyal’s blog post. It is a very good post description and pointed me toward the right direction.

If you want a different point of view though (also considering that I am using mongoid), here is my dirty summary on how to configure an app that would use these gems:

First of all install devise:

Devise 2.0 works with Rails 3.1 onwards. You can add it to your Gemfile with: gem ‘devise’ or run the bundle command.

After devise has been installed, run the generator:

rails generate devise:install

The generator installs an initializer describing All devise’s configuration options and it is advised to have a look at it. Then add devise to your models using the generator:

rails generate devise MODEL 

More details are available herehttps://github.com/plataformatec/devise; together with links to some example app that could be extended to suit your needs.

Now let’s install cancan. In Rails 2 you can add it to your Gemfile and or run the bundle command:

gem "cancan"

Continue with using the generator for creating the Ability class for a User:

rails g cancan:ability

Simple as that. You can find out more here.

Now how should you model your User and Admin accounts? You can do this with a single user class, or you can do it with different classes :).

I personally prefer to use different classes to keep the two roles separately.

Mar 10

otacon22 asked: Hai fatto parte del POuL un po' di tempo fa? :-)

Si :)

Anche tu? Ci conosciamo? Il mio nick era Hiromi.

Feb 21

How to load a feed with Google Feed API and JQuery

Here is a quick and dirty way to load a xml feed with Google Feed API with Jquery. Enjoy!! :)

First step include the necessary external calls:

<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js”</script>

<script type=”text/javascript” src=”https://www.google.com/jsapi”></script>

Second step your javascript function:

<script>

    /*

*  How to load a feed via the Feeds API.

*/

google.load(“feeds”, “1”);

// Our callback function, for when a feed is loaded.

function feedLoaded(result) {

 if (!result.error) {

    // Grab the container we will put the results into

    var container = $(“#latest”);

    $(“#latest”).empty();

    html = “<div><ul>”;

   // Loop through the feeds, putting the titles onto the page.

    // Check out the result object for a list of properties returned in each entry.

   // http://code.google.com/apis/ajaxfeeds/documentation/reference.html#JSON

    for (var i = 0; i < result.feed.entries.length; i++) {

     var entry = result.feed.entries[i];

     html += “<li>” 

          + entry.title 

        + “</li>”;

    }

    html += “</ul></div> “;

    $(“#latest”).append(html);

  }

}

function OnLoad() {

 // Create a feed instance that will grab Digg’s feed.

 var feed = new google.feeds.Feed(“https://gdata.youtube.com/feeds/api/users/devinsupertramp/uploads”);

 // Calling load sends the request off.  It requires a callback function.

 feed.load(feedLoaded);

}

google.setOnLoadCallback(OnLoad);

</script>

Third and last step, add a div into your code to contain the results:

<div id=”latest”></div>

Super quick!! Enjoy!!