Skip to content

Instantly share code, notes, and snippets.

Created November 28, 2011 10:50
Show Gist options
  • Save anonymous/1399960 to your computer and use it in GitHub Desktop.
Save anonymous/1399960 to your computer and use it in GitHub Desktop.
class Category < ActiveRecord::Base
has_many :posts, :dependent => :destroy
has_many :tags, :through => :posts, :uniq => true
has_one :navigation_item, :as => :navigatable
before_create :set_navigation_item
private
def set_navigation_item
self.navigation_item = NavigationItem.new(:name => name) if navigation_item.nil?
end
end
require "spec_helper"
describe "NavigationItem" do
it "should have categories" do
n = NavigationItem.new(:name => "huch", :position => 1)
n.navigatable = Category.new(:name => "huch")
n.save
Category.find_by_name("huch").navigation_item.should == n
end
it "should have categories moep" do
n = Category.new(:name => "huch")
n.navigation_item = NavigationItem.new(:name => "huch", :position => 1)
n.save
NavigationItem.find_by_name("huch").should == n.navigation_item
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment