testimonials 0.7.10 → 0.8.1

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.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/AGENTS.md +4 -1
  3. data/CHANGELOG.md +66 -0
  4. data/README.md +2 -0
  5. data/app/controllers/concerns/testimonials/request_context.rb +75 -0
  6. data/app/controllers/testimonials/application_controller.rb +10 -56
  7. data/app/controllers/testimonials/dashboard_controller.rb +40 -0
  8. data/app/controllers/testimonials/nps_responses_controller.rb +1 -4
  9. data/app/controllers/testimonials/submissions_controller.rb +169 -0
  10. data/app/controllers/testimonials/testimonials_controller.rb +6 -166
  11. data/app/views/layouts/testimonials/application.html.erb +5 -8
  12. data/app/views/testimonials/nps_responses/index.html.erb +154 -152
  13. data/app/views/testimonials/nps_responses/show.html.erb +7 -5
  14. data/app/views/testimonials/shared/_dashboard.html.erb +18 -0
  15. data/app/views/testimonials/testimonials/index.html.erb +110 -108
  16. data/app/views/testimonials/testimonials/show.html.erb +7 -5
  17. data/config/routes.rb +18 -5
  18. data/lib/generators/testimonials/install/install_generator.rb +2 -6
  19. data/lib/generators/testimonials/install/templates/create_testimonials_tables.rb.tt +3 -4
  20. data/lib/generators/testimonials/install/templates/initializer.rb.tt +10 -2
  21. data/lib/generators/testimonials/migration_helpers.rb +35 -0
  22. data/lib/generators/testimonials/nps/nps_generator.rb +2 -6
  23. data/lib/generators/testimonials/nps/templates/create_testimonials_nps_responses.rb.tt +1 -1
  24. data/lib/generators/testimonials/prompt_events/prompt_events_generator.rb +2 -6
  25. data/lib/generators/testimonials/prompt_events/templates/create_testimonials_prompt_events.rb.tt +1 -1
  26. data/lib/generators/testimonials/tenant/templates/add_tenant_to_testimonials.rb.tt +0 -1
  27. data/lib/generators/testimonials/tenant/tenant_generator.rb +2 -6
  28. data/lib/testimonials/configuration.rb +24 -1
  29. data/lib/testimonials/dashboard.css +278 -221
  30. data/lib/testimonials/version.rb +1 -1
  31. data/lib/testimonials.rb +8 -0
  32. metadata +6 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f7ed14e07e92b6d456853cf8b9c04ece86f36d17873091f515f5c47e393894ca
4
- data.tar.gz: c05b6be1e36b1592eabe270c18f7da4a15dafdca192a01141389141b76a37aca
3
+ metadata.gz: bfd44ded7b03a811fbbe8d6db999c3d04c9a1ca3d5401e1ae650fc407487e3a5
4
+ data.tar.gz: c683180efb5063687ca5ac8410caa6f51dff44ffe1de7ef65c83d30b4bc5052e
5
5
  SHA512:
6
- metadata.gz: ff625ff280d473f959693892b942f52faa9b9e4f00ae139e7d07a94424d08da845ef387df6639cf1b37c536740aa0aa8d2350cd51416fc332a9b54bf22d810de
7
- data.tar.gz: 2f721d94802015c6061a159d8e01797d09a2e1f5c453397f4519ed11508a464607009908685108b4c94d149336a5b1bf1d4d3998a0d04917402eee98a9780fa0
6
+ metadata.gz: 4ec04128d604bceebd38bec356b7511e4fa2823256ec1e6a59c5224d51aca6359bc8420d0f5e76bc303f46df001494b024e38b94ee888536fa4f97b0ff47f179
7
+ data.tar.gz: 94512ae9beeca12776e3aadcf5641e550b5733650c233d60e16ec449568d88ea38e4a4a84e36ceda03f2e0f80c133cd47315569ee14b73a64703183c9053d823
data/AGENTS.md CHANGED
@@ -148,7 +148,7 @@ organization.testimonials.approved
148
148
 
149
149
  - **Do not hand-write the migration or the initializer.** Use the generators; the templates track the schema.
150
150
  - **Do not copy `widget.js` into `app/javascript`, or add a `<script>` tag for it.** `testimonials_tag` renders both tags itself, and the engine serves the code at `/testimonials/widget.js` with a content fingerprint. There is no build step and nothing for esbuild/importmap/Tailwind to know about.
151
- - **Do not edit views inside the gem.** Override the dashboard's shell with `config.admin_layout = "admin/application"` if it needs to live inside an existing admin.
151
+ - **Do not edit views inside the gem.** To put the dashboard inside an existing admin, set `config.base_controller_class = "Admin::BaseController"` (it inherits that controller's layout, helpers, authentication and request context) or, for the shell alone, `config.admin_layout = "admin/application"`. The dashboard's stylesheet and script are declared by its views, so both work with nothing else wired up.
152
152
  - **Do not check whether a table exists to decide if a feature is on.** `Testimonials.config.nps` and `Testimonials.config.prompt_events` are the source of truth, deliberately — nothing in the gem introspects the schema at runtime.
153
153
  - **Do not set config outside the initializer.** `rate_limit` in particular is read once when the controller class loads; assigning config in a controller or per-request is a global mutation across the process.
154
154
  - **Do not add `testimonials_tag` more than once per page,** and do not put it in a partial that some pages skip while still calling `testimonial_prompt!` — the prompt is consumed from the flash by whatever renders next.
@@ -163,6 +163,7 @@ Everything is optional; a fresh install works with zero config. Full list with c
163
163
  | `current_user`, `user_display`, `tenant`, `enabled` | no-ops | Lambdas over the raw request. |
164
164
  | `nps` | `true` | `false` = no prompt, no public NPS page, no dashboard tab, no `nps_score`. |
165
165
  | `prompt_events` | `true` | `false` = no prompt history, and therefore no auto-prompts at all. |
166
+ | `base_controller_class` | `ActionController::Base` | Controller the DASHBOARD inherits. Public endpoints never do, so an admin base controller here cannot gate the widget. |
166
167
  | `reprompt_after`, `max_prompts` | `90.days`, `3` | Throttle for auto-prompts only. |
167
168
  | `video`, `avatars` | `true` | Need Active Storage; they self-disable when it isn't loaded. |
168
169
  | `public_collection` | `true` | `/testimonials/new` and `/testimonials/nps/new`. Shareable links. |
@@ -183,6 +184,8 @@ Turbo Drive and nonce-based CSP work out of the box; the widget config rides in
183
184
  | `no such table: testimonials_nps_responses` (or `..._prompt_events`) | A flag was flipped to `true` before running the matching generator and `db:migrate`. |
184
185
  | `/testimonials/nps_responses` 404s | `config.nps` is false. The rows are still there; flip it back to read them. |
185
186
  | Video recording missing | Active Storage not installed, or `config.video = false`. |
187
+ | `NameError` for a host helper in the dashboard | `isolate_namespace` scopes `helper` to the engine. Use `config.base_controller_class` so the dashboard inherits your helpers, rather than `admin_layout` alone. |
188
+ | `NotNullViolation` attaching a video on a uuid-keyed app | The tables were generated bigint. Set `config.generators` `primary_key_type` before installing, or migrate the tables to uuid. |
186
189
  | `undefined local variable current_user` in the initializer | A gate lambda treated its argument as a controller. It is a `request`. |
187
190
 
188
191
  ---
data/CHANGELOG.md CHANGED
@@ -1,5 +1,71 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.8.1
4
+
5
+ - Dropped the redundant `(tenant, status)` index from the install and tenant
6
+ migration templates. A B-tree serves any leftmost prefix, so
7
+ `(tenant, status, consent_given)` already covers it, and every scope on the
8
+ model starts with `for_tenant` — carrying both only cost write time and disk.
9
+ Flagged by `active_record_doctor` in a host app.
10
+ - Existing installs keep the index until they drop it themselves; nothing
11
+ breaks either way. To reclaim it:
12
+
13
+ ```ruby
14
+ class RemoveRedundantTestimonialsIndex < ActiveRecord::Migration[8.0]
15
+ def change
16
+ remove_index :testimonials_testimonials, %i[tenant status]
17
+ end
18
+ end
19
+ ```
20
+
21
+ - A generator test now fails the build if any index is a leftmost prefix of
22
+ another on the same table.
23
+
24
+ ## 0.8.0
25
+
26
+ - **`config.admin_layout` now works on its own.** The dashboard's stylesheet and
27
+ script were declared in the gem's layout, so replacing that layout dropped
28
+ both: the dashboard rendered unstyled with its delete confirmations and
29
+ auto-submitting filter dead. They move into the views (a shared
30
+ `testimonials/shared/_dashboard` partial), so every layout gets them with
31
+ nothing asked of the host.
32
+ - **The dashboard stylesheet no longer claims selectors it does not own.** It
33
+ styled bare `body`, `a`, `table` and `*`, and its `.card`, `.badge`, `.tabs`
34
+ and `.container` are names Bootstrap and daisyUI use too, so a host that did
35
+ load it had its sidebar and topbar restyled. Component rules now nest inside a
36
+ `.tml-dashboard` wrapper the views render, every custom property is `--tml-`
37
+ prefixed, and `.container`/`.nav` became `.tml-page`/`.tml-nav`. The
38
+ full-viewport rules stay keyed to the `tm-index`/`tm-nps-index`/`tm-show` body
39
+ classes that only the gem's own layout sets, so inside a host admin the
40
+ dashboard scrolls with the host's page instead of fighting it for the
41
+ viewport. Two tests fail the build if a selector or property escapes again.
42
+ - **Added `config.base_controller_class`.** Name the controller your own admin
43
+ inherits from and the dashboard adopts its layout, helpers, authentication and
44
+ request context — the things `admin_layout` cannot give you, and which hosts
45
+ were hand-wiring as a shim layout plus a concern to populate `Current`. Same
46
+ hook pgbus, avo and mission_control-jobs use. Default is unchanged.
47
+ Layout precedence with it: a host base controller keeps its own layout, unless the host also named an `admin_layout` explicitly.
48
+ - **Migrations follow the host's `primary_key_type`,** the same
49
+ `Rails.configuration.generators` lookup Rails' own Active Storage, Action Text
50
+ and Action Mailbox migrations do. A uuid-keyed app has a uuid
51
+ `active_storage_attachments.record_id`, so bigint tables here could never hold
52
+ a video or avatar — `attach` raised `NotNullViolation`. A host that set
53
+ nothing gets no `id:` option and an identical migration to before.
54
+ - **Dropped the `id: /\d+/` route constraints,** which were what forced the
55
+ tables to be bigint in the first place: uuid tables would have 404d show,
56
+ update, destroy and all three media routes. The constraint was never
57
+ load-bearing, since every fixed-name route is declared before the flat
58
+ `/:id` routes.
59
+ - **`create` moved to `Testimonials::SubmissionsController`** and `POST` to the
60
+ mount path routes there. One controller used to serve both the widget's write
61
+ endpoint and the triage actions, which meant `base_controller_class` would
62
+ have put staff authentication in front of every member leaving a review. If
63
+ you referenced `Testimonials::TestimonialsController#create`, that is the
64
+ breaking change in this release; the URL is unchanged.
65
+ - The shared request context (`current_author`, `current_tenant`,
66
+ `tenant_scope`, the gates) is now a `Testimonials::RequestContext` concern,
67
+ since the engine has two controller roots.
68
+
3
69
  ## 0.7.10
4
70
 
5
71
  - The prompt-history ledger is now optional too. `bin/rails generate
data/README.md CHANGED
@@ -163,6 +163,8 @@ Everything is optional — a fresh install works with zero config. In
163
163
  | Option | Default | What it does |
164
164
  | --- | --- | --- |
165
165
  | `authorize_admin` | development only | **Who can read the dashboard.** Override before deploying |
166
+ | `base_controller_class` | `ActionController::Base` | The controller the dashboard inherits — name your admin's and it adopts its layout, helpers and auth |
167
+ | `admin_layout` | the gem's own | Just the shell, if you don't want the whole controller |
166
168
  | `app_name` | Rails app name | Shown as "Enjoying %{app}?" and in the questions |
167
169
  | `enabled` | everyone | Who gets the widget. `false` hides it and rejects posts |
168
170
  | `current_user` | `nil` | Attribute a submission to a user. Receives the request |
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Testimonials
4
+ # Who is asking, which tenant they are in, and the gates that answer both.
5
+ #
6
+ # A concern rather than inherited behaviour because the engine has two
7
+ # controller roots: the public endpoints hang off ActionController::Base, and
8
+ # the dashboard hangs off whatever the host set as `base_controller_class`.
9
+ # Both need everything here.
10
+ module RequestContext
11
+ extend ActiveSupport::Concern
12
+
13
+ private
14
+
15
+ def testimonials_admin_layout
16
+ Testimonials.config.admin_layout
17
+ end
18
+
19
+ def current_author
20
+ return @current_author if defined?(@current_author)
21
+
22
+ @current_author = Testimonials.config.current_user.call(request)
23
+ end
24
+
25
+ def current_author_id
26
+ current_author.respond_to?(:id) ? current_author.id : nil
27
+ end
28
+
29
+ # The tenant for this request (nil = the single global collection). Every
30
+ # read and write in the engine scopes to it, so a resolved-tenant admin
31
+ # only ever sees and writes their own tenant's records.
32
+ def current_tenant
33
+ return @current_tenant if defined?(@current_tenant)
34
+
35
+ @current_tenant = Testimonials.tenant(request)
36
+ end
37
+
38
+ # Every dashboard query and the create endpoint's one-per-user lookup start
39
+ # here, so neither can reach across tenants.
40
+ def tenant_scope
41
+ Testimonial.for_tenant(current_tenant)
42
+ end
43
+
44
+ def require_enabled
45
+ head :forbidden unless Testimonials.enabled?(request)
46
+ end
47
+
48
+ def render_rate_limited
49
+ message = I18n.t('testimonials.error_rate_limited',
50
+ default: 'Too many submissions. Please wait a moment and try again.')
51
+ render json: { errors: [message] }, status: :too_many_requests
52
+ end
53
+
54
+ # Server-side gate for the dashboard. Default: development only.
55
+ def require_admin
56
+ return if Testimonials.admin?(request)
57
+
58
+ render plain: 'Forbidden. Set Testimonials.config.authorize_admin to grant access.',
59
+ status: :forbidden
60
+ end
61
+
62
+ # Guests get a permanent random token so the throttle ledger can
63
+ # remember them across visits. Signed-in users are keyed by author_id
64
+ # instead and never receive the cookie.
65
+ def ensure_visitor_token
66
+ return if current_author
67
+
68
+ cookies[:testimonials_vid].presence || begin
69
+ token = SecureRandom.base58(24)
70
+ cookies.permanent[:testimonials_vid] = { value: token, httponly: true, same_site: :lax }
71
+ token
72
+ end
73
+ end
74
+ end
75
+ end
@@ -1,63 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Testimonials
4
+ # Root of the engine's PUBLIC surface: widget.js, the submission endpoints,
5
+ # the prompt-event ledger, the public collection pages, media, and the read
6
+ # API. These stay on a plain ActionController::Base deliberately — a member
7
+ # posting a testimonial must not be routed through a host's admin controller,
8
+ # which would demand a staff session for the widget.
9
+ #
10
+ # The dashboard's root is DashboardController, and that is where
11
+ # `config.base_controller_class` applies.
4
12
  class ApplicationController < ActionController::Base
5
- protect_from_forgery with: :exception
6
-
7
- private
8
-
9
- def testimonials_admin_layout
10
- Testimonials.config.admin_layout
11
- end
12
-
13
- def current_author
14
- return @current_author if defined?(@current_author)
15
-
16
- @current_author = Testimonials.config.current_user.call(request)
17
- end
18
-
19
- def current_author_id
20
- current_author.respond_to?(:id) ? current_author.id : nil
21
- end
22
-
23
- # The tenant for this request (nil = the single global collection). Every
24
- # read and write in the engine scopes to it, so a resolved-tenant admin
25
- # only ever sees and writes their own tenant's records.
26
- def current_tenant
27
- return @current_tenant if defined?(@current_tenant)
13
+ include RequestContext
28
14
 
29
- @current_tenant = Testimonials.tenant(request)
30
- end
31
-
32
- def require_enabled
33
- head :forbidden unless Testimonials.enabled?(request)
34
- end
35
-
36
- def render_rate_limited
37
- message = I18n.t('testimonials.error_rate_limited',
38
- default: 'Too many submissions. Please wait a moment and try again.')
39
- render json: { errors: [message] }, status: :too_many_requests
40
- end
41
-
42
- # Server-side gate for the dashboard. Default: development only.
43
- def require_admin
44
- return if Testimonials.admin?(request)
45
-
46
- render plain: 'Forbidden. Set Testimonials.config.authorize_admin to grant access.',
47
- status: :forbidden
48
- end
49
-
50
- # Guests get a permanent random token so the throttle ledger can
51
- # remember them across visits. Signed-in users are keyed by author_id
52
- # instead and never receive the cookie.
53
- def ensure_visitor_token
54
- return if current_author
55
-
56
- cookies[:testimonials_vid].presence || begin
57
- token = SecureRandom.base58(24)
58
- cookies.permanent[:testimonials_vid] = { value: token, httponly: true, same_site: :lax }
59
- token
60
- end
61
- end
15
+ protect_from_forgery with: :exception
62
16
  end
63
17
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Testimonials
4
+ # Root of the STAFF surface: the testimonial queue and the NPS pages.
5
+ #
6
+ # Inherits from `config.base_controller_class` — by default a plain
7
+ # ActionController::Base, which is why `authorize_admin` exists. Point it at
8
+ # the controller your own admin already inherits from and the dashboard picks
9
+ # up that stack wholesale: your layout, your helpers, your authentication,
10
+ # and whatever request context your before_actions establish (a `Current`
11
+ # attribute the layout reads, say). `config.admin_layout` only ever solved
12
+ # the first of those.
13
+ #
14
+ # Only the dashboard hangs off it. The widget's endpoints stay on
15
+ # ApplicationController, so wiring an admin base controller here can never
16
+ # demand a staff session from a member leaving a review.
17
+ class DashboardController < Testimonials.base_controller
18
+ include RequestContext
19
+
20
+ # A host base controller brings its own layout, and declaring one here would
21
+ # override it — the dashboard would render in the gem's standalone shell
22
+ # inside an app that just told us where its admin lives. So the gem only
23
+ # claims the layout when it owns the decision: no host base controller, or a
24
+ # host that named an `admin_layout` explicitly.
25
+ layout :testimonials_admin_layout unless superclass != ActionController::Base &&
26
+ Testimonials.config.admin_layout ==
27
+ Configuration::DEFAULT_ADMIN_LAYOUT
28
+
29
+ before_action :require_admin
30
+
31
+ # `authorize_admin` is a gate of last resort, and it is the only one when
32
+ # the host supplied no base controller. When they did, its default is
33
+ # development-only, so a host whose base controller already authenticates
34
+ # staff should widen or replace it.
35
+ #
36
+ # CSRF likewise: a host base controller has configured it already, and
37
+ # declaring it twice would run the check twice.
38
+ protect_from_forgery with: :exception if superclass == ActionController::Base
39
+ end
40
+ end
@@ -2,12 +2,9 @@
2
2
 
3
3
  module Testimonials
4
4
  # The NPS side of the dashboard: overall score plus the response list.
5
- class NpsResponsesController < ApplicationController
5
+ class NpsResponsesController < DashboardController
6
6
  PER_PAGE = 50
7
7
 
8
- layout :testimonials_admin_layout
9
-
10
- before_action :require_admin
11
8
  # An install run with --skip-nps has no table behind these pages, and the
12
9
  # nav already hides the tab whenever config.nps is off.
13
10
  before_action :require_nps
@@ -0,0 +1,169 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Testimonials
4
+ # The widget's write endpoint: POST to the mount path.
5
+ #
6
+ # Public, so it stays on ApplicationController and never inherits a host's
7
+ # admin base controller — a member leaving a review must not be asked for a
8
+ # staff session. The dashboard's read/triage actions live in
9
+ # TestimonialsController, which does inherit it.
10
+ class SubmissionsController < ApplicationController
11
+ before_action :require_enabled
12
+
13
+ # Throttle the public endpoint per IP so one user or bot can't flood the
14
+ # table (a submission may carry a video). Uses the rate limiter built
15
+ # into Rails 7.2+ (backed by Rails.cache); on Rails 7.1 this is a no-op.
16
+ # Tune or disable via config.rate_limit — read once at boot.
17
+ if respond_to?(:rate_limit) && Testimonials.config.rate_limit
18
+ rate_limit(**Testimonials.config.rate_limit, only: :create, with: -> { render_rate_limited })
19
+ end
20
+
21
+ def create
22
+ testimonial = build_testimonial
23
+
24
+ error = attach_video(testimonial) || attach_avatar(testimonial)
25
+ return render json: { errors: [error] }, status: :unprocessable_entity if error
26
+
27
+ if testimonial.save
28
+ # Purge only after a valid save, so a failed update can't strand a
29
+ # review with its video already gone.
30
+ if remove_video? && testimonial.video_attached?
31
+ testimonial.video_file.purge
32
+ testimonial.poster.purge if testimonial.poster_attached?
33
+ end
34
+ record_submission
35
+ notify_host(testimonial)
36
+ head :created
37
+ else
38
+ render json: { errors: testimonial.errors.full_messages }, status: :unprocessable_entity
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ # One review per signed-in user: a re-submission edits their existing
45
+ # review in place (the App Store model). The edit goes back through
46
+ # moderation, and the admin's best_line is cleared — it may no longer
47
+ # match the new wording. Guests have no reliable identity, so each guest
48
+ # submission stays a new record.
49
+ def build_testimonial
50
+ testimonial = existing_testimonial || Testimonial.new
51
+ testimonial.assign_attributes(testimonial_params)
52
+ if testimonial.persisted?
53
+ testimonial.status = 'pending'
54
+ testimonial.best_line = nil
55
+ end
56
+ testimonial.kind = wants_video?(testimonial) ? 'video' : 'text'
57
+ testimonial.source = 'widget' unless Testimonial::SOURCES.include?(testimonial.source)
58
+ testimonial.consent_text = Testimonials.consent_text_for(testimonial.consent_given?)
59
+ testimonial.locale = I18n.locale.to_s
60
+ testimonial.tenant = current_tenant
61
+ testimonial.user_agent = request.user_agent
62
+ attribute_author(testimonial)
63
+ testimonial
64
+ end
65
+
66
+ # One review per signed-in user, *per tenant*: the same person can leave a
67
+ # separate review in each tenant, but only one within any single tenant.
68
+ def existing_testimonial
69
+ return if current_author_id.blank?
70
+
71
+ tenant_scope.where(author_id: current_author_id.to_s).newest_first.first
72
+ end
73
+
74
+ # A fresh upload makes it a video review; on an edit without a new
75
+ # upload, an already-attached video stays — unless the user removed it.
76
+ def wants_video?(testimonial)
77
+ return true if params.dig(:testimonial, :video_file).present?
78
+
79
+ testimonial.video_attached? && !remove_video?
80
+ end
81
+
82
+ def remove_video?
83
+ params.dig(:testimonial, :remove_video).present? && params.dig(:testimonial, :video_file).blank?
84
+ end
85
+
86
+ def testimonial_params
87
+ params.require(:testimonial)
88
+ .permit(:body, :rating, :consent_given, :page_url, :source,
89
+ :name, :email, :title_company)
90
+ end
91
+
92
+ # Signed-in users are attributed server-side — the widget never sends
93
+ # (and the endpoint never trusts) contact fields for them.
94
+ def attribute_author(testimonial)
95
+ author = current_author
96
+ return if author.nil?
97
+
98
+ testimonial.author_id = author.id.to_s if author.respond_to?(:id)
99
+ display = Testimonials.config.user_display.call(author) || {}
100
+ testimonial.name = display[:name].presence || testimonial.name
101
+ testimonial.email = display[:email].presence || testimonial.email
102
+ testimonial.title_company = display[:title_company].presence || testimonial.title_company
103
+ end
104
+
105
+ # Returns an error message, or nil when everything is fine.
106
+ def attach_video(testimonial)
107
+ file = params.dig(:testimonial, :video_file)
108
+ return nil if file.blank?
109
+ return error_label(:error_save) unless Testimonials.config.video_enabled?
110
+ return error_label(:error_video_too_large) if file.size > Testimonials.config.max_video_size
111
+ return error_label(:error_save) unless file.content_type.to_s.start_with?('video/', 'audio/')
112
+
113
+ testimonial.video_file.attach(file)
114
+ attach_poster(testimonial)
115
+ nil
116
+ end
117
+
118
+ # The poster rides with a new video upload. It's best-effort — a missing
119
+ # or malformed poster never blocks the testimonial. Attaching replaces any
120
+ # previous poster; a new video with no usable poster (e.g. an uploaded
121
+ # file, which we can't frame-grab) drops the stale one so it can't show a
122
+ # thumbnail from the wrong video.
123
+ def attach_poster(testimonial)
124
+ file = params.dig(:testimonial, :poster)
125
+ usable = file.present? && file.content_type.to_s.start_with?('image/') &&
126
+ file.size <= Testimonials.config.max_avatar_size
127
+
128
+ if usable
129
+ testimonial.poster.attach(file)
130
+ elsif testimonial.poster_attached?
131
+ testimonial.poster.purge
132
+ end
133
+ end
134
+
135
+ def attach_avatar(testimonial)
136
+ file = params.dig(:testimonial, :avatar)
137
+ return nil if file.blank?
138
+ return error_label(:error_save) unless Testimonials.config.avatars_enabled?
139
+ return error_label(:error_save) if file.size > Testimonials.config.max_avatar_size
140
+ return error_label(:error_save) unless file.content_type.to_s.start_with?('image/')
141
+
142
+ testimonial.avatar.attach(file)
143
+ nil
144
+ end
145
+
146
+ def record_submission
147
+ PromptEvent.record!(kind: 'testimonial', action: 'submitted', tenant: current_tenant,
148
+ author_id: current_author_id, visitor_token: ensure_visitor_token)
149
+ end
150
+
151
+ # The host's hook must never turn a saved submission into a 500 — the
152
+ # testimonial is in the database; notification failures are the host's
153
+ # logs' problem.
154
+ def notify_host(record)
155
+ Testimonials.config.on_submit.call(record)
156
+ rescue StandardError => e
157
+ Rails.logger.error("testimonials: on_submit hook raised #{e.class}: #{e.message}")
158
+ end
159
+
160
+ def error_label(key)
161
+ defaults = {
162
+ error_save: 'Could not send. Please try again.',
163
+ error_video_too_large: 'The video is too large (max %{size} MB).'
164
+ }
165
+ I18n.t(key, scope: :testimonials, default: defaults[key],
166
+ size: Testimonials.config.max_video_size / (1024 * 1024))
167
+ end
168
+ end
169
+ end