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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5c94b3b42cf789877750aece79699150f3283fef
4
+ data.tar.gz: 67b296976ba5ca794f6b69b14305b5c698747a1d
5
+ SHA512:
6
+ metadata.gz: 9eb182702c6814527dbe38045e0817686f24223a10cb7a0923c690dd577bc5f25238c4d7aac8006c4d1128d425ae8fc9e434ae3efcdd4f13f07a8a0eb6d2a3b7
7
+ data.tar.gz: cfc178aad70c13942eb96d85fb4bc41d0f86457d7b0f10bc21a01f9e60aaa9fe18176708addb609e7609be57708420531e7a39de094b77881c4de0eb4240e291
data/.empty ADDED
File without changes
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /gemfiles/*.lock
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
data/.keep ADDED
File without changes
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
@@ -0,0 +1 @@
1
+ bookmark_system
@@ -0,0 +1 @@
1
+ ruby-2.2.1
@@ -0,0 +1,23 @@
1
+ language:
2
+ - ruby
3
+
4
+ rvm:
5
+ - 2.0.0
6
+ - 2.1.3
7
+ - 2.2.1
8
+
9
+ gemfile:
10
+ - gemfiles/rails4.1.gemfile
11
+ - gemfiles/rails4.2.gemfile
12
+
13
+ install:
14
+ - "travis_retry bundle install"
15
+
16
+ before_script:
17
+
18
+ script:
19
+ - "bundle exec rake"
20
+
21
+ branches:
22
+ only:
23
+ - master
@@ -0,0 +1,7 @@
1
+ appraise "rails4.1" do
2
+ gem "rails", "4.1.10"
3
+ end
4
+
5
+ appraise "rails4.2" do
6
+ gem "rails", "4.2.1"
7
+ end
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bookmark_system.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Pablo Martin Viva
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,93 @@
1
+ # BookmarkSystem
2
+
3
+ [![Build Status](https://travis-ci.org/pmviva/bookmark_system.png?gbranch=master)](https://travis-ci.org/pmviva/bookmark_system)
4
+ [![Gem Version](https://badge.fury.io/rb/bookmark_system.svg)](http://badge.fury.io/rb/bookmark_system)
5
+
6
+ An active record bookmark system developed using ruby on rails 4.1 applying domain driven design and test driven development principles.
7
+
8
+ This gem is heavily influenced by cmer/socialization.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'bookmark_system'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ ```ruby
21
+ $ bundle
22
+ ```
23
+
24
+ Or install it yourself as:
25
+
26
+ ```ruby
27
+ $ gem install bookmark_system
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ ### Run the generator
33
+
34
+ ```ruby
35
+ $ rails g bookmark_system
36
+ ```
37
+
38
+ Let's suppose for a moment that you have a blog application and a User can bookmark a Post or several Post models.
39
+ The user model becomes the bookmarker and the post model becomes the bookmarkee.
40
+
41
+ ### Post object
42
+ ```ruby
43
+ class Post < ActiveRecord::Base
44
+ act_as_bookmarkee
45
+
46
+ validates :content, presence: true
47
+ end
48
+ ```
49
+
50
+ ### User object
51
+ ```ruby
52
+ class User < ActiveRecord::Base
53
+ act_as_bookmarker
54
+
55
+ validates :username, { presence: true, uniqueness: true }
56
+ end
57
+ ```
58
+
59
+ ### Bookmarkee object methods
60
+ ```ruby
61
+ post.is_bookmarkee? # returns true
62
+
63
+ post.bookmarked_by?(user) # returns true if user bookmarks the post object, false otherwise
64
+
65
+ post.bookmarkers_by(User) # returns a scope of BookmarkSystem::Bookmark join model that belongs to the post object and belongs to bookmarker objects of type User
66
+ ```
67
+
68
+
69
+ ### Bookmarker object methods
70
+ ```ruby
71
+ user.is_bookmarker? # returns true
72
+
73
+ user.bookmark(post) # Creates an instance of BookmarkSystem::Bookmark join model associating the user object and the post object, returns true if succeded, false otherwise
74
+
75
+ user.unbookmark(post) # Destroys an instance of BookmarkSystem::Bookmark join model that associates the user object and the post object, returns true if succeded, false otherwise
76
+
77
+ user.toggle_bookmark(post) # Bookmarks / unbookmarks the post
78
+
79
+ user.bookmarks?(post) # returns true if the user object bookmarks the post object, false otherwise
80
+
81
+ user.bookmarkees_by(Post) # returns a scope of BookmarkSystem::Bookmark join model that belongs to the user object and belongs to bookmarkee objects of type Post
82
+ ```
83
+
84
+ For more information read the [api documentation](http://rubydoc.info/gems/bookmark_system).
85
+
86
+ ## Contributing
87
+
88
+ 1. Fork it ( https://github.com/pmviva/bookmark_system/fork )
89
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
90
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
91
+ 4. Push to the branch (`git push origin my-new-feature`)
92
+ 5. Create a new Pull Request
93
+
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :default => [:spec]
4
+ desc 'run Rspec specs'
5
+ task :spec do
6
+ sh 'rspec spec'
7
+ end
8
+
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bookmark_system/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bookmark_system"
8
+ spec.version = BookmarkSystem::VERSION
9
+ spec.authors = ["Pablo Martin Viva"]
10
+ spec.email = ["pmviva@gmail.com"]
11
+ spec.summary = %q{An active record bookmark system.}
12
+ spec.description = %q{An active record bookmark system developed using ruby on rails 4.1 applying domain driven design and test driven development principles.}
13
+ spec.homepage = "http://github.com/pmviva/bookmark_system"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.0.0")
21
+
22
+ spec.add_dependency "rails", [ ">= 4.1", "< 5" ]
23
+
24
+ spec.add_development_dependency "appraisal", "~> 2.0"
25
+ spec.add_development_dependency "bundler", "~> 1.7"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.1"
28
+ spec.add_development_dependency "shoulda-matchers", "~> 2.7"
29
+ spec.add_development_dependency "sqlite3", "~> 1.3"
30
+ end
31
+
File without changes
File without changes
File without changes
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "4.1.10"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "4.2.1"
6
+
7
+ gemspec :path => "../"
File without changes
File without changes
File without changes
@@ -0,0 +1,45 @@
1
+ require 'bookmark_system/bookmark'
2
+ require 'bookmark_system/bookmarkee'
3
+ require 'bookmark_system/bookmarker'
4
+
5
+ ###
6
+ # BookmarkSystem module
7
+ #
8
+ # This module defines common behavior in bookmark system
9
+ ###
10
+ module BookmarkSystem
11
+ ###
12
+ # Specifies if self can be bookmarked by {Bookmarker} objects
13
+ #
14
+ # @return [Boolean]
15
+ ###
16
+ def is_bookmarkee?
17
+ false
18
+ end
19
+
20
+ ###
21
+ # Specifies if self can bookmark {Bookmarkee} objects
22
+ #
23
+ # @return [Boolean]
24
+ ###
25
+ def is_bookmarker?
26
+ false
27
+ end
28
+
29
+ ###
30
+ # Instructs self to act as bookmarkee
31
+ ###
32
+ def act_as_bookmarkee
33
+ include Bookmarkee
34
+ end
35
+
36
+ ###
37
+ # Instructs self to act as bookmarker
38
+ ###
39
+ def act_as_bookmarker
40
+ include Bookmarker
41
+ end
42
+ end
43
+
44
+ ActiveRecord::Base.extend BookmarkSystem
45
+
File without changes
File without changes
File without changes
@@ -0,0 +1,155 @@
1
+ ###
2
+ # BookmarkSystem module
3
+ #
4
+ # This module defines common behavior in bookmark system
5
+ ###
6
+ module BookmarkSystem
7
+ ###
8
+ # Bookmark class
9
+ #
10
+ # This class defines the bookmark model in bookmark system
11
+ ###
12
+ class Bookmark < ActiveRecord::Base
13
+ ###
14
+ # Belongs to bookmarkee association configuration
15
+ ###
16
+ belongs_to :bookmarkee, polymorphic: :true
17
+
18
+ ###
19
+ # Belongs to bookmarker association configuration
20
+ ###
21
+ belongs_to :bookmarker, polymorphic: :true
22
+
23
+ ###
24
+ # Creates a {Bookmark} relationship between a {Bookmarker} object and a {Bookmarkee} object
25
+ #
26
+ # @param [Bookmarker] bookmarker - the {Bookmarker} of the relationship
27
+ # @param [Bookmarkee] bookmarkee - the {Bookmarkee} of the relationship
28
+ # @return [Boolean]
29
+ ###
30
+ def self.bookmark(bookmarker, bookmarkee)
31
+ validate_bookmarkee(bookmarkee)
32
+ validate_bookmarker(bookmarker)
33
+
34
+ if bookmarks?(bookmarker, bookmarkee)
35
+ false
36
+ else
37
+ bookmark = scope_by_bookmarker(bookmarker).scope_by_bookmarkee(bookmarkee).build
38
+ bookmark.save
39
+ true
40
+ end
41
+ end
42
+
43
+ ###
44
+ # Destroys a {Bookmark} relationship between a {Bookmarker} object and a {Bookmarkee} object
45
+ #
46
+ # @param [Bookmarker] bookmarker - the {Bookmarker} of the relationship
47
+ # @param [Bookmarkee] bookmarkee - the {Bookmarkee} of the relationship
48
+ # @return [Boolean]
49
+ ###
50
+ def self.unbookmark(bookmarker, bookmarkee)
51
+ validate_bookmarkee(bookmarkee)
52
+ validate_bookmarker(bookmarker)
53
+
54
+ if bookmarks?(bookmarker, bookmarkee)
55
+ bookmark = scope_by_bookmarker(bookmarker).scope_by_bookmarkee(bookmarkee).take
56
+ bookmark.destroy
57
+ true
58
+ else
59
+ false
60
+ end
61
+ end
62
+
63
+ ###
64
+ # Toggles a {Bookmark} relationship between a {Bookmarker} object and a {Bookmarkee} object
65
+ #
66
+ # @param [Bookmarker] bookmarker - the {Bookmarker} of the relationship
67
+ # @param [Bookmarkee] bookmarkee - the {Bookmarkee} of the relationship
68
+ # @return [Boolean]
69
+ ###
70
+ def self.toggle_bookmark(bookmarker, bookmarkee)
71
+ validate_bookmarkee(bookmarkee)
72
+ validate_bookmarker(bookmarker)
73
+
74
+ if bookmarks?(bookmarker, bookmarkee)
75
+ unbookmark(bookmarker, bookmarkee)
76
+ else
77
+ bookmark(bookmarker, bookmarkee)
78
+ end
79
+ end
80
+
81
+ ###
82
+ # Specifies if a {Bookmarker} object bookmarks a {Bookmarkee} object
83
+ #
84
+ # @param [Bookmarker] bookmarker - the {Bookmarker} object to test against
85
+ # @param [Bookmarkee] bookmarkee - the {Bookmarkee} object to test against
86
+ # @return [Boolean]
87
+ ###
88
+ def self.bookmarks?(bookmarker, bookmarkee)
89
+ validate_bookmarkee(bookmarkee)
90
+ validate_bookmarker(bookmarker)
91
+
92
+ scope_by_bookmarker(bookmarker).scope_by_bookmarkee(bookmarkee).exists?
93
+ end
94
+
95
+ ###
96
+ # Retrieves a scope of {Bookmark} objects filtered by a {Bookmarkee} object
97
+ #
98
+ # @param [Bookmarkee] bookmarkee - the {Bookmarkee} to filter
99
+ # @return [ActiveRecord::Relation]
100
+ ###
101
+ def self.scope_by_bookmarkee(bookmarkee)
102
+ where(bookmarkee: bookmarkee)
103
+ end
104
+
105
+ ###
106
+ # Retrieves a scope of {Bookmark} objects filtered by a {Bookmarkee} type
107
+ #
108
+ # @param [Class] klass - the {Class} to filter
109
+ # @return [ActiveRecord::Relation]
110
+ ###
111
+ def self.scope_by_bookmarkee_type(klass)
112
+ where(bookmarkee_type: klass.to_s.classify)
113
+ end
114
+
115
+ ###
116
+ # Retrieves a scope of {Bookmark} objects filtered by a {Bookmarker} object
117
+ #
118
+ # @param [Bookmarker] bookmarker - the {Bookmarker} to filter
119
+ # @return [ActiveRecord::Relation]
120
+ ###
121
+ def self.scope_by_bookmarker(bookmarker)
122
+ where(bookmarker: bookmarker)
123
+ end
124
+
125
+ ###
126
+ # Retrieves a scope of {Bookmark} objects filtered by a {Bookmarker} type
127
+ #
128
+ # @param [Class] klass - the {Class} to filter
129
+ # @return [ActiveRecord::Relation]
130
+ ###
131
+ def self.scope_by_bookmarker_type(klass)
132
+ where(bookmarker_type: klass.to_s.classify)
133
+ end
134
+
135
+ private
136
+ ###
137
+ # Validates a bookmarkee object
138
+ #
139
+ # @raise [ArgumentError] if the bookmarkee object is invalid
140
+ ###
141
+ def self.validate_bookmarkee(bookmarkee)
142
+ raise ArgumentError.new unless bookmarkee.respond_to?(:is_bookmarkee?) && bookmarkee.is_bookmarkee?
143
+ end
144
+
145
+ ###
146
+ # Validates a bookmarker object
147
+ #
148
+ # @raise [ArgumentError] if the bookmarker object is invalid
149
+ ###
150
+ def self.validate_bookmarker(bookmarker)
151
+ raise ArgumentError.new unless bookmarker.respond_to?(:is_bookmarker?) && bookmarker.is_bookmarker?
152
+ end
153
+ end
154
+ end
155
+