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.

Constant Summary collapse

DEFAULT_ADMIN_LAYOUT =

The gem's own dashboard layout. Compared against, so DashboardController can tell "the host left this alone" from "the host chose this".

'i18n_proofreading/application'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/i18n_proofreading/configuration.rb', line 92

def initialize
  @enabled_environments = %w[development staging]
  @enabled = ->(_request) { true }
  @authorize_admin = ->(_request) { Rails.env.development? }
  @admin_layout = DEFAULT_ADMIN_LAYOUT
  @base_controller_class = 'ActionController::Base'
  @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".



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

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.



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

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.



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

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.



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

def auto_inject
  @auto_inject
end

#available_localesObject

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



58
59
60
# File 'lib/i18n_proofreading/configuration.rb', line 58

def available_locales
  @available_locales
end

#base_controller_classObject

The controller the DASHBOARD inherits from, as a String so it resolves lazily rather than at config time. Default: a plain 'ActionController::Base', where authorize_admin is the only gate.

Name the controller your own admin already inherits from and the dashboard adopts that whole stack — layout, helpers, authentication, and any request context your before_actions set up. admin_layout covers only the layout, which leaves a host layout calling its own helpers to raise NameError under the engine's isolated namespace.

Only the dashboard uses it. The widget's endpoints stay on the engine's own public controller, so an admin base controller here can never demand a staff session from someone suggesting a translation.



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

def base_controller_class
  @base_controller_class
end

#current_userObject

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



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

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.



19
20
21
# File 'lib/i18n_proofreading/configuration.rb', line 19

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.



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

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.



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

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.



85
86
87
# File 'lib/i18n_proofreading/configuration.rb', line 85

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



75
76
77
# File 'lib/i18n_proofreading/configuration.rb', line 75

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.



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

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



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

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.



81
82
83
# File 'lib/i18n_proofreading/configuration.rb', line 81

def toggle_param
  @toggle_param
end

Instance Method Details

#environment_enabled?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/i18n_proofreading/configuration.rb', line 110

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

#suggestions_endpointObject



114
115
116
# File 'lib/i18n_proofreading/configuration.rb', line 114

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