Sha256: f8d4d0a97ea0264b92d17f8d70d75d295d4dd970316ca47ce66788f59a4073fe

Contents?: true

Size: 1.61 KB

Versions: 58

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

require "active_support/concern"

module Decidim
  module HasSettings
    extend ActiveSupport::Concern

    included do
      after_initialize :default_values
    end

    def settings
      settings_schema(:global).new(self[:settings]["global"])
    end

    def settings=(data)
      self[:settings]["global"] = settings_schema(:global).new(data)
    end

    def current_settings
      if participatory_space.allows_steps?
        active_step_settings
      else
        default_step_settings
      end
    end

    def default_step_settings
      settings_schema(:step).new(self[:settings]["default_step"])
    end

    def default_step_settings=(data)
      self[:settings]["default_step"] = settings_schema(:step).new(data)
    end

    def step_settings
      return {} unless participatory_space.allows_steps?

      participatory_space.steps.each_with_object({}) do |step, result|
        result[step.id.to_s] = settings_schema(:step).new(self[:settings].dig("steps", step.id.to_s))
      end
    end

    def step_settings=(data)
      self[:settings]["steps"] = data.each_with_object({}) do |(key, value), result|
        result[key.to_s] = settings_schema(:step).new(value)
      end
    end

    private

    def active_step_settings
      return unless participatory_space.allows_steps?

      active_step = participatory_space.active_step
      return default_step_settings unless active_step

      step_settings.fetch(active_step.id.to_s)
    end

    def settings_schema(name)
      manifest.settings(name.to_sym).schema
    end

    def default_values
      self[:settings] ||= {}
    end
  end
end

Version data entries

58 entries across 58 versions & 2 rubygems

Version Path
decidim-core-0.18.1 lib/decidim/has_settings.rb
decidim-core-0.17.2 lib/decidim/has_settings.rb
decidim-core-0.18.0 lib/decidim/has_settings.rb
decidim-core-0.17.1 lib/decidim/has_settings.rb
decidim-core-0.16.1 lib/decidim/has_settings.rb
decidim-core-0.17.0 lib/decidim/has_settings.rb
decidim-core-0.16.0 lib/decidim/has_settings.rb
decidim-core-0.15.2 lib/decidim/has_settings.rb
decidim-core-0.15.1 lib/decidim/has_settings.rb
decidim-core-0.15.0 lib/decidim/has_settings.rb
decidim-core-0.14.4 lib/decidim/has_settings.rb
decidim-core-0.14.3 lib/decidim/has_settings.rb
decidim-core-0.14.2 lib/decidim/has_settings.rb
decidim-core-0.14.1 lib/decidim/has_settings.rb
decidim-core-0.13.1 lib/decidim/has_settings.rb
decidim-core-0.12.2 lib/decidim/has_settings.rb
decidim-core-0.13.0 lib/decidim/has_settings.rb
decidim-core-0.12.1 lib/decidim/has_settings.rb
decidim-core-0.13.0.pre1 lib/decidim/has_settings.rb
decidim-core-0.12.0 lib/decidim/has_settings.rb