Class: ZendeskGuideArticleAttachment

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/generators/guidepost/templates/zendesk_guide_article_attachment.rb

Class Method Summary collapse

Class Method Details

.find_or_create_article_attachments(options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
# File 'lib/generators/guidepost/templates/zendesk_guide_article_attachment.rb', line 4

def self.find_or_create_article_attachments(options={})
    article_attachments = options[:article_attachments]
    article_objects = options[:article_objects]
    allowed_attributes = [
        :article_id,
        :article_attachment_id,
        :url,
        :file_name,
        :content_url,
        :size,
        :inline,
        :article_attachment_created_at,
        :article_attachment_updated_at
    ]

    article_attachments.each do |aa|
        article_attachment_hash = aa.clone

        article_attachment_hash[:article_attachment_id] = article_attachment_hash["id"]
        article_attachment_hash.delete("id")

        article_attachment_hash[:article_attachment_created_at] = article_attachment_hash["created_at"]
        article_attachment_hash.delete("created_at")

        article_attachment_hash[:article_attachment_updated_at] = article_attachment_hash["updated_at"]
        article_attachment_hash.delete("updated_at")

        article_attachment_hash.symbolize_keys!

        article_attachment = ZendeskGuideArticleAttachment.where(article_attachment_id: article_attachment_hash[:article_attachment_id]).first
        article_attachment.update(article_attachment_hash) if !article_attachment.nil?
        article_attachment = ZendeskGuideArticleAttachment.create(article_attachment_hash) if article_attachment.nil?
        
        article_objects.each do |a|
            is_correct_article = (article_attachment.article_id == a.article_id)
            if is_correct_article
                article_attachment.zendesk_guide_article = a
                article_attachment.save
                break
            end
        end
    end
end