Class: Decidim::Proposals::ProposalForm

Inherits:
Form
  • Object
show all
Includes:
AttachmentAttributes, HasUploadValidations, TranslatableAttributes
Defined in:
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

Instance Method Summary collapse

Instance Method Details

#categoryObject

Finds the Category from the category_id.

Returns a Decidim::Category



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

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

#component_automatic_hashtagsObject



108
109
110
# File 'app/forms/decidim/proposals/proposal_form.rb', line 108

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

#component_suggested_hashtagsObject



112
113
114
# File 'app/forms/decidim/proposals/proposal_form.rb', line 112

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

#extra_hashtagsObject



95
96
97
# File 'app/forms/decidim/proposals/proposal_form.rb', line 95

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

#geocoded?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'app/forms/decidim/proposals/proposal_form.rb', line 91

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

#geocoding_enabled?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'app/forms/decidim/proposals/proposal_form.rb', line 83

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

#has_address?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'app/forms/decidim/proposals/proposal_form.rb', line 87

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

#map_model(model) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/forms/decidim/proposals/proposal_form.rb', line 44

def map_model(model)
  self.title = translated_attribute(model.title)
  self.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))

  self.user_group_id = model.user_groups.first&.id
  self.category_id = model.categorization.decidim_category_id if model.categorization

  # 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



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

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



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

def scope_id
  super || scope&.id
end

#suggested_hashtag_checked?(hashtag) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'app/forms/decidim/proposals/proposal_form.rb', line 104

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

#suggested_hashtagsObject



99
100
101
102
# File 'app/forms/decidim/proposals/proposal_form.rb', line 99

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