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
# 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_slug: nil,
    component_id: nil,
    component_manifest: nil
  }
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



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

def context
  @context
end

#defaultsObject



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

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

Instance Method Details

#configObject

config processed in context



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

def config
  @config ||= calculate_config
end

#context_from_component(component) ⇒ Object

convert component to manifest, slug and id



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

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



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

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



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

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

#enabled_for?(setting) ⇒ Boolean

Checks if some config option es enabled in a certain context

Returns:

  • (Boolean)


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

def enabled_for?(setting)
  config[setting]
end

#organization_configObject

config processed for the organization config, without context



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

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

#setting_for(var) ⇒ Object



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

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

#unfiltered_configObject

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



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

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

  map_defaults do |key|
    valid[key].presence
  end
end

#valid_in_context?(constraints) ⇒ Boolean

checks if some constraint blocks the validity fot the current context

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
# File 'lib/decidim/decidim_awesome/config.rb', line 83

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

  # check if current context matches some constraint
  constraints.detect do |constraint|
    # 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