Class: Decidim::ParticipatoryProcesses::ParticipatoryProcessImporter

Inherits:
Importers::Importer show all
Defined in:
decidim-participatory_processes/app/serializers/decidim/participatory_processes/participatory_process_importer.rb

Overview

A factory class to ensure we always create ParticipatoryProcesses the same way since it involves some logic.

Instance Method Summary collapse

Constructor Details

#initialize(organization, user) ⇒ ParticipatoryProcessImporter

Returns a new instance of ParticipatoryProcessImporter.



7
8
9
10
# File 'decidim-participatory_processes/app/serializers/decidim/participatory_processes/participatory_process_importer.rb', line 7

def initialize(organization, user)
  @organization = organization
  @user = user
end

Instance Method Details

#import(attributes, _user, opts) ⇒ Object

Public: Creates a new ParticipatoryProcess.

attributes - The Hash of attributes to create the ParticipatoryProcess with. user - The user that performs the action. opts - The options MUST contain:

- title: The +title+ for the new PartidicpatoryProcess
- slug: The +slug+ for the new PartidicpatoryProcess

Returns a ParticipatoryProcess.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'decidim-participatory_processes/app/serializers/decidim/participatory_processes/participatory_process_importer.rb', line 21

def import(attributes, _user, opts)
  title = opts[:title]
  slug = opts[:slug]
  Decidim.traceability.perform_action!(:create, ParticipatoryProcess, @user, visibility: "all") do
    @imported_process = ParticipatoryProcess.new(
      organization: @organization,
      title:,
      slug:,
      subtitle: attributes["subtitle"],
      hashtag: attributes["hashtag"],
      description: attributes["description"],
      short_description: attributes["short_description"],
      promoted: attributes["promoted"],
      developer_group: attributes["developer_group"],
      local_area: attributes["local_area"],
      target: attributes["target"],
      participatory_scope: attributes["participatory_scope"],
      participatory_structure: attributes["participatory_structure"],
      meta_scope: attributes["meta_scope"],
      start_date: attributes["start_date"],
      end_date: attributes["end_date"],
      announcement: attributes["announcement"],
      private_space: attributes["private_space"],
      scopes_enabled: attributes["scopes_enabled"],
      show_metrics: attributes["show_metrics"],
      show_statistics: attributes["show_statistics"],
      participatory_process_group: import_process_group(attributes["participatory_process_group"]),
      participatory_process_type: import_participatory_process_type(attributes["participatory_process_type"])
    )
    @imported_process.attached_uploader(:hero_image).remote_url = attributes["remote_hero_image_url"] if attributes["remote_hero_image_url"].present?
    @imported_process.attached_uploader(:banner_image).remote_url = attributes["remote_banner_image_url"] if attributes["remote_banner_image_url"].present?

    @imported_process.save!
    @imported_process
  end
end

#import_categories(categories) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'decidim-participatory_processes/app/serializers/decidim/participatory_processes/participatory_process_importer.rb', line 108

def import_categories(categories)
  return if categories.nil?

  categories.map do |category_attributes|
    category = Decidim.traceability.create!(
      Category,
      @user,
      name: category_attributes["name"],
      description: category_attributes["description"],
      parent_id: category_attributes["parent_id"],
      participatory_space: @imported_process
    )
    next if category_attributes["subcategories"].nil?

    category_attributes["subcategories"].map do |subcategory_attributes|
      Decidim.traceability.create!(
        Category,
        @user,
        name: subcategory_attributes["name"],
        description: subcategory_attributes["description"],
        parent_id: category.id,
        participatory_space: @imported_process
      )
    end
  end
end

#import_components(components) ⇒ Object

components: An Array of Hashes, each corresponding with the settings of a Decidim::Component.



167
168
169
170
171
172
# File 'decidim-participatory_processes/app/serializers/decidim/participatory_processes/participatory_process_importer.rb', line 167

def import_components(components)
  return if components.nil?

  importer = Decidim::Importers::ParticipatorySpaceComponentsImporter.new(@imported_process)
  importer.import(components, @user)
end

#import_folders_and_attachments(attachments) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'decidim-participatory_processes/app/serializers/decidim/participatory_processes/participatory_process_importer.rb', line 135

def import_folders_and_attachments(attachments)
  return if attachments["files"].nil?

  attachments["files"].map do |file|
    next unless remote_file_exists?(file["remote_file_url"])

    file_tmp = URI.parse(file["remote_file_url"]).open

    Decidim.traceability.perform_action!("create", Attachment, @user) do
      attachment = Attachment.new(
        title: file["title"],
        description: file["description"],
        content_type: file_tmp.content_type,
        attached_to: @imported_process,
        weight: file["weight"],
        file: file_tmp, # Define attached_to before this
        file_size: file_tmp.size
      )
      attachment.create_attachment_collection(file["attachment_collection"])
      attachment.save!
      attachment
    end
  end

  attachments["attachment_collections"].map do |collection|
    Decidim.traceability.perform_action!("create", AttachmentCollection, @user) do
      create_attachment_collection(collection)
    end
  end
end

#import_participatory_process_steps(steps) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'decidim-participatory_processes/app/serializers/decidim/participatory_processes/participatory_process_importer.rb', line 90

def import_participatory_process_steps(steps)
  return if steps.nil?

  steps.map do |step_attributes|
    Decidim.traceability.create!(
      ParticipatoryProcessStep,
      @user,
      title: step_attributes["title"],
      description: step_attributes["description"],
      start_date: step_attributes["start_date"],
      end_date: step_attributes["end_date"],
      participatory_process: @imported_process,
      active: step_attributes["active"],
      position: step_attributes["position"]
    )
  end
end

#import_participatory_process_type(participatory_process_type) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'decidim-participatory_processes/app/serializers/decidim/participatory_processes/participatory_process_importer.rb', line 77

def import_participatory_process_type(participatory_process_type)
  return if participatory_process_type.blank?

  return if compact_translation(participatory_process_type["title"]).blank?

  Decidim.traceability.perform_action!("create", ParticipatoryProcessType, @user) do
    Decidim::ParticipatoryProcessType.find_or_create_by(
      title: participatory_process_type["title"],
      organization: @organization
    )
  end
end

#import_process_group(attributes) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'decidim-participatory_processes/app/serializers/decidim/participatory_processes/participatory_process_importer.rb', line 58

def import_process_group(attributes)
  title = compact_translation(attributes["title"] || attributes["name"])
  description = compact_translation(attributes["description"])

  return if title.blank? && description.blank?

  Decidim.traceability.perform_action!("create", ParticipatoryProcessGroup, @user) do
    group = ParticipatoryProcessGroup.find_or_initialize_by(
      title: attributes["title"] || attributes["name"],
      description: attributes["description"],
      organization: @organization
    )

    group.remote_hero_image_url = attributes["remote_hero_image_url"] if remote_file_exists?(attributes["remote_hero_image_url"])
    group.save!
    group
  end
end