diff options
Diffstat (limited to 'app/models/favorite.rb')
-rw-r--r-- | app/models/favorite.rb | 41 |
1 files changed, 21 insertions, 20 deletions
diff --git a/app/models/favorite.rb b/app/models/favorite.rb index 701b8ca..5962f1c 100644 --- a/app/models/favorite.rb +++ b/app/models/favorite.rb @@ -2,30 +2,31 @@ class Favorite < ActiveRecord::Base belongs_to :tweet belongs_to :user - # Registers favorite event in bulk from an array of Streaming API events. - # This method doesn't update Tweet#reactions_count. - # - # @param [Array] array An array of Streaming API events. - def self.create_bulk_from_json(array) - return if array.empty? + class << self + # Registers favorite event in bulk from an array of Streaming API events. + # This method doesn't update Tweet#reactions_count. + # + # @param [Array] array An array of Streaming API events. + def create_bulk_from_json(array) + return if array.empty? - objects = array.map do |json| - { - user_id: json[:source][:id], - tweet_id: json[:target_object][:id] + keys = [:user_id, :tweet_id] + objects = array.map {|json| + [json[:source][:id], json[:target_object][:id]] } - end - self.import(objects.first.keys, objects.map(&:values), ignore: true) - end + import(keys, objects, ignore: true) + end - # Unregisters favorite event in bulk from an array of Streaming API 'unfavorite' events. - # This method doesn't update Tweet#reactions_count. - # - # @param [Array] array An array of Streaming API events. - def self.delete_bulk_from_json(array) - array.each do |json| - self.delete_all(user_id: json[:source][:id], tweet_id: json[:target_object][:id]) + # Unregisters favorite event in bulk from an array of Streaming API 'unfavorite' events. + # This method doesn't update Tweet#reactions_count. + # + # @param [Array] array An array of Streaming API events. + def delete_bulk_from_json(array) + array.each do |json| + delete_all(user_id: json[:source][:id], + tweet_id: json[:target_object][:id]) + end end end end |