Class: Decidim::Proposals::ProposalForm

Inherits:
ProposalWizardCreateStepForm show all
Includes:
AttachmentAttributes, HasUploadValidations, TranslatableAttributes
Defined in:
decidim-proposals/app/forms/decidim/proposals/proposal_form.rb

Overview

A form object to be used when public users want to create a proposal.

Direct Known Subclasses

CollaborativeDraftForm

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 HasUploadValidations

#attached_uploader, #maximum_avatar_size, #maximum_upload_size

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

Finds the Category from the category_id.

Returns a Decidim::Category



50
51
52
# File 'decidim-proposals/app/forms/decidim/proposals/proposal_form.rb', line 50

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

#component_automatic_hashtagsObject



93
94
95
# File 'decidim-proposals/app/forms/decidim/proposals/proposal_form.rb', line 93

def component_automatic_hashtags
  @component_automatic_hashtags ||= ordered_hashtag_list(current_component.current_settings.automatic_hashtags)
end

#component_suggested_hashtagsObject



97
98
99
# File 'decidim-proposals/app/forms/decidim/proposals/proposal_form.rb', line 97

def component_suggested_hashtags
  @component_suggested_hashtags ||= ordered_hashtag_list(current_component.current_settings.suggested_hashtags)
end

#extra_hashtagsObject



80
81
82
# File 'decidim-proposals/app/forms/decidim/proposals/proposal_form.rb', line 80

def extra_hashtags
  @extra_hashtags ||= (component_automatic_hashtags + suggested_hashtags).uniq
end

#geocoded?Boolean

Returns:



76
77
78
# File 'decidim-proposals/app/forms/decidim/proposals/proposal_form.rb', line 76

def geocoded?
  latitude.present? && longitude.present?
end

#geocoding_enabled?Boolean

Returns:



68
69
70
# File 'decidim-proposals/app/forms/decidim/proposals/proposal_form.rb', line 68

def geocoding_enabled?
  Decidim::Map.available?(:geocoding) && current_component.settings.geocoding_enabled?
end

#has_address?Boolean

Returns:



72
73
74
# File 'decidim-proposals/app/forms/decidim/proposals/proposal_form.rb', line 72

def has_address?
  geocoding_enabled? && address.present?
end

#map_model(model) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'decidim-proposals/app/forms/decidim/proposals/proposal_form.rb', line 31

def map_model(model)
  super

  body = translated_attribute(model.body)
  @suggested_hashtags = Decidim::ContentRenderers::HashtagRenderer.new(body).extra_hashtags.map(&:name).map(&:downcase)

  presenter = ProposalPresenter.new(model)
  self.body = presenter.editor_body(all_locales: body.is_a?(Hash))

  # The scope attribute is with different key (decidim_scope_id), so it
  # has to be manually mapped.
  self.scope_id = model.scope.id if model.scope

  self.documents = model.attachments
end

#scopeObject

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

Returns a Decidim::Scope



57
58
59
# File 'decidim-proposals/app/forms/decidim/proposals/proposal_form.rb', line 57

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 proposal



64
65
66
# File 'decidim-proposals/app/forms/decidim/proposals/proposal_form.rb', line 64

def scope_id
  super || scope&.id
end

#suggested_hashtag_checked?(hashtag) ⇒ Boolean

Returns:



89
90
91
# File 'decidim-proposals/app/forms/decidim/proposals/proposal_form.rb', line 89

def suggested_hashtag_checked?(hashtag)
  suggested_hashtags.member?(hashtag)
end

#suggested_hashtagsObject



84
85
86
87
# File 'decidim-proposals/app/forms/decidim/proposals/proposal_form.rb', line 84

def suggested_hashtags
  downcased_suggested_hashtags = super.to_set(&:downcase)
  component_suggested_hashtags.select { |hashtag| downcased_suggested_hashtags.member?(hashtag.downcase) }
end