Class: Mensa::Tables::ViewsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/mensa/tables/views_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject

Persists the current filters, ordering and search query as a named custom view, owned by the current user. Without a current user there is nobody to own the view, so the request is rejected (and the UI hides the save button in that case).



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/mensa/tables/views_controller.rb', line 8

def create
  user = current_mensa_user
  return head(:forbidden) if user.blank?

  view = Mensa::TableView.new(
    table_name: params[:table_id],
    name: params[:name],
    description: params[:description],
    config: view_config,
    user: user
  )

  if view.save
    respond_to do |format|
      format.turbo_stream do
        # Re-use the same turbo_frame_id that the client sent so the
        # generated element IDs match what is already in the DOM.
        table_config = view.config
          .deep_transform_keys(&:to_sym)
          .merge(turbo_frame_id: params[:turbo_frame_id])

        @table = Mensa.for_name(params[:table_id], table_config)
        @table.request = request
        @table.original_view_context = helpers
        @table.table_view = view
      end
      format.json { render json: {id: view.id, name: view.name}, status: :created }
    end
  else
    render json: {errors: view.errors.full_messages}, status: :unprocessable_entity
  end
end

#destroyObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/mensa/tables/views_controller.rb', line 73

def destroy
  user = current_mensa_user
  return head(:forbidden) if user.blank?

  view = Mensa::TableView.find_by(table_name: params[:table_id], id: params[:id], user: user)
  return head(:not_found) if view.blank?

  view.destroy

  respond_to do |format|
    format.turbo_stream do
      @table = Mensa.for_name(params[:table_id], {turbo_frame_id: params[:turbo_frame_id]})
      @table.request = request
      @table.original_view_context = helpers
    end
    format.json { head :no_content }
  end
end

#updateObject



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
68
69
70
71
# File 'app/controllers/mensa/tables/views_controller.rb', line 41

def update
  user = current_mensa_user
  return head(:forbidden) if user.blank?

  view = Mensa::TableView.find_by(table_name: params[:table_id], id: params[:id], user: user)
  return head(:not_found) if view.blank?

  # When only renaming, preserve the existing config rather than overwriting
  # with an empty hash from view_config.
  new_config = view_config
  update_attrs = {config: new_config.present? ? new_config : view.config}
  update_attrs[:name] = params[:name] if params[:name].present?

  if view.update(update_attrs)
    respond_to do |format|
      format.turbo_stream do
        table_config = view.config
          .deep_transform_keys(&:to_sym)
          .merge(turbo_frame_id: params[:turbo_frame_id])

        @table = Mensa.for_name(params[:table_id], table_config)
        @table.request = request
        @table.original_view_context = helpers
        @table.table_view = view
      end
      format.json { render json: {id: view.id, name: view.name} }
    end
  else
    render json: {errors: view.errors.full_messages}, status: :unprocessable_entity
  end
end