Module: RailsInteractable::InstanceMethods
- Defined in:
- lib/rails_interactable.rb
Instance Method Summary collapse
-
#add_interaction(operator, type) ⇒ Object
通用方法.
-
#interacted_by?(operator, type) ⇒ Boolean
通用方法 - 使用 interaction_type.
- #interaction_count(type) ⇒ Object
-
#interactors(type) ⇒ Object
修改 interactors 方法,移除 joins,避免多态关联加载错误.
-
#interactors_ids(type) ⇒ Object
新增:获取互动者ID列表.
- #remove_interaction(operator, type) ⇒ Object
- #toggle_interaction(operator, type) ⇒ Object
Instance Method Details
#add_interaction(operator, type) ⇒ Object
通用方法
183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/rails_interactable.rb', line 183 def add_interaction(operator, type) unless RailsInteractable::Interaction.where( target: self, operator: operator, interaction_type: type.to_s ).exists? RailsInteractable::Interaction.create!( target: self, operator: operator, interaction_type: type.to_s ) end end |
#interacted_by?(operator, type) ⇒ Boolean
通用方法 - 使用 interaction_type
141 142 143 144 145 146 147 |
# File 'lib/rails_interactable.rb', line 141 def interacted_by?(operator, type) RailsInteractable::Interaction.where( target: self, operator: operator, interaction_type: type.to_s ).exists? end |
#interaction_count(type) ⇒ Object
178 179 180 |
# File 'lib/rails_interactable.rb', line 178 def interaction_count(type) RailsInteractable::Interaction.where(target: self, interaction_type: type.to_s).count end |
#interactors(type) ⇒ Object
修改 interactors 方法,移除 joins,避免多态关联加载错误
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/rails_interactable.rb', line 150 def interactors(type) # 首先获取所有相关交互的 operator_type 和 operator_id operator_data = RailsInteractable::Interaction .where(target: self, interaction_type: type.to_s) .pluck(:operator_type, :operator_id) # 按 operator_type 分组 operators_by_type = operator_data.group_by(&:first).transform_values { |group| group.map(&:last).uniq } # 为每种类型批量查询对应的模型实例 results = [] operators_by_type.each do |operator_type, operator_ids| operator_class = operator_type.constantize instances = operator_class.where(id: operator_ids) results.concat(instances) end results end |
#interactors_ids(type) ⇒ Object
新增:获取互动者ID列表
171 172 173 174 175 176 |
# File 'lib/rails_interactable.rb', line 171 def interactors_ids(type) RailsInteractable::Interaction .where(target: self, interaction_type: type.to_s) .pluck(:operator_id) .uniq end |
#remove_interaction(operator, type) ⇒ Object
197 198 199 200 201 202 203 204 |
# File 'lib/rails_interactable.rb', line 197 def remove_interaction(operator, type) interaction = RailsInteractable::Interaction.where( target: self, operator: operator, interaction_type: type.to_s ).first interaction&.destroy end |
#toggle_interaction(operator, type) ⇒ Object
206 207 208 209 210 211 212 |
# File 'lib/rails_interactable.rb', line 206 def toggle_interaction(operator, type) if interacted_by?(operator, type) remove_interaction(operator, type) else add_interaction(operator, type) end end |