Class: Georgia::RevisionsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/georgia/revisions_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#current_ability, #current_locale

Instance Method Details

#approveObject



82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/georgia/revisions_controller.rb', line 82

def approve
  authorize @revision
  if @revision.update(status: :published)
    @page.current_revision.update(status: :revision)
    @page.update(revision_id: @revision.id)
    CreateActivity.new(@revision, :approve, owner: current_user).call
    redirect_to edit_page_revision_path(@page, @revision), notice: "You have successfully approved and published #{@revision.title}."
  else
    redirect_to edit_page_revision_path(@page, @revision), alert: "Oups! Something went wrong."
  end
end

#declineObject



94
95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/georgia/revisions_controller.rb', line 94

def decline
  authorize @revision
  if @revision.update(status: :draft)
    CreateActivity.new(@page, :decline, owner: current_user).call
    message = "#{current_user.name} has successfully declined a review for #{@revision.title}."
    @revision.destroy
    redirect_to [:edit, @page], notice: message
  else
    redirect_to edit_page_revision_path(@page, @revision), alert: "Oups! Something went wrong."
  end
end

#destroyObject



43
44
45
46
47
48
49
50
51
# File 'app/controllers/georgia/revisions_controller.rb', line 43

def destroy
  authorize @revision
  # FIXME: Check if this is the last revision for the page. Can't delete the last revision.
  if @revision.destroy
    redirect_to [:edit, @page], notice: "Revision was successfully deleted."
  else
    redirect_to page_revisions_path(@page), alert: "Oups! Something went wrong."
  end
end

#draftObject



60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/georgia/revisions_controller.rb', line 60

def draft
  authorize @revision
  ensure_remove_previous_drafts_of_the_same_page
  if @draft = Georgia::CloneRevision.create(@revision, status: 'draft', revised_by_id: current_user.id)
    CreateActivity.new(@draft, :draft, owner: current_user).call
    redirect_to edit_page_revision_path(@page, @draft), notice: "You successfully created a new draft."
  else
    redirect_to edit_page_revision_path(@page, @revision), alert: "Oups! Something went wrong."
  end
end

#editObject



18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/georgia/revisions_controller.rb', line 18

def edit
  authorize @revision
  locale = params.fetch(:locale, current_locale)
  @activities = (@page.activities + @revision.activities).uniq.sort_by(&:created_at).reverse
  @slides = @revision.slides.ordered.with_locale(locale)
  @ui_sections = Georgia::UiSection.all
  @status_message = RevisionStatusMessage.new(current_user, @page, @revision)
  @awaiting_revisions = @page.revisions.where(status: Georgia::Revision.statuses[:review])
  @draft_revision = @page.revisions.where.not(id: @revision.id).where(user: current_user).where(status: [Georgia::Revision.statuses[:review], Georgia::Revision.statuses[:draft]]).first
end

#indexObject



8
9
10
11
# File 'app/controllers/georgia/revisions_controller.rb', line 8

def index
  @revisions = @page.revisions.order(created_at: :desc)
  authorize @revisions
end

#previewObject

Sends revision to main_app router FIXME: bypass this once Georgia will be loaded on root



55
56
57
58
# File 'app/controllers/georgia/revisions_controller.rb', line 55

def preview
  authorize @page
  redirect_to preview_url
end

#request_reviewObject



71
72
73
74
75
76
77
78
79
80
# File 'app/controllers/georgia/revisions_controller.rb', line 71

def request_review
  authorize @revision
  if @revision.update(status: :review)
    CreateActivity.new(@revision, :review, owner: current_user).call
    notify("#{current_user.name} is asking you to review #{@revision.title}.", edit_page_revision_path(@page, @revision, only_path: false))
    redirect_to edit_page_revision_path(@page, @revision), notice: "You successfully submited #{@revision.title} for review."
  else
    redirect_to edit_page_revision_path(@page, @revision), alert: "Oups! Something went wrong."
  end
end

#restoreObject



106
107
108
109
110
111
112
113
114
115
116
# File 'app/controllers/georgia/revisions_controller.rb', line 106

def restore
  authorize @revision
  if @revision.update(status: :published)
    @page.current_revision.update(status: :revision)
    @page.update(revision_id: @revision.id)
    CreateActivity.new(@revision, :restore, owner: current_user).call
    redirect_to @page, notice: "#{current_user.name} has successfully restored a previous revision for #{@revision.title}."
  else
    redirect_to page_revisions_path(@page), alert: "Oups! Something went wrong."
  end
end

#showObject



13
14
15
16
# File 'app/controllers/georgia/revisions_controller.rb', line 13

def show
  authorize @revision
  redirect_to edit_page_revision_path(@page, @revision)
end

#updateObject

Stores a copy of the current revision before updating



30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/georgia/revisions_controller.rb', line 30

def update
  authorize @revision
  update_with_service do |service|
    service_object = service.new(current_user, @page, @revision, sanitized_attributes)
    if service_object.call
      CreateActivity.new(@revision, :update, owner: current_user).call
      redirect_to edit_page_revision_path(@page, service_object.revision), notice: "#{service_object.revision.title} was successfully updated."
    else
      redirect_to edit_page_revision_path(@page, @revision), alert: service_object.revision.errors.full_messages.join('. ')
    end
  end
end