category snippets

Recurse through a Ruby tree

Tagged recursive, tree, category, ruby  Languages css

The model:

class Category
...
  def recurse
    yield(self)

    children.each do |child|
      child.recurse {|sibling| yield sibling}
    end
  end
end

The recusion:

Category.root.recurse do |child|
  puts child
end

How to implement collapsible content (folders, categories, jimmy the cat) with jQuery

Tagged collapse, expand, jquery, folder, category  Languages javascript

This here is a quick-and-dirty implementation of folders for jQuery:

Markup

Any markup following the naming conventions should work, for example:

<ul class="collapsible">
  <li>
     <a href="" class="toggle-button">This be the folder</a>
    <ul style="display:none" class="collapsible-content">
        <li>This be the folder content</li>
        <li>This be the folder content</li>
    </ul>
  </li>
</ul>

JavaScript

/**
 * Collapsible categories implementation.
 * 
 */
var category = {
  find_category: function(target) {
    var category = target;
    // This isn't the category, so look for it
    if(!target.hasClass('collapsible')) {
      category = target.closest('.collapsible');
    }
    return category;
  },
  collapse: function(target) {
    var category = this.find_category(target);
    category.find('.collapsible-content').hide();
    category.removeClass('expanded');
    category.addClass('collapsed');
  },
  expand: function(target) {
    var category = this.find_category(target);
    category.find('.collapsible-content').show();
    category.addClass('expanded');
    category.removeClass('collapsed');
  },
  toggle: function(target) {
    var category = this.find_category(target);

    var is_expanded = category.hasClass('expanded');

    if(is_expanded) {
      this.collapse(category);
    } else {
      this.expand(category);
    }
  }
}

Usage

$('.toggle-button').live('click', function() {
  category.toggle($(this));
  return false;
});

Atom template Rails builder template

Tagged atom, builder, category, tags  Languages ruby
atom_feed(:url => formatted_posts_url(:atom)) do |feed|
  feed.title(@category.name)
  feed.updated(@posts.first ? @posts.first.created_at : Time.now.utc)

  for post in @posts
    feed.entry(post) do |entry|
      entry.title(post.title)
      entry.content(post.body_html, :type => 'html')
      entry.updated post.updated_at

      for tag in post.tags
        entry.category :term => url_for(tag), :label => tag.name
      end
    end
  end
end