Class: PapertrailController

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

Constant Summary

Constants included from ProjectsHelper

ProjectsHelper::CLASSIFIER, ProjectsHelper::CLASSIFIER_ANNOTATION

Instance Method Summary collapse

Methods included from RedirectHelper

#destroy_redirect

Methods included from RequestType

#json_request?

Methods included from LogRecent

#log_user_recent_route

Methods included from Cookies

#digest_cookie, #digested_cookie_exists?

Methods included from Whitelist

#whitelist_constantize

Methods included from ProjectsHelper

#cumulative_gb_per_year, #document_cumulative_gb_per_year, #document_gb_per_year, #gb_per_year, #image_cumulative_gb_per_year, #image_gb_per_year, #invalid_object, #project_classification, #project_link, #project_matches, #project_tag, #projects_list, #projects_search_form, #taxonworks_classification, #week_in_review_graphs

Methods included from Api::Intercept

#intercept_api

Methods included from TokenAuthentication

#intercept_project, #intercept_user, #intercept_user_or_project, #project_token_authenticate, #token_authenticate

Instance Method Details

#compareObject



69
70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/papertrail_controller.rb', line 69

def compare
  klass = whitelist_constantize(params.require(:object_type))
  @object = klass.find(params.require(:object_id))

  if invalid_object(@object)
    record_not_found
  else
    @result = Vendor::Papertrail.compare(@object, compare_params)
    @result ? render('compare') : record_not_found
  end
end

#compare_paramsObject (protected)



93
94
95
# File 'app/controllers/papertrail_controller.rb', line 93

def compare_params
  params.permit(:version_a, :version_b)
end

#indexObject



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'app/controllers/papertrail_controller.rb', line 4

def index
  respond_to do |format|
    format.html {
      redirect_to hub_path, notice: 'You requested a papertrail for nothing.' and return if params[:object_type].blank?
      klass = whitelist_constantize(params.require(:object_type))
      @object = klass.find(params[:object_id])
      record_not_found if invalid_object(@object)
      render :papertrail
    }
    # Oldest is *first*, newest last.
    format.json { @versions = papertrail_versions }
  end
end

#papertrail_versionsObject (protected)



83
84
85
86
87
88
89
90
# File 'app/controllers/papertrail_controller.rb', line 83

def papertrail_versions
  if o = GlobalID::Locator.locate(params.require(:object_global_id))
    render head 404, "content_type" => 'text/plain' if o.respond_to?(:project_id) && o.project_id != sessions_current_project_id
    o.versions
  else
    []
  end
end

#showObject



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

def show
  @version = PaperTrail::Version.find(params[:id])
  @object = @version.item
  if invalid_object(@object)
    record_not_found
  else
    render 'papertrail'
  end
end

#updateObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/papertrail_controller.rb', line 28

def update
  klass = whitelist_constantize(params.require(:object_type))
  @object = klass.find(params[:object_id])

  if invalid_object(@object)
    record_not_found
  else
    new_attributes = params[:attributes]

    if !new_attributes.nil?
      new_attributes.each do |key, value|
        if @object.has_attribute?(key)
          @object.assign_attributes(Hash[key, value])
        end
      end
    end

    if !@object.changed?
      flash[:notice] = 'No changes made!'
    elsif @object.save
      flash[:notice] = 'Successfully restored!'
    else
      flash[:alert] = 'Unsuccessfully restored!'
    end

    json_resp = { "url": papertrail_path(object_type: @object.class.base_class, object_id: @object.id) }

    # If the object is a child class of "ControlledVocabularyTerm" then we need to use the
    # type member variable since the class member variable doesn't reflect the new class
    # yet and the type is the correct one thus the link thats generated will be correct
    if ControlledVocabularyTerm > @object.class
      json_resp['url'] = papertrail_path(object_type: @object.type, object_id: @object.id)
    end

    respond_to do |format|
      format.html { redirect_to(papertrail_path(object_type: @object.type, object_id: @object.id)) }
      format.json { render json: json_resp }
    end
  end
end