Class: WcmsComponents::ChangesController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/wcms_components/changes_controller.rb

Instance Method Summary collapse

Instance Method Details

#indexObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/wcms_components/changes_controller.rb', line 6

def index
  @changes = policy_scope(Change)
  @changes = @changes.where(association_chain: {'$elemMatch' => {name: params[:klass].classify}})

  # Filter Values
  @available_users = User.find(@changes.distinct(:modifier_id))

  @changes = @changes.where(modifier_id: params[:user_id]) if params[:user_id].present?
  @changes = @changes.where(action: params[:action_taken]) if params[:action_taken].present?
  @changes = @changes.by_last_change(params[:last_change]) if params[:last_change].present?

  @changes = @changes.desc(:updated_at)
  @changes = @changes.page(params[:page]).per(25)
end

#object_indexObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/wcms_components/changes_controller.rb', line 21

def object_index
  # Retrieve object of who's history you are desiring after.
  @object = params[:klass].safe_constantize.find(params[:id])
  @changes = []
  @available_users = []
  object_history = @object.history_tracks # TODO: this seems to not be pulling the embedded document history
  @available_users += User.find(object_history.distinct(:modifier_id)) if object_history

  # Apply the filters -- if there are no results then it will be as an empty array
  @changes += set_filters(object_history)

  # Retrieve applicable nested object histories defined in the respective publishers settings
  fetch_nested_histories(@object) do |histories|
    if histories.present?
      @available_users += User.find(histories.distinct(:modifier_id))
      @changes += set_filters(histories)
    end
  end

  @available_users.uniq!

  @changes.sort!{ |a,b| b.created_at <=> a.created_at }
end

#undoObject



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/wcms_components/changes_controller.rb', line 45

def undo
  set_modifier

  if @change.undo! current_user
    if @change.action == 'create'
      flash[:info] = "Change was successfully reversed. <a href=wcms_components/changes/#{Change.last.id}/undo_destroy>Undo</a>"
    else
      flash[:info] = "Change was successfully reversed."
    end
  else
    flash[:error] = "Something went wrong. Please try again."
  end

  # Ensure that the object wasn't just undone into nonexistence.
  #  For the time being referenced documents will not be able to be undone as
  #  we have no way to redirect back to the owning object.
  @parent = params[:owning_class].safe_constantize
  if @parent.where(id: params[:owning_id]).present?
    redirect_to @parent.find(params[:owning_id])
  else
    redirect_to @parent
  end
end

#undo_destroyObject



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

def undo_destroy
  # Undo destory at the moment is only available in flash notices after destroy is completed.
  #  Because of this the modifier for the change will be set as the last known object modifier.
  #  This could cause issues so we may want to find a way to set it since mongoid history wont.
  if @change.undo!
    flash[:info] = "#{@change.original[:title]} has been successfully recreated."
  else
    flash[:error] = "Something went wrong. Please try again."
  end
  redirect_to :back
end