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
这就是一个给国人用的 关注(点赞)有关的 Gem 目前仅仅只想支持Rails5
这就是一个给国人用的 关注、浏览、点赞等有关的 Gem
## Installation
Add this line to your application's Gemfile:
```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:
@ -16,17 +22,18 @@ And then execute:
## Usage
最开始你需要:
### 关注功能
```ruby
rails g acts_as_followable
rails db:migrate
```
然后有两个类方法可以放在需要使用的类中:
以下方法可以放在需要使用的类中:
```ruby
acts_as_followable #被关注
acts_as_follower #关注者
class Article < ApplicationRecord
acts_as_followable
end

View File

@ -1,17 +1,17 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'acts_as_followable/version'
require 'acts_as_able/version'
Gem::Specification.new do |spec|
spec.name = "acts_as_followable"
spec.version = ActsAsFollowable::VERSION
spec.authors = ["ZengTao"]
spec.email = ["so.zengtao@gmail.com"]
spec.name = "acts_as_able"
spec.version = ActsAsAble::VERSION
spec.authors = ["Viletyy"]
spec.email = ["yystopf@gmail.com"]
spec.summary = %q{ActsAsFollowable Just Save Time For You.}
spec.description = %q{ActsAsFollowable Just Save Time For You.}
spec.homepage = "https://github.com/w-zengtao"
spec.summary = %q{ActsAsAble Just Save Time For You.}
spec.description = %q{ActsAsAble Just Save Time For You.}
spec.homepage = "https://github.com/viletyy"
spec.license = "MIT"
# 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 FollowableExt #这个是被关注
module ActsAsAble
module AbleExt #这个是被关注
private
# 有可能有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 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 ActsAsAble
# 关注
module Follower
module ClassMethods
def acts_as_follower

View File

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

View File

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

View File

@ -1,18 +1,15 @@
require "acts_as_followable/version"
require "acts_as_able/version"
module ActsAsFollowable
extend ActiveSupport::Autoload
autoload :Followable, 'acts_as_followable/followable'
autoload :FollowableExt, 'acts_as_followable/followable_ext'
autoload :Unlikable, 'acts_as_followable/unlikable'
autoload :Unliker, 'acts_as_followable/unliker'
autoload :AbleExt, 'acts_as_able/able_ext'
autoload :Followable, 'acts_as_able/followable'
autoload :Follower, 'acts_as_able/follower'
# require 'acts_as_followable/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
ActiveSupport.on_load(:active_record) do
include ActsAsFollowable::Followable
include ActsAsFollowable::Follower
include ActsAsFollowable::Unlikable
include ActsAsFollowable::Unliker
include ActsAsAble::Followable
include ActsAsAble::Follower
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
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
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
def create_model
template "follow.rb", File.join('app/models', "follow.rb")
template "unlike.rb", File.join('app/models', "unlike.rb")
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'
describe ActsAsFollowable do
describe ActsAsAble do
it 'has a version number' do
expect(ActsAsFollowable::VERSION).not_to be nil
expect(ActsAsAble::VERSION).not_to be nil
end
it 'does something useful' do

View File

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