Module: RailsInteractable::OperatorInstanceMethods

Defined in:
lib/rails_interactable.rb

Overview

新增:Operator 相关的实例方法

Instance Method Summary collapse

Instance Method Details

#interacted_with?(target, type) ⇒ Boolean

通用方法:检查操作者是否对某个目标执行了特定类型互动



271
272
273
274
275
276
277
# File 'lib/rails_interactable.rb', line 271

def interacted_with?(target, type)
  RailsInteractable::Interaction.where(
    operator: self,
    target: target,
    interaction_type: type.to_s
  ).exists?
end

#interactions_of_type(type) ⇒ Object

通用方法:获取操作者发起的特定类型互动



218
219
220
221
222
223
# File 'lib/rails_interactable.rb', line 218

def interactions_of_type(type)
  RailsInteractable::Interaction.where(
    operator: self,
    interaction_type: type.to_s
  )
end

#target_ids_of_type(type) ⇒ Object

通用方法:获取操作者发起的特定类型互动的目标对象ID



254
255
256
257
258
259
# File 'lib/rails_interactable.rb', line 254

def target_ids_of_type(type)
  RailsInteractable::Interaction
    .where(operator: self, interaction_type: type.to_s)
    .pluck(:target_id)
    .uniq
end

#target_ids_of_type_and_class(type, target_class) ⇒ Object

新增:获取操作者发起的特定类型、特定目标模型的互动对象ID



262
263
264
265
266
267
268
# File 'lib/rails_interactable.rb', line 262

def target_ids_of_type_and_class(type, target_class)
  target_class_name = target_class.name
  RailsInteractable::Interaction
    .where(operator: self, interaction_type: type.to_s, target_type: target_class_name)
    .pluck(:target_id)
    .uniq
end

#targets_of_type(type) ⇒ Object

通用方法:获取操作者发起的特定类型互动的目标对象



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/rails_interactable.rb', line 226

def targets_of_type(type)
  interaction_targets = RailsInteractable::Interaction
                           .where(operator: self, interaction_type: type.to_s)
                           .pluck(:target_type, :target_id)

  targets_by_type = interaction_targets.group_by(&:first).transform_values { |group| group.map(&:last).uniq }

  results = []
  targets_by_type.each do |target_type, target_ids|
    target_class = target_type.constantize
    instances = target_class.where(id: target_ids)
    results.concat(instances)
  end

  results
end

#targets_of_type_and_class(type, target_class) ⇒ Object

新增:获取操作者发起的特定类型、特定目标模型的互动对象



244
245
246
247
248
249
250
251
# File 'lib/rails_interactable.rb', line 244

def targets_of_type_and_class(type, target_class)
  target_class_name = target_class.name
  interaction_target_ids = RailsInteractable::Interaction
                              .where(operator: self, interaction_type: type.to_s, target_type: target_class_name)
                              .pluck(:target_id)

  target_class.where(id: interaction_target_ids)
end