Class: Decidim::DecidimAwesome::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/decidim/decidim_awesome/config.rb

Overview

The current awesome config for the organization.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(organization) ⇒ Config

Returns a new instance of Config.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/decidim/decidim_awesome/config.rb', line 7

def initialize(organization)
  @organization = organization
  @vars = AwesomeConfig.for_organization(organization).includes(:constraints)
  @context = {
    participatory_space_manifest: nil,
    participatory_space_slug: nil,
    component_id: nil,
    component_manifest: nil
  }
  @sub_configs = {}
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



19
20
21
# File 'lib/decidim/decidim_awesome/config.rb', line 19

def context
  @context
end

#defaultsObject



22
23
24
# File 'lib/decidim/decidim_awesome/config.rb', line 22

def defaults
  @defaults || Decidim::DecidimAwesome.config
end

#organizationObject (readonly)

Returns the value of attribute organization.



19
20
21
# File 'lib/decidim/decidim_awesome/config.rb', line 19

def organization
  @organization
end

#varsObject (readonly)

Returns the value of attribute vars.



19
20
21
# File 'lib/decidim/decidim_awesome/config.rb', line 19

def vars
  @vars
end

Instance Method Details

#collect_sub_configs_values(singular_key) ⇒ Object

Merges all subconfigs values for custom_styles or any other scoped confs by default filtered according to the current scope, a block can be passed for custom filtering ie, collect everything:

collect_sub_configs_values("scoped_style") { true }


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/decidim/decidim_awesome/config.rb', line 124

def collect_sub_configs_values(singular_key)
  plural_key = singular_key.pluralize.to_sym
  return [] unless config[plural_key].respond_to?(:filter)

  fields = config[plural_key]&.filter do |key, _value|
    subconfig = sub_configs_for(singular_key)[key]
    # allow custom filtering if block given
    if block_given?
      yield subconfig
    else
      valid_in_context?(subconfig&.all_constraints)
    end
  end
  fields.values
end

#configObject

config processed in context



50
51
52
# File 'lib/decidim/decidim_awesome/config.rb', line 50

def config
  @config ||= calculate_config
end

#constrained_in_context?(setting) ⇒ Boolean

returns true if some setting is constrained in the current context if no constraints defined, applies to everything

Returns:

  • (Boolean)


85
86
87
88
89
# File 'lib/decidim/decidim_awesome/config.rb', line 85

def constrained_in_context?(setting)
  return true unless @vars.exists?(var: setting)

  @vars.where(var: setting).any? { |v| valid_in_context?(v.all_constraints) }
end

#context_from_component(component) ⇒ Object

convert component to manifest, slug and id



38
39
40
41
# File 'lib/decidim/decidim_awesome/config.rb', line 38

def context_from_component(component)
  @config = nil
  @context = Decidim::DecidimAwesome::ContextAnalyzers::ComponentAnalyzer.context_for component
end

#context_from_participatory_space(space) ⇒ Object

convert participatory space to manifest, slug and id



44
45
46
47
# File 'lib/decidim/decidim_awesome/config.rb', line 44

def context_from_participatory_space(space)
  @config = nil
  @context = Decidim::DecidimAwesome::ContextAnalyzers::ParticipatorySpaceAnalyzer.context_for space
end

#context_from_request(request) ⇒ Object

convert context to manifest, slug and id



32
33
34
35
# File 'lib/decidim/decidim_awesome/config.rb', line 32

def context_from_request(request)
  @config = nil
  @context = Decidim::DecidimAwesome::ContextAnalyzers::RequestAnalyzer.context_for request
end

#enabled_in_context?(setting) ⇒ Boolean

Checks if some config setting is enabled in a certain context

Returns:

  • (Boolean)


79
80
81
# File 'lib/decidim/decidim_awesome/config.rb', line 79

def enabled_in_context?(setting)
  config[setting]
end

#inject_sub_config_constraints(singular_key, subkey, constraints) ⇒ Object

adds some custom constraints for the instance that can be generated dynamically



116
117
118
# File 'lib/decidim/decidim_awesome/config.rb', line 116

def inject_sub_config_constraints(singular_key, subkey, constraints)
  sub_configs_for(singular_key)[subkey.to_sym]&.add_constraints constraints
end

#organization_configObject

config processed for the organization config, without context



55
56
57
58
59
60
# File 'lib/decidim/decidim_awesome/config.rb', line 55

def organization_config
  @organization_config ||= unfiltered_config.to_h do |key, value|
    value = defaults[key] unless enabled_for_organization? key
    [key, value]
  end
end

#setting_for(var) ⇒ Object



71
72
73
74
75
76
# File 'lib/decidim/decidim_awesome/config.rb', line 71

def setting_for(var)
  @vars.find_or_initialize_by(
    organization: @organization,
    var: var
  )
end

#sub_configs_for(singular_key) ⇒ Object



140
141
142
143
144
145
146
147
148
149
# File 'lib/decidim/decidim_awesome/config.rb', line 140

def sub_configs_for(singular_key)
  return @sub_configs[singular_key] if @sub_configs[singular_key]

  plural_key = singular_key.pluralize.to_sym
  return {} unless config[plural_key]

  @sub_configs[singular_key] = config[plural_key].to_h do |key, _value|
    [key, AwesomeConfig.find_by(var: "#{singular_key}_#{key}", organization: @organization)]
  end
end

#unfiltered_configObject

config normalized according default values, without context, without organization config



63
64
65
66
67
68
69
# File 'lib/decidim/decidim_awesome/config.rb', line 63

def unfiltered_config
  valid = @vars.to_h { |v| [v.var.to_sym, v.value] }

  map_defaults do |key, val|
    valid.has_key?(key) ? valid[key] : val
  end
end

#valid_in_context?(constraints) ⇒ Boolean

checks if some constraint blocks the validity fot the current context

Returns:

  • (Boolean)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/decidim/decidim_awesome/config.rb', line 92

def valid_in_context?(constraints)
  # if no constraints defined, applies to everything
  return true if constraints.blank?

  # if containts the "none" constraints, deactivate everything else
  return false if constraints.detect { |c| c.settings["participatory_space_manifest"] == "none" }

  # check if current context matches some constraint
  constraints.detect do |constraint|
    settings = constraint.settings.symbolize_keys
    match_method = settings.delete(:match)
    if match_method == "exclusive"
      # all keys must match
      settings == context
    else
      # constraints keys can match the context partially (ie: if slug is not specified, any space matches in the same manifest)
      # if some setting is different, rejects
      invalid = constraint.settings.detect { |key, val| context[key.to_sym].to_s != val.to_s }
      invalid.blank?
    end
  end
end