Commit aa8811ee by Ivan Lan

Pasteable

parent 1aa69952
......@@ -3,6 +3,7 @@ require "acts_as_pasting/version"
module ActsAsPasting
require 'acts_as_pasting/errors'
autoload :Pasted, 'acts_as_pasting/pasted'
autoload :Pasteable, 'acts_as_pasting/pasteable'
autoload :Pasting, 'acts_as_pasting/pasting'
def self.table_name_prefix
......
module ActsAsPasting
module Pasteable
extend ActiveSupport::Concern
# 带 prefix 的 pasted_with,独立于不带 prefix 的之外
included do
has_many :pastings, as: :pasteable, class_name: 'ActsAsPasting::Pasting'
has_many :hosts, through: :pastings
# ary => [[klass, id], [klass, id]]
# ary => [obj, obj]
scope :pasted_with_any, ->(ary, prefix: '') {
ary = parsed_condition_ary(ary)
ids = ary.map do |klass, id|
joins(:pastings).where(acts_as_pasting_pastings: {
pasted_type: klass.constantize.base_class.name, pasted_id: id, type: prefix
}).pluck(:id)
end.reduce(:|)
where(id: ids)
}
scope :pasted_with_all, ->(ary, prefix: '') {
ary = parsed_condition_ary(ary)
ids = ary.map do |klass, id|
joins(:pastings).where(acts_as_pasting_pastings: {
pasted_type: klass.constantize.base_class.name, pasted_id: id, type: prefix
}).pluck(:id)
end.reduce(:&)
where(id: ids)
}
end
module ClassMethods
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment