Class: ZendeskGuideArticle

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

Class Method Summary collapse

Class Method Details

.find_or_create_articles(options = {}) ⇒ Object



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
# File 'lib/generators/guidepost/templates/zendesk_guide_article.rb', line 11

def self.find_or_create_articles(options={})
    articles = options[:articles]
    article_objects = options[:article_objects]
    section_objects = options[:section_objects]
    user_segment_objects = options[:user_segment_objects]
    permission_group_objects = options[:permission_group_objects]
    allowed_attributes = [
        :section_id,
        :article_id,
        :url,
        :html_url,
        :title,
        :body,
        :locale,
        :source_locale,
        :author_id,
        :comments_disabled, 
        :outdated_locales,
        :label_names,
        :draft, 
        :promoted, 
        :position,
        :vote_sum,
        :vote_count,
        :user_segment_id,
        :permission_group_id,
        :article_created_at,
        :article_edited_at,
        :article_updated_at
    ]

    articles.each do |a|
        article_hash = a.clone

        article_hash[:article_id] = article_hash["id"]
        article_hash.delete("id")

        article_hash[:article_created_at] = article_hash["created_at"]
        article_hash.delete("created_at")

        article_hash[:article_updated_at] = article_hash["updated_at"]
        article_hash.delete("updated_at")

        article_hash.symbolize_keys!

        article_hash.each_key do |k|
            article_hash.delete(k) if !allowed_attributes.include?(k)
        end

        article = ZendeskGuideArticle.where(article_id: article_hash[:article_id]).first
        article.update(article_hash) if !article.nil?
        article = ZendeskGuideArticle.create(article_hash) if article.nil?
        
        changed = false

        section_objects.each do |so|
            is_correct_section = (article.section_id == so.section_id)
            if is_correct_section
                article.zendesk_guide_section = so
                changed = true
            end
        end

        user_segment_objects.each do |us|
            is_correct_user_segment = (article.user_segment_id == us.user_segment_id)
            if is_correct_user_segment
                article.zendesk_guide_user_segment = us
                changed = true
            end
        end

        permission_group_objects.each do |pg|
            is_correct_permission_group = (article.permission_group_id == pg.permission_group_id)
            if is_correct_permission_group
                article.zendesk_guide_permission_group = pg
                changed = true
            end
        end

        article.save if changed

        article_objects << article
    end
end