Skip to content
This repository has been archived by the owner on Dec 27, 2022. It is now read-only.

Commit

Permalink
quick tagging fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lusis committed Apr 11, 2011
1 parent 293c86f commit 3110020
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/noah/linkable.rb
Expand Up @@ -7,7 +7,7 @@ def self.included(model)
def link!(link_name)
link = Noah::Link.find_or_create(:path => link_name)
link.nodes = self
self.links << link
links << link
end

def unlink!(link_name)
Expand Down
2 changes: 2 additions & 0 deletions lib/noah/models/link.rb
Expand Up @@ -19,9 +19,11 @@ def nodes=(node)
when "Array"
node.each do |n|
self.key[:nodes].sadd(n.key)
n.links << self
end
else
self.key[:nodes].sadd(node.key)
n.links << self
end
end

Expand Down
30 changes: 26 additions & 4 deletions lib/noah/models/tags.rb
@@ -1,6 +1,7 @@
module Noah
class Tag < Model
attribute :name
attribute :members
index :name

def validate
Expand All @@ -9,10 +10,25 @@ def validate
assert_unique :name
end

def tagged(tag)
# TODO:
#logic to find all models with a given tag
#will need hooks added to taggable module
def members=(member)
self.key[:members].sadd(member.key)
end

def members
hsh = Hash.new
self.key[:members].smembers.each do |member|
n = node_to_class(member)
cls = class_to_lower(n.class.to_s)
hash_key = "#{cls}s".to_sym
hsh[hash_key] = Array.new unless hsh.has_key?(hash_key)
hsh[hash_key] << n.name
end
hsh
end

def self.tagged(tag)
t = find(:name => tag).first
t.members
end

class <<self
Expand All @@ -28,6 +44,12 @@ def find_or_create(opts={})
end
end
end

private
def node_to_class(node)
node.match(/^Noah::(.*):(\d+)$/)
Noah.const_get($1).send(:[], $2)
end
end

end
8 changes: 6 additions & 2 deletions lib/noah/taggable.rb
Expand Up @@ -8,10 +8,14 @@ def tag!(tag_name)
case tag_name.class.to_s
when "Array"
tag_name.each do |t|
tags << ::Noah::Tag.find_or_create(:name => t)
my_tag = ::Noah::Tag.find_or_create(:name => t)
tags << my_tag
my_tag.members = self
end
else
tags << Noah::Tag.find_or_create(:name => tag_name)
my_tag = ::Noah::Tag.find_or_create(:name => tag_name)
tags << my_tag
my_tag.members = self
end
end

Expand Down

0 comments on commit 3110020

Please sign in to comment.