ideasbugs 1.1.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0f39a47c15692a574b7777e0c2fa49e6553de46115a5c971d0094710a5e64d3a
4
- data.tar.gz: aea8f55dc68b7a7a0cca8f683f7bbec1911a5fdc30ff8343ef82c87adc4a2dd6
3
+ metadata.gz: 9102e94f1a6b6dc0e69308a962680dbe6d8cfe827406ea40bf1176d75bc7c27f
4
+ data.tar.gz: 5de52c52583b442ea96453c59f7b2994b908465756e2a73a774681fbd948fe6f
5
5
  SHA512:
6
- metadata.gz: 96b2d648ca7b806b311af9cdf04b10066e6e4550a5ed0618c997534247367927275b49a644d4f6c7398ecd562db8061b96bbb7c76aeb6cf3e88306ed10631b4b
7
- data.tar.gz: 9478ed6b52f47b699d2c9431f5ba412b02b08490cb3833d8cb681f0ca5ed257591d49aa3f3a47e428f35900c5e89fbfbf848061402032c86c018536bd9cb923a
6
+ metadata.gz: 31abc9d426e616bf028ae4ae30ec5e8a6336204ca78d5f3daf4f7586b4cad8094aae4b13b87116b543310c47622716277cf6a1748d6fe2eb3fe59c9695dd44f8
7
+ data.tar.gz: 2ea6471a64ba1aacf7fb43d4deb7a6c1462d3aef605037cb4f25db9697bbd3a5ef648ffae65dc5704c0c663c5739407dd940f913b4133b158c6df889b6b4eb11
data/AGENTS.md CHANGED
@@ -90,6 +90,10 @@ With `show_button = false`, any element carrying `data-ideasbugs-open` opens the
90
90
 
91
91
  `open → in_review → resolved`, as plain strings in `Ideasbugs::Feedback::STATUSES` with a scope per status (`Feedback.open`, `.in_review`, `.resolved`) plus `newest_first`. Deliberately not an Active Record enum — `open` as an enum scope would collide with `Kernel#open`. Do not "modernize" it into an enum.
92
92
 
93
+ ### Duplicates
94
+
95
+ `duplicate.mark_duplicate_of!(original)` links two feedbacks asking for the same thing; `unmark_duplicate!` undoes it. A duplicate is a link, not a merge: never copy, move or delete its message, author or screenshots, and never change it through `update_all`. Go through these two methods, which keep the link one level deep and on one tenant. `bin/rails generate ideasbugs:duplicates` exists **only** to add the column to installs made before 1.3; a fresh install already has it.
96
+
93
97
  ### Multi-tenancy
94
98
 
95
99
  One resolver returning an **opaque key** — GlobalID, id, subdomain, slug. The gem never takes a foreign key into host models:
@@ -116,6 +120,7 @@ customer.feedback.open
116
120
  - **Do not expose screenshots by blob URL** — the gated route exists so a leaked signed URL cannot hand over a customer's screenshot.
117
121
  - **Do not set config outside the initializer.** `rate_limit` in particular is read once when the controller class loads; assigning config per-request mutates it process-wide.
118
122
  - **Do not convert the status strings to an enum** (see above).
123
+ - **Do not re-implement submission rules in a host API endpoint.** Call `Ideasbugs::Feedback.submit` (README → File feedback from your own API). The host endpoint brings its own authentication, `enabled`-style gate, throttle and tenant key.
119
124
 
120
125
  ### Configuration worth knowing
121
126
 
data/CHANGELOG.md CHANGED
@@ -2,6 +2,33 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [1.3.0] - 2026-11-10
6
+
7
+ - **Duplicates.** Mark a feedback as a duplicate of another from the dashboard
8
+ (`Feedback#mark_duplicate_of!`, undone with `unmark_duplicate!`). The
9
+ duplicate leaves the queue and its counts, keeps its own message, author and
10
+ screenshots, and is listed on the original, which shows how many it has. The
11
+ link stays one level deep and within one tenant. Deleting an original puts
12
+ its duplicates back in the queue.
13
+ - **Cost:** one nullable `duplicate_of_id` column. Existing installs run
14
+ `bin/rails generate ideasbugs:duplicates && bin/rails db:migrate`; until
15
+ then the dashboard behaves exactly as in 1.2, without duplicate controls.
16
+ Fresh installs get the column from `ideasbugs:install`. No initializer
17
+ change.
18
+ - Add a README recipe for filing feedback from a host's own API through
19
+ `Feedback.submit`.
20
+
21
+ ## [1.2.0] - 2026-11-09
22
+
23
+ - Add `Ideasbugs::Feedback.submit(attributes, author:, tenant:, user_agent:,
24
+ screenshots:)`, the widget's submission rules as one call: page URL privacy
25
+ filtering, author attribution, screenshot limits and the `on_submit` hook.
26
+ A host exposing feedback through its own API calls it instead of
27
+ re-implementing them. Only `kind`, `section`, `message` and `page_url` are
28
+ taken from attributes. The widget endpoint now uses the same method; its
29
+ responses and error messages are unchanged. No migration or initializer
30
+ change.
31
+
5
32
  ## [1.1.0] - 2026-10-18
6
33
 
7
34
  - Add Rails edge CI and experimental Ruby 4.0+ Ractor request support. The host
data/README.md CHANGED
@@ -74,7 +74,7 @@ the things it should not do. It ships inside the gem, so
74
74
  | | |
75
75
  | ------------ | ------------------------------------------------------------------------ |
76
76
  | **Widget** | Type (bug / feature / other), optional section, message, up to 3 screenshots |
77
- | **Dashboard**| Status tabs (open → in review → resolved), type filter, search, inline screenshots |
77
+ | **Dashboard**| Status tabs (open → in review → resolved), type filter, search, inline screenshots, duplicates |
78
78
  | **Storage** | `ideasbugs_feedbacks` — ordinary Active Record rows you can query and export |
79
79
  | **Deps** | None. Plain JS — no Tailwind, no Stimulus, no importmap, no build step |
80
80
  | **Auth** | Lambdas over the raw request — Devise, Rails 8 auth, Basic auth, anything |
@@ -223,7 +223,90 @@ end
223
223
  Each row stores `kind`, `section`, `message`, `status` (`open` / `in_review` /
224
224
  `resolved`), `page_url`, `user_agent`, an optional `tenant`, and optional
225
225
  `author_id` / `author_label`. Screenshots are Active Storage attachments
226
- (`feedback.screenshots`).
226
+ (`feedback.screenshots`). `duplicate_of_id` points a duplicate at its original.
227
+
228
+ ## Duplicates
229
+
230
+ When two people ask for the same thing, open one in the dashboard and mark it
231
+ as a duplicate of the other by number. The duplicate leaves the queue but keeps
232
+ its own words, author and screenshots, and the original lists it with a count.
233
+ **Not a duplicate** puts it back. Nothing is merged, so marking is safe to undo.
234
+
235
+ ```ruby
236
+ duplicate.mark_duplicate_of!(original) # one level deep, see below
237
+ original.duplicates # an ordinary relation
238
+ Ideasbugs::Feedback.originals # everything that isn't a duplicate
239
+ duplicate.unmark_duplicate!
240
+ ```
241
+
242
+ The link is always one level deep. A duplicate of a duplicate points at the
243
+ original, marking an original moves its own duplicates along with it, and
244
+ marking an original as a duplicate of one of its duplicates swaps the two.
245
+ Both must be on the same board (tenant). Deleting an original puts its
246
+ duplicates back in the queue. A duplicate follows its original's status in the
247
+ dashboard and keeps its own `status` for when it is unmarked.
248
+
249
+ **Upgrading from 1.2 or earlier:** run
250
+ `bin/rails generate ideasbugs:duplicates && bin/rails db:migrate`. Until then
251
+ the dashboard works as before, without duplicate controls.
252
+
253
+ ## File feedback from your own API
254
+
255
+ The widget is one way in. If your app already has an API, one endpoint lets
256
+ scripts, integrations and AI agents file feedback with the tokens they already
257
+ use for the rest of it. `Ideasbugs::Feedback.submit` applies the widget's rules
258
+ (page URL privacy filtering, attribution, screenshot limits, `on_submit`), so
259
+ API feedback lands in the same dashboard and nothing drifts:
260
+
261
+ ```ruby
262
+ # config/routes.rb
263
+ namespace :api do
264
+ resource :feedback, only: :create
265
+ end
266
+ ```
267
+
268
+ ```ruby
269
+ # app/controllers/api/feedbacks_controller.rb
270
+ class Api::FeedbacksController < Api::BaseController # your token-authenticated base
271
+ rate_limit to: 10, within: 1.minute, by: -> { current_user.id }
272
+
273
+ def create
274
+ feedback = Ideasbugs::Feedback.submit(
275
+ params.require(:feedback).permit(:kind, :section, :message, :page_url),
276
+ author: current_user,
277
+ tenant: Current.organization&.to_gid&.to_s, # what config.tenant returns; omit for one board
278
+ user_agent: request.user_agent
279
+ )
280
+
281
+ if feedback.persisted?
282
+ render json: { id: feedback.id, kind: feedback.kind, status: feedback.status }, status: :created
283
+ else
284
+ render json: { errors: feedback.errors.full_messages }, status: :unprocessable_entity
285
+ end
286
+ end
287
+ end
288
+ ```
289
+
290
+ ```bash
291
+ curl -X POST https://app.example.com/api/feedback \
292
+ -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
293
+ -d '{"feedback":{"kind":"bug","message":"The yearly export times out."}}'
294
+ ```
295
+
296
+ `submit` reads only `kind`, `section`, `message` and `page_url`, and returns
297
+ the record: check `persisted?` and `errors`. Your endpoint still owns:
298
+
299
+ - **Authentication.** The gem has no tokens of its own. Attribute feedback to
300
+ the token's user so the dashboard shows who sent it.
301
+ - **The gates.** `config.enabled` and `config.rate_limit` guard the widget's
302
+ endpoint only. Apply the same rule and a throttle here.
303
+ - **The tenant.** Pass the key `config.tenant` would return for that user, or
304
+ the feedback lands on the global board.
305
+ - **Screenshots.** Pass uploaded files as `screenshots:` from a multipart
306
+ request. A JSON-only endpoint can stay text-only.
307
+
308
+ To let agents find it, list the endpoint in your API docs or OpenAPI schema
309
+ like any other.
227
310
 
228
311
  ## Multi-tenancy
229
312
 
@@ -276,8 +359,10 @@ stable under semantic versioning:
276
359
 
277
360
  - `Ideasbugs::Feedback`, including `kind`, `section`, `message`, `status`,
278
361
  `page_url`, `user_agent`, `tenant`, `author_id`, `author_label`, timestamps,
279
- `STATUSES`, status helpers/scopes, `newest_first`, `for_tenant`, and gated
280
- `screenshots`.
362
+ `STATUSES`, status helpers/scopes, `newest_first`, `for_tenant`, gated
363
+ `screenshots`, `Feedback.submit`, and the duplicate API (`duplicate_of`,
364
+ `duplicates`, `duplicate?`, `originals`, `mark_duplicate_of!`,
365
+ `unmark_duplicate!`).
281
366
  - `has_feedback` and the host-model collection it defines.
282
367
  - `Ideasbugs.configure` and the documented configuration options.
283
368
  - `mount_ideasbugs`, `ideasbugs_tag`, and `data-ideasbugs-open`.
@@ -1,7 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'uri'
4
-
5
3
  module Ideasbugs
6
4
  # Who is asking, which tenant they are in, and the gates that answer both.
7
5
  #
@@ -49,23 +47,5 @@ module Ideasbugs
49
47
  def tenant_scope
50
48
  Feedback.for_tenant(current_tenant)
51
49
  end
52
-
53
- # Browser URLs can carry password-reset tokens, signed ids, and campaign
54
- # details in their query or fragment. Store only a bounded HTTP(S) location
55
- # without credentials before it reaches the feedback table.
56
- def clean_page_url(value)
57
- raw = value.to_s
58
- return if raw.blank? || raw.length > 2_048
59
-
60
- uri = URI.parse(raw)
61
- return unless %w[http https].include?(uri.scheme&.downcase)
62
- return if uri.host.blank? || uri.userinfo.present?
63
-
64
- uri.query = nil
65
- uri.fragment = nil
66
- uri.to_s.first(255)
67
- rescue URI::InvalidURIError
68
- nil
69
- end
70
50
  end
71
51
  end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ideasbugs
4
+ # Files a feedback under another one asking for the same thing, or takes it
5
+ # back out. Both ends resolve through the tenant scope, so a duplicate can
6
+ # never point across boards.
7
+ class DuplicatesController < DashboardController
8
+ before_action -> { head :not_found unless Feedback.tracks_duplicates? }
9
+ before_action -> { @feedback = tenant_scope.find(params[:feedback_id]) }
10
+
11
+ def create
12
+ original = tenant_scope.find_by(id: params[:original_id].to_s.delete_prefix('#').strip)
13
+ unless original
14
+ return refuse(t('ideasbugs.dashboard.duplicate_not_found', default: 'No feedback with that number.'))
15
+ end
16
+
17
+ @feedback.mark_duplicate_of!(original)
18
+ original = @feedback.duplicate_of
19
+ redirect_to feedbacks_path(status: original.status, feedback_id: original.id), status: :see_other
20
+ rescue ActiveRecord::RecordInvalid => e
21
+ refuse(e.record.errors.full_messages.to_sentence)
22
+ end
23
+
24
+ def destroy
25
+ @feedback.unmark_duplicate!
26
+ redirect_to feedbacks_path(status: @feedback.status, feedback_id: @feedback.id), status: :see_other
27
+ end
28
+
29
+ private
30
+
31
+ def refuse(message)
32
+ redirect_back fallback_location: feedbacks_path(feedback_id: @feedback.id), alert: message, status: :see_other
33
+ end
34
+ end
35
+ end
@@ -11,15 +11,16 @@ module Ideasbugs
11
11
  @status = Feedback::STATUSES.include?(params[:status]) ? params[:status] : 'open'
12
12
  @kind = Ideasbugs.config.kinds.map(&:to_s).include?(params[:kind]) ? params[:kind] : nil
13
13
  @query = params[:q].to_s.strip.presence
14
- @counts = tenant_scope.group(:status).count
14
+ @counts = tenant_scope.originals.group(:status).count
15
15
 
16
- scope = tenant_scope.where(status: @status)
16
+ scope = tenant_scope.originals.where(status: @status)
17
17
  scope = scope.where(kind: @kind) if @kind
18
18
  scope = search(scope) if @query
19
19
  @page = [params[:page].to_i, 1].max
20
20
  @feedbacks = scope.newest_first.offset((@page - 1) * PER_PAGE).limit(PER_PAGE + 1).to_a
21
21
  @more = @feedbacks.size > PER_PAGE
22
22
  @feedbacks = @feedbacks.first(PER_PAGE)
23
+ @duplicate_counts = duplicate_counts(@feedbacks)
23
24
 
24
25
  @selected_feedback = tenant_scope.find_by(id: params[:feedback_id]) if params[:feedback_id].present?
25
26
 
@@ -43,6 +44,13 @@ module Ideasbugs
43
44
 
44
45
  private
45
46
 
47
+ # One grouped query for the whole page rather than a count per row.
48
+ def duplicate_counts(feedbacks)
49
+ return {} unless Feedback.tracks_duplicates?
50
+
51
+ tenant_scope.where(duplicate_of_id: feedbacks.map(&:id)).group(:duplicate_of_id).count
52
+ end
53
+
46
54
  def set_feedback
47
55
  @feedback = tenant_scope.find(params[:id])
48
56
  end
@@ -22,17 +22,15 @@ module Ideasbugs
22
22
  end
23
23
 
24
24
  def create
25
- feedback = Feedback.new(feedback_params)
26
- feedback.page_url = clean_page_url(feedback.page_url)
27
- feedback.user_agent = request.user_agent
28
- feedback.tenant = current_tenant
29
- attribute_author(feedback)
30
-
31
- error = attach_screenshots(feedback)
32
- return render json: { errors: [error] }, status: :unprocessable_entity if error
33
-
34
- if feedback.save
35
- notify_host(feedback)
25
+ feedback = Feedback.submit(
26
+ params.require(:feedback).permit(*Feedback::SUBMITTED_ATTRIBUTES),
27
+ author: current_author,
28
+ tenant: current_tenant,
29
+ user_agent: request.user_agent,
30
+ screenshots: params.dig(:feedback, :screenshots)
31
+ )
32
+
33
+ if feedback.persisted?
36
34
  head :created
37
35
  else
38
36
  render json: { errors: feedback.errors.full_messages }, status: :unprocessable_entity
@@ -41,61 +39,8 @@ module Ideasbugs
41
39
 
42
40
  private
43
41
 
44
- # The host's hook must never turn a saved submission into a 500 — the
45
- # feedback is in the database; notification failures are the host's logs'
46
- # problem.
47
- def notify_host(feedback)
48
- Ideasbugs.config.on_submit.call(feedback)
49
- rescue StandardError => e
50
- Rails.logger.error("ideasbugs: on_submit hook raised #{e.class}: #{e.message}")
51
- end
52
-
53
- def feedback_params
54
- params.require(:feedback).permit(:kind, :section, :message, :page_url)
55
- end
56
-
57
- def attribute_author(feedback)
58
- author = current_author
59
- return unless author
60
-
61
- feedback.author_id = author.id.to_s if author.respond_to?(:id)
62
- feedback.author_label = Ideasbugs.config.author_label.call(author)
63
- end
64
-
65
- # Validates and attaches uploads. Returns an error message, or nil when
66
- # everything (including "no screenshots at all") is fine.
67
- def attach_screenshots(feedback)
68
- files = Array(params.dig(:feedback, :screenshots)).reject(&:blank?)
69
- return nil if files.empty?
70
- return t_error(:error_save) unless Ideasbugs.config.screenshots_enabled?
71
- return t_error(:error_too_many, count: Ideasbugs.config.max_screenshots) if too_many?(files)
72
- return t_error(:error_too_large, size: max_size_mb) if files.any? { |f| f.size > max_size }
73
- return t_error(:error_save) unless files.all? { |f| f.content_type.to_s.start_with?('image/') }
74
-
75
- feedback.screenshots.attach(files)
76
- nil
77
- end
78
-
79
- def too_many?(files)
80
- files.size > Ideasbugs.config.max_screenshots
81
- end
82
-
83
- def max_size_mb
84
- max_size / (1024 * 1024)
85
- end
86
-
87
- def max_size
88
- Ideasbugs.config.max_screenshot_size
89
- end
90
-
91
- def t_error(key, **args)
92
- defaults = {
93
- error_save: 'Could not send feedback. Please try again.',
94
- error_too_many: 'Too many screenshots (max %{count}).',
95
- error_too_large: 'A screenshot is too large (max %{size} MB).',
96
- error_rate_limited: 'Too many submissions. Please wait a moment and try again.'
97
- }
98
- I18n.t(key, scope: :ideasbugs, default: defaults[key], **args)
42
+ def t_error(key, **)
43
+ Feedback.submission_error(key, **)
99
44
  end
100
45
  end
101
46
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'uri'
4
+
3
5
  module Ideasbugs
4
6
  # One piece of user feedback. Author attribution is optional and stored as
5
7
  # loose fields (no foreign key to the host's user table) so the model is
@@ -32,6 +34,74 @@ module Ideasbugs
32
34
  scope status, -> { where(status:) }
33
35
  end
34
36
 
37
+ # A duplicate is a judgment, not a merge: the row keeps its own words,
38
+ # author and screenshots, and points at the original asking for the same
39
+ # thing. Always one level deep, so nothing walks a chain. Installs from
40
+ # before 1.3 lack the column until they run `ideasbugs:duplicates`, and
41
+ # everything below stays inert until then; hence no `dependent:` here.
42
+ belongs_to :duplicate_of, class_name: 'Ideasbugs::Feedback', optional: true, inverse_of: :duplicates
43
+ has_many :duplicates, class_name: 'Ideasbugs::Feedback', foreign_key: :duplicate_of_id,
44
+ inverse_of: :duplicate_of
45
+
46
+ validate :duplicate_of_an_original, if: -> { self.class.tracks_duplicates? && duplicate_of_id_changed? }
47
+ before_destroy :release_duplicates
48
+
49
+ # What the queue lists: everything that is not a duplicate of something else.
50
+ scope :originals, -> { tracks_duplicates? ? where(duplicate_of_id: nil) : all }
51
+
52
+ def self.tracks_duplicates?
53
+ column_names.include?('duplicate_of_id')
54
+ end
55
+
56
+ SUBMITTED_ATTRIBUTES = %i[kind section message page_url].freeze
57
+
58
+ # Files feedback the way the widget does: privacy-filtered page URL, author
59
+ # attribution, screenshot limits, then the host's on_submit hook. A host
60
+ # with its own API calls this rather than re-implementing those rules.
61
+ # Returns the feedback; check persisted? and errors.
62
+ def self.submit(attributes, author: nil, tenant: nil, user_agent: nil, screenshots: nil)
63
+ feedback = new(attributes.to_h.symbolize_keys.slice(*SUBMITTED_ATTRIBUTES))
64
+ feedback.page_url = clean_page_url(feedback.page_url)
65
+ feedback.user_agent = user_agent
66
+ feedback.tenant = tenant.presence&.to_s
67
+ feedback.attribute_to(author)
68
+
69
+ if (error = feedback.attach_screenshots(Array(screenshots).compact_blank))
70
+ feedback.errors.add(:base, error)
71
+ elsif feedback.save
72
+ feedback.notify_host
73
+ end
74
+ feedback
75
+ end
76
+
77
+ # Browser URLs can carry password-reset tokens, signed ids, and campaign
78
+ # details in their query or fragment. Store only a bounded HTTP(S) location
79
+ # without credentials before it reaches the feedback table.
80
+ def self.clean_page_url(value)
81
+ raw = value.to_s
82
+ return if raw.blank? || raw.length > 2_048
83
+
84
+ uri = URI.parse(raw)
85
+ return unless %w[http https].include?(uri.scheme&.downcase)
86
+ return if uri.host.blank? || uri.userinfo.present?
87
+
88
+ uri.query = nil
89
+ uri.fragment = nil
90
+ uri.to_s.first(255)
91
+ rescue URI::InvalidURIError
92
+ nil
93
+ end
94
+
95
+ def self.submission_error(key, **args)
96
+ defaults = {
97
+ error_save: 'Could not send feedback. Please try again.',
98
+ error_too_many: 'Too many screenshots (max %{count}).',
99
+ error_too_large: 'A screenshot is too large (max %{size} MB).',
100
+ error_rate_limited: 'Too many submissions. Please wait a moment and try again.'
101
+ }
102
+ I18n.t(key, scope: :ideasbugs, default: defaults[key], **args)
103
+ end
104
+
35
105
  def open? = status == 'open'
36
106
  def in_review? = status == 'in_review'
37
107
  def resolved? = status == 'resolved'
@@ -39,5 +109,92 @@ module Ideasbugs
39
109
  def screenshots?
40
110
  respond_to?(:screenshots) && screenshots.attached?
41
111
  end
112
+
113
+ def duplicate?
114
+ self.class.tracks_duplicates? && duplicate_of_id.present?
115
+ end
116
+
117
+ # Files this feedback under `original` as asking for the same thing. The
118
+ # link stays one level deep: a duplicate of a duplicate points at its
119
+ # original, this row's own duplicates move along with it, and marking an
120
+ # original as a duplicate of one of its own duplicates swaps the two.
121
+ def mark_duplicate_of!(original)
122
+ original = original.duplicate_of if original.duplicate_of_id && original.duplicate_of_id != id
123
+
124
+ transaction do
125
+ # Moving links fires nothing, and must not trip validations that a row
126
+ # filed under an older config would fail today.
127
+ duplicates.where.not(id: original.id).update_all(duplicate_of_id: original.id, updated_at: Time.current)
128
+ original.update_column(:duplicate_of_id, nil) if original.duplicate_of_id == id
129
+ update!(duplicate_of: original)
130
+ end
131
+ end
132
+
133
+ def unmark_duplicate!
134
+ update!(duplicate_of: nil)
135
+ end
136
+
137
+ def attribute_to(author)
138
+ return unless author
139
+
140
+ self.author_id = author.id.to_s if author.respond_to?(:id)
141
+ self.author_label = Ideasbugs.config.author_label.call(author)
142
+ end
143
+
144
+ # Attaches uploads, or returns the error that rejects them. No screenshots
145
+ # at all is fine.
146
+ def attach_screenshots(files)
147
+ return if files.empty?
148
+
149
+ key, args = screenshot_error(files)
150
+ return self.class.submission_error(key, **args.to_h) if key
151
+
152
+ screenshots.attach(files)
153
+ nil
154
+ end
155
+
156
+ # The host's hook must never turn a saved submission into a failure: the
157
+ # feedback is in the database; notification failures are the host's logs'
158
+ # problem.
159
+ def notify_host
160
+ Ideasbugs.config.on_submit.call(self)
161
+ rescue StandardError => e
162
+ Rails.logger.error("ideasbugs: on_submit hook raised #{e.class}: #{e.message}")
163
+ end
164
+
165
+ private
166
+
167
+ def duplicate_of_an_original
168
+ return if duplicate_of_id.nil?
169
+
170
+ original = duplicate_of
171
+ if original.nil? || original.id == id
172
+ errors.add(:duplicate_of, 'must be another feedback')
173
+ elsif original.tenant != tenant
174
+ errors.add(:duplicate_of, 'must be on the same board')
175
+ elsif original.duplicate_of_id
176
+ errors.add(:duplicate_of, 'is itself a duplicate')
177
+ elsif persisted? && duplicates.exists?
178
+ errors.add(:base, 'Feedback with duplicates of its own cannot be a duplicate')
179
+ end
180
+ end
181
+
182
+ # Deleting an original puts its duplicates back in the queue.
183
+ def release_duplicates
184
+ return unless self.class.tracks_duplicates?
185
+
186
+ duplicates.update_all(duplicate_of_id: nil, updated_at: Time.current)
187
+ end
188
+
189
+ def screenshot_error(files)
190
+ config = Ideasbugs.config
191
+ return [:error_save] unless config.screenshots_enabled?
192
+ return [:error_too_many, { count: config.max_screenshots }] if files.size > config.max_screenshots
193
+
194
+ max_size = config.max_screenshot_size
195
+ return [:error_too_large, { size: max_size / (1024 * 1024) }] if files.any? { |f| f.size > max_size }
196
+
197
+ [:error_save] unless files.all? { |f| f.content_type.to_s.start_with?('image/') }
198
+ end
42
199
  end
43
200
  end
@@ -9,7 +9,9 @@
9
9
 
10
10
  <%# The shared status switch, carrying the status the heading badge used to
11
11
  state: the current one is lit and inert, every other one is a submit
12
- that files the report under it. One form, no JS. %>
12
+ that files the report under it. One form, no JS. A duplicate has no
13
+ lifecycle of its own: it follows its original. %>
14
+ <% unless feedback.duplicate? %>
13
15
  <%= form_with url: feedback_path(feedback), method: :patch, class: 'status-switch' do %>
14
16
  <% Ideasbugs::Feedback::STATUSES.each do |status| %>
15
17
  <% label = t("ideasbugs.statuses.#{status}", default: status.humanize) %>
@@ -23,9 +25,28 @@
23
25
  <% end %>
24
26
  <% end %>
25
27
  <% end %>
28
+ <% end %>
26
29
  </div>
27
30
 
28
31
  <div class="detail-scroll">
32
+ <% if flash[:alert] %>
33
+ <p class="flash alert" role="alert"><%= flash[:alert] %></p>
34
+ <% end %>
35
+
36
+ <% if feedback.duplicate? && (original = feedback.duplicate_of) %>
37
+ <div class="card pad duplicate-of">
38
+ <p>
39
+ <%= t('ideasbugs.dashboard.duplicate_of', default: 'Duplicate of') %>
40
+ <%= link_to "##{original.id}", feedbacks_path(status: original.status, feedback_id: original.id) %>
41
+ <span class="badge status-<%= original.status %>">
42
+ <%= t("ideasbugs.statuses.#{original.status}", default: original.status.humanize) %>
43
+ </span>
44
+ </p>
45
+ <p class="muted"><%= truncate(original.message, length: 160) %></p>
46
+ <%= button_to t('ideasbugs.dashboard.not_duplicate', default: 'Not a duplicate'),
47
+ feedback_duplicate_path(feedback), method: :delete, class: 'small' %>
48
+ </div>
49
+ <% end %>
29
50
  <div class="card pad">
30
51
  <dl>
31
52
  <% if feedback.section.present? %>
@@ -63,9 +84,38 @@
63
84
  <% end %>
64
85
  </div>
65
86
 
87
+ <% duplicates = Ideasbugs::Feedback.tracks_duplicates? ? feedback.duplicates.newest_first.to_a : [] %>
88
+ <% if duplicates.any? %>
89
+ <section class="card duplicates" aria-labelledby="duplicates-<%= feedback.id %>">
90
+ <h2 id="duplicates-<%= feedback.id %>">
91
+ <%= t('ideasbugs.dashboard.duplicates', default: 'Duplicates') %> <span class="count"><%= duplicates.size %></span>
92
+ </h2>
93
+ <% duplicates.each do |duplicate| %>
94
+ <%= link_to feedbacks_path(status: feedback.status, feedback_id: duplicate.id), class: 'record-row' do %>
95
+ <span class="record-main">
96
+ <span class="record-copy"><%= truncate(duplicate.message, length: 120) %></span>
97
+ <span class="muted record-meta">
98
+ <span>#<%= duplicate.id %></span>
99
+ <% if duplicate.author_label.present? %>
100
+ <span><%= duplicate.author_label %></span>
101
+ <% end %>
102
+ </span>
103
+ </span>
104
+ <% end %>
105
+ <% end %>
106
+ </section>
107
+ <% end %>
108
+
66
109
  <%# Status lives in the switch in the heading; only what a switch cannot say
67
110
  is left here. %>
68
111
  <div class="actions">
112
+ <% if Ideasbugs::Feedback.tracks_duplicates? && !feedback.duplicate? %>
113
+ <%= form_with url: feedback_duplicate_path(feedback), method: :post, class: 'mark-duplicate' do %>
114
+ <label for="original_id_<%= feedback.id %>"><%= t('ideasbugs.dashboard.mark_duplicate', default: 'Mark as duplicate of #') %></label>
115
+ <input type="text" name="original_id" id="original_id_<%= feedback.id %>" required autocomplete="off">
116
+ <button><%= t('ideasbugs.dashboard.mark_duplicate_submit', default: 'Mark duplicate') %></button>
117
+ <% end %>
118
+ <% end %>
69
119
  <%= button_to t('ideasbugs.dashboard.delete', default: 'Delete'),
70
120
  feedback_path(feedback),
71
121
  method: :delete, class: 'danger',
@@ -60,6 +60,9 @@
60
60
  <% if feedback.author_label.present? %>
61
61
  <span><%= feedback.author_label %></span>
62
62
  <% end %>
63
+ <% if (duplicates = @duplicate_counts[feedback.id]) %>
64
+ <span><%= t('ideasbugs.dashboard.duplicates_count', count: duplicates, default: 'Duplicates: %{count}') %></span>
65
+ <% end %>
63
66
  </span>
64
67
  </span>
65
68
  <% if feedback.screenshots? %>
@@ -45,3 +45,10 @@ en:
45
45
  move_to_resolved: "Mark resolved"
46
46
  delete: "Delete"
47
47
  delete_confirm: "Delete this feedback?"
48
+ duplicate_of: "Duplicate of"
49
+ duplicates: "Duplicates"
50
+ duplicates_count: "Duplicates: %{count}"
51
+ mark_duplicate: "Mark as duplicate of #"
52
+ mark_duplicate_submit: "Mark duplicate"
53
+ not_duplicate: "Not a duplicate"
54
+ duplicate_not_found: "No feedback with that number."
data/config/routes.rb CHANGED
@@ -14,6 +14,7 @@ Ideasbugs::Engine.routes.draw do
14
14
  # Screenshots stream through the dashboard's own gate, never via public
15
15
  # Active Storage blob URLs.
16
16
  resources :screenshots, only: :show
17
+ resource :duplicate, only: %i[create destroy]
17
18
  end
18
19
 
19
20
  root to: 'feedbacks#index'
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+ require 'rails/generators/active_record'
5
+ require_relative '../migration_helpers'
6
+
7
+ module Ideasbugs
8
+ module Generators
9
+ # For apps that installed ideasbugs before 1.3: adds the nullable
10
+ # `duplicate_of_id` column (and its index) to ideasbugs_feedbacks. A fresh
11
+ # install already gets it. Additive: existing rows are originals, and the
12
+ # dashboard offers duplicate marking once the column exists.
13
+ #
14
+ # bin/rails generate ideasbugs:duplicates && bin/rails db:migrate
15
+ class DuplicatesGenerator < Rails::Generators::Base
16
+ include ActiveRecord::Generators::Migration
17
+ include MigrationHelpers
18
+
19
+ source_root File.expand_path('templates', __dir__)
20
+
21
+ desc 'Adds the duplicate_of column to ideasbugs_feedbacks (duplicates upgrade).'
22
+
23
+ def create_migration_file
24
+ migration_template 'add_duplicate_of_to_ideasbugs_feedbacks.rb.tt',
25
+ 'db/migrate/add_duplicate_of_to_ideasbugs_feedbacks.rb'
26
+ end
27
+
28
+ def post_install
29
+ say "\nduplicate_of column queued. Run `rails db:migrate`, then mark", :green
30
+ say 'duplicates from any feedback in the dashboard.'
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddDuplicateOfToIdeasbugsFeedbacks < ActiveRecord::Migration<%= migration_version %>
4
+ def change
5
+ add_reference :ideasbugs_feedbacks, :duplicate_of<%= reference_type_option %>, index: true
6
+ end
7
+ end
@@ -12,6 +12,7 @@ class CreateIdeasbugsFeedbacks < ActiveRecord::Migration<%= migration_version %>
12
12
  t.string :author_id
13
13
  t.string :author_label
14
14
  t.string :tenant # opaque per-tenant key; nil = single global board
15
+ t.references :duplicate_of<%= reference_type_option %>, index: true
15
16
 
16
17
  t.timestamps
17
18
  end
@@ -24,9 +24,18 @@ module Ideasbugs
24
24
  # template is expanded at generate time: emitting the method name would
25
25
  # put `id: primary_key_type` in the migration, where nothing defines it.
26
26
  def primary_key_type_option
27
+ primary_key_type ? ", id: :#{primary_key_type}" : ''
28
+ end
29
+
30
+ # The same lookup for a column that points back at a feedback row, so
31
+ # `duplicate_of_id` matches a uuid-keyed table's own id.
32
+ def reference_type_option
33
+ primary_key_type ? ", type: :#{primary_key_type}" : ''
34
+ end
35
+
36
+ def primary_key_type
27
37
  config = Rails.configuration.generators
28
- type = config.options[config.orm][:primary_key_type]
29
- type ? ", id: :#{type}" : ''
38
+ config.options[config.orm][:primary_key_type]
30
39
  end
31
40
  end
32
41
  end
@@ -286,8 +286,8 @@
286
286
  }
287
287
 
288
288
  /* ---------------------------------------------------------------------------
289
- * GEM-SPECIFIC — ideasbugs only: the kind and status badges, and the
290
- * screenshots a reporter attached.
289
+ * GEM-SPECIFIC — ideasbugs only: the kind and status badges, the
290
+ * screenshots a reporter attached, and duplicates.
291
291
  * ------------------------------------------------------------------------- */
292
292
 
293
293
  :root {
@@ -314,6 +314,14 @@
314
314
  & .status-switch button.current.status-in_review { color: var(--ib-in-review); }
315
315
  & .status-switch button.current.status-resolved { color: var(--ib-resolved); }
316
316
 
317
+ /* A duplicate's link to its original, and the original's list of them. */
318
+ & .duplicate-of p { margin: 0 0 8px; }
319
+ & .duplicates h2 { margin: 0; padding: 12px 14px; border-bottom: 1px solid var(--ib-border); font-size: 14px; }
320
+ & .duplicates .record-row:last-child { border-bottom: 0; }
321
+ & .actions form.mark-duplicate { display: inline-flex; flex-wrap: wrap; align-items: center; gap: 8px; }
322
+ & .mark-duplicate label { color: var(--ib-muted); font-size: 13px; white-space: nowrap; }
323
+ & .mark-duplicate input { width: 9em; }
324
+
317
325
  & .shots { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 8px; }
318
326
  & .shots img { max-width: 280px; max-height: 200px; border: 1px solid var(--ib-border); border-radius: 8px; }
319
327
  }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ideasbugs
4
- VERSION = '1.1.0'
4
+ VERSION = '1.3.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ideasbugs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yaroslav Shmarov
@@ -49,6 +49,7 @@ files:
49
49
  - app/controllers/concerns/ideasbugs/request_context.rb
50
50
  - app/controllers/ideasbugs/application_controller.rb
51
51
  - app/controllers/ideasbugs/dashboard_controller.rb
52
+ - app/controllers/ideasbugs/duplicates_controller.rb
52
53
  - app/controllers/ideasbugs/feedbacks_controller.rb
53
54
  - app/controllers/ideasbugs/screenshots_controller.rb
54
55
  - app/controllers/ideasbugs/submissions_controller.rb
@@ -98,6 +99,8 @@ files:
98
99
  - docs/screenshots/dashboard-light.png
99
100
  - docs/screenshots/widget-dark.png
100
101
  - docs/screenshots/widget-light.png
102
+ - lib/generators/ideasbugs/duplicates/duplicates_generator.rb
103
+ - lib/generators/ideasbugs/duplicates/templates/add_duplicate_of_to_ideasbugs_feedbacks.rb.tt
101
104
  - lib/generators/ideasbugs/install/install_generator.rb
102
105
  - lib/generators/ideasbugs/install/templates/create_ideasbugs_feedbacks.rb.tt
103
106
  - lib/generators/ideasbugs/install/templates/initializer.rb