From e633745824ce010c8235b94d1f32d3914a8e8c63 Mon Sep 17 00:00:00 2001 From: "vilet.yy" Date: Fri, 14 May 2021 16:38:45 +0800 Subject: [PATCH] fix: remname namespace --- README.md | 15 +++++--- ...followable.gemspec => acts_as_able.gemspec | 16 ++++----- .../able_ext.rb} | 4 +-- lib/acts_as_able/followable.rb | 34 ++++++++++++++++++ .../follower.rb} | 36 ++----------------- .../railtie.rb | 0 .../unlikable.rb | 5 +-- .../unliker.rb | 5 +-- .../version.rb | 0 lib/acts_as_followable.rb | 15 ++++---- lib/acts_as_unlikeable.rb | 15 ++++++++ lib/generators/acts_as_able_generator.rb | 14 ++++++++ .../acts_as_followable_generator.rb | 10 ------ .../acts_as_unlikeable_generator.rb | 14 ++++++++ ...ollowable_spec.rb => acts_as_able_spec.rb} | 4 +-- spec/spec_helper.rb | 2 +- 16 files changed, 115 insertions(+), 74 deletions(-) rename acts_as_followable.gemspec => acts_as_able.gemspec (71%) rename lib/{acts_as_followable/followable_ext.rb => acts_as_able/able_ext.rb} (79%) create mode 100644 lib/acts_as_able/followable.rb rename lib/{acts_as_followable/followable.rb => acts_as_able/follower.rb} (53%) rename lib/{acts_as_followable => acts_as_able}/railtie.rb (100%) rename lib/{acts_as_followable => acts_as_able}/unlikable.rb (86%) rename lib/{acts_as_followable => acts_as_able}/unliker.rb (92%) rename lib/{acts_as_followable => acts_as_able}/version.rb (100%) create mode 100644 lib/acts_as_unlikeable.rb create mode 100644 lib/generators/acts_as_able_generator.rb create mode 100644 lib/generators/acts_as_unlikeable_generator.rb rename spec/{acts_as_followable_spec.rb => acts_as_able_spec.rb} (62%) diff --git a/README.md b/README.md index 5af6219..b9d217d 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,19 @@ + # 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 diff --git a/acts_as_followable.gemspec b/acts_as_able.gemspec similarity index 71% rename from acts_as_followable.gemspec rename to acts_as_able.gemspec index db97665..13dc556 100644 --- a/acts_as_followable.gemspec +++ b/acts_as_able.gemspec @@ -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' diff --git a/lib/acts_as_followable/followable_ext.rb b/lib/acts_as_able/able_ext.rb similarity index 79% rename from lib/acts_as_followable/followable_ext.rb rename to lib/acts_as_able/able_ext.rb index 94a50a6..1011183 100644 --- a/lib/acts_as_followable/followable_ext.rb +++ b/lib/acts_as_able/able_ext.rb @@ -1,5 +1,5 @@ -module ActsAsFollowable - module FollowableExt #这个是被关注 +module ActsAsAble + module AbleExt #这个是被关注 private # 有可能有STI的情况出现 diff --git a/lib/acts_as_able/followable.rb b/lib/acts_as_able/followable.rb new file mode 100644 index 0000000..cfd939a --- /dev/null +++ b/lib/acts_as_able/followable.rb @@ -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 diff --git a/lib/acts_as_followable/followable.rb b/lib/acts_as_able/follower.rb similarity index 53% rename from lib/acts_as_followable/followable.rb rename to lib/acts_as_able/follower.rb index 90e373b..90a730e 100644 --- a/lib/acts_as_followable/followable.rb +++ b/lib/acts_as_able/follower.rb @@ -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 diff --git a/lib/acts_as_followable/railtie.rb b/lib/acts_as_able/railtie.rb similarity index 100% rename from lib/acts_as_followable/railtie.rb rename to lib/acts_as_able/railtie.rb diff --git a/lib/acts_as_followable/unlikable.rb b/lib/acts_as_able/unlikable.rb similarity index 86% rename from lib/acts_as_followable/unlikable.rb rename to lib/acts_as_able/unlikable.rb index c5a1e72..8cf969d 100644 --- a/lib/acts_as_followable/unlikable.rb +++ b/lib/acts_as_able/unlikable.rb @@ -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 diff --git a/lib/acts_as_followable/unliker.rb b/lib/acts_as_able/unliker.rb similarity index 92% rename from lib/acts_as_followable/unliker.rb rename to lib/acts_as_able/unliker.rb index c7b2cb2..34c3263 100644 --- a/lib/acts_as_followable/unliker.rb +++ b/lib/acts_as_able/unliker.rb @@ -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 diff --git a/lib/acts_as_followable/version.rb b/lib/acts_as_able/version.rb similarity index 100% rename from lib/acts_as_followable/version.rb rename to lib/acts_as_able/version.rb diff --git a/lib/acts_as_followable.rb b/lib/acts_as_followable.rb index c53b6d8..c157555 100644 --- a/lib/acts_as_followable.rb +++ b/lib/acts_as_followable.rb @@ -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 diff --git a/lib/acts_as_unlikeable.rb b/lib/acts_as_unlikeable.rb new file mode 100644 index 0000000..9dca440 --- /dev/null +++ b/lib/acts_as_unlikeable.rb @@ -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 diff --git a/lib/generators/acts_as_able_generator.rb b/lib/generators/acts_as_able_generator.rb new file mode 100644 index 0000000..9b43147 --- /dev/null +++ b/lib/generators/acts_as_able_generator.rb @@ -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 diff --git a/lib/generators/acts_as_followable_generator.rb b/lib/generators/acts_as_followable_generator.rb index 02f8825..bfb5d7a 100644 --- a/lib/generators/acts_as_followable_generator.rb +++ b/lib/generators/acts_as_followable_generator.rb @@ -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 diff --git a/lib/generators/acts_as_unlikeable_generator.rb b/lib/generators/acts_as_unlikeable_generator.rb new file mode 100644 index 0000000..4e3090a --- /dev/null +++ b/lib/generators/acts_as_unlikeable_generator.rb @@ -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 diff --git a/spec/acts_as_followable_spec.rb b/spec/acts_as_able_spec.rb similarity index 62% rename from spec/acts_as_followable_spec.rb rename to spec/acts_as_able_spec.rb index 9166caa..7169a05 100644 --- a/spec/acts_as_followable_spec.rb +++ b/spec/acts_as_able_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e1a587e..c5633a0 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,2 +1,2 @@ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) -require 'acts_as_followable' +require 'acts_as_able'