my_annotations 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. data/.gitignore +6 -0
  2. data/.rvmrc +1 -0
  3. data/AUTHORS.rdoc +5 -0
  4. data/CHANGELOG.rdoc +64 -0
  5. data/INDEX.rdoc +17 -0
  6. data/generators/annotations_migration/annotations_migration_generator.rb +32 -0
  7. data/generators/annotations_migration/templates/migration_v1.rb +60 -0
  8. data/generators/annotations_migration/templates/migration_v2.rb +9 -0
  9. data/generators/annotations_migration/templates/migration_v3.rb +74 -0
  10. data/generators/annotations_migration/templates/migration_v4.rb +13 -0
  11. data/install.rb +1 -0
  12. data/lib/annotations/acts_as_annotatable.rb +271 -0
  13. data/lib/annotations/acts_as_annotation_source.rb +117 -0
  14. data/lib/annotations/acts_as_annotation_value.rb +115 -0
  15. data/lib/annotations/config.rb +148 -0
  16. data/lib/annotations/routing.rb +8 -0
  17. data/lib/annotations/util.rb +82 -0
  18. data/lib/annotations_version_fu.rb +119 -0
  19. data/lib/app/controllers/annotations_controller.rb +162 -0
  20. data/lib/app/controllers/application_controller.rb +2 -0
  21. data/lib/app/helpers/application_helper.rb +2 -0
  22. data/lib/app/models/annotation.rb +413 -0
  23. data/lib/app/models/annotation_attribute.rb +37 -0
  24. data/lib/app/models/annotation_value_seed.rb +48 -0
  25. data/lib/app/models/number_value.rb +23 -0
  26. data/lib/app/models/text_value.rb +23 -0
  27. data/my_annotations.gemspec +4 -9
  28. data/rails/init.rb +8 -0
  29. data/test/acts_as_annotatable_test.rb +186 -0
  30. data/test/acts_as_annotation_source_test.rb +84 -0
  31. data/test/acts_as_annotation_value_test.rb +17 -0
  32. data/test/annotation_attribute_test.rb +22 -0
  33. data/test/annotation_test.rb +213 -0
  34. data/test/annotation_value_seed_test.rb +14 -0
  35. data/test/annotation_version_test.rb +39 -0
  36. data/test/annotations_controller_test.rb +27 -0
  37. data/test/app_root/app/controllers/application_controller.rb +9 -0
  38. data/test/app_root/app/models/book.rb +5 -0
  39. data/test/app_root/app/models/chapter.rb +5 -0
  40. data/test/app_root/app/models/group.rb +3 -0
  41. data/test/app_root/app/models/tag.rb +6 -0
  42. data/test/app_root/app/models/user.rb +3 -0
  43. data/test/app_root/app/views/annotations/edit.html.erb +12 -0
  44. data/test/app_root/app/views/annotations/index.html.erb +1 -0
  45. data/test/app_root/app/views/annotations/new.html.erb +11 -0
  46. data/test/app_root/app/views/annotations/show.html.erb +3 -0
  47. data/test/app_root/config/boot.rb +115 -0
  48. data/test/app_root/config/environment.rb +16 -0
  49. data/test/app_root/config/environments/mysql.rb +0 -0
  50. data/test/app_root/config/routes.rb +4 -0
  51. data/test/app_root/db/migrate/001_create_test_models.rb +38 -0
  52. data/test/app_root/db/migrate/002_annotations_migration_v1.rb +60 -0
  53. data/test/app_root/db/migrate/003_annotations_migration_v2.rb +9 -0
  54. data/test/app_root/db/migrate/004_annotations_migration_v3.rb +72 -0
  55. data/test/config_test.rb +383 -0
  56. data/test/fixtures/annotation_attributes.yml +49 -0
  57. data/test/fixtures/annotation_value_seeds.csv +16 -0
  58. data/test/fixtures/annotation_versions.yml +259 -0
  59. data/test/fixtures/annotations.yml +239 -0
  60. data/test/fixtures/books.yml +13 -0
  61. data/test/fixtures/chapters.yml +27 -0
  62. data/test/fixtures/groups.yml +7 -0
  63. data/test/fixtures/number_value_versions.csv +2 -0
  64. data/test/fixtures/number_values.csv +2 -0
  65. data/test/fixtures/text_value_versions.csv +35 -0
  66. data/test/fixtures/text_values.csv +35 -0
  67. data/test/fixtures/users.yml +8 -0
  68. data/test/number_value_version_test.rb +40 -0
  69. data/test/routing_test.rb +27 -0
  70. data/test/test_helper.rb +41 -0
  71. data/test/text_value_version_test.rb +40 -0
  72. metadata +77 -7
@@ -0,0 +1,186 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class ActsAsAnnotatableTest < ActiveSupport::TestCase
4
+
5
+ def test_has_many_annotations_association
6
+ assert_equal 6, books(:h).annotations.length
7
+ assert_equal 5, books(:r).annotations.length
8
+ assert_equal 2, chapters(:bh_c10).annotations.length
9
+ assert_equal 4, chapters(:br_c2).annotations.length
10
+ end
11
+
12
+ def test_find_annotations_for_class_method
13
+ assert_equal 6, Book.find_annotations_for(books(:h).id).length
14
+ assert_equal 6, Book.find_annotations_for(books(:h).id, true).length
15
+ assert_equal 5, Book.find_annotations_for(books(:r).id).length
16
+ assert_equal 5, Book.find_annotations_for(books(:r).id, true).length
17
+ assert_equal 2, Chapter.find_annotations_for(chapters(:bh_c10).id).length
18
+ assert_equal 2, Chapter.find_annotations_for(chapters(:bh_c10).id, true).length
19
+ assert_equal 4, Chapter.find_annotations_for(chapters(:br_c2).id).length
20
+ assert_equal 4, Chapter.find_annotations_for(chapters(:br_c2).id, true).length
21
+ end
22
+
23
+ def test_find_annotations_by_class_method
24
+ assert_equal 4, Book.find_annotations_by("User", users(:jane)).length
25
+ assert_equal 4, Book.find_annotations_by("User", users(:jane), true).length
26
+ assert_equal 1, Book.find_annotations_by("Group", groups(:sci_fi_geeks)).length
27
+ assert_equal 1, Book.find_annotations_by("Group", groups(:sci_fi_geeks), true).length
28
+ assert_equal 3, Chapter.find_annotations_by("User", users(:john)).length
29
+ assert_equal 3, Chapter.find_annotations_by("User", users(:john), true).length
30
+ assert_equal 2, Chapter.find_annotations_by("Group", groups(:classical_fans)).length
31
+ assert_equal 2, Chapter.find_annotations_by("Group", groups(:classical_fans), true).length
32
+ end
33
+
34
+ def test_annotatable_name_field_class_attribute
35
+ h = books(:h)
36
+
37
+ hs = Book.find(:all, :conditions => { Book.annotatable_name_field.to_sym => h.title })
38
+
39
+ assert !hs.empty?
40
+ end
41
+
42
+ def test_annotatable_name_instance_method
43
+ assert_equal "Learning Ruby in 2 Seconds", books(:r).annotatable_name
44
+ assert_equal "Hashing It Up", chapters(:br_c2).annotatable_name
45
+ end
46
+
47
+ def test_latest_annotations_instance_method
48
+ assert_equal 6, books(:h).latest_annotations.length
49
+ assert_equal 6, books(:h).latest_annotations(nil, true).length
50
+ assert_equal 2, chapters(:bh_c10).latest_annotations.length
51
+ assert_equal 2, chapters(:bh_c10).latest_annotations(nil, true).length
52
+
53
+ assert_equal 2, books(:h).latest_annotations(2).length
54
+ assert_equal 2, books(:h).latest_annotations(2, true).length
55
+ end
56
+
57
+ def test_annotations_with_attribute_instance_method
58
+ assert_equal 2, books(:h).annotations_with_attribute("tag").length
59
+ assert_equal 2, books(:h).annotations_with_attribute("tag", true).length
60
+ assert_equal 0, books(:r).annotations_with_attribute("doesnt_exist").length
61
+ assert_equal 0, books(:r).annotations_with_attribute("doesnt_exist", true).length
62
+ assert_equal 1, chapters(:bh_c10).annotations_with_attribute("endingType").length
63
+ assert_equal 1, chapters(:bh_c10).annotations_with_attribute("endingType", true).length
64
+ assert_equal 1, chapters(:br_c202).annotations_with_attribute("Title").length
65
+ assert_equal 1, chapters(:br_c202).annotations_with_attribute("Title", true).length
66
+ end
67
+
68
+ def test_annotations_with_attributes_instance_method
69
+ assert_equal 4, books(:h).annotations_with_attributes([ "tag", "summary", "LENGTH" ]).length
70
+ assert_equal 4, books(:h).annotations_with_attributes([ "tag", "summary", "LENGTH" ], true).length
71
+ assert_equal 0, books(:h).annotations_with_attributes([ "doesnt_exist", "also doesn't exist" ]).length
72
+ assert_equal 0, books(:h).annotations_with_attributes([ "doesnt_exist", "also doesn't exist" ], true).length
73
+ assert_equal 1, chapters(:bh_c10).annotations_with_attributes([ "endingType" ]).length
74
+ assert_equal 1, chapters(:bh_c10).annotations_with_attributes([ "endingType" ], true).length
75
+ assert_equal 2, chapters(:br_c202).annotations_with_attributes([ "Title", "complexity", "doesn't exist but still"]).length
76
+ assert_equal 2, chapters(:br_c202).annotations_with_attributes([ "Title", "complexity", "doesn't exist but still"], true).length
77
+ end
78
+
79
+ def test_annotations_with_attribute_and_by_source_instance_method
80
+ assert_equal 1, books(:h).annotations_with_attribute_and_by_source("tag", users(:jane)).length
81
+ assert_equal 1, books(:h).annotations_with_attribute_and_by_source("tag", users(:jane), true).length
82
+ assert_equal 0, books(:r).annotations_with_attribute_and_by_source("doesnt_exist", users(:jane)).length
83
+ assert_equal 0, books(:r).annotations_with_attribute_and_by_source("doesnt_exist", users(:jane), true).length
84
+ assert_equal 1, chapters(:bh_c10).annotations_with_attribute_and_by_source("endingType", groups(:sci_fi_geeks)).length
85
+ assert_equal 1, chapters(:bh_c10).annotations_with_attribute_and_by_source("endingType", groups(:sci_fi_geeks), true).length
86
+ assert_equal 1, chapters(:br_c202).annotations_with_attribute_and_by_source("Title", users(:john)).length
87
+ assert_equal 1, chapters(:br_c202).annotations_with_attribute_and_by_source("Title", users(:john), true).length
88
+ end
89
+
90
+ def test_all_annotations_excluding_attributes
91
+ assert_equal 4, books(:h).all_annotations_excluding_attributes([ "TITLE", "length" ]).length
92
+ assert_equal 4, books(:h).all_annotations_excluding_attributes([ "TITLE", "length" ], true).length
93
+ assert_equal 5, books(:r).all_annotations_excluding_attributes([ "doesnt_exist" ]).length
94
+ assert_equal 5, books(:r).all_annotations_excluding_attributes([ "doesnt_exist" ], true).length
95
+ assert_equal 1, chapters(:bh_c10).all_annotations_excluding_attributes([ "endingType" ]).length
96
+ assert_equal 1, chapters(:bh_c10).all_annotations_excluding_attributes([ "endingType" ], true).length
97
+ assert_equal 2, chapters(:br_c202).all_annotations_excluding_attributes([ "tag", "doesn't exist but who cares" ]).length
98
+ assert_equal 2, chapters(:br_c202).all_annotations_excluding_attributes([ "tag", "doesn't exist but who cares" ], true).length
99
+ end
100
+
101
+ def test_count_annotations_by_instance_method
102
+ assert_equal 6, books(:h).count_annotations_by("all")
103
+ assert_equal 2, books(:h).count_annotations_by("Group")
104
+ assert_equal 4, chapters(:br_c2).count_annotations_by("All")
105
+ assert_equal 3, chapters(:br_c2).count_annotations_by("User")
106
+ end
107
+
108
+ def test_create_annotations_instance_method
109
+ bk = Book.create
110
+
111
+ data1 = {
112
+ :test1 => "test123",
113
+ "test2" => nil,
114
+ " test3" => "",
115
+ :foo => 1,
116
+ :bar => [ "one", "two", 3, "", nil ]
117
+ }
118
+
119
+ anns1 = bk.create_annotations(data1, users(:jane))
120
+
121
+ assert_equal 5, anns1.length
122
+ anns1.each do |a|
123
+ assert_kind_of Annotation, a
124
+ end
125
+ assert_equal 5, bk.annotations.length
126
+
127
+
128
+ data2 = { :tagx => "ko", :tagy => [ "oii", "tuo" ] }
129
+
130
+ anns2 = bk.create_annotations(data2, users(:jane))
131
+
132
+ assert_equal 3, anns2.length
133
+ anns2.each do |a|
134
+ assert_kind_of Annotation, a
135
+ end
136
+ assert_equal 8, bk.annotations(true).length
137
+
138
+
139
+ data3 = { :tagx => "ko", :tagy => [ "oii", "piopjpjnp" ] }
140
+
141
+ anns3 = bk.create_annotations(data3, users(:jane))
142
+
143
+ assert_equal 1, anns3.length
144
+ anns2.each do |a|
145
+ assert_kind_of Annotation, a
146
+ end
147
+ assert_equal 9, bk.annotations(true).length
148
+ end
149
+
150
+ def test_adding_of_annotation
151
+ ch = chapters(:bh_c10)
152
+ assert_equal 2, ch.annotations.length
153
+ ann1 = Annotation.new(:attribute_id => AnnotationAttribute.find_or_create_by_name("tag").id,
154
+ :source_type => "User",
155
+ :source_id => 1)
156
+ ann1.value = "test"
157
+ ch.annotations << ann1
158
+
159
+ ann2 = Annotation.new(:attribute_name => "description",
160
+ :source_type => "User",
161
+ :source_id => 2)
162
+ ann2.value = "test2"
163
+ ch.annotations << ann2
164
+
165
+ assert_not_nil(ann1)
166
+ assert_not_nil(ann2)
167
+ assert_equal 4, ch.annotations(true).length
168
+ end
169
+
170
+ def test_annotations_hash_method
171
+ book1 = books(:h)
172
+ expected_hash1 = {
173
+ "Summary" => "Something interesting happens",
174
+ "length" => 345,
175
+ "Title" => "Harry Potter and the Exploding Men's Locker Room",
176
+ "Tag" => [ "wizadry", "amusing rhetoric" ],
177
+ "rating" => "4/5"
178
+ }
179
+ assert_equal expected_hash1, book1.annotations_hash
180
+
181
+ book2 = Book.create(:title => "YAB")
182
+ expected_hash2 = { }
183
+ assert_equal expected_hash2, book2.annotations_hash
184
+ end
185
+
186
+ end
@@ -0,0 +1,84 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class ActsAsAnnotationSourceTest < ActiveSupport::TestCase
4
+
5
+ def test_has_many_annotations_association
6
+ assert_equal 7, users(:john).annotations_by.length
7
+ assert_equal 6, users(:jane).annotations_by.length
8
+ assert_equal 3, groups(:sci_fi_geeks).annotations_by.length
9
+ assert_equal 4, groups(:classical_fans).annotations_by.length
10
+ end
11
+
12
+ def test_annotations_by_class_method
13
+ assert_equal 7, User.annotations_by(users(:john).id).length
14
+ assert_equal 7, User.annotations_by(users(:john).id, true).length
15
+ assert_equal 6, User.annotations_by(users(:jane).id).length
16
+ assert_equal 6, User.annotations_by(users(:jane).id, true).length
17
+ assert_equal 3, Group.annotations_by(groups(:sci_fi_geeks).id).length
18
+ assert_equal 3, Group.annotations_by(groups(:sci_fi_geeks).id, true).length
19
+ assert_equal 4, Group.annotations_by(groups(:classical_fans).id).length
20
+ assert_equal 4, Group.annotations_by(groups(:classical_fans).id, true).length
21
+ end
22
+
23
+ def test_annotations_for_class_method
24
+ assert_equal 4, User.annotations_for("Book", books(:h).id).length
25
+ assert_equal 4, User.annotations_for("Book", books(:h).id, true).length
26
+ assert_equal 1, User.annotations_for("Chapter", chapters(:bh_c10).id).length
27
+ assert_equal 1, User.annotations_for("Chapter", chapters(:bh_c10).id, true).length
28
+ assert_equal 1, Group.annotations_for("Book", books(:r).id).length
29
+ assert_equal 1, Group.annotations_for("Book", books(:r).id, true).length
30
+ assert_equal 1, Group.annotations_for("Chapter", chapters(:br_c2).id).length
31
+ assert_equal 1, Group.annotations_for("Chapter", chapters(:br_c2).id, true).length
32
+ end
33
+
34
+ def test_latest_annotations_instance_method
35
+ assert_equal 6, users(:jane).latest_annotations.length
36
+ assert_equal 6, users(:jane).latest_annotations(nil, true).length
37
+ assert_equal 3, groups(:sci_fi_geeks).latest_annotations.length
38
+ assert_equal 3, groups(:sci_fi_geeks).latest_annotations(nil, true).length
39
+
40
+ assert_equal 3, users(:john).latest_annotations(3).length
41
+ assert_equal 3, users(:john).latest_annotations(3, true).length
42
+ end
43
+
44
+ def test_annotation_source_name_instance_method
45
+ assert_equal "john", users(:john).annotation_source_name
46
+ assert_equal "Classical Fans", groups(:classical_fans).annotation_source_name
47
+ end
48
+
49
+ def test_adding_of_annotation
50
+ us = users(:jane)
51
+ assert_equal 6, us.annotations_by.length
52
+ ann1 = Annotation.new(:attribute_id => AnnotationAttribute.find_or_create_by_name("tag").id,
53
+ :annotatable_type => "Book",
54
+ :annotatable_id => 1)
55
+ ann1.value = "test"
56
+ us.annotations_by << ann1
57
+
58
+ ann2 = Annotation.new(:attribute_name => "description",
59
+ :annotatable_type => "Book",
60
+ :annotatable_id => 2)
61
+ ann2.value = "test2"
62
+ us.annotations_by << ann2
63
+
64
+ assert_not_nil(ann1)
65
+ assert_not_nil(ann2)
66
+ assert_equal 8, us.annotations_by(true).length
67
+ end
68
+
69
+ def test_annotations_by_hash_method
70
+ user1 = users(:jane)
71
+ expected_hash1 = {
72
+ "Tag" => [ "programming", "wizadry" ],
73
+ "Note" => "Remember to buy milk!",
74
+ "Title" => [ "Ruby Hashes", "And It All Falls Down" ],
75
+ "rating" => "4/5"
76
+ }
77
+ assert_equal expected_hash1, user1.annotations_by_hash
78
+
79
+ user2 = User.create(:name => "Jim")
80
+ expected_hash2 = { }
81
+ assert_equal expected_hash2, user2.annotations_by_hash
82
+ end
83
+
84
+ end
@@ -0,0 +1,17 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class ActsAsAnnotationValueTest < ActiveSupport::TestCase
4
+
5
+ # TODO: duplication tests!!
6
+
7
+ def test_ann_content_setter
8
+ val = Annotation.first.value
9
+
10
+ new_content = "hellow world, testing test_ann_content_setter"
11
+
12
+ val.ann_content = new_content
13
+
14
+ assert_equal new_content, val.send(val.class.ann_value_content_field)
15
+ end
16
+
17
+ end
@@ -0,0 +1,22 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class AnnotationAttributeTest < ActiveSupport::TestCase
4
+
5
+ def test_annotation_attribute_class_loaded
6
+ assert_kind_of AnnotationAttribute, AnnotationAttribute.new
7
+ end
8
+
9
+ def test has_many_annotations_association
10
+ assert_equal 6, annotations_attributes(:aa_tag).annotations.length
11
+ assert_equal 1, annotations_attributes(:aa_contextualtag).annotations.length
12
+ end
13
+
14
+ def test_add_duplicates
15
+ assert_not_nil AnnotationAttribute.find_by_name("tag")
16
+
17
+ assert_raise ActiveRecord::RecordInvalid do
18
+ AnnotationAttribute.create!(:name => 'Tag')
19
+ end
20
+ end
21
+
22
+ end
@@ -0,0 +1,213 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class AnnotationTest < ActiveSupport::TestCase
4
+
5
+ def test_annotation_class_loaded
6
+ assert_kind_of Annotation, Annotation.new
7
+ end
8
+
9
+ def test_fixtures_loaded
10
+ assert_equal 2, Book.count(:all)
11
+ assert_equal 4, Chapter.count(:all)
12
+ assert_equal 2, User.count(:all)
13
+ assert_equal 2, Group.count(:all)
14
+ assert_equal 20, Annotation.count(:all)
15
+
16
+ assert_equal 1, books(:h).id
17
+ assert_equal 2, chapters(:bh_c10).id
18
+ assert_equal 2, users(:jane).id
19
+ assert_equal 1, groups(:sci_fi_geeks).id
20
+ #assert_equal 2, annotations(:bh_length_1).id # Doesn't work due to the autonumbering used in the annotations fixtures.
21
+ end
22
+
23
+ def test_belongs_to_annotatable_association
24
+ assert_equal books(:h), annotations(:bh_summary_1).annotatable
25
+ assert_equal books(:r), annotations(:br_author_1).annotatable
26
+ assert_equal chapters(:bh_c10), annotations(:bh_c10_title_1).annotatable
27
+ assert_equal chapters(:br_c2), annotations(:br_c2_tag_2).annotatable
28
+ end
29
+
30
+ def test_belongs_to_source_association
31
+ assert_equal users(:john), annotations(:bh_title_1).source
32
+ assert_equal users(:jane), annotations(:br_c2_title_2).source
33
+ assert_equal groups(:sci_fi_geeks), annotations(:br_summary_2).source
34
+ assert_equal groups(:classical_fans), annotations(:br_c202_tag_1).source
35
+ end
36
+
37
+ def test_belongs_to_value_association
38
+ assert_equal TextValue.find(33), annotations(:br_c202_title_1).value
39
+ assert_equal TextValue.find(23), annotations(:br_author_1).value
40
+ assert_equal TextValue.find(19), annotations(:bh_tag_1).value
41
+ assert_equal NumberValue.find(1), annotations(:bh_length_1).value
42
+ end
43
+
44
+ def test_belongs_to_attribute_association
45
+ assert_equal annotation_attributes(:aa_length), annotations(:bh_length_1).attribute
46
+ assert_equal annotation_attributes(:aa_tag), annotations(:br_tag_1).attribute
47
+ end
48
+
49
+ def test_include_values_named_scope
50
+ assert_equal 20, Annotation.include_values.length
51
+ assert_not_nil Annotation.include_values.find(annotations(:bh_title_1).id)
52
+ end
53
+
54
+ def test_by_source_named_scope_finder
55
+ assert_equal 7, Annotation.by_source('User', users(:john).id).length
56
+ assert_equal 6, Annotation.by_source('User', users(:jane).id).length
57
+ assert_equal 3, Annotation.by_source('Group', groups(:sci_fi_geeks).id).length
58
+ assert_equal 4, Annotation.by_source('Group', groups(:classical_fans).id).length
59
+ end
60
+
61
+ def test_for_annotatable_named_scope_finder
62
+ assert_equal 6, Annotation.for_annotatable('Book', books(:h).id).length
63
+ assert_equal 5, Annotation.for_annotatable('Book', books(:r).id).length
64
+ assert_equal 3, Annotation.for_annotatable('Chapter', chapters(:br_c202).id).length
65
+ assert_equal 0, Annotation.for_annotatable('Chapter', chapters(:bh_c1).id).length
66
+ end
67
+
68
+ def test_with_attribute_name_named_scope_finder
69
+ assert_equal 6, Annotation.with_attribute_name('tag').length
70
+ assert_equal 5, Annotation.with_attribute_name('title').length
71
+ assert_equal 1, Annotation.with_attribute_name('note').length
72
+ assert_equal 0, Annotation.with_attribute_name('does_not_exist_zzzzzz').length
73
+ end
74
+
75
+ def test_find_annotatable_class_method
76
+ assert_equal books(:h), Annotation.find_annotatable('Book', books(:h).id)
77
+ assert_equal books(:r), Annotation.find_annotatable('Book', books(:r).id)
78
+ assert_equal chapters(:bh_c10), Annotation.find_annotatable('Chapter', chapters(:bh_c10).id)
79
+ assert_equal chapters(:br_c2), Annotation.find_annotatable('Chapter', chapters(:br_c2).id)
80
+ end
81
+
82
+ def test_attribute_name_getter
83
+ assert_equal "tag", annotations(:bh_tag_2).attribute_name.downcase
84
+ assert_equal "title", annotations(:bh_c10_title_1).attribute_name.downcase
85
+ end
86
+
87
+ def test_attribute_name_setter
88
+ attr_name = "Testing attribute name setter"
89
+
90
+ ann = Annotation.new
91
+ ann.attribute_name = attr_name
92
+
93
+ attr = AnnotationAttribute.find_by_name(attr_name)
94
+
95
+ assert_equal ann.attribute, attr
96
+ end
97
+
98
+ def test_annotation_create_with_implicit_value
99
+ source = users(:john)
100
+
101
+ ann = Annotation.new(:attribute_name => "tag",
102
+ :value => "hot",
103
+ :source_type => source.class.name,
104
+ :source_id => source.id,
105
+ :annotatable_type => "Book",
106
+ :annotatable_id => 1)
107
+
108
+ assert ann.valid?
109
+ assert ann.save
110
+
111
+ ann_again = Annotation.find_by_id(ann.id)
112
+
113
+ assert_not_nil ann_again
114
+ assert_equal "User", ann_again.source_type
115
+ assert_equal "TextValue", ann_again.value_type
116
+ assert_not_nil ann_again.value
117
+ assert_equal "hot", ann_again.value_content
118
+ end
119
+
120
+ def test_annotation_create_with_explicit_value
121
+ source = users(:john)
122
+
123
+ val = NumberValue.new :number => 0
124
+
125
+ ann = Annotation.new(:attribute_name => "rating",
126
+ :value => val,
127
+ :source_type => source.class.name,
128
+ :source_id => source.id,
129
+ :annotatable_type => "Book",
130
+ :annotatable_id => 1)
131
+
132
+ assert ann.valid?
133
+ assert ann.save
134
+
135
+ ann_again = Annotation.find_by_id(ann.id)
136
+
137
+ assert_not_nil ann_again
138
+ assert_equal "User", ann_again.source_type
139
+ assert_equal "NumberValue", ann_again.value_type
140
+ assert_not_nil ann_again.value
141
+ assert_equal 0, ann_again.value_content
142
+ end
143
+
144
+ def test_cannot_create_annotation_with_invalid_annotatable
145
+ source = users(:john)
146
+
147
+ ann1 = Annotation.new(:attribute_name => "tag",
148
+ :value => "hot",
149
+ :source_type => source.class.name,
150
+ :source_id => source.id,
151
+ :annotatable_type => "Book",
152
+ :annotatable_id => 100)
153
+
154
+ assert ann1.invalid? # TODO: check for the specific error, not just that it's invalid!
155
+ assert !ann1.save
156
+
157
+ ann2 = Annotation.new(:attribute_name => "tag",
158
+ :value => "hot",
159
+ :source_type => source.class.name,
160
+ :source_id => source.id,
161
+ :annotatable_type => "Whale",
162
+ :annotatable_id => 1)
163
+
164
+ assert ann2.invalid? # TODO: check for the specific error, not just that it's invalid!
165
+ assert !ann2.save
166
+ end
167
+
168
+ def test_cannot_create_annotation_with_invalid_source
169
+ bk = books(:h)
170
+
171
+ ann1 = Annotation.new(:attribute_name => "tag",
172
+ :value => "hot",
173
+ :source_type => "User",
174
+ :source_id => 100,
175
+ :annotatable_type => bk.class.name,
176
+ :annotatable_id => bk.id)
177
+
178
+ assert ann1.invalid? # TODO: check for the specific error, not just that it's invalid!
179
+ assert !ann1.save
180
+
181
+ ann2 = Annotation.new(:attribute_name => "tag",
182
+ :value => "hot",
183
+ :source_type => "Monkey",
184
+ :source_id => 1,
185
+ :annotatable_type => bk.class.name,
186
+ :annotatable_id => bk.id)
187
+
188
+ assert ann2.invalid? # TODO: check for the specific error, not just that it's invalid!
189
+ assert !ann2.save
190
+ end
191
+
192
+ def test_cannot_create_annotation_with_invalid_value
193
+ bk = books(:h)
194
+ source = users(:john)
195
+
196
+ ann = Annotation.new(:attribute_name => "tag")
197
+ ann.annotatable = bk
198
+ ann.source = source
199
+ ann.value = nil
200
+
201
+ assert ann.invalid? # TODO: check for the specific error, not just that it's invalid!
202
+ assert !ann.save
203
+
204
+ ann.value = bk
205
+
206
+ assert ann.invalid? # TODO: check for the specific error, not just that it's invalid!
207
+ assert !ann.save
208
+ end
209
+
210
+ def test_reload_versioned_columns
211
+ assert Annotation.reload_versioned_columns_info
212
+ end
213
+ end
@@ -0,0 +1,14 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class AnnotationValueSeedTest < ActiveSupport::TestCase
4
+
5
+ def test_annotation_value_seed_class_loaded
6
+ assert_kind_of AnnotationValueSeed, AnnotationValueSeed.new
7
+ end
8
+
9
+ def test_find_by_key
10
+ assert_equal 10, AnnotationValueSeed.find_by_attribute_name("tag").length
11
+ assert_equal 5, AnnotationValueSeed.find_by_attribute_name("Author").length
12
+ end
13
+
14
+ end
@@ -0,0 +1,39 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class AnnotationVersionTest < ActiveSupport::TestCase
4
+
5
+ def test_annotation_version_class_loaded
6
+ assert_kind_of Annotation::Version, Annotation::Version.new
7
+ end
8
+
9
+ def test_versioning_on_update
10
+ ann = annotations(:bh_title_1)
11
+ orig_value_id = ann.value_id
12
+ orig_content = ann.value_content
13
+ new_content = "Harry Potter IIIIIII"
14
+
15
+ # Check number of versions
16
+ assert_equal 1, ann.versions.length
17
+
18
+ # Update the value and check that a version has been created
19
+
20
+ ann.value = new_content
21
+ ann.version_creator = users(:john)
22
+
23
+ assert ann.valid?
24
+
25
+ assert ann.save
26
+
27
+ assert_equal 2, ann.versions.length
28
+ assert_equal new_content, ann.value_content
29
+ assert_equal 2, ann.versions.latest.version
30
+ assert_equal new_content, ann.versions.latest.value_content
31
+ assert_equal 1, ann.versions.latest.previous.version
32
+ assert_equal orig_content, ann.versions.latest.previous.value_content
33
+ assert_equal orig_value_id, ann.versions.latest.previous.value_id
34
+ assert_equal users(:john).id, ann.version_creator_id
35
+ assert_equal users(:john).id, ann.versions.latest.version_creator_id
36
+ assert_equal nil, ann.versions.latest.previous.version_creator_id
37
+ end
38
+
39
+ end
@@ -0,0 +1,27 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+ require 'annotations_controller'
3
+ require 'action_controller/test_process'
4
+
5
+ # Manually override the rescue_action in the controller to raise the exception back.
6
+ class AnnotationsController; def rescue_action(e) raise e end; end
7
+
8
+ class AnnotationsControllerTest < ActionController::TestCase
9
+ def setup
10
+ ActionController::Routing::Routes.draw do |map|
11
+ Annotations.map_routes(map)
12
+ end
13
+
14
+ @controller = AnnotationsController.new
15
+ @request = ActionController::TestRequest.new
16
+ @response = ActionController::TestResponse.new
17
+ end
18
+
19
+ def test_current_user_is available
20
+ assert_not_nil @controller.current_user
21
+ end
22
+
23
+ def test_index
24
+ get :index
25
+ assert_response :success
26
+ end
27
+ end
@@ -0,0 +1,9 @@
1
+ class ApplicationController < ActionController::Base
2
+ def logged_in?
3
+ true
4
+ end
5
+
6
+ def current_user
7
+ @current_user ||= User.find(1)
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ class Book < ActiveRecord::Base
2
+ acts_as_annotatable :name_field => :title
3
+
4
+ has_many :chapters
5
+ end
@@ -0,0 +1,5 @@
1
+ class Chapter < ActiveRecord::Base
2
+ acts_as_annotatable :name_field => :title
3
+
4
+ belongs_to :book
5
+ end
@@ -0,0 +1,3 @@
1
+ class Group < ActiveRecord::Base
2
+ acts_as_annotation_source
3
+ end
@@ -0,0 +1,6 @@
1
+ class Tag < ActiveRecord::Base
2
+ acts_as_annotation_value :content_field => :name
3
+
4
+ validates_presence_of :name
5
+ validates_uniqueness_of :name
6
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ acts_as_annotation_source
3
+ end
@@ -0,0 +1,12 @@
1
+ <h1>Editing annotation</h1>
2
+
3
+ <% form_for(@annotation) do |f| %>
4
+ <%= f.error_messages %>
5
+
6
+ <p>
7
+ <%= f.submit "Update" %>
8
+ </p>
9
+ <% end %>
10
+
11
+ <%= link_to 'Show', @annotation %> |
12
+ <%= link_to 'Back', annotations_path %>
@@ -0,0 +1 @@
1
+ <%= pluralize @annotations.length, "annotation" -%>
@@ -0,0 +1,11 @@
1
+ <h1>New annotation</h1>
2
+
3
+ <% form_for(@annotation) do |f| %>
4
+ <%= f.error_messages %>
5
+
6
+ <p>
7
+ <%= f.submit "Create" %>
8
+ </p>
9
+ <% end %>
10
+
11
+ <%= link_to 'Back', annotations_path %>