Class: Admin::PerspectivesController

Inherits:
BaseController
  • Object
show all
Defined in:
lib/generators/open_loam/install/templates/admin/perspectives_controller.rb

Overview

Managing saved views (OpenLoam::Perspective) for an entity. A view is CREATED by "save current view" on the entity index (capturing the live filters/sort); it starts private, owned by you. Here you rename it, widen its audience, make it the default, or delete it.

Who may manage a view: its owner always; a manager for role/tenant-visibility ones. A private view of someone else's is invisible here by construction (OpenLoam::Perspectives.visible_to never returns it).

Constant Summary

Constants included from Pagination

Admin::Pagination::PER_PAGE

Instance Method Summary collapse

Methods inherited from BaseController

skip_authorization!

Methods included from Pagination

#paginate

Instance Method Details

#create ⇒ Object

From the entity index: save the CURRENT filters/sort as a new private view.



30
31
32
33
34
35
36
37
38
39
# File 'lib/generators/open_loam/install/templates/admin/perspectives_controller.rb', line 30

def create
  perspective = OpenLoam::Perspective.create!(
    entity_type: params[:entity_type].to_s,
    name: params[:name].presence || "Saved view",
    visibility: "private",
    owner_id: current_actor.id,
    config: captured_config
  )
  redirect_to index_path_for(perspective), notice: "Saved the current view."
end

#destroy ⇒ Object



63
64
65
66
67
# File 'lib/generators/open_loam/install/templates/admin/perspectives_controller.rb', line 63

def destroy
  perspective = @perspective
  perspective.destroy!
  redirect_to index_path_for(perspective), notice: "View deleted."
end

#index ⇒ Object



24
25
26
27
# File 'lib/generators/open_loam/install/templates/admin/perspectives_controller.rb', line 24

def index
  @entity_type = params[:entity_type].to_s
  @perspectives = OpenLoam::Perspectives.visible_to(@entity_type, user: current_actor)
end

#set_default ⇒ Object



58
59
60
61
# File 'lib/generators/open_loam/install/templates/admin/perspectives_controller.rb', line 58

def set_default
  @perspective.make_default!
  redirect_to index_path_for(@perspective), notice: "Default view set."
end

#update ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/generators/open_loam/install/templates/admin/perspectives_controller.rb', line 41

def update
  # Widening a private view to role/tenant is a manager action; and changing
  # the audience clears the default flag so it is re-chosen deliberately.
  attributes = perspective_params
  if attributes[:visibility].present? && attributes[:visibility] != @perspective.visibility
    raise OpenLoam::NotAuthorizedError if attributes[:visibility] != "private" && current_role != :manager

    attributes[:is_default] = false
  end

  if @perspective.update(attributes)
    redirect_to index_path_for(@perspective), notice: "View updated."
  else
    redirect_to index_path_for(@perspective), alert: @perspective.errors.full_messages.to_sentence
  end
end