Guest User

Untitled

a guest
Feb 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. require 'dm-migrations'
  2. require 'migration_runner'
  3. module DataMapper
  4. module Types
  5. class TagCollection < DataMapper::Type
  6. primitive String
  7. def self.load(value, property)
  8. value.split(', ').to_a
  9. end
  10.  
  11. def self.dump(value, property)
  12. result = typecast(value, property)
  13. result.nil? ? result : result.join(', ')
  14. end
  15.  
  16. def self.typecast(value, property)
  17. case value
  18. when nil: nil
  19. when String; value.split(', ').to_a.uniq
  20. when Array ; value.uniq
  21. when Set ; value.to_a
  22. else ; raise ArgumentError.new \
  23. "+value+ must be nil String, Set or Array"
  24. end
  25. end
  26. end # class Tag
  27. end # module Types
  28. end # module DataMapper
  29.  
  30. class Post
  31. property :categories, TagCollection
  32. property :tags , TagCollection
  33. def self.elements(type)
  34. elements = Set.new
  35. p all
  36. all.each {|r| p r;p r.tags;elements.merge(r.tags)}
  37. return elements.to_a
  38. end
  39. end
  40. include Sinatra::RenderingHelpers
  41. include Sinatra::Erb
  42.  
  43. template :thing do
  44. <<-HAML
  45. <% for category in Post.elements(:tags) %>
  46. <li><%= category %></li>
  47. <% end %>
  48. HAML
  49. end
  50. Plugins::Views.menu ||= erb(:thing)
Add Comment
Please, Sign In to add comment