acts_as_able 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/CODE_OF_CONDUCT.md +49 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +70 -0
  9. data/Rakefile +6 -0
  10. data/acts_as_able.gemspec +33 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +8 -0
  13. data/init.rb +2 -0
  14. data/lib/acts_as_able.rb +31 -0
  15. data/lib/acts_as_able/able_ext.rb +15 -0
  16. data/lib/acts_as_able/commentable.rb +36 -0
  17. data/lib/acts_as_able/commenter.rb +49 -0
  18. data/lib/acts_as_able/dissable.rb +32 -0
  19. data/lib/acts_as_able/disser.rb +45 -0
  20. data/lib/acts_as_able/followable.rb +34 -0
  21. data/lib/acts_as_able/follower.rb +44 -0
  22. data/lib/acts_as_able/likable.rb +32 -0
  23. data/lib/acts_as_able/liker.rb +45 -0
  24. data/lib/acts_as_able/railtie.rb +14 -0
  25. data/lib/acts_as_able/version.rb +3 -0
  26. data/lib/acts_as_able/viewable.rb +32 -0
  27. data/lib/acts_as_able/viewer.rb +40 -0
  28. data/lib/generators/acts_as_able_generator.rb +14 -0
  29. data/lib/generators/acts_as_commentable_generator.rb +11 -0
  30. data/lib/generators/acts_as_dissable_generator.rb +11 -0
  31. data/lib/generators/acts_as_followable_generator.rb +11 -0
  32. data/lib/generators/acts_as_likable_generator.rb +11 -0
  33. data/lib/generators/acts_as_viewable_generator.rb +11 -0
  34. data/lib/generators/templates/comment.rb +10 -0
  35. data/lib/generators/templates/comment_migration.rb +17 -0
  36. data/lib/generators/templates/diss.rb +6 -0
  37. data/lib/generators/templates/diss_migration.rb +13 -0
  38. data/lib/generators/templates/follow.rb +10 -0
  39. data/lib/generators/templates/follow_migration.rb +14 -0
  40. data/lib/generators/templates/like.rb +6 -0
  41. data/lib/generators/templates/like_migration.rb +13 -0
  42. data/lib/generators/templates/view.rb +6 -0
  43. data/lib/generators/templates/view_migration.rb +13 -0
  44. metadata +141 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b9b0001a19a3557390ad09e790f82aeeea82bb236ef0204bf06682454d9dda03
4
+ data.tar.gz: c70052968040c5f9a8fab587ab7f92c5af1333819b1ac9d43810752dfd3e9b26
5
+ SHA512:
6
+ metadata.gz: f304b0bd272210243d6ff822d6295dbecdf9c8bc2e0dd2dbdf496184c381805d2c3e9abe761ae2d90b3a167c89eb605372595cb77b3f544535d7100ab2cdb9f9
7
+ data.tar.gz: a0ef45ec93a5d48e9f8c6b94d19661c14501c2d994c5219b8e4cb852e1965852473b7f8abc72de429387b60123055835ea20a05b66b0df68c8a7c8ab62438152
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at so.zengtao@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in acts_as_followable.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 ZengTao
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ <!--
2
+ * @Date: 2021-05-14 15:55:52
3
+ * @LastEditors: viletyy
4
+ * @LastEditTime: 2021-05-14 16:37:30
5
+ * @FilePath: /acts_as_followable/README.md
6
+ -->
7
+ # ActsAsFollowable
8
+
9
+ 这就是一个给国人用的 关注、浏览、点赞等有关的 Gem
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'acts_as_able', git: 'git://github.com/viletyy/acts_as_able.git'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ ## Usage
24
+
25
+ ### 关注功能
26
+ ```ruby
27
+ rails g acts_as_followable
28
+ rails db:migrate
29
+ ```
30
+
31
+ 以下方法可以放在需要使用的类中:
32
+ ```ruby
33
+ acts_as_followable #被关注
34
+ acts_as_follower #关注者
35
+
36
+
37
+ class Article < ApplicationRecord
38
+ acts_as_followable
39
+ end
40
+
41
+ class User < ApplicationRecord
42
+ acts_as_follower
43
+ end
44
+ ```
45
+
46
+ acts_as_followable
47
+ ```ruby
48
+ acts_as_followable
49
+
50
+ #找出某个模型关注我的所有的对象
51
+ Article.followers_by_type(User)
52
+ ```
53
+
54
+ acts_as_follower
55
+ ```ruby
56
+ acts_as_follower
57
+
58
+ # 关注某个模型的某个对象
59
+ User.first.follow(Article.first)
60
+ # 万恶的取关
61
+ User.first.unfollow(Article.first)
62
+ # 查看是否关注某个对象
63
+ User.first.follow?(Article.first)
64
+ # 查看关注模型的所有对象
65
+ User.first.followings(Article)
66
+ ```
67
+ 以上是目前实现的功能 还有黑名单等功能待实现
68
+ ## License
69
+
70
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require 'acts_as_able/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "acts_as_able"
7
+ spec.version = ActsAsAble::VERSION
8
+ spec.authors = ["Viletyy"]
9
+ spec.email = ["yystopf@gmail.com"]
10
+
11
+ spec.summary = %q{ActsAsAble Just Save Time For You.}
12
+ spec.description = %q{ActsAsAble Just Save Time For You.}
13
+ spec.homepage = "https://github.com/viletyy/acts_as_able"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
20
+ # else
21
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ # end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.16"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "rspec", "~> 3.0"
32
+ spec.add_development_dependency "rails", "~> 6.0"
33
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "acts_as_able"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ $:.unshift "#{File.dirname(__FILE__)}/lib"
2
+ require "acts_as_able"
@@ -0,0 +1,31 @@
1
+ require "acts_as_able/version"
2
+
3
+ module ActsAsAble
4
+ extend ActiveSupport::Autoload
5
+ autoload :AbleExt, 'acts_as_able/able_ext'
6
+ autoload :Commentable, 'acts_as_able/commentable'
7
+ autoload :Commenter, 'acts_as_able/commenter'
8
+ autoload :Dissable, 'acts_as_able/dissable'
9
+ autoload :Disser, 'acts_as_able/disser'
10
+ autoload :Followable, 'acts_as_able/followable'
11
+ autoload :Follower, 'acts_as_able/follower'
12
+ autoload :Likable, 'acts_as_able/likable'
13
+ autoload :Liker, 'acts_as_able/liker'
14
+ autoload :Viewable, 'acts_as_able/viewable'
15
+ autoload :Viewer, 'acts_as_able/viewer'
16
+
17
+ # require 'acts_as_followable/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
18
+ ActiveSupport.on_load(:active_record) do
19
+ include ActsAsAble::Commentable
20
+ include ActsAsAble::Commenter
21
+ include ActsAsAble::Dissable
22
+ include ActsAsAble::Disser
23
+ include ActsAsAble::Followable
24
+ include ActsAsAble::Follower
25
+ include ActsAsAble::Likable
26
+ include ActsAsAble::Liker
27
+ include ActsAsAble::Viewable
28
+ include ActsAsAble::Viewer
29
+ end
30
+
31
+ end
@@ -0,0 +1,15 @@
1
+ module ActsAsAble
2
+ module AbleExt #这个是被关注
3
+ private
4
+
5
+ # 有可能有STI的情况出现
6
+ def class_name(obj)
7
+ klass = obj.class
8
+ loop do
9
+ break if klass.superclass.name == 'ApplicationRecord'
10
+ klass = klass.superclass
11
+ end
12
+ klass.name
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,36 @@
1
+ module ActsAsAble
2
+ # 被评论的对象
3
+ module Commentable
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ base.send :include, InstanceMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ # Those call this method has the ability to be unlike by others
11
+ def acts_as_commentable
12
+ include ActsAsAble::AbleExt
13
+ has_many :commenters, as: :commentable, dependent: :destroy, class_name: 'Comment'
14
+ end
15
+ end
16
+
17
+ module InstanceMethods
18
+ def root_commenters
19
+ self.commenters.where(parent_id: nil)
20
+ end
21
+
22
+ def comment_count
23
+ self.commenters.count
24
+ end
25
+
26
+ def commenters_by_type(commenter_type, options = {})
27
+ ids = Diss.
28
+ where('commentable_id' => self.id,
29
+ 'commentable_type' => class_name(self),
30
+ 'commenter_type' => commenter_type.name
31
+ ).pluck('commenter_id')
32
+ return commenter_type.where("id in (?)", ids)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,49 @@
1
+ module ActsAsAble
2
+ # 发起评论的对象
3
+ module Commenter
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ base.send :include, InstanceMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ # Those call this method has the ability to be unlike by others
11
+ def acts_as_commenter
12
+ include ActsAsAble::AbleExt
13
+ has_many :comments, as: :commenter, dependent: :destroy, class_name: 'Comment'
14
+ end
15
+ end
16
+
17
+ module InstanceMethods
18
+ # 所有的一级评论
19
+ def root_comments
20
+ self.comments.where(parent_id: nil)
21
+ end
22
+
23
+ # 评论某对象
24
+ def comment(content, obj, parent_comment = nil)
25
+ if parent_comment.present? && parent_comment.is_a?(Comment)
26
+ self.comments.create(content: content,commentable_type: class_name(obj), commentable_id: obj.id, parent_id: parent_comment.id )
27
+ else
28
+ self.comments.create(content: content, commentable_type: class_name(obj), commentable_id: obj.id,)
29
+ end
30
+ end
31
+
32
+ # 是否评论了某对象 & 回复了某对象的评论
33
+ def comment?(obj)
34
+ !comment_by(obj).blank?
35
+ end
36
+
37
+ # 查看某种类型评论的所有对象
38
+ def commentings(commentable_type)
39
+ return commentable_type.constantize.where(id: self.comments.where(commentable_type: commentable_type).pluck(:commentable_id))
40
+ end
41
+
42
+ private
43
+
44
+ def comment_by(obj)
45
+ self.comments.find_by(commentable_type: class_name(obj), commentable_id: obj.id)
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,32 @@
1
+ module ActsAsAble
2
+ # 被踩的对象
3
+ module Dissable
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ base.send :include, InstanceMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ # Those call this method has the ability to be unlike by others
11
+ def acts_as_dissable
12
+ include ActsAsAble::AbleExt
13
+ has_many :dissers, as: :dissable, dependent: :destroy, class_name: 'Diss'
14
+ end
15
+ end
16
+
17
+ module InstanceMethods
18
+ def diss_count
19
+ self.dissers.count
20
+ end
21
+
22
+ def dissers_by_type(disser_type, options = {})
23
+ ids = Diss.
24
+ where('dissable_id' => self.id,
25
+ 'dissable_type' => class_name(self),
26
+ 'disser_type' => disser_type.name
27
+ ).pluck('disser_id')
28
+ return disser_type.where("id in (?)", ids)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,45 @@
1
+ module ActsAsAble
2
+ # 发起踩的对象
3
+ module Disser
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ base.send :include, InstanceMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ # Those call this method has the ability to be unlike by others
11
+ def acts_as_disser
12
+ include ActsAsAble::AbleExt
13
+ has_many :disses, as: :disser, dependent: :destroy, class_name: 'Diss' # 有很多不喜欢的 Object
14
+ end
15
+ end
16
+
17
+ module InstanceMethods
18
+ # 踩某对象
19
+ def diss(obj)
20
+ self.disses.find_or_create_by(dissable_type: class_name(obj), dissable_id: obj.id)
21
+ end
22
+
23
+ # 取消对某对象对踩
24
+ def undiss(obj)
25
+ diss_by(obj).destroy
26
+ end
27
+
28
+ # 是否踩了某对象
29
+ def diss?(obj)
30
+ !diss_by(obj).blank?
31
+ end
32
+
33
+ # 查看某种类型踩的所有对象
34
+ def dissings(dissable_type)
35
+ return dissable_type.constantize.where(id: self.disses.where(dissable_type: dissable_type).pluck(:dissable_id))
36
+ end
37
+
38
+ private
39
+
40
+ def diss_by(obj)
41
+ self.disses.find_by(dissable_type: class_name(obj), dissable_id: obj.id)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,34 @@
1
+ module ActsAsAble
2
+ module Followable #这个是被关注
3
+
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ base.send :include, InstanceMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def acts_as_followable #This means can be followed - 被关注
11
+ include ActsAsAble::AbleExt
12
+
13
+ has_many :followers, as: :followable, dependent: :destroy, class_name: 'Follow'
14
+ end
15
+ end
16
+
17
+ module InstanceMethods
18
+ # 1: 查看某个 模型 关注我的所有对象
19
+ # 2: 查看某个 模型 的某个 实例 是否关注我了
20
+ def followers_count
21
+ self.followers.count
22
+ end
23
+
24
+ def followers_by_type(follower_type, options = {})
25
+ ids = Follow.
26
+ where('followable_id' => self.id,
27
+ 'followable_type' => class_name(self),
28
+ 'follower_type' => follower_type.name
29
+ ).pluck('follower_id')
30
+ return follower_type.where("id in (?)", ids)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,44 @@
1
+ module ActsAsAble
2
+ # 关注
3
+ module Follower
4
+ module ClassMethods
5
+ def acts_as_follower
6
+ include ActsAsFollowable::FollowableExt
7
+ has_many :follows, as: :follower, dependent: :destroy, class_name: 'Follow'
8
+ end
9
+ end
10
+
11
+ def self.included(receiver)
12
+ receiver.extend ClassMethods
13
+ receiver.send :include, InstanceMethods
14
+ end
15
+
16
+ module InstanceMethods
17
+ # # 1:   关注某人
18
+ # # 2: 取消关注
19
+ # # 3: 是否关注
20
+ # # 4: 关注了某个 model 多少个 obj
21
+ def follow(obj)
22
+ self.follows.find_or_create_by(followable_id: obj.id, followable_type: class_name(obj))
23
+ end
24
+
25
+ def unfollow(obj)
26
+ get_follow_by_obj(obj).destroy
27
+ end
28
+
29
+ def follow?(obj)
30
+ !get_follow_by_obj(obj).blank?
31
+ end
32
+
33
+ def followings(followable_type)
34
+ return followable_type.constantize.where(id: self.follows.where(followable_type: followable_type).pluck(:followable_id))
35
+ end
36
+
37
+ private
38
+ def get_follow_by_obj(obj)
39
+ self.follows.find_by(followable_id: obj.id, followable_type: class_name(obj))
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,32 @@
1
+ module ActsAsAble
2
+ # 被赞的对象
3
+ module Likable
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ base.send :include, InstanceMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ # Those call this method has the ability to be unlike by others
11
+ def acts_as_likable
12
+ include ActsAsAble::AbleExt
13
+ has_many :likers, as: :likable, dependent: :destroy, class_name: 'Like'
14
+ end
15
+ end
16
+
17
+ module InstanceMethods
18
+ def like_count
19
+ self.likers.count
20
+ end
21
+
22
+ def likers_by_type(liker_type, options = {})
23
+ ids = Like.
24
+ where('likable_id' => self.id,
25
+ 'likable_type' => class_name(self),
26
+ 'liker_type' => liker_type.name
27
+ ).pluck('liker_id')
28
+ return liker_type.where("id in (?)", ids)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,45 @@
1
+ module ActsAsAble
2
+ # 发起赞的对象
3
+ module Liker
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ base.send :include, InstanceMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ # Those call this method has the ability to be unlike by others
11
+ def acts_as_liker
12
+ include ActsAsAble::AbleExt
13
+ has_many :likes, as: :liker, dependent: :destroy, class_name: 'Like' # 有很多不喜欢的 Object
14
+ end
15
+ end
16
+
17
+ module InstanceMethods
18
+ # 赞某对象
19
+ def like(obj)
20
+ self.likes.find_or_create_by(likable_type: class_name(obj), likable_id: obj.id)
21
+ end
22
+
23
+ # 取消对某对象的赞
24
+ def unlike(obj)
25
+ like_by(obj).destroy
26
+ end
27
+
28
+ # 是否赞了某对象
29
+ def like?(obj)
30
+ !like_by(obj).blank?
31
+ end
32
+
33
+ # 查看某种类型踩的所有对象
34
+ def likings(likable_type)
35
+ return likable_type.constantize.where(id: self.likes.where(likable_type: likable_type).pluck(:likable_id))
36
+ end
37
+
38
+ private
39
+
40
+ def like_by(obj)
41
+ self.likes.find_by(likable_type: class_name(obj), likable_id: obj.id)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,14 @@
1
+ # require 'acts_as_followable'
2
+ # require 'rails'
3
+
4
+ # module ActsAsFollowable
5
+ # class Railtie < Rails::Railtie
6
+ # initializer "acts_as_followable.active_record" do |app|
7
+ # ActiveSupport.on_load :active_record do
8
+ # include ActsAsFollowable::Followable
9
+ # include ActsAsFollowable::Follower
10
+ # include ActsAsFollowable::Unlikable
11
+ # end
12
+ # end
13
+ # end
14
+ # end
@@ -0,0 +1,3 @@
1
+ module ActsAsAble
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,32 @@
1
+ module ActsAsAble
2
+ # 浏览的对象
3
+ module Viewable
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ base.send :include, InstanceMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ # Those call this method has the ability to be unlike by others
11
+ def acts_as_viewable
12
+ include ActsAsAble::AbleExt
13
+ has_many :viewers, as: :viewable, dependent: :destroy, class_name: 'View'
14
+ end
15
+ end
16
+
17
+ module InstanceMethods
18
+ def view_count
19
+ self.viewers.count
20
+ end
21
+
22
+ def viewers_by_type(viewer_type, options = {})
23
+ ids = View.
24
+ where('viewable_id' => self.id,
25
+ 'viewable_type' => class_name(self),
26
+ 'viewer_type' => viewer_type.name
27
+ ).pluck('viewer_id')
28
+ return viewer_type.where("id in (?)", ids)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,40 @@
1
+ module ActsAsAble
2
+ # 发起浏览的对象
3
+ module Viewer
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ base.send :include, InstanceMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ # Those call this method has the ability to be unlike by others
11
+ def acts_as_viewer
12
+ include ActsAsAble::AbleExt
13
+ has_many :views, as: :viewer, dependent: :destroy, class_name: 'View' # 有很多不喜欢的 Object
14
+ end
15
+ end
16
+
17
+ module InstanceMethods
18
+ # 浏览某对象
19
+ def view(obj)
20
+ self.views.create(viewable_type: class_name(obj), viewable_id: obj.id)
21
+ end
22
+
23
+ # 是否浏览某对象
24
+ def view?(obj)
25
+ !view_by(obj).blank?
26
+ end
27
+
28
+ # 查看某种类型浏览的所有对象
29
+ def viewings(viewable_type)
30
+ return viewable_type.constantize.where(id: self.views.where(viewable_type: viewable_type).pluck(:viewable_id))
31
+ end
32
+
33
+ private
34
+
35
+ def view_by(obj)
36
+ self.views.find_by(viewable_type: class_name(obj), viewable_id: obj.id)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,14 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class ActsAsAbleGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+
7
+ def self.source_root
8
+ @source_root ||= File.join(File.dirname(__FILE__), 'templates')
9
+ end
10
+
11
+ def self.next_migration_number(dirname)
12
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ require 'generators/acts_as_able_generator'
2
+
3
+ class ActsAsCommentableGenerator < ActsAsAbleGenerator
4
+ def create_migration_file
5
+ migration_template 'comment_migration.rb', 'db/migrate/acts_as_commentable_migration.rb'
6
+ end
7
+
8
+ def create_model
9
+ template "comment.rb", File.join('app/models', "comment.rb")
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'generators/acts_as_able_generator'
2
+
3
+ class ActsAsDissableGenerator < ActsAsAbleGenerator
4
+ def create_migration_file
5
+ migration_template 'diss_migration.rb', 'db/migrate/acts_as_dissable_migration.rb'
6
+ end
7
+
8
+ def create_model
9
+ template "diss.rb", File.join('app/models', "diss.rb")
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'generators/acts_as_able_generator'
2
+
3
+ class ActsAsFollowableGenerator < ActsAsAbleGenerator
4
+ def create_migration_file
5
+ migration_template 'follow_migration.rb', 'db/migrate/acts_as_followable_migration.rb'
6
+ end
7
+
8
+ def create_model
9
+ template "follow.rb", File.join('app/models', "follow.rb")
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'generators/acts_as_able_generator'
2
+
3
+ class ActsAsLikableGenerator < ActsAsAbleGenerator
4
+ def create_migration_file
5
+ migration_template 'like_migration.rb', 'db/migrate/acts_as_likable_migration.rb'
6
+ end
7
+
8
+ def create_model
9
+ template "like.rb", File.join('app/models', "like.rb")
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ require 'generators/acts_as_able_generator'
2
+
3
+ class ActsAsViewableGenerator < ActsAsAbleGenerator
4
+ def create_migration_file
5
+ migration_template 'view_migration.rb', 'db/migrate/acts_as_viewable_migration.rb'
6
+ end
7
+
8
+ def create_model
9
+ template "view.rb", File.join('app/models', "view.rb")
10
+ end
11
+ end
@@ -0,0 +1,10 @@
1
+ class Comment < ApplicationRecord
2
+
3
+ belongs_to :commentable, :polymorphic => true
4
+ belongs_to :commenter, :polymorphic => true
5
+
6
+ belongs_to :parent_comment, optional: true, foreign_key: :parent_id, class_name: 'Comment', counter_cache: :children_comments_count
7
+ has_many :children_comments, foreign_key: :parent_id, dependent: :destroy, class_name: 'Comment'
8
+
9
+
10
+ end
@@ -0,0 +1,17 @@
1
+ class ActsAsCommentableMigration < ActiveRecord::Migration[5.0]
2
+ def self.up
3
+ create_table :comments, force: true do |t|
4
+ t.references :commentable, polymorphic: true, null: false
5
+ t.references :commenter, polymorphic: true, null: false
6
+ t.integer :parent_id
7
+ t.integer :children_comments_count, default: 0
8
+ t.text :content
9
+ t.timestamps
10
+ end
11
+ add_index :comments, :parent_id
12
+ end
13
+
14
+ def self.down
15
+ drop_table :comments
16
+ end
17
+ end
@@ -0,0 +1,6 @@
1
+ class Diss < ApplicationRecord
2
+
3
+ belongs_to :dissable, :polymorphic => true
4
+ belongs_to :disser, :polymorphic => true
5
+
6
+ end
@@ -0,0 +1,13 @@
1
+ class ActsAsDissableMigration < ActiveRecord::Migration[5.0]
2
+ def self.up
3
+ create_table :disses, force: true do |t|
4
+ t.references :dissable, polymorphic: true, null: false
5
+ t.references :disser, polymorphic: true, null: false
6
+ t.timestamps
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table :disses
12
+ end
13
+ end
@@ -0,0 +1,10 @@
1
+ class Follow < ApplicationRecord
2
+
3
+ belongs_to :followable, :polymorphic => true
4
+ belongs_to :follower, :polymorphic => true
5
+
6
+ def block!
7
+ self.update_attribute(:blocked, true)
8
+ end
9
+
10
+ end
@@ -0,0 +1,14 @@
1
+ class ActsAsFollowableMigration < ActiveRecord::Migration[5.0]
2
+ def self.up
3
+ create_table :follows, force: true do |t|
4
+ t.references :followable, polymorphic: true, null: false
5
+ t.references :follower, polymorphic: true, null: false
6
+ t.boolean :blocked, default: false, null: false
7
+ t.timestamps
8
+ end
9
+ end
10
+
11
+ def self.down
12
+ drop_table :follows
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ class Like < ApplicationRecord
2
+
3
+ belongs_to :likable, :polymorphic => true
4
+ belongs_to :liker, :polymorphic => true
5
+
6
+ end
@@ -0,0 +1,13 @@
1
+ class ActsAsLikableMigration < ActiveRecord::Migration[5.0]
2
+ def self.up
3
+ create_table :likes, force: true do |t|
4
+ t.references :likable, polymorphic: true, null: false
5
+ t.references :liker, polymorphic: true, null: false
6
+ t.timestamps
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table :likes
12
+ end
13
+ end
@@ -0,0 +1,6 @@
1
+ class View < ApplicationRecord
2
+
3
+ belongs_to :viewable, :polymorphic => true
4
+ belongs_to :viewer, :polymorphic => true
5
+
6
+ end
@@ -0,0 +1,13 @@
1
+ class ActsAsViewableMigration < ActiveRecord::Migration[5.0]
2
+ def self.up
3
+ create_table :views, force: true do |t|
4
+ t.references :viewable, polymorphic: true, null: false
5
+ t.references :viewer, polymorphic: true, null: false
6
+ t.timestamps
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table :views
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_able
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Viletyy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-05-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '6.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '6.0'
69
+ description: ActsAsAble Just Save Time For You.
70
+ email:
71
+ - yystopf@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - CODE_OF_CONDUCT.md
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - acts_as_able.gemspec
85
+ - bin/console
86
+ - bin/setup
87
+ - init.rb
88
+ - lib/acts_as_able.rb
89
+ - lib/acts_as_able/able_ext.rb
90
+ - lib/acts_as_able/commentable.rb
91
+ - lib/acts_as_able/commenter.rb
92
+ - lib/acts_as_able/dissable.rb
93
+ - lib/acts_as_able/disser.rb
94
+ - lib/acts_as_able/followable.rb
95
+ - lib/acts_as_able/follower.rb
96
+ - lib/acts_as_able/likable.rb
97
+ - lib/acts_as_able/liker.rb
98
+ - lib/acts_as_able/railtie.rb
99
+ - lib/acts_as_able/version.rb
100
+ - lib/acts_as_able/viewable.rb
101
+ - lib/acts_as_able/viewer.rb
102
+ - lib/generators/acts_as_able_generator.rb
103
+ - lib/generators/acts_as_commentable_generator.rb
104
+ - lib/generators/acts_as_dissable_generator.rb
105
+ - lib/generators/acts_as_followable_generator.rb
106
+ - lib/generators/acts_as_likable_generator.rb
107
+ - lib/generators/acts_as_viewable_generator.rb
108
+ - lib/generators/templates/comment.rb
109
+ - lib/generators/templates/comment_migration.rb
110
+ - lib/generators/templates/diss.rb
111
+ - lib/generators/templates/diss_migration.rb
112
+ - lib/generators/templates/follow.rb
113
+ - lib/generators/templates/follow_migration.rb
114
+ - lib/generators/templates/like.rb
115
+ - lib/generators/templates/like_migration.rb
116
+ - lib/generators/templates/view.rb
117
+ - lib/generators/templates/view_migration.rb
118
+ homepage: https://github.com/viletyy/acts_as_able
119
+ licenses:
120
+ - MIT
121
+ metadata: {}
122
+ post_install_message:
123
+ rdoc_options: []
124
+ require_paths:
125
+ - lib
126
+ required_ruby_version: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ required_rubygems_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubygems_version: 3.1.4
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: ActsAsAble Just Save Time For You.
141
+ test_files: []