docbrown/app/lib/seeder.rb
Mac Siri d587ba962d
Add AlgoliaSearchable for User (#20869)
* Add AlgoliaSearchable::SearchableUser

* Update spec

* Refactor

* Refactor

* Add more test

* Refactor

* Update seeder to clear Algolia index if it exist

* All Algolia directly in update_score

* Fix broken spec

* Remove new queue

* Account for more user statuses

* Remove unncessary test for now
2024-04-19 14:03:04 +00:00

51 lines
1.3 KiB
Ruby

# rubocop:disable Rails/Output
class Seeder
def initialize
if Rails.env.production?
puts "Can't run seeds in production"
# rubocop:disable Rails/Exit
exit 1
# rubocop:enable Rails/Exit
end
@counter = 0
end
# Used when the block is idempotent by itself and needs no further checks.
def create(message)
@counter += 1
puts " #{@counter}. #{message}."
yield
end
def create_if_none(klass, count = nil)
@counter += 1
plural = klass.name.pluralize
if klass.none?
message = ["Creating", count, plural].compact.join(" ")
if klass.respond_to?(:algolia_search) && Settings::General.algolia_search_enabled?
puts " Algolia search enabled, clearing index for #{klass}..."
klass.clear_index!
end
puts " #{@counter}. #{message}."
yield
else
puts " #{@counter}. #{plural} already exist. Skipping."
end
end
def create_if_doesnt_exist(klass, attribute_name, attribute_value)
record = klass.find_by("#{attribute_name}": attribute_value)
if record.nil?
puts " #{klass} with #{attribute_name} = #{attribute_value} not found, proceeding..."
yield
else
puts " #{klass} with #{attribute_name} = #{attribute_value} found, skipping."
end
end
end
# rubocop:enable Rails/Output