Skip to content

Instantly share code, notes, and snippets.

Created December 11, 2008 12:25
Show Gist options
  • Save anonymous/34679 to your computer and use it in GitHub Desktop.
Save anonymous/34679 to your computer and use it in GitHub Desktop.
require 'dm-migrations'
require 'migration_runner'
module DataMapper
module Types
class TagCollection < DataMapper::Type
primitive String
def self.load(value, property)
value.split(', ').to_a
end
def self.dump(value, property)
result = typecast(value, property)
result.nil? ? result : result.join(', ')
end
def self.typecast(value, property)
case value
when nil: nil
when String; value.split(', ').to_a.uniq
when Array ; value.uniq
when Set ; value.to_a
else ; raise ArgumentError.new \
"+value+ must be nil String, Set or Array"
end
end
end # class Tag
end # module Types
end # module DataMapper
class Post
property :categories, TagCollection
property :tags , TagCollection
def self.elements(type)
elements = Set.new
p all
all.each {|r| elements.merge(r.tags)}
return elements.to_a
end
end
include Sinatra::RenderingHelpers
include Sinatra::Erb
template :thing do
<<-HAML
<% for category in Post.elements(:tags) %>
<li><%= category %></li>
<% end %>
HAML
end
Plugins::Views.menu ||= erb(:thing)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment