Class: Decidim::Assemblies::AssemblyImporter

Inherits:
Importers::Importer
  • Object
show all
Defined in:
app/serializers/decidim/assemblies/assembly_importer.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(organization, user) ⇒ AssemblyImporter

Returns a new instance of AssemblyImporter.



9
10
11
12
13
# File 'app/serializers/decidim/assemblies/assembly_importer.rb', line 9

def initialize(organization, user)
  @organization = organization
  @user = user
  @warnings = []
end

Instance Attribute Details

#warningsObject (readonly)

Returns the value of attribute warnings.



7
8
9
# File 'app/serializers/decidim/assemblies/assembly_importer.rb', line 7

def warnings
  @warnings
end

Instance Method Details

#import(attributes, _user, opts) ⇒ Object

Public: Creates a new Assembly.

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

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

Returns an Assembly instance.



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
57
58
59
60
61
62
63
64
65
# File 'app/serializers/decidim/assemblies/assembly_importer.rb', line 24

def import(attributes, _user, opts)
  title = opts[:title]
  slug = opts[:slug]
  Decidim.traceability.perform_action!(:create, Assembly, @user, visibility: "all") do
    @imported_assembly = Assembly.new(
      organization: @organization,
      title:,
      slug:,
      subtitle: attributes["subtitle"],
      short_description: attributes["short_description"],
      description: attributes["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"],
      reference: attributes["reference"],
      purpose_of_action: attributes["purpose_of_action"],
      composition: attributes["composition"],
      duration: attributes["duration"],
      creation_date: attributes["creation_date"],
      closing_date_reason: attributes["closing_date_reason"],
      included_at: attributes["included_at"],
      closing_date: attributes["closing_date"],
      created_by_other: attributes["created_by_other"],
      internal_organisation: attributes["internal_organisation"],
      special_features: attributes["special_features"],
      twitter_handler: attributes["twitter_handler"],
      instagram_handler: attributes["instagram_handler"],
      facebook_handler: attributes["facebook_handler"],
      youtube_handler: attributes["youtube_handler"],
      github_handler: attributes["github_handler"],
      created_by: attributes["created_by"],
      meta_scope: attributes["meta_scope"],
      access_mode: resolve_access_mode(attributes)
    )
    import_hero_image(attributes["remote_hero_image_url"])
    @imported_assembly.save!
    @imported_assembly
  end
end

#import_components(components) ⇒ Object

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



118
119
120
121
122
123
# File 'app/serializers/decidim/assemblies/assembly_importer.rb', line 118

def import_components(components)
  return if components.nil?

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

#import_folders_and_attachments(attachments) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/serializers/decidim/assemblies/assembly_importer.rb', line 67

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

  attachments["files"].map do |file|
    url = file["remote_file_url"]
    next if url.blank?

    error = remote_file_error(url)
    if error.present?
      @warnings << I18n.t(
        "decidim.assemblies.admin.imports.attachment_error",
        title: attachment_title(file),
        error:
      )
      next
    end

    Decidim.traceability.perform_action!("create", Attachment, @user) do
      attachment = Attachment.new(
        title: file["title"],
        description: file["description"],
        attached_to: @imported_assembly,
        weight: file["weight"]
      )
      begin
        attachment.attached_uploader(:file).remote_url = url
        attachment.set_content_type_and_size
      rescue OpenURI::HTTPError, Errno::ENOENT, Errno::ECONNREFUSED, SocketError, Net::OpenTimeout, Net::ReadTimeout => e
        @warnings << I18n.t(
          "decidim.assemblies.admin.imports.attachment_error",
          title: attachment_title(file),
          error: format_error(e)
        )
        next
      end
      attachment.create_attachment_collection(file["attachment_collection"])
      attachment.save!
      attachment
    end
  end

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