Guest User

Untitled

a guest
Oct 19th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. require 'active_support/inflector'
  2.  
  3. class TagTypeDecorator
  4. def self.has_tag_types(*types)
  5. types.each do |sym|
  6. define_method sym do
  7. taggable.tags.select { |t| t.tag_type == sym.to_s.singularize.capitalize }
  8. end
  9. end
  10. end
  11.  
  12. has_tag_types :sections, :propositions, :audiences
  13. attr_accessor :taggable
  14.  
  15. def initialize(taggable)
  16. raise "Isn't taggable" unless taggable.respond_to?(:tags)
  17.  
  18. @taggable = taggable
  19. end
  20.  
  21. def method_missing(method, *args)
  22. args.empty? ? taggable.send(method) : taggable.send(method, *args)
  23. end
  24. end
  25.  
  26. require 'minitest/autorun'
  27.  
  28. class DecoratorTest < MiniTest::Unit::TestCase
  29. class Tag
  30. attr_accessor :tag_id, :tag_type
  31. def initialize(attrs = {})
  32. attrs.each { |k, v| __send__("#{k}=", v) }
  33. end
  34. end
  35.  
  36. class SampleArtefact
  37. attr_accessor :stored_title
  38.  
  39. def title
  40. @stored_title ||= "Bizarre"
  41. @stored_title
  42. end
  43.  
  44. def title=(value)
  45. @stored_title = value
  46. end
  47.  
  48. def tags
  49. [
  50. Tag.new(tag_id: 'driving/cars', tag_type: 'Section'),
  51. Tag.new(tag_id: 'driving/bicycles', tag_type: 'Section'),
  52. Tag.new(tag_id: 'policemen', tag_type: 'Audience'),
  53. Tag.new(tag_id: 'business', tag_type: 'Proposition')
  54. ]
  55. end
  56. end
  57.  
  58. def setup
  59. @artefact = SampleArtefact.new
  60. @decorator = TagTypeDecorator.new(@artefact)
  61. end
  62.  
  63. def test_it_gives_access_to_sections
  64. assert_equal ['driving/cars', 'driving/bicycles'], @decorator.sections.collect(&:tag_id)
  65. end
  66.  
  67. def test_it_gives_access_to_propositions
  68. assert_equal ['business'], @decorator.propositions.collect(&:tag_id)
  69. end
  70.  
  71. def test_it_gives_access_to_audiences
  72. assert_equal ['policemen'], @decorator.audiences.collect(&:tag_id)
  73. end
  74.  
  75. def test_it_should_retain_access_to_raw_tags
  76. assert_equal 'driving/cars', @decorator.tags.first.tag_id
  77. end
  78.  
  79. def test_it_should_retain_access_to_artefact_title
  80. assert_equal 'Bizarre', @decorator.title
  81. end
  82.  
  83. def test_it_should_retain_access_to_artefact_title_setter
  84. @decorator.title= "Normal"
  85. assert_equal 'Normal', @decorator.title
  86. end
  87. end
Add Comment
Please, Sign In to add comment