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
18
# 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,
    application_context: "anonymous"
  }
  @sub_configs = {}
end

Instance Attribute Details

#application_contextObject (readonly)

Returns the value of attribute application_context.



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

def application_context
  @application_context
end

#contextObject

Returns the value of attribute context.



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

def context
  @context
end

#defaultsObject



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

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

#organizationObject (readonly)

Returns the value of attribute organization.



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

def organization
  @organization
end

#varsObject (readonly)

Returns the value of attribute vars.



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

def vars
  @vars
end

Instance Method Details

#application_context!(ctx = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/decidim/decidim_awesome/config.rb', line 50

def application_context!(ctx = {})
  @application_context = ctx
  @context[:application_context] = case ctx[:current_user]
                                   when Decidim::User
                                     "user_logged_in"
                                   else
                                     "anonymous"
                                   end
end

#collect_sub_configs_values(singular_key) ⇒ Object

Merges all sub-configs values for custom_styles or any other scoped configs 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 }


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

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



61
62
63
# File 'lib/decidim/decidim_awesome/config.rb', line 61

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)


96
97
98
99
100
# File 'lib/decidim/decidim_awesome/config.rb', line 96

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



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

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



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

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



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

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)


90
91
92
# File 'lib/decidim/decidim_awesome/config.rb', line 90

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



127
128
129
# File 'lib/decidim/decidim_awesome/config.rb', line 127

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



66
67
68
69
70
71
# File 'lib/decidim/decidim_awesome/config.rb', line 66

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



82
83
84
85
86
87
# File 'lib/decidim/decidim_awesome/config.rb', line 82

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

#sub_configs_for(singular_key) ⇒ Object



151
152
153
154
155
156
157
158
159
160
# File 'lib/decidim/decidim_awesome/config.rb', line 151

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



74
75
76
77
78
79
80
# File 'lib/decidim/decidim_awesome/config.rb', line 74

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)


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/decidim/decidim_awesome/config.rb', line 103

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

  # if contains 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