lib/active_recall/models/deck.rb in active_recall-1.6.3 vs lib/active_recall/models/deck.rb in active_recall-1.6.4
- old
+ new
@@ -40,25 +40,16 @@
ActiveRecall::Item
.find_by(deck: self, source_id: source.id, source_type: source.class.name)
.destroy
end
- # OPTIMIZE: Attempt in active record, rather than array of Ruby records
def review
- %i[untested failed expired].inject([]) do |words, s|
- words += items.send(s).order(random_order_function).map(&:source)
- end
+ _review.map(&:source)
end
- # OPTIMIZE: Use optimized #review and build only the record to be returned
def next
- word = nil
- %i[untested failed expired].each do |category|
- word = items.send(category).order(random_order_function).limit(1).map(&:source).first
- break if word
- end
- word
+ _review.first.try(:source)
end
def last
items.order('created_at desc').limit(1).first.try(:source)
end
@@ -86,9 +77,17 @@
def source_class
user.deck_name.to_s.singularize.titleize.constantize
end
private
+
+ def _review
+ items
+ .untested
+ .or(items.failed)
+ .or(items.expired)
+ .order(random_order_function)
+ end
def random_order_function
Arel.sql(mysql? ? 'RAND()' : 'random()')
end