Skip to content

Commit

Permalink
Add tags interactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kevindew committed Apr 17, 2019
1 parent 493835b commit b595e61
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 12 deletions.
14 changes: 2 additions & 12 deletions app/controllers/tags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,8 @@ def edit
end

def update
Edition.find_and_lock_current(document: params[:document]) do |edition|
updater = Versioning::RevisionUpdater.new(edition.revision, current_user)
updater.assign(tags: update_params(edition))

if updater.changed?
edition.assign_revision(updater.next_revision, current_user).save!
TimelineEntry.create_for_revision(entry_type: :updated_tags, edition: edition)
PreviewService.new(edition).try_create_preview
end

redirect_to edition.document
end
Tags::UpdateInteractor.call(params: params, user: current_user)
redirect_to document_path(params[:document])
end

private
Expand Down
54 changes: 54 additions & 0 deletions app/interactors/tags/update_interactor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

class Tags::UpdateInteractor
include Interactor

delegate :params,
:user,
:edition,
:unchanged,
to: :context

def call
Edition.transaction do
find_and_lock_edition
update_edition

create_timeline_entry
update_preview
end
end

private

def find_and_lock_edition
context.edition = Edition.lock.find_current(document: params[:document])
end

def update_edition
updater = Versioning::RevisionUpdater.new(edition.revision, user)
updater.assign(tags: update_params(edition))

if updater.changed?
edition.assign_revision(updater.next_revision, user).save!
else
context.fail!(unchanged: true)
end
end

def create_timeline_entry
TimelineEntry.create_for_revision(entry_type: :updated_tags, edition: edition)
end

def update_preview
PreviewService.new(edition).try_create_preview
end

def update_params(edition)
permits = edition.document_type.tags.map do |tag_field|
[tag_field.id, []]
end

params.fetch(:tags, {}).permit(Hash[permits])
end
end

0 comments on commit b595e61

Please sign in to comment.