Class: Observ::AnnotationsController

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

Instance Method Summary collapse

Instance Method Details

#createObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/observ/annotations_controller.rb', line 58

def create
  @annotation = @annotatable.annotations.build(annotation_params)

  if @annotation.save
    respond_to do |format|
      format.turbo_stream do
        streams = [
          turbo_stream.prepend(
            "annotations-list",
            partial: "observ/annotations/annotation",
            locals: { annotation: @annotation, annotatable: @annotatable }
          ),
          turbo_stream.replace(
            "annotation-form",
            partial: "observ/annotations/form",
            locals: { annotatable: @annotatable, annotation: @annotatable.annotations.build }
          ),
          turbo_stream.update("annotations-count", @annotatable.annotations.count)
        ]

        empty_state = helpers.(:div, id: "annotations-empty-state")
        streams << turbo_stream.remove("annotations-empty-state") if @annotatable.annotations.count == 1

        render turbo_stream: streams
      end
      format.html { redirect_back(fallback_location: root_path, notice: "Annotation added successfully.") }
    end
  else
    respond_to do |format|
      format.turbo_stream do
        render turbo_stream: turbo_stream.replace(
          "annotation-form",
          partial: "observ/annotations/form",
          locals: { annotatable: @annotatable, annotation: @annotation }
        )
      end
      format.html { redirect_back(fallback_location: root_path, alert: "Failed to add annotation.") }
    end
  end
end

#destroyObject



99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/observ/annotations_controller.rb', line 99

def destroy
  @annotation = @annotatable.annotations.find(params[:id])
  @annotation.destroy

  respond_to do |format|
    format.turbo_stream do
      render turbo_stream: turbo_stream.remove("annotation_#{@annotation.id}")
    end
    format.html { redirect_back(fallback_location: root_path, notice: "Annotation deleted.") }
  end
end

#exportObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/observ/annotations_controller.rb', line 39

def export
  @annotations = case params[:type]
  when "sessions"
    Observ::Annotation.where(annotatable_type: "Observ::Session").includes(:annotatable).order(created_at: :desc)
  when "traces"
    Observ::Annotation.where(annotatable_type: "Observ::Trace").includes(:annotatable).order(created_at: :desc)
  else
    Observ::Annotation.all.includes(:annotatable).order(created_at: :desc)
  end

  respond_to do |format|
    format.csv do
      send_data generate_csv(@annotations),
                filename: "annotations_#{params[:type] || 'all'}_#{Time.current.strftime('%Y%m%d_%H%M%S')}.csv",
                type: "text/csv"
    end
  end
end

#indexObject



5
6
7
# File 'app/controllers/observ/annotations_controller.rb', line 5

def index
  @annotations = @annotatable.annotations
end

#sessions_indexObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/controllers/observ/annotations_controller.rb', line 9

def sessions_index
  @sessions = Observ::Session.joins(:annotations)
    .distinct
    .order(created_at: :desc)
    .page(params[:page])
    .per(Observ.config.pagination_per_page)

  @annotations = Observ::Annotation
    .where(annotatable_type: "Observ::Session")
    .includes(:annotatable)
    .order(created_at: :desc)
    .page(params[:page])
    .per(Observ.config.pagination_per_page)
end

#traces_indexObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/observ/annotations_controller.rb', line 24

def traces_index
  @traces = Observ::Trace.joins(:annotations)
    .distinct
    .order(created_at: :desc)
    .page(params[:page])
    .per(Observ.config.pagination_per_page)

  @annotations = Observ::Annotation
    .where(annotatable_type: "Observ::Trace")
    .includes(:annotatable)
    .order(created_at: :desc)
    .page(params[:page])
    .per(Observ.config.pagination_per_page)
end