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

#addressObject



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

def address
  return unless has_address

  super
end

#categoryObject

Finds the Category from the category_id.

Returns a Decidim::Category



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

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

#component_automatic_hashtagsObject



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

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

#component_suggested_hashtagsObject



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

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

#extra_hashtagsObject



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

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

#geocoded?Boolean

Returns:



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

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

#geocoding_enabled?Boolean

Returns:



74
75
76
# File 'decidim-proposals/app/forms/decidim/proposals/proposal_form.rb', line 74

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

#has_address?Boolean

Returns:



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

def has_address?
  return unless has_address

  geocoding_enabled? && address.present?
end

#map_model(model) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'decidim-proposals/app/forms/decidim/proposals/proposal_form.rb', line 34

def map_model(model)
  super

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

  # 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.has_address = true if model.address.present?

  # Proposals have the "photos" field reserved for the proposal card image
  # so we don't want to show all photos there. Instead, only show the
  # first photo.
  self.photos = [model.photo].compact.select { |p| p.weight.zero? }
  self.documents = model.attachments - photos
end

#scopeObject

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

Returns a Decidim::Scope



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

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



70
71
72
# File 'decidim-proposals/app/forms/decidim/proposals/proposal_form.rb', line 70

def scope_id
  super || scope&.id
end

#suggested_hashtag_checked?(hashtag) ⇒ Boolean

Returns:



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

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

#suggested_hashtagsObject



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

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