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.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/i18n_proofreading/configuration.rb', line 67

def initialize
  @enabled_environments = %w[development staging]
  @enabled = ->(_request) { true }
  @authorize_admin = ->(_request) { Rails.env.development? }
  @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

#author_labelObject

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



30
31
32
# File 'lib/i18n_proofreading/configuration.rb', line 30

def author_label
  @author_label
end

#authorize_adminObject

Per-request gate for the triage dashboard (browse suggestions, change their status, delete). 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.



22
23
24
# File 'lib/i18n_proofreading/configuration.rb', line 22

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.



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

def auto_inject
  @auto_inject
end

#available_localesObject

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



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

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.



26
27
28
# File 'lib/i18n_proofreading/configuration.rb', line 26

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.



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

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.



60
61
62
# File 'lib/i18n_proofreading/configuration.rb', line 60

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).



50
51
52
# File 'lib/i18n_proofreading/configuration.rb', line 50

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.



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

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).



46
47
48
# File 'lib/i18n_proofreading/configuration.rb', line 46

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.



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

def toggle_param
  @toggle_param
end

Instance Method Details

#environment_enabled?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/i18n_proofreading/configuration.rb', line 83

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

#suggestions_endpointObject



87
88
89
# File 'lib/i18n_proofreading/configuration.rb', line 87

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