Class: Decidim::OrganizationSettings
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- Decidim::OrganizationSettings
- Defined in:
- decidim-core/lib/decidim/organization_settings.rb
Overview
Takes care of holding and accessing organization settings for each organization or the default organization.
Class Method Summary collapse
-
.default(*chain) ⇒ Object
Returns the default configuration value for the given setting key chain.
-
.defaults ⇒ Decidim::OrganizationSettings
This is a class level helper to get a “dummy” default settings object that responds to the same methods as the normal organization specific settings objects.
-
.for(organization) ⇒ Decidim::OrganizationSettings
Fetches or creates a settings object for the given organization.
-
.reload(organization) ⇒ Decidim::OrganizationSettings
Reloads the settings object for a given organization.
-
.reset! ⇒ Object
The settings should be reset during the tests in order for the new tests to always load the latest settings.
Instance Method Summary collapse
-
#initialize(organization) ⇒ OrganizationSettings
constructor
A new instance of OrganizationSettings.
- #wrap_upload_allowed_content_types(value) ⇒ Object
- #wrap_upload_allowed_content_types_admin(value) ⇒ Object
- #wrap_upload_maximum_file_size(value) ⇒ Object
- #wrap_upload_maximum_file_size_avatar(value) ⇒ Object
Constructor Details
#initialize(organization) ⇒ OrganizationSettings
Returns a new instance of OrganizationSettings.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'decidim-core/lib/decidim/organization_settings.rb', line 151 def initialize(organization) # This maps the local configuration top level keys to the methods/column # names in the organization model that provide the values for these # settings. keys_map = { upload: :file_upload_settings } # Pass a configuration hash to the parent constructor that combines the # default settings with the organization level settings. This ensures that # all configurations have values even when the organization settings do # not define them. super( keys_map.to_h do |config, method| [ config.to_s, generate_config( organization.public_send(method) || {}, self.class.default(config) ) ] end ) keys_map.keys.each do |config| define_config_accessors(public_send(config), [config]) end end |
Class Method Details
.default(*chain) ⇒ Object
Returns the default configuration value for the given setting key chain.
For example:
Decidim::OrganizationSettings.default(:upload, :maximum_file_size, :default)
#=> 10
Note: this cannot fetch the default settings from the class instance variable “defaults” because that would cause an infinite loop.
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'decidim-core/lib/decidim/organization_settings.rb', line 54 def default(*chain) return if chain.blank? configs = defaults_hash while (lookup = chain.shift) configs = configs[lookup.to_s] return configs if chain.empty? return unless configs.is_a?(Hash) end nil end |
.defaults ⇒ Decidim::OrganizationSettings
This is a class level helper to get a “dummy” default settings object that responds to the same methods as the normal organization specific settings objects. This allows for the following kind of syntactic sugar:
settings = Decidim::OrganizationSettings.for(actual_org)
settings.target_config_accessor #=> returns the organization setting
settings = Decidim::OrganizationSettings.defaults
settings.target_config_accessor #=> returns the default setting
This can be used through the Decidim module as follows:
Decidim.organization_settings(org).target_config_accessor
#=> returns the organization specific setting
Decidim.organization_settings(model_belonging_to_org).target_config_accessor
#=> returns the organization specific setting for model's organization
Decidim.organization_settings(nil).target_config_accessor
#=> returns the default setting
89 90 91 |
# File 'decidim-core/lib/decidim/organization_settings.rb', line 89 def defaults @defaults ||= new(OpenStruct.new) end |
.for(organization) ⇒ Decidim::OrganizationSettings
Fetches or creates a settings object for the given organization.
15 16 17 18 19 20 21 22 |
# File 'decidim-core/lib/decidim/organization_settings.rb', line 15 def for(organization) # Before the organization has an ID attached to it, it cannot be stored # to the registry. return new(organization) if organization.new_record? @registry ||= {} @registry[organization.id] ||= new(organization) end |
.reload(organization) ⇒ Decidim::OrganizationSettings
Reloads the settings object for a given organization. Should be called when the configurations have changed and need to be reloaded.
29 30 31 32 |
# File 'decidim-core/lib/decidim/organization_settings.rb', line 29 def reload(organization) @registry.delete(organization.id) if @registry self.for(organization) end |
.reset! ⇒ Object
The settings should be reset during the tests in order for the new tests to always load the latest settings. The results of the previous tests should not affect the settings of the following test.
37 38 39 |
# File 'decidim-core/lib/decidim/organization_settings.rb', line 37 def reset! @registry = {} end |
Instance Method Details
#wrap_upload_allowed_content_types(value) ⇒ Object
186 187 188 |
# File 'decidim-core/lib/decidim/organization_settings.rb', line 186 def wrap_upload_allowed_content_types(value) content_type_array(value) end |
#wrap_upload_allowed_content_types_admin(value) ⇒ Object
190 191 192 |
# File 'decidim-core/lib/decidim/organization_settings.rb', line 190 def wrap_upload_allowed_content_types_admin(value) content_type_array(value) end |
#wrap_upload_maximum_file_size(value) ⇒ Object
178 179 180 |
# File 'decidim-core/lib/decidim/organization_settings.rb', line 178 def wrap_upload_maximum_file_size(value) value.megabytes end |
#wrap_upload_maximum_file_size_avatar(value) ⇒ Object
182 183 184 |
# File 'decidim-core/lib/decidim/organization_settings.rb', line 182 def wrap_upload_maximum_file_size_avatar(value) value.megabytes end |