Improve handling of authors on stories
This commit is contained in:
parent
3a650b1cef
commit
742be4b6c4
2 changed files with 20 additions and 9 deletions
|
|
@ -62,6 +62,7 @@ module FicTracker::Models
|
|||
|
||||
def authors=(authors)
|
||||
to_add = []
|
||||
to_keep = []
|
||||
to_remove = self.authors.map(&:id)
|
||||
|
||||
authors.each do |entry|
|
||||
|
|
@ -70,17 +71,16 @@ module FicTracker::Models
|
|||
if aut
|
||||
to_add << aut
|
||||
else
|
||||
aut = self.authors.find { |c| c.slug == entry[:slug] }
|
||||
aut = FicTracker::Models::Author.find(backend_name: backend.name, slug: entry[:slug])
|
||||
|
||||
if aut
|
||||
aut.set(**entry)
|
||||
else
|
||||
unless aut
|
||||
entry[:backend_name] = backend.name
|
||||
aut = FicTracker::Models::Author.new(**entry)
|
||||
to_add << aut
|
||||
to_add << aut if id
|
||||
end
|
||||
to_keep << aut unless id
|
||||
end
|
||||
to_remove.delete aut.id
|
||||
to_remove.delete aut.id if id
|
||||
end
|
||||
|
||||
if id
|
||||
|
|
@ -94,7 +94,7 @@ module FicTracker::Models
|
|||
author_dataset.where(id: to_remove).destroy
|
||||
end
|
||||
else
|
||||
@authors = (@authors || []) + to_add - to_remove
|
||||
@authors = to_keep
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -2,9 +2,14 @@
|
|||
|
||||
require 'sinatra/base'
|
||||
|
||||
require_relative 'server/helpers'
|
||||
require_relative 'server/story'
|
||||
|
||||
module FicTracker
|
||||
# Web server for providing your fic tracking needs
|
||||
class Server < Sinatra::Base
|
||||
include Helpers
|
||||
|
||||
def initialize(*)
|
||||
@task_runner = Thread.new { background_tasks }
|
||||
|
||||
|
|
@ -17,6 +22,9 @@ module FicTracker
|
|||
end
|
||||
|
||||
configure do
|
||||
require 'sinatra/namespace'
|
||||
register Sinatra::Namespace
|
||||
|
||||
root = File.join(__dir__, '../..')
|
||||
|
||||
set :views, File.join(root, 'views')
|
||||
|
|
@ -98,6 +106,10 @@ module FicTracker
|
|||
collection&.save_changes
|
||||
end
|
||||
|
||||
map '/story' do
|
||||
run Story.new
|
||||
end
|
||||
|
||||
head '/story/:backend/*.*' do |_backend_name, slug, format|
|
||||
mime = nil
|
||||
case format
|
||||
|
|
@ -210,9 +222,8 @@ module FicTracker
|
|||
|
||||
FicTracker.cache.expire
|
||||
rescue StandardError => e
|
||||
FicTracker.logger.error "Failed when running background tasks, #{e.class}: #{e}\n#{e.backtrace[-5,5].join("\n ")}"
|
||||
FicTracker.logger.error "Error in background tasks, #{e.class}: #{e}\n#{e.backtrace[-5,5].join("\n ")}"
|
||||
ensure
|
||||
iter += 1
|
||||
sleep 30 * 60
|
||||
end
|
||||
rescue StandardError => e
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue