Module: SuperSettings::ControllerActions
- Defined in:
- lib/super_settings/controller_actions.rb
Overview
Module used to build the SuperSettings::SettingsController for Rails applications. This controller is defined at runtime since it is assumed that the superclass will be one of the application's own base controller classes since the application will want to define authentication and authorization criteria.
The controller is built by extending the class defined by the Configuration object and then mixing in this module.
Class Method Summary collapse
Instance Method Summary collapse
-
#api_js ⇒ Object
Serve up the api.js file that defines a JavaScript client for the REST API.
-
#authorized ⇒ Object
API endpoint for checking if the user is authorized to edit settings.
-
#history ⇒ Object
API endpoint for getting the history of a setting.
-
#index ⇒ Object
API endpoint for getting active settings.
-
#last_updated_at ⇒ Object
API endpoint for getting the last time a setting was changed.
-
#root ⇒ Object
Render the HTML application for managing settings.
-
#show ⇒ Object
API endpoint for getting a setting.
-
#update ⇒ Object
API endpoint for updating settings.
-
#updated_since ⇒ Object
API endpoint for getting settings that have changed since specified time.
Class Method Details
.included(base) ⇒ Object
14 15 16 17 18 |
# File 'lib/super_settings/controller_actions.rb', line 14 def self.included(base) base.layout "super_settings/settings" base.helper SettingsHelper base.protect_from_forgery with: :exception, if: :protect_from_forgery? end |
Instance Method Details
#api_js ⇒ Object
Serve up the api.js file that defines a JavaScript client for the REST API.
95 96 97 98 |
# File 'lib/super_settings/controller_actions.rb', line 95 def api_js js = File.read(File.(File.join("application", "api.js"), __dir__)) render js: js.html_safe, content_type: "application/javascript; charset=utf-8" end |
#authorized ⇒ Object
API endpoint for checking if the user is authorized to edit settings.
87 88 89 90 91 92 |
# File 'lib/super_settings/controller_actions.rb', line 87 def = super_settings_read_only? ? "read-only" : "read-write" headers["super-settings-permission"] = headers["cache-control"] = "no-cache" render json: {authorized: true, permission: } end |
#history ⇒ Object
API endpoint for getting the history of a setting. See SuperSettings::RestAPI for details.
62 63 64 65 66 67 68 69 |
# File 'lib/super_settings/controller_actions.rb', line 62 def history setting_history = SuperSettings::RestAPI.history(params[:key], offset: params[:offset], limit: params[:limit]) if setting_history render json: setting_history else render json: nil, status: 404 end end |
#index ⇒ Object
API endpoint for getting active settings. See SuperSettings::RestAPI for details.
32 33 34 |
# File 'lib/super_settings/controller_actions.rb', line 32 def index render json: SuperSettings::RestAPI.index end |
#last_updated_at ⇒ Object
API endpoint for getting the last time a setting was changed. See SuperSettings::RestAPI for details.
72 73 74 |
# File 'lib/super_settings/controller_actions.rb', line 72 def last_updated_at render json: SuperSettings::RestAPI.last_updated_at end |
#root ⇒ Object
Render the HTML application for managing settings.
21 22 23 24 25 26 27 28 29 |
# File 'lib/super_settings/controller_actions.rb', line 21 def root application = SuperSettings::Application.new( read_only: super_settings_read_only?, locale: I18n.locale, color_scheme: SuperSettings.configuration.controller.color_scheme, dark_mode_selector: SuperSettings.configuration.controller.resolved_dark_mode_selector ) render html: application.render.html_safe, layout: true end |
#show ⇒ Object
API endpoint for getting a setting. See SuperSettings::RestAPI for details.
37 38 39 40 41 42 43 44 |
# File 'lib/super_settings/controller_actions.rb', line 37 def show setting = SuperSettings::RestAPI.show(params[:key]) if setting render json: setting else render json: nil, status: 404 end end |
#update ⇒ Object
API endpoint for updating settings. See SuperSettings::RestAPI for details.
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/super_settings/controller_actions.rb', line 47 def update if super_settings_read_only? render json: {error: "Access denied"}, status: 403 return end changed_by = SuperSettings.configuration.controller.changed_by(self) result = SuperSettings::RestAPI.update(params[:settings], changed_by) if result[:success] render json: result else render json: result, status: 422 end end |
#updated_since ⇒ Object
API endpoint for getting settings that have changed since specified time. See SuperSettings::RestAPI for details.
77 78 79 80 81 82 83 84 |
# File 'lib/super_settings/controller_actions.rb', line 77 def updated_since result = SuperSettings::RestAPI.updated_since(params[:time]) if result render json: result else render json: {error: "Invalid time parameter"}, status: 400 end end |