fix: remname namespace

This commit is contained in:
vilet.yy 2021-05-14 16:38:45 +08:00
parent c0f6474b51
commit e633745824
16 changed files with 115 additions and 74 deletions

View File

@ -1,13 +1,19 @@
<!--
* @Date: 2021-05-14 15:55:52
* @LastEditors: viletyy
* @LastEditTime: 2021-05-14 16:37:30
* @FilePath: /acts_as_followable/README.md
-->
# ActsAsFollowable # ActsAsFollowable
这就是一个给国人用的 关注(点赞)有关的 Gem 目前仅仅只想支持Rails5 这就是一个给国人用的 关注、浏览、点赞等有关的 Gem
## Installation ## Installation
Add this line to your application's Gemfile: Add this line to your application's Gemfile:
```ruby ```ruby
gem 'acts_as_followable', git: 'git://github.com/w-zengtao/acts_as_followable.git' gem 'acts_as_able', git: 'git://github.com/viletyy/acts_as_able.git'
``` ```
And then execute: And then execute:
@ -16,17 +22,18 @@ And then execute:
## Usage ## Usage
最开始你需要: ### 关注功能
```ruby ```ruby
rails g acts_as_followable rails g acts_as_followable
rails db:migrate rails db:migrate
``` ```
然后有两个类方法可以放在需要使用的类中: 以下方法可以放在需要使用的类中:
```ruby ```ruby
acts_as_followable #被关注 acts_as_followable #被关注
acts_as_follower #关注者 acts_as_follower #关注者
class Article < ApplicationRecord class Article < ApplicationRecord
acts_as_followable acts_as_followable
end end

View File

@ -1,17 +1,17 @@
# coding: utf-8 # coding: utf-8
lib = File.expand_path('../lib', __FILE__) lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'acts_as_followable/version' require 'acts_as_able/version'
Gem::Specification.new do |spec| Gem::Specification.new do |spec|
spec.name = "acts_as_followable" spec.name = "acts_as_able"
spec.version = ActsAsFollowable::VERSION spec.version = ActsAsAble::VERSION
spec.authors = ["ZengTao"] spec.authors = ["Viletyy"]
spec.email = ["so.zengtao@gmail.com"] spec.email = ["yystopf@gmail.com"]
spec.summary = %q{ActsAsFollowable Just Save Time For You.} spec.summary = %q{ActsAsAble Just Save Time For You.}
spec.description = %q{ActsAsFollowable Just Save Time For You.} spec.description = %q{ActsAsAble Just Save Time For You.}
spec.homepage = "https://github.com/w-zengtao" spec.homepage = "https://github.com/viletyy"
spec.license = "MIT" spec.license = "MIT"
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host' # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'

View File

@ -1,5 +1,5 @@
module ActsAsFollowable module ActsAsAble
module FollowableExt #这个是被关注 module AbleExt #这个是被关注
private private
# 有可能有STI的情况出现 # 有可能有STI的情况出现

View File

@ -0,0 +1,34 @@
module ActsAsAble
module Followable #这个是被关注
def self.included(base)
base.extend ClassMethods
base.send :include, InstanceMethods
end
module ClassMethods
def acts_as_followable #This means can be followed - 被关注
include ActsAsAble::AbleExt
has_many :followers, as: :followable, dependent: :destroy, class_name: 'Follow'
end
end
module InstanceMethods
# 1: 查看某个 模型 关注我的所有对象
# 2: 查看某个 模型 的某个 实例 是否关注我了
def followers_count
self.followers.count
end
def followers_by_type(follower_type, options = {})
ids = Follow.
where('followable_id' => self.id,
'followable_type' => class_name(self),
'follower_type' => follower_type.name
).pluck('follower_id')
return follower_type.where("id in (?)", ids)
end
end
end
end

View File

@ -1,37 +1,5 @@
module ActsAsFollowable module ActsAsAble
module Followable #这个是被关注 # 关注
def self.included(base)
base.extend ClassMethods
base.send :include, InstanceMethods
end
module ClassMethods
def acts_as_followable #This means can be followed - 被关注
include ActsAsFollowable::FollowableExt
has_many :followers, as: :followable, dependent: :destroy, class_name: 'Follow'
end
end
module InstanceMethods
# 1: 查看某个 模型 关注我的所有对象
# 2: 查看某个 模型 的某个 实例 是否关注我了
def followers_count
self.followers.count
end
def followers_by_type(follower_type, options = {})
ids = Follow.
where('followable_id' => self.id,
'followable_type' => class_name(self),
'follower_type' => follower_type.name
).pluck('follower_id')
return follower_type.where("id in (?)", ids)
end
end
end
module Follower module Follower
module ClassMethods module ClassMethods
def acts_as_follower def acts_as_follower

View File

@ -1,4 +1,5 @@
module ActsAsFollowable module ActsAsAble
# 不喜欢的对象
module Unlikable module Unlikable
def self.included(base) def self.included(base)
base.extend ClassMethods base.extend ClassMethods
@ -8,7 +9,7 @@ module ActsAsFollowable
module ClassMethods module ClassMethods
# Those call this method has the ability to be unlike by others # Those call this method has the ability to be unlike by others
def acts_as_unlikable def acts_as_unlikable
include ActsAsFollowable::FollowableExt include ActAsAble::AbleExt
has_many :unlikers, as: :unlikable, dependent: :destroy, class_name: 'Unlike' has_many :unlikers, as: :unlikable, dependent: :destroy, class_name: 'Unlike'
end end
end end

View File

@ -1,4 +1,5 @@
module ActsAsFollowable module ActsAsAble
# 不喜欢
module Unliker module Unliker
def self.included(base) def self.included(base)
base.extend ClassMethods base.extend ClassMethods
@ -8,7 +9,7 @@ module ActsAsFollowable
module ClassMethods module ClassMethods
# Those call this method has the ability to be unlike by others # Those call this method has the ability to be unlike by others
def acts_as_unliker def acts_as_unliker
include ActsAsFollowable::FollowableExt include ActsAsAble::AbleExt
has_many :unlikes, as: :unliker, dependent: :destroy, class_name: 'Unlike' # 有很多不喜欢的 Object has_many :unlikes, as: :unliker, dependent: :destroy, class_name: 'Unlike' # 有很多不喜欢的 Object
end end
end end

View File

@ -1,18 +1,15 @@
require "acts_as_followable/version" require "acts_as_able/version"
module ActsAsFollowable module ActsAsFollowable
extend ActiveSupport::Autoload extend ActiveSupport::Autoload
autoload :Followable, 'acts_as_followable/followable' autoload :AbleExt, 'acts_as_able/able_ext'
autoload :FollowableExt, 'acts_as_followable/followable_ext' autoload :Followable, 'acts_as_able/followable'
autoload :Unlikable, 'acts_as_followable/unlikable' autoload :Follower, 'acts_as_able/follower'
autoload :Unliker, 'acts_as_followable/unliker'
# require 'acts_as_followable/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3 # require 'acts_as_followable/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
ActiveSupport.on_load(:active_record) do ActiveSupport.on_load(:active_record) do
include ActsAsFollowable::Followable include ActsAsAble::Followable
include ActsAsFollowable::Follower include ActsAsAble::Follower
include ActsAsFollowable::Unlikable
include ActsAsFollowable::Unliker
end end
end end

15
lib/acts_as_unlikeable.rb Normal file
View File

@ -0,0 +1,15 @@
require "acts_as_able/version"
module ActsAsFollowable
extend ActiveSupport::Autoload
autoload :AbleExt, 'acts_as_able/able_ext'
autoload :Unlikable, 'acts_as_able/unlikable'
autoload :Unliker, 'acts_as_able/unliker'
# require 'acts_as_followable/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
ActiveSupport.on_load(:active_record) do
include ActsAsAble::Unlikable
include ActsAsAble::Unliker
end
end

View File

@ -0,0 +1,14 @@
require 'rails/generators'
require 'rails/generators/migration'
class ActsAsAbleGenerator < Rails::Generators::Base
include Rails::Generators::Migration
def self.source_root
@source_root ||= File.join(File.dirname(__FILE__), 'templates')
end
def self.next_migration_number(dirname)
Time.now.utc.strftime("%Y%m%d%H%M%S")
end
end

View File

@ -4,21 +4,11 @@ require 'rails/generators/migration'
class ActsAsFollowableGenerator < Rails::Generators::Base class ActsAsFollowableGenerator < Rails::Generators::Base
include Rails::Generators::Migration include Rails::Generators::Migration
def self.source_root
@source_root ||= File.join(File.dirname(__FILE__), 'templates')
end
def self.next_migration_number(dirname)
Time.now.utc.strftime("%Y%m%d%H%M%S")
end
def create_migration_file def create_migration_file
migration_template 'follow_migration.rb', 'db/migrate/acts_as_followable_migration.rb' migration_template 'follow_migration.rb', 'db/migrate/acts_as_followable_migration.rb'
migration_template 'unlike_migration.rb', 'db/migrate/acts_as_unlikable_migration.rb'
end end
def create_model def create_model
template "follow.rb", File.join('app/models', "follow.rb") template "follow.rb", File.join('app/models', "follow.rb")
template "unlike.rb", File.join('app/models', "unlike.rb")
end end
end end

View File

@ -0,0 +1,14 @@
require 'rails/generators'
require 'rails/generators/migration'
class ActsAsUnlikeableGenerator < Rails::Generators::Base
include Rails::Generators::Migration
def create_migration_file
migration_template 'unlike_migration.rb', 'db/migrate/acts_as_unlikable_migration.rb'
end
def create_model
template "unlike.rb", File.join('app/models', "unlike.rb")
end
end

View File

@ -1,8 +1,8 @@
require 'spec_helper' require 'spec_helper'
describe ActsAsFollowable do describe ActsAsAble do
it 'has a version number' do it 'has a version number' do
expect(ActsAsFollowable::VERSION).not_to be nil expect(ActsAsAble::VERSION).not_to be nil
end end
it 'does something useful' do it 'does something useful' do

View File

@ -1,2 +1,2 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
require 'acts_as_followable' require 'acts_as_able'