Class: Decidim::Feature

Inherits:
ApplicationRecord show all
Defined in:
app/models/decidim/feature.rb

Overview

A Feature represents a self-contained group of functionalities usually defined via a FeatureManifest. It’s meant to be able to provide a single feature that spans over several steps.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.publishedObject

Public: Filters the features that are published and, therefore, visible by the end user.

Returns an ActiveRecord::Relation.



21
22
23
# File 'app/models/decidim/feature.rb', line 21

def self.published
  where.not(published_at: nil)
end

.unpublishedObject

Public: Filters the features that are unpublished and, therefore, not visible by the end user.

Returns an ActiveRecord::Relation.



29
30
31
# File 'app/models/decidim/feature.rb', line 29

def self.unpublished
  where(published_at: nil)
end

Instance Method Details

#active_step_settingsObject



84
85
86
87
88
89
# File 'app/models/decidim/feature.rb', line 84

def active_step_settings
  active_step = participatory_process.active_step
  return default_step_settings unless active_step

  step_settings.fetch(active_step.id.to_s)
end

#default_step_settingsObject



64
65
66
# File 'app/models/decidim/feature.rb', line 64

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

#default_step_settings=(data) ⇒ Object



68
69
70
# File 'app/models/decidim/feature.rb', line 68

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

#manifestObject

Public: Finds the manifest this feature is associated to.

Returns a FeatureManifest.



43
44
45
# File 'app/models/decidim/feature.rb', line 43

def manifest
  Decidim.find_feature_manifest(manifest_name)
end

#manifest=(manifest) ⇒ Object

Public: Assigns a manifest to this feature.

manifest - The FeatureManifest for this Feature.

Returns nothing.



52
53
54
# File 'app/models/decidim/feature.rb', line 52

def manifest=(manifest)
  self.manifest_name = manifest.name
end

#primary_statObject

Public: Returns the value of the registered primary stat.



92
93
94
# File 'app/models/decidim/feature.rb', line 92

def primary_stat
  @primary_stat ||= manifest.stats.filter(primary: true).with_context([self]).map { |name, value| [name, value] }.first&.last
end

#published?Boolean

Public: Finds out whether this feature is published.

Returns true if published, false otherwise.

Returns:

  • (Boolean)


36
37
38
# File 'app/models/decidim/feature.rb', line 36

def published?
  published_at.present?
end

#settingsObject



56
57
58
# File 'app/models/decidim/feature.rb', line 56

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

#settings=(data) ⇒ Object



60
61
62
# File 'app/models/decidim/feature.rb', line 60

def settings=(data)
  self[:settings]["global"] = serialize_settings(settings_schema(:global), data)
end

#step_settingsObject



72
73
74
75
76
# File 'app/models/decidim/feature.rb', line 72

def step_settings
  participatory_process.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

#step_settings=(data) ⇒ Object



78
79
80
81
82
# File 'app/models/decidim/feature.rb', line 78

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