views:

594

answers:

2

Hi, rails newb here.

I want to create a navigation system for my web application where there are several main categories for articles and several sub tags for each category.

For example, in category "Writing" there might be subcategories like "Essays," "Poetry" and "Fiction."

Each article would have a list of tags. If someone clicks the tag "Italy" on the fiction category show page I wouldn't want articles with the same tag of "Italy" from other categories like Essays or Poetry to appear on that page.

When a tag is clicked, the show page would be a category show page with a tag filter for all the articles with that particular tag in that category. It would not be a Tag show page.

Also, I'd like a sidebar with related tags. These would be tags that are in the same articles as those displayed on the category show page in descending order. I got related tags to work on the Tag page itself but not with the category constraint.

So far, I can get category pages and tag pages to work seperately, but I'd like to integrate the two.

I'm currently using Acts as Taggable on Steroids for the Tags and Acts as Tree for the categories.

Any ideas?

+1  A: 

I think you are mixing two concepts here, a least in the question, perhaps not in your application. You have a category list modeled with acts_as_tree. That supports subcategories. The tags should not belong to subcategories. You want to filter by tag and category, more or less. You should be able to write simple queries that return lists of articles, such as:

@articles = Article.find(:all, 
        :include => "categories"
        :conditions => ["(category_id = ? OR categories.parent_id = ?) AND tag_id = ?",
                         category_id, category_id, tag_id])

One more complex approach that I have used to do this is by doing faceted navigation with Solr. It supports combining these filters with full text search.

MattMcKnight
A: 

Matt, thanks a lot! This seems a lot simpler than what I had originally thought of.

However, my goal was to include subcategories in the navigation. The idea is that the parent categories would have everything in the subcategories and that if you clicked the sub categories it would narrow it down. I think that I'll check out Solr thought and see if it makes my code simpler.

After a bit of trial and error I got my code to work. It seems messy though. Any suggestions on simplification?

BTW some of these methods are defined through AATOS

In categories view#show:

<% for article in @articles %>

  <%= link_to article.name, article %>
  <% unless article.tag_list.empty? %>
  <p class="tags">
   Tags:
   <%= render :partial  => article.tags %>
   </p>
  <% end %>

And for the tag/navigation sidebar I have:

<% tag_cloud @tags, %w(css1 css2 css3 css4) do |tag, css_class| %>
<%= link_to tag, category_path(:filter => tag.name), :class => css_class %>
<% end %>

The category model, (not ideal but it works)

class Category < ActiveRecord::Base
acts_as_tree
has_many :articles
has_many :child_articles, :through => :children, :source => :articles

def grand_articles
children.map {|c| c.child_articles}.flatten.uniq
end

def to_param
name
end

def family
child_articles + articles + grand_articles
end

end

Finally the ugly controller code:

class CategoriesController < ApplicationController

 def show
 @category = Category.find_by_name(params[:id])
 if (params[:filter]).nil?
 @articles = @category.family
 else
 @articles = Article.find_tagged_with(params[:filter],
 :conditions => ["articles.id IN (?)", @category.family])
 end
 @tags = 
 if (params[:filter]).nil?
 Article.tag_counts :conditions => ["articles.id IN (?)", @category.family]
 else
 Article.find_related_tags(params[:filter],
 :conditions => ["tags.id IN (?)",  @category.family.map {|a| a.tags}.flatten.uniq])
 end

It works but again it's ugly and brittle code. I'm sure I've committed some terrible sins here. Any suggestions for cleaning it up is appreciated.

Kenji Crosland