Class: I18nProofreading::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n_proofreading/configuration.rb

Overview

Host-tunable settings. Everything has a safe default, so a fresh install works with zero configuration in development; the hooks below let an app decide who sees the tool and how suggestions are attributed.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/i18n_proofreading/configuration.rb', line 73

def initialize
  @enabled_environments = %w[development staging]
  @enabled = ->(_request) { true }
  @authorize_admin = ->(_request) { Rails.env.development? }
  @admin_layout = 'i18n_proofreading/application'
  @current_user = ->(_request) {}
  @author_label = ->(user) { user.respond_to?(:email) ? user.email : user&.to_s }
  @available_locales = -> { I18n.available_locales.map(&:to_s) }
  @auto_inject = true
  @mount_path = '/i18n_proofreading'
  @show_pill = true
  @pill_label = nil
  @toggle_param = 'i18n_proofreading'
  @on_submit = ->(_suggestion) {}
  @rate_limit = { to: 30, within: 60 }
end

Instance Attribute Details

#admin_layoutObject

Layout used by the triage dashboard. Override this to render it inside your app's admin shell, e.g. "admin/application".



28
29
30
# File 'lib/i18n_proofreading/configuration.rb', line 28

def admin_layout
  @admin_layout
end

#author_labelObject

Turn a resolved user into a short label shown in the "already suggested" list. Receives whatever #current_user returned.



36
37
38
# File 'lib/i18n_proofreading/configuration.rb', line 36

def author_label
  @author_label
end

#authorize_adminObject

Per-request gate for the triage dashboard, which is read-only — browse and read suggestions; there is deliberately no update or destroy, since the tool never writes to the host's locale files. Independent of enabled and enabled_environments: the widget is dev/staging-only, but a maintainer may want to triage from production. Defaults to development only, so a fresh install never exposes the dashboard until you wire it to your own admin check.



24
25
26
# File 'lib/i18n_proofreading/configuration.rb', line 24

def authorize_admin
  @authorize_admin
end

#auto_injectObject

Inject the widget into HTML responses automatically. Set false to place it yourself with <%= i18n_proofreading_tag %> in your layout.



43
44
45
# File 'lib/i18n_proofreading/configuration.rb', line 43

def auto_inject
  @auto_inject
end

#available_localesObject

The locales a suggestion may target. Used to validate submissions.



39
40
41
# File 'lib/i18n_proofreading/configuration.rb', line 39

def available_locales
  @available_locales
end

#current_userObject

Resolve the current user for attribution (optional). Return an object responding to #id, or nil. Receives the Rack::Request.



32
33
34
# File 'lib/i18n_proofreading/configuration.rb', line 32

def current_user
  @current_user
end

#enabledObject

Per-request gate, on top of the environment check. Return false to hide the tool for this request. Receives the Rack::Request. Plug in an admin check, a feature flag, an allowlist, etc.



15
16
17
# File 'lib/i18n_proofreading/configuration.rb', line 15

def enabled
  @enabled
end

#enabled_environmentsObject

Environments the tool is active in. It is never active anywhere else, so a production deploy can't accidentally expose the key markers or the endpoint.



10
11
12
# File 'lib/i18n_proofreading/configuration.rb', line 10

def enabled_environments
  @enabled_environments
end

#mount_pathObject

Where the engine is mounted. The widget posts suggestions to "##mount_path/suggestions", so keep this in sync with the mount line in your routes.



48
49
50
# File 'lib/i18n_proofreading/configuration.rb', line 48

def mount_path
  @mount_path
end

#on_submitObject

Called with each saved suggestion — notify Slack, send an email, open a ticket. Runs inline after save; keep it fast or hand off to a job.



66
67
68
# File 'lib/i18n_proofreading/configuration.rb', line 66

def on_submit
  @on_submit
end

#pill_labelObject

Text on the floating toggle pill. Leave nil to use the localized default ("Suggest edits", translated via the i18n_proofreading.pill I18n key).



56
57
58
# File 'lib/i18n_proofreading/configuration.rb', line 56

def pill_label
  @pill_label
end

#rate_limitObject

Per-IP throttle for the public submission endpoint, as keyword arguments for Rails' rate limiter (Rails 7.2+; ignored on 7.1). Read once when the controller loads — set it in an initializer. nil disables throttling.



71
72
73
# File 'lib/i18n_proofreading/configuration.rb', line 71

def rate_limit
  @rate_limit
end

#show_pillObject

Show the floating toggle pill. Set false to hide it and drive suggest mode from your own link instead (see #toggle_param).



52
53
54
# File 'lib/i18n_proofreading/configuration.rb', line 52

def show_pill
  @show_pill
end

#toggle_paramObject

The query-string parameter that turns suggest mode on/off, so a host can link to it from anywhere: "?i18n_proofreading=true" enables it, "false" exits. The choice is remembered in a cookie, so the rest of the app stays in suggest mode without the parameter.



62
63
64
# File 'lib/i18n_proofreading/configuration.rb', line 62

def toggle_param
  @toggle_param
end

Instance Method Details

#environment_enabled?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/i18n_proofreading/configuration.rb', line 90

def environment_enabled?
  enabled_environments.map(&:to_s).include?(Rails.env.to_s)
end

#suggestions_endpointObject



94
95
96
# File 'lib/i18n_proofreading/configuration.rb', line 94

def suggestions_endpoint
  "#{mount_path.chomp('/')}/suggestions"
end