bookmark_system 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +7 -0
  2. data/.empty +0 -0
  3. data/.gitignore +15 -0
  4. data/.keep +0 -0
  5. data/.rspec +2 -0
  6. data/.ruby-gemset +1 -0
  7. data/.ruby-version +1 -0
  8. data/.travis.yml +23 -0
  9. data/Appraisals +7 -0
  10. data/Gemfile +4 -0
  11. data/LICENSE.txt +22 -0
  12. data/README.md +93 -0
  13. data/Rakefile +8 -0
  14. data/bookmark_system.gemspec +31 -0
  15. data/gemfiles/.empty +0 -0
  16. data/gemfiles/.gitignore +0 -0
  17. data/gemfiles/.keep +0 -0
  18. data/gemfiles/rails4.1.gemfile +7 -0
  19. data/gemfiles/rails4.2.gemfile +7 -0
  20. data/lib/.empty +0 -0
  21. data/lib/.gitignore +0 -0
  22. data/lib/.keep +0 -0
  23. data/lib/bookmark_system.rb +45 -0
  24. data/lib/bookmark_system/.empty +0 -0
  25. data/lib/bookmark_system/.gitignore +0 -0
  26. data/lib/bookmark_system/.keep +0 -0
  27. data/lib/bookmark_system/bookmark.rb +155 -0
  28. data/lib/bookmark_system/bookmarkee.rb +58 -0
  29. data/lib/bookmark_system/bookmarker.rb +88 -0
  30. data/lib/bookmark_system/version.rb +12 -0
  31. data/lib/generators/.empty +0 -0
  32. data/lib/generators/.gitignore +0 -0
  33. data/lib/generators/.keep +0 -0
  34. data/lib/generators/bookmark_system/.empty +0 -0
  35. data/lib/generators/bookmark_system/.gitignore +0 -0
  36. data/lib/generators/bookmark_system/.keep +0 -0
  37. data/lib/generators/bookmark_system/bookmark_system_generator.rb +46 -0
  38. data/lib/generators/bookmark_system/templates/.empty +0 -0
  39. data/lib/generators/bookmark_system/templates/.gitignore +0 -0
  40. data/lib/generators/bookmark_system/templates/.keep +0 -0
  41. data/lib/generators/bookmark_system/templates/migration.rb +47 -0
  42. data/spec/.empty +0 -0
  43. data/spec/.gitignore +0 -0
  44. data/spec/.keep +0 -0
  45. data/spec/bookmark_system/.empty +0 -0
  46. data/spec/bookmark_system/.gitignore +0 -0
  47. data/spec/bookmark_system/.keep +0 -0
  48. data/spec/bookmark_system/bookmark_spec.rb +177 -0
  49. data/spec/bookmark_system/bookmarkee_spec.rb +69 -0
  50. data/spec/bookmark_system/bookmarker_spec.rb +96 -0
  51. data/spec/db/.empty +0 -0
  52. data/spec/db/.gitignore +0 -0
  53. data/spec/db/.keep +0 -0
  54. data/spec/db/migrate/.empty +0 -0
  55. data/spec/db/migrate/.gitignore +0 -0
  56. data/spec/db/migrate/.keep +0 -0
  57. data/spec/db/migrate/20140926000000_create_bookmarks.rb +47 -0
  58. data/spec/db/migrate/20140926000005_create_dummy_bookmarkers.rb +22 -0
  59. data/spec/db/migrate/20140926000010_create_dummy_bookmarkees.rb +22 -0
  60. data/spec/spec_helper.rb +116 -0
  61. data/spec/support/.empty +0 -0
  62. data/spec/support/.gitignore +0 -0
  63. data/spec/support/.keep +0 -0
  64. data/spec/support/active_record.rb +12 -0
  65. data/spec/support/shoulda_matchers.rb +2 -0
  66. metadata +237 -0
@@ -0,0 +1,58 @@
1
+ ###
2
+ # BookmarkSystem module
3
+ #
4
+ # This module defines common behavior in bookmark system
5
+ ###
6
+ module BookmarkSystem
7
+ ###
8
+ # Bookmarkee module
9
+ #
10
+ # This module defines bookmarkee behavior in bookmark system
11
+ ###
12
+ module Bookmarkee
13
+ ###
14
+ # Extends ActiveSupport::Concern
15
+ ###
16
+ extend ActiveSupport::Concern
17
+
18
+ ###
19
+ # Included configuration
20
+ ###
21
+ included do
22
+ ###
23
+ # Has many bookmarkers association configuration
24
+ ###
25
+ has_many :bookmarkers, class_name: "BookmarkSystem::Bookmark", as: :bookmarkee, dependent: :destroy
26
+ end
27
+
28
+ ###
29
+ # Specifies if self can be bookmarked by {Bookmarker} objects
30
+ #
31
+ # @return [Boolean]
32
+ ###
33
+ def is_bookmarkee?
34
+ true
35
+ end
36
+
37
+ ###
38
+ # Specifies if self is bookmarked by a {Bookmarker} object
39
+ #
40
+ # @param [Bookmarker] bookmarker - the {Bookmarker} object to test against
41
+ # @return [Boolean]
42
+ ###
43
+ def bookmarked_by?(bookmarker)
44
+ Bookmark.bookmarks?(bookmarker, self)
45
+ end
46
+
47
+ ###
48
+ # Retrieves a scope of {Bookmark} objects that bookmarks self filtered {Bookmarker} type
49
+ #
50
+ # @param [Class] klass - the {Class} to filter
51
+ # @return [ActiveRecord::Relation]
52
+ ###
53
+ def bookmarkers_by(klass)
54
+ Bookmark.scope_by_bookmarkee(self).scope_by_bookmarker_type(klass)
55
+ end
56
+ end
57
+ end
58
+
@@ -0,0 +1,88 @@
1
+ ###
2
+ # BookmarkSystem module
3
+ #
4
+ # This module defines common behavior in bookmark system
5
+ ###
6
+ module BookmarkSystem
7
+ ###
8
+ # Bookmarker module
9
+ #
10
+ # This module defines bookmarker behavior in bookmark system
11
+ ###
12
+ module Bookmarker
13
+ ###
14
+ # Extends ActiveSupport::Concern
15
+ ###
16
+ extend ActiveSupport::Concern
17
+
18
+ ###
19
+ # Included configuration
20
+ ###
21
+ included do
22
+ ###
23
+ # Has many bookmarkees association configuration
24
+ ###
25
+ has_many :bookmarkees, class_name: "BookmarkSystem::Bookmark", as: :bookmarker, dependent: :destroy
26
+ end
27
+
28
+ ###
29
+ # Specifies if self can bookmark {Bookmarkee} objects
30
+ #
31
+ # @return [Boolean]
32
+ ###
33
+ def is_bookmarker?
34
+ true
35
+ end
36
+
37
+ ###
38
+ # Creates a {Bookmark} relationship between self and a {Bookmarkee} object
39
+ #
40
+ # @param [Bookmarkee] bookmarkee - the bookmarkee of the {Bookmark} relationship
41
+ # @return [Boolean]
42
+ ###
43
+ def bookmark(bookmarkee)
44
+ Bookmark.bookmark(self, bookmarkee)
45
+ end
46
+
47
+ ###
48
+ # Destroys a {Bookmark} relationship between self and a {Bookmarkee} object
49
+ #
50
+ # @param [Bookmarkee] bookmarkee - the bookmarkee of the {Bookmark} relationship
51
+ # @return [Boolean]
52
+ ###
53
+ def unbookmark(bookmarkee)
54
+ Bookmark.unbookmark(self, bookmarkee)
55
+ end
56
+
57
+ ###
58
+ # Toggles a {Bookmark} relationship between self and a {Bookmarkee} object
59
+ #
60
+ # @param [Bookmarkee] bookmarkee - the bookmarkee of the {Bookmark} relationship
61
+ # @return [Boolean]
62
+ ###
63
+ def toggle_bookmark(bookmarkee)
64
+ Bookmark.toggle_bookmark(self, bookmarkee)
65
+ end
66
+
67
+ ###
68
+ # Specifies if self bookmarks a {Bookmarker} object
69
+ #
70
+ # @param [Bookmarkee] bookmarkee - the {Bookmarkee} object to test against
71
+ # @return [Boolean]
72
+ ###
73
+ def bookmarks?(bookmarkee)
74
+ Bookmark.bookmarks?(self, bookmarkee)
75
+ end
76
+
77
+ ###
78
+ # Retrieves a scope of {Bookmark} objects that are bookmarked by self
79
+ #
80
+ # @param [Class] klass - the {Class} to include
81
+ # @return [ActiveRecord::Relation]
82
+ ###
83
+ def bookmarkees_by(klass)
84
+ Bookmark.scope_by_bookmarker(self).scope_by_bookmarkee_type(klass)
85
+ end
86
+ end
87
+ end
88
+
@@ -0,0 +1,12 @@
1
+ ###
2
+ # BookmarkSystem module
3
+ #
4
+ # This module defines common behavior in bookmark system
5
+ ###
6
+ module BookmarkSystem
7
+ ###
8
+ # Version constant definition
9
+ ###
10
+ VERSION = "0.0.6"
11
+ end
12
+
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,46 @@
1
+ require 'rails/generators/migration'
2
+ require 'rails/generators/active_record'
3
+
4
+ ###
5
+ # BookmarkSystemGenerator class
6
+ #
7
+ # This class generates the bookmark model migration in bookmark system
8
+ ###
9
+ class BookmarkSystemGenerator < Rails::Generators::Base
10
+ ###
11
+ # Includes Rails::Generators::Migration
12
+ ###
13
+ include Rails::Generators::Migration
14
+
15
+ ###
16
+ # Generator description definition
17
+ ###
18
+ desc "Generates a migration for the Bookmark model"
19
+
20
+ ###
21
+ # Retrieves the source root
22
+ #
23
+ # @return [String]
24
+ ###
25
+ def self.source_root
26
+ @source_root ||= File.dirname(__FILE__) + '/templates'
27
+ end
28
+
29
+ ###
30
+ # Retrieves the next migration number
31
+ #
32
+ # @path [String] path - the path to calculate the next migration number
33
+ # @return [String]
34
+ ###
35
+ def self.next_migration_number(path)
36
+ ActiveRecord::Generators::Base.next_migration_number(path)
37
+ end
38
+
39
+ ###
40
+ # Generates the migration
41
+ ###
42
+ def generate_migration
43
+ migration_template 'migration.rb', 'db/migrate/create_bookmarks.rb'
44
+ end
45
+ end
46
+
@@ -0,0 +1,47 @@
1
+ ###
2
+ # CreateBookmarks class
3
+ #
4
+ # This class defines the create bookmarks migration in bookmark system
5
+ ###
6
+ class CreateBookmarks < ActiveRecord::Migration
7
+ ###
8
+ # Changes the database
9
+ ###
10
+ def change
11
+ ###
12
+ # Bookmarks table creation
13
+ ###
14
+ create_table :bookmarks do |t|
15
+ ###
16
+ # Bookmarkee id field and bookmarkee type field definition
17
+ ###
18
+ t.references :bookmarkee, polymorphic: true
19
+
20
+ ###
21
+ # Bookmarker id fiel and bookmarker type field definition
22
+ ###
23
+ t.references :bookmarker, polymorphic: true
24
+
25
+ ###
26
+ # Timestamps fields definition
27
+ ###
28
+ t.timestamps null: false
29
+ end
30
+
31
+ ###
32
+ # Bookmarks table bookmarkee id field and bookmarkee type field index addition
33
+ ###
34
+ add_index :bookmarks, [:bookmarkee_id, :bookmarkee_type], name: "bookmarks_bookmarkee_idx"
35
+
36
+ ###
37
+ # Bookmarks table bookmarker id field and bookmarker type field index addition
38
+ ###
39
+ add_index :bookmarks, [:bookmarker_id, :bookmarker_type], name: "bookmarks_bookmarker_idx"
40
+
41
+ ###
42
+ # Bookmarks table bookmarkee id field and bookmarkee type field and bookmarker id field and bookmarker type field unique index addition
43
+ ###
44
+ add_index :bookmarks, [:bookmarkee_id, :bookmarkee_type, :bookmarker_id, :bookmarker_type], name: "bookmarks_bookmarkee_bookmarker_idx", unique: true
45
+ end
46
+ end
47
+
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,177 @@
1
+ require 'spec_helper'
2
+
3
+ ###
4
+ # Describes BookmarkSystem::Bookmark
5
+ ###
6
+ describe BookmarkSystem::Bookmark do
7
+ ###
8
+ # Let bookmarkee be DummyBookmarkee.create
9
+ ###
10
+ let(:bookmarkee) { DummyBookmarkee.create }
11
+
12
+ ###
13
+ # Let bookmarker be DummyBookmarker.create
14
+ ###
15
+ let(:bookmarker) { DummyBookmarker.create }
16
+
17
+ ###
18
+ # Describes associations
19
+ ###
20
+ describe "associations" do
21
+ ###
22
+ # Should belong to bookmarkee
23
+ ###
24
+ it "should belong to bookmarkee" do
25
+ should belong_to(:bookmarkee)
26
+ end
27
+
28
+ ###
29
+ # Should belong to bookmarker
30
+ ###
31
+ it "should belong to bookmarker" do
32
+ should belong_to(:bookmarker)
33
+ end
34
+ end
35
+
36
+ ###
37
+ # Describes class methods
38
+ ###
39
+ describe "class methods" do
40
+ ###
41
+ # Should raise argument error on invalid bookmarkee when bookmarks
42
+ ###
43
+ it "should raise argument error on invalid bookmarkee when bookmarks" do
44
+ expect { BookmarkSystem::Bookmark.bookmark(bookmarker, bookmarker) }.to raise_error ArgumentError
45
+ end
46
+
47
+ ###
48
+ # Should raise argument error on invalid bookmarker when bookmarks
49
+ ###
50
+ it "should raise argument error on invalid bookmarker when bookmarks " do
51
+ expect { BookmarkSystem::Bookmark.bookmark(bookmarkee, bookmarkee) }.to raise_error ArgumentError
52
+ end
53
+
54
+ ###
55
+ # Should bookmark
56
+ ###
57
+ it "should bookmark" do
58
+ expect(BookmarkSystem::Bookmark.bookmark(bookmarker, bookmarkee)).to equal(true)
59
+ end
60
+
61
+ ###
62
+ # Should not bookmark
63
+ ###
64
+ it "should not bookmark" do
65
+ BookmarkSystem::Bookmark.bookmark(bookmarker, bookmarkee)
66
+
67
+ expect(BookmarkSystem::Bookmark.bookmark(bookmarker, bookmarkee)).to equal(false)
68
+ end
69
+
70
+ ###
71
+ # Should raise argument error on invalid bookmarkee when unbookmarks
72
+ ###
73
+ it "should raise argument error on invalid bookmarkee when unbookmarks" do
74
+ expect { BookmarkSystem::Bookmark.unbookmark(bookmarker, bookmarker) }.to raise_error ArgumentError
75
+ end
76
+
77
+ ###
78
+ # Should raise argument error on invalid bookmarker when unbookmarks
79
+ ###
80
+ it "should raise argument error on invalid bookmarker when unbookmarks" do
81
+ expect { BookmarkSystem::Bookmark.unbookmark(bookmarkee, bookmarkee) }.to raise_error ArgumentError
82
+ end
83
+
84
+ ###
85
+ # Should unbookmark
86
+ ###
87
+ it "should unbookmark" do
88
+ BookmarkSystem::Bookmark.bookmark(bookmarker, bookmarkee)
89
+
90
+ expect(BookmarkSystem::Bookmark.unbookmark(bookmarker, bookmarkee)).to equal(true)
91
+ end
92
+
93
+ ###
94
+ # Should not unbookmark
95
+ ###
96
+ it "should not unbookmark" do
97
+ expect(BookmarkSystem::Bookmark.unbookmark(bookmarker, bookmarkee)).to equal(false)
98
+ end
99
+
100
+ ###
101
+ # Should raise argument error on invalid bookmarkee when toggle bookmark
102
+ ###
103
+ it "should raise argument error on invalid bookmarkee when toggle bookmark" do
104
+ expect { BookmarkSystem::Bookmark.toggle_bookmark(bookmarker, bookmarker) }.to raise_error ArgumentError
105
+ end
106
+
107
+ ###
108
+ # Should raise argument error on invalid bookmarker when toggle bookmark
109
+ ###
110
+ it "should raise argument error on invalid bookmarker when toggle bookmark" do
111
+ expect { BookmarkSystem::Bookmark.toggle_bookmark(bookmarkee, bookmarkee) }.to raise_error ArgumentError
112
+ end
113
+
114
+ ###
115
+ # Should toggle bookmark
116
+ ###
117
+ it "should toggle bookmark" do
118
+ expect(BookmarkSystem::Bookmark.bookmarks?(bookmarker, bookmarkee)).to equal(false)
119
+
120
+ BookmarkSystem::Bookmark.toggle_bookmark(bookmarker, bookmarkee)
121
+
122
+ expect(BookmarkSystem::Bookmark.bookmarks?(bookmarker, bookmarkee)).to equal(true)
123
+
124
+ BookmarkSystem::Bookmark.toggle_bookmark(bookmarker, bookmarkee)
125
+
126
+ expect(BookmarkSystem::Bookmark.bookmarks?(bookmarker, bookmarkee)).to equal(false)
127
+ end
128
+
129
+ ###
130
+ # Should specify if bookmarks
131
+ ###
132
+ it "should specify if bookmarks" do
133
+ expect(BookmarkSystem::Bookmark.bookmarks?(bookmarker, bookmarkee)).to equal(false)
134
+
135
+ BookmarkSystem::Bookmark.bookmark(bookmarker, bookmarkee)
136
+
137
+ expect(BookmarkSystem::Bookmark.bookmarks?(bookmarker, bookmarkee)).to equal(true)
138
+ end
139
+
140
+ ###
141
+ # Should scope bookmarks by bookmarkee
142
+ ###
143
+ it "should scope bookmarks by bookmarkee" do
144
+ scope = BookmarkSystem::Bookmark.where(bookmarkee: bookmarkee)
145
+
146
+ expect(BookmarkSystem::Bookmark.scope_by_bookmarkee(bookmarkee)).to eq(scope)
147
+ end
148
+
149
+ ###
150
+ # Should scope bookmarks by bookmarkee type
151
+ ####
152
+ it "should scope bookmarks by bookmarkee type" do
153
+ scope = BookmarkSystem::Bookmark.where(bookmarkee_type: DummyBookmarkee)
154
+
155
+ expect(BookmarkSystem::Bookmark.scope_by_bookmarkee_type(DummyBookmarkee)).to eq(scope)
156
+ end
157
+
158
+ ###
159
+ # Should scope bookmarks by bookmarker
160
+ ###
161
+ it "should scope bookmarks by bookmarker" do
162
+ scope = BookmarkSystem::Bookmark.where(bookmarker: bookmarker)
163
+
164
+ expect(BookmarkSystem::Bookmark.scope_by_bookmarker(bookmarker)).to eq(scope)
165
+ end
166
+
167
+ ###
168
+ # Should scope bookmarks by bookmarker type
169
+ ####
170
+ it "should scope bookmarks by bookmarker type" do
171
+ scope = BookmarkSystem::Bookmark.where(bookmarker_type: DummyBookmarker)
172
+
173
+ expect(BookmarkSystem::Bookmark.scope_by_bookmarker_type(DummyBookmarker)).to eq(scope)
174
+ end
175
+ end
176
+ end
177
+