class Decidim::DecidimAwesome::Config

The current awesome config for the organization.

Attributes

context[R]
defaults[W]

Public Class Methods

new(organization) click to toggle source
# 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

Public Instance Methods

config() click to toggle source

config processed in context

# File lib/decidim/decidim_awesome/config.rb, line 49
def config
  @config ||= calculate_config
end
context=(context) click to toggle source
# File lib/decidim/decidim_awesome/config.rb, line 25
def context=(context)
  @config = nil
  @context = context
end
context_from_component(component) click to toggle source

convert component to manifest, slug and id

# 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) click to toggle source

convert participatory space to manifest, slug and id

# 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) click to toggle source

convert context to manifest, slug and id

# 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
defaults() click to toggle source
# File lib/decidim/decidim_awesome/config.rb, line 21
def defaults
  @defaults || Decidim::DecidimAwesome.config
end
enabled_for?(setting) click to toggle source

Checks if some config option es enabled in a certain context

# File lib/decidim/decidim_awesome/config.rb, line 78
def enabled_for?(setting)
  config[setting]
end
organization_config() click to toggle source

config processed for the organization config, without context

# 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) click to toggle source
# File lib/decidim/decidim_awesome/config.rb, line 70
def setting_for(var)
  @vars.find_or_initialize_by(
    organization: @organization,
    var: var
  )
end
unfiltered_config() click to toggle source

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

# 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) click to toggle source

checks if some constraint blocks the validity fot the current context

# 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

Private Instance Methods

calculate_config() click to toggle source
# File lib/decidim/decidim_awesome/config.rb, line 108
def calculate_config
  # filter vars compliant with current context
  valid = @vars.filter { |item| enabled_for_organization?(item.var) && valid_in_context?(item.constraints) }
               .map { |v| [v.var.to_sym, v.value] }.to_h

  map_defaults do |key|
    valid[key].presence
  end
end
enabled_for_organization?(key) click to toggle source

extra checks that may be relevant for the key

# File lib/decidim/decidim_awesome/config.rb, line 119
def enabled_for_organization?(key)
  case key.to_sym
  when :allow_images_in_proposals
    return false if @organization.rich_text_editor_in_public_views
  end
  true
end
map_defaults() { |key| ... } click to toggle source
# File lib/decidim/decidim_awesome/config.rb, line 97
def map_defaults
  defaults.map do |key, val|
    value = false
    unless val == :disabled
      value = yield(key) || val
      value = val.merge(value.transform_keys(&:to_sym)) if val.is_a? Hash
    end
    [key, value]
  end.to_h
end