Class: Decidim::OrganizationSettings

Inherits:
OpenStruct
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(organization) ⇒ OrganizationSettings

Returns a new instance of OrganizationSettings.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'decidim-core/lib/decidim/organization_settings.rb', line 140

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.

Parameters:

  • *chain (Symbol, String)

    The configuration key(s) to dig into inside the default configurations hash.

Returns:

  • The value found from the default configurations hash.



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

.defaultsDecidim::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_beloging_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

Returns:



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.

Parameters:

Returns:



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.

Parameters:

Returns:



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



175
176
177
# File 'decidim-core/lib/decidim/organization_settings.rb', line 175

def wrap_upload_allowed_content_types(value)
  content_type_array(value)
end

#wrap_upload_allowed_content_types_admin(value) ⇒ Object



179
180
181
# File 'decidim-core/lib/decidim/organization_settings.rb', line 179

def wrap_upload_allowed_content_types_admin(value)
  content_type_array(value)
end

#wrap_upload_maximum_file_size(value) ⇒ Object



167
168
169
# File 'decidim-core/lib/decidim/organization_settings.rb', line 167

def wrap_upload_maximum_file_size(value)
  value.megabytes
end

#wrap_upload_maximum_file_size_avatar(value) ⇒ Object



171
172
173
# File 'decidim-core/lib/decidim/organization_settings.rb', line 171

def wrap_upload_maximum_file_size_avatar(value)
  value.megabytes
end