Class: RailsMarkup::DashboardController

Inherits:
Object
  • Object
show all
Defined in:
app/controllers/rails_markup/dashboard_controller.rb

Constant Summary collapse

ALLOWED_STATUSES =
%w[all pending acknowledged resolved dismissed].freeze
ALLOWED_ROLES =
%w[agent user].freeze

Instance Method Summary collapse

Instance Method Details

#boardObject

GET /feedback/board



93
94
95
96
97
98
99
100
# File 'app/controllers/rails_markup/dashboard_controller.rb', line 93

def board
  @columns = {
    pending: Annotation.pending.recent.limit(50),
    acknowledged: Annotation.acknowledged.recent.limit(50),
    resolved: Annotation.resolved.recent.limit(20),
    dismissed: Annotation.dismissed.recent.limit(20)
  }
end

#dismiss_allObject

POST /feedback/dismiss_all



103
104
105
106
107
108
109
110
111
# File 'app/controllers/rails_markup/dashboard_controller.rb', line 103

def dismiss_all
  status = params[:status]
  unless status.in?(%w[pending acknowledged])
    return redirect_to root_path, alert: "Invalid status for bulk dismiss."
  end

  count = Annotation.where(status: status).update_all(status: "dismissed")
  redirect_to root_path(status: "dismissed"), notice: "#{count} annotations dismissed."
end

#export_csvObject

GET /feedback/export.csv



138
139
140
141
142
# File 'app/controllers/rails_markup/dashboard_controller.rb', line 138

def export_csv
  scope = build_export_scope
  csv_data = generate_csv(scope)
  send_data csv_data, filename: "annotations-#{Date.current}.csv", type: "text/csv"
end

#export_jsonObject

GET /feedback/export.json



145
146
147
148
149
# File 'app/controllers/rails_markup/dashboard_controller.rb', line 145

def export_json
  scope = build_export_scope
  json_data = scope.map(&:as_api_json).to_json
  send_data json_data, filename: "annotations-#{Date.current}.json", type: "application/json"
end

#indexObject

GET /feedback



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/controllers/rails_markup/dashboard_controller.rb', line 46

def index
  @current_status = ALLOWED_STATUSES.include?(params[:status]) ? params[:status] : "pending"
  base_scope = build_base_scope

  # Single grouped count query instead of 6 separate queries
  counts = base_scope.group(:status).count
  @total_count = counts.values.sum
  @pending_count = counts["pending"] || 0
  @acknowledged_count = counts["acknowledged"] || 0
  @resolved_count = counts["resolved"] || 0
  @dismissed_count = counts["dismissed"] || 0

  paginate(filtered_scope)

  @page_urls = Annotation.distinct.pluck(:page_url).sort
  @current_page_url = params[:page_url]
  @authors = Annotation.distinct_authors
  @current_author = params[:author]
  @current_query = params[:q]
end

#load_moreObject

GET /feedback/load_more



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

def load_more
  @current_status = ALLOWED_STATUSES.include?(params[:status]) ? params[:status] : "pending"
  @current_page_url = params[:page_url]
  @current_author = params[:author]
  @current_query = params[:q]

  # load_more only ever fetches the NEXT page. A missing/invalid cursor
  # (stale ?page= link, malformed cursor) must not silently re-serve page
  # one, which would append duplicate cards. Return an empty page instead.
  if valid_cursor?
    paginate(filtered_scope)
  else
    @annotations = []
    @next_page = false
    @remaining = 0
  end

  render partial: "annotation_page", layout: false
end

#showObject

GET /feedback/annotations/:id



68
69
# File 'app/controllers/rails_markup/dashboard_controller.rb', line 68

def show
end

#updateObject

PATCH /feedback/annotations/:id



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/controllers/rails_markup/dashboard_controller.rb', line 114

def update
  case params[:action_type]
  when "acknowledge" then @annotation.acknowledge!
  when "resolve"     then @annotation.resolve!(summary: params[:summary].presence)
  when "dismiss"     then @annotation.dismiss!(reason: params[:reason].presence)
  when "reply"
    role = ALLOWED_ROLES.include?(params[:role]) ? params[:role] : "agent"
    @annotation.add_reply!(message: params[:message], role: role)
  when "transition"
    new_status = params[:status]
    if Annotation::STATUSES.include?(new_status)
      @annotation.update!(status: new_status)
      return head :ok
    else
      return render json: { error: "invalid status" }, status: :unprocessable_entity
    end
  else
    return redirect_to annotation_path(@annotation), alert: "Unknown action."
  end

  redirect_to annotation_path(@annotation), notice: "Annotation updated."
end