Class: Decidim::Debates::DebateForm

Inherits:
Form show all
Includes:
TranslatableAttributes
Defined in:
decidim-debates/app/forms/decidim/debates/debate_form.rb

Overview

This class holds a Form to create/update debates from Decidim's public views.

Constant Summary

Constants included from AttributeObject::TypeMap

AttributeObject::TypeMap::Boolean, AttributeObject::TypeMap::Decimal

Instance Attribute Summary

Attributes inherited from AttributeObject::Form

#context

Instance Method Summary collapse

Methods included from TranslatableAttributes

#default_locale?

Methods inherited from AttributeObject::Form

ensure_hash, from_model, from_params, hash_from, infer_model_name, mimic, mimicked_model_name, model_name, #persisted?, #to_key, #to_model, #to_param, #valid?, #with_context

Methods included from AttributeObject::Model

#[], #[]=, #attributes, #attributes_with_values, #initialize, #to_h

Instance Method Details

#categoryObject


37
38
39
# File 'decidim-debates/app/forms/decidim/debates/debate_form.rb', line 37

def category
  @category ||= current_component.categories.find_by(id: category_id)
end

#debateObject


55
56
57
# File 'decidim-debates/app/forms/decidim/debates/debate_form.rb', line 55

def debate
  @debate ||= Debate.find_by(id: id)
end

#map_model(debate) ⇒ Object


22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'decidim-debates/app/forms/decidim/debates/debate_form.rb', line 22

def map_model(debate)
  super
  # Debates can be translated in different languages from the admin but
  # the public form doesn't allow it. When a user creates a debate the
  # user locale is taken as the text locale.
  self.title = debate.title.values.first
  self.description = debate.description.values.first
  self.user_group_id = debate.decidim_user_group_id

  if debate.category.present?
    self.category_id = debate.category.id
    @category = debate.category
  end
end

#scopeObject

Finds the Scope from the given scope_id, uses component scope if missing.

Returns a Decidim::Scope


44
45
46
# File 'decidim-debates/app/forms/decidim/debates/debate_form.rb', line 44

def scope
  @scope ||= @attributes["scope_id"].value ? current_component.scopes.find_by(id: @attributes["scope_id"].value) : current_component.scope
end

#scope_idObject

Scope identifier

Returns the scope identifier related to the debate


51
52
53
# File 'decidim-debates/app/forms/decidim/debates/debate_form.rb', line 51

def scope_id
  super || scope&.id
end