Tag Archives: container

Containers in the path

Coinciding with the submission of a paper for the ws://rest.2010, several controller methods have been refactorized.

We have made an intensive analysis of use cases of containers in the path, in cases like:


/spaces/global/events/meeting/agenda

/groups/3/tasks/4/turns

The following methods are now supported:

path_containers will return an array of all the containers in the path. In the last examples, well get [ spaces-global, events-meeting ] and [ groups-3, tasks-4 ]
current_container will search for a container both in path containers, as well as in the containers of current resource. Consider this example:


/spaces/global/events/meeting/agenda/agenda_entries/2
/events/meetings/agenda_entries/2
/agenda_entries/2

current_container(:type => :space) #=> returns space-global in all the former cases

Import Feeds to your Containers

Station provides basic (and still in development) support for Atom/RSS feeds. You can import feeds to your Containers and create new Contents from each Atom/RSS element.

A Container is any model that give context to other models. Lets take the following example:

class Space
  has_many :posts
end

The has_many relation indicates Space is probably a Container.  If a posts always belongs_to a space, then you have it. Space is a Container.

Station provides an easy way to import feeds to your Containers. This is achieved by the Sources support.

Add the sources table to your database.
The sources table is defined in station/db/migrate/station_2_to_3.rb and will be included in the next version of the Station migration schema.

Indicate Station that your Space is a Container and it supports Sources.

Just add the suitable option to the container declaration:

class Space
  has_many :posts
  acts_as_container :sources => true
end

Write the Atom Parser in your models

You’ll have to describe the conversion between the source feed elements and your models. This is easily achieved using the params_from_atom function


class Posts
  def self.params_from_atom(entry)
    { :title => entry.title.to_s,
       :text => entry.content.to_s }
  end
end

And that’s it! Now you can visit /spaces/1/sources and automatically add new Posts to your Spaces.

Getting several AR objects in one query: ActiveRecord::Content::Inquirer

There are some issues already included in Station I want to blog, because I think they are interesting.
One of them is ActiveRecord::Content::Inquirer

In Station, a container is an ActiveRecord model that have many contents, like posts, images, videos, bookmarks, etc.. What if you want to obtain all the contents in a Container using just one SQL query? And you also want to paginate them? And, of course, getting back instances of every Content!

ActiveRecord::Content::Inquirer does just this. You use it:


ActiveRecord::Content::Inquirer.all :container => somespace,
                                    :page => params[:page],
                                    :per_page => 10

It even accepts an Array in :container.

Some of the magic is under this hack:


class Inquirer < ActiveRecord::Base
  @colums = Array.new
  @columns_hash = { "type" => :fake }


  ...
end

This way, it’s possible to use ActiveRecord::Base goodies without a real table in the database.