1
0

preview_card_batch.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. # frozen_string_literal: true
  2. class Trends::PreviewCardBatch
  3. include ActiveModel::Model
  4. include Authorization
  5. attr_accessor :preview_card_ids, :action, :current_account, :precision
  6. def save
  7. case action
  8. when 'approve'
  9. approve!
  10. when 'approve_providers'
  11. approve_providers!
  12. when 'reject'
  13. reject!
  14. when 'reject_providers'
  15. reject_providers!
  16. end
  17. end
  18. private
  19. def preview_cards
  20. @preview_cards ||= PreviewCard.where(id: preview_card_ids)
  21. end
  22. def preview_card_providers
  23. @preview_card_providers ||= preview_cards.map(&:domain).uniq.map { |domain| PreviewCardProvider.matching_domain(domain) || PreviewCardProvider.new(domain: domain) }
  24. end
  25. def approve!
  26. preview_cards.each { |preview_card| authorize(preview_card, :review?) }
  27. preview_cards.update_all(trendable: true)
  28. end
  29. def approve_providers!
  30. preview_card_providers.each do |provider|
  31. authorize(provider, :review?)
  32. provider.update(trendable: true, reviewed_at: action_time)
  33. end
  34. # Reset any individual overrides
  35. preview_cards.update_all(trendable: nil)
  36. end
  37. def reject!
  38. preview_cards.each { |preview_card| authorize(preview_card, :review?) }
  39. preview_cards.update_all(trendable: false)
  40. end
  41. def reject_providers!
  42. preview_card_providers.each do |provider|
  43. authorize(provider, :review?)
  44. provider.update(trendable: false, reviewed_at: action_time)
  45. end
  46. # Reset any individual overrides
  47. preview_cards.update_all(trendable: nil)
  48. end
  49. def action_time
  50. @action_time ||= Time.now.utc
  51. end
  52. end