add: 整合赞、踩功能
This commit is contained in:
parent
29c2082605
commit
d7dc889023
|
@ -3,19 +3,23 @@ require "acts_as_able/version"
|
|||
module ActsAsAble
|
||||
extend ActiveSupport::Autoload
|
||||
autoload :AbleExt, 'acts_as_able/able_ext'
|
||||
autoload :Dissable, 'acts_as_able/dissable'
|
||||
autoload :Disser, 'acts_as_able/disser'
|
||||
autoload :Followable, 'acts_as_able/followable'
|
||||
autoload :Follower, 'acts_as_able/follower'
|
||||
autoload :Unlikable, 'acts_as_able/unlikable'
|
||||
autoload :Unliker, 'acts_as_able/unliker'
|
||||
autoload :Likable, 'acts_as_able/likable'
|
||||
autoload :Liker, 'acts_as_able/liker'
|
||||
autoload :Viewable, 'acts_as_able/viewable'
|
||||
autoload :Viewer, 'acts_as_able/viewer'
|
||||
|
||||
# require 'acts_as_followable/railtie' if defined?(Rails) && Rails::VERSION::MAJOR >= 3
|
||||
ActiveSupport.on_load(:active_record) do
|
||||
include ActsAsAble::Dissable
|
||||
include ActsAsAble::Disser
|
||||
include ActsAsAble::Followable
|
||||
include ActsAsAble::Follower
|
||||
include ActsAsAble::Unlikable
|
||||
include ActsAsAble::Unliker
|
||||
include ActsAsAble::Likable
|
||||
include ActsAsAble::Liker
|
||||
include ActsAsAble::Viewable
|
||||
include ActsAsAble::Viewer
|
||||
end
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
module ActsAsAble
|
||||
# 被踩的对象
|
||||
module Dissable
|
||||
def self.included(base)
|
||||
base.extend ClassMethods
|
||||
base.send :include, InstanceMethods
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# Those call this method has the ability to be unlike by others
|
||||
def acts_as_disable
|
||||
include ActsAsAble::AbleExt
|
||||
has_many :dissers, as: :dissable, dependent: :destroy, class_name: 'Diss'
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
def diss_count
|
||||
self.dissers.count
|
||||
end
|
||||
|
||||
def dissers_by_type(disser_type, options = {})
|
||||
ids = Diss.
|
||||
where('dissable_id' => self.id,
|
||||
'dissable_type' => class_name(self),
|
||||
'disser_type' => disser_type.name
|
||||
).pluck('disser_id')
|
||||
return disser_type.where("id in (?)", ids)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,45 @@
|
|||
module ActsAsAble
|
||||
# 发起踩的对象
|
||||
module Disser
|
||||
def self.included(base)
|
||||
base.extend ClassMethods
|
||||
base.send :include, InstanceMethods
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# Those call this method has the ability to be unlike by others
|
||||
def acts_as_disser
|
||||
include ActsAsAble::AbleExt
|
||||
has_many :disses, as: :disser, dependent: :destroy, class_name: 'Diss' # 有很多不喜欢的 Object
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
# 踩某对象
|
||||
def diss(obj)
|
||||
self.disses.find_or_create_by(dissable_type: class_name(obj), dissable_id: obj.id)
|
||||
end
|
||||
|
||||
# 取消对某对象对踩
|
||||
def undiss(obj)
|
||||
diss_by(obj).destroy
|
||||
end
|
||||
|
||||
# 是否踩了某对象
|
||||
def diss?(obj)
|
||||
!diss_by(obj).blank?
|
||||
end
|
||||
|
||||
# 查看某种类型踩的所有对象
|
||||
def dissings(dissable_type)
|
||||
return dissable_type.constantize.where(id: self.disses.where(dissable_type: dissable_type).pluck(:dissable_id))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def diss_by(obj)
|
||||
self.disses.find_by(dissable_type: class_name(obj), dissable_id: obj.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -19,7 +19,7 @@ module ActsAsAble
|
|||
# # 3: 是否关注
|
||||
# # 4: 关注了某个 model 多少个 obj
|
||||
def follow(obj)
|
||||
self.follows.find_or_create_by(followable_id: obj.id, followable_type: class_name(obj)) if obj != self
|
||||
self.follows.find_or_create_by(followable_id: obj.id, followable_type: class_name(obj))
|
||||
end
|
||||
|
||||
def unfollow(obj)
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
module ActsAsAble
|
||||
# 被赞的对象
|
||||
module Likable
|
||||
def self.included(base)
|
||||
base.extend ClassMethods
|
||||
base.send :include, InstanceMethods
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# Those call this method has the ability to be unlike by others
|
||||
def acts_as_likable
|
||||
include ActsAsAble::AbleExt
|
||||
has_many :likers, as: :likable, dependent: :destroy, class_name: 'Like'
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
def like_count
|
||||
self.likers.count
|
||||
end
|
||||
|
||||
def likers_by_type(liker_type, options = {})
|
||||
ids = Like.
|
||||
where('likable_id' => self.id,
|
||||
'likable_type' => class_name(self),
|
||||
'liker_type' => liker_type.name
|
||||
).pluck('liker_id')
|
||||
return liker_type.where("id in (?)", ids)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,45 @@
|
|||
module ActsAsAble
|
||||
# 发起赞的对象
|
||||
module Liker
|
||||
def self.included(base)
|
||||
base.extend ClassMethods
|
||||
base.send :include, InstanceMethods
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# Those call this method has the ability to be unlike by others
|
||||
def acts_as_liker
|
||||
include ActsAsAble::AbleExt
|
||||
has_many :likes, as: :liker, dependent: :destroy, class_name: 'Like' # 有很多不喜欢的 Object
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
# 赞某对象
|
||||
def like(obj)
|
||||
self.likes.find_or_create_by(likable_type: class_name(obj), likable_id: obj.id)
|
||||
end
|
||||
|
||||
# 取消对某对象的赞
|
||||
def unlike(obj)
|
||||
like_by(obj).destroy
|
||||
end
|
||||
|
||||
# 是否赞了某对象
|
||||
def like?(obj)
|
||||
!like_by(obj).blank?
|
||||
end
|
||||
|
||||
# 查看某种类型踩的所有对象
|
||||
def likings(likable_type)
|
||||
return likable_type.constantize.where(id: self.likes.where(likable_type: likable_type).pluck(:likable_id))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def like_by(obj)
|
||||
self.likes.find_by(likable_type: class_name(obj), likable_id: obj.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,23 +0,0 @@
|
|||
module ActsAsAble
|
||||
# 不喜欢的对象
|
||||
module Unlikable
|
||||
def self.included(base)
|
||||
base.extend ClassMethods
|
||||
base.send :include, InstanceMethods
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# Those call this method has the ability to be unlike by others
|
||||
def acts_as_unlikable
|
||||
include ActsAsAble::AbleExt
|
||||
has_many :unlikers, as: :unlikable, dependent: :destroy, class_name: 'Unlike'
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
def unlike_count
|
||||
self.unlikers.count
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,35 +0,0 @@
|
|||
module ActsAsAble
|
||||
# 不喜欢
|
||||
module Unliker
|
||||
def self.included(base)
|
||||
base.extend ClassMethods
|
||||
base.send :include, InstanceMethods
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# Those call this method has the ability to be unlike by others
|
||||
def acts_as_unliker
|
||||
include ActsAsAble::AbleExt
|
||||
has_many :unlikes, as: :unliker, dependent: :destroy, class_name: 'Unlike' # 有很多不喜欢的 Object
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
# 不喜欢某对象
|
||||
def unlike(obj)
|
||||
self.unlikes.find_or_create_by(unlikable_type: class_name(obj), unlikable_id: obj.id) if obj != self
|
||||
end
|
||||
|
||||
# 是否不喜欢某对象
|
||||
def unlike?(obj)
|
||||
!unlike_by(obj).blank?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def unlike_by(obj)
|
||||
self.unlikes.find_by(unlikable_type: class_name(obj), unlikable_id: obj.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,3 +1,3 @@
|
|||
module ActsAsAble
|
||||
VERSION = "0.1.1"
|
||||
VERSION = "1.0.0"
|
||||
end
|
||||
|
|
|
@ -18,6 +18,15 @@ module ActsAsAble
|
|||
def view_count
|
||||
self.viewers.count
|
||||
end
|
||||
|
||||
def viewers_by_type(viewer_type, options = {})
|
||||
ids = View.
|
||||
where('viewable_id' => self.id,
|
||||
'viewable_type' => class_name(self),
|
||||
'viewer_type' => viewer_type.name
|
||||
).pluck('viewer_id')
|
||||
return viewer_type.where("id in (?)", ids)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
module ActsAsAble
|
||||
# 不喜欢
|
||||
# 发起浏览的对象
|
||||
module Viewer
|
||||
def self.included(base)
|
||||
base.extend ClassMethods
|
||||
|
@ -15,16 +15,21 @@ module ActsAsAble
|
|||
end
|
||||
|
||||
module InstanceMethods
|
||||
# 不喜欢某对象
|
||||
# 浏览某对象
|
||||
def view(obj)
|
||||
self.views.find_or_create_by(viewable_type: class_name(obj), viewable_id: obj.id) if obj != self
|
||||
self.views.create(viewable_type: class_name(obj), viewable_id: obj.id)
|
||||
end
|
||||
|
||||
# 是否不喜欢某对象
|
||||
# 是否浏览某对象
|
||||
def view?(obj)
|
||||
!view_by(obj).blank?
|
||||
end
|
||||
|
||||
# 查看某种类型浏览的所有对象
|
||||
def viewings(viewable_type)
|
||||
return viewable_type.constantize.where(id: self.views.where(viewable_type: viewable_type).pluck(:viewable_id))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def view_by(obj)
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
require 'generators/acts_as_able_generator'
|
||||
|
||||
class ActsAsDissableGenerator < ActsAsAbleGenerator
|
||||
def create_migration_file
|
||||
migration_template 'diss_migration.rb', 'db/migrate/acts_as_diss_migration.rb'
|
||||
end
|
||||
|
||||
def create_model
|
||||
template "diss.rb", File.join('app/models', "diss.rb")
|
||||
end
|
||||
end
|
|
@ -0,0 +1,11 @@
|
|||
require 'generators/acts_as_able_generator'
|
||||
|
||||
class ActsAsLikableGenerator < ActsAsAbleGenerator
|
||||
def create_migration_file
|
||||
migration_template 'like_migration.rb', 'db/migrate/acts_as_like_migration.rb'
|
||||
end
|
||||
|
||||
def create_model
|
||||
template "like.rb", File.join('app/models', "like.rb")
|
||||
end
|
||||
end
|
|
@ -1,11 +0,0 @@
|
|||
require 'generators/acts_as_able_generator'
|
||||
|
||||
class ActsAsUnlikableGenerator < ActsAsAbleGenerator
|
||||
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
|
|
@ -0,0 +1,6 @@
|
|||
class Diss < ApplicationRecord
|
||||
|
||||
belongs_to :dissable, :polymorphic => true
|
||||
belongs_to :disser, :polymorphic => true
|
||||
|
||||
end
|
|
@ -0,0 +1,13 @@
|
|||
class ActsAsDissableMigration < ActiveRecord::Migration[5.0]
|
||||
def self.up
|
||||
create_table :disses, force: true do |t|
|
||||
t.references :dissable, polymorphic: true, null: false
|
||||
t.references :disser, polymorphic: true, null: false
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
|
||||
def self.down
|
||||
drop_table :disses
|
||||
end
|
||||
end
|
|
@ -1,4 +1,4 @@
|
|||
class Follow < ActiveRecord::Base
|
||||
class Follow < ApplicationRecord
|
||||
|
||||
belongs_to :followable, :polymorphic => true
|
||||
belongs_to :follower, :polymorphic => true
|
||||
|
|
|
@ -0,0 +1,6 @@
|
|||
class Like < ApplicationRecord
|
||||
|
||||
belongs_to :likable, :polymorphic => true
|
||||
belongs_to :liker, :polymorphic => true
|
||||
|
||||
end
|
|
@ -0,0 +1,13 @@
|
|||
class ActsAsLikableMigration < ActiveRecord::Migration[5.0]
|
||||
def self.up
|
||||
create_table :likes, force: true do |t|
|
||||
t.references :likable, polymorphic: true, null: false
|
||||
t.references :liker, polymorphic: true, null: false
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
|
||||
def self.down
|
||||
drop_table :likes
|
||||
end
|
||||
end
|
|
@ -1,6 +0,0 @@
|
|||
class Unlike < ActiveRecord::Base
|
||||
|
||||
belongs_to :unlikable, :polymorphic => true
|
||||
belongs_to :unliker, :polymorphic => true
|
||||
|
||||
end
|
|
@ -1,13 +0,0 @@
|
|||
class ActsAsUnlikableMigration < ActiveRecord::Migration[5.0]
|
||||
def self.up
|
||||
create_table :unlikes, force: true do |t|
|
||||
t.references :unlikable, polymorphic: true, null: false
|
||||
t.references :unliker, polymorphic: true, null: false
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
|
||||
def self.down
|
||||
drop_table :unlikes
|
||||
end
|
||||
end
|
|
@ -1,4 +1,4 @@
|
|||
class View < ActiveRecord::Base
|
||||
class View < ApplicationRecord
|
||||
|
||||
belongs_to :viewable, :polymorphic => true
|
||||
belongs_to :viewer, :polymorphic => true
|
||||
|
|
Loading…
Reference in New Issue