Class: ZendeskGuideSection

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

Class Method Summary collapse

Class Method Details

.find_or_create_sections(options = {}) ⇒ Object



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/generators/guidepost/templates/zendesk_guide_section.rb', line 8

def self.find_or_create_sections(options={})
    sections = options[:sections]
    section_objects = options[:section_objects]
    category_objects = options[:category_objects]

    allowed_attributes = [
        :category_id,
        :section_id,
        :name,
        :description,
        :locale,
        :source_locale,
        :url,
        :html_url,
        :outdated,
        :position,
        :section_created_at,
        :section_updated_at
    ]

    sections.each do |s|
        section_hash = s.clone

        section_hash[:section_id] = section_hash["id"]
        section_hash.delete("id")

        section_hash[:section_created_at] = section_hash["created_at"]
        section_hash.delete("created_at")

        section_hash[:section_updated_at] = section_hash["updated_at"]
        section_hash.delete("updated_at")

        section_hash.symbolize_keys!

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

        section = ZendeskGuideSection.where(section_id: section_hash[:section_id]).first
        section.update(section_hash) if !section.nil?
        section = ZendeskGuideSection.create(section_hash) if section.nil?
        
        category_objects.each do |co|
            is_correct_category = (section.category_id == co.category_id)
            if is_correct_category
                section.zendesk_guide_category = co
                section.save
                break
            end
        end

        section_objects << section
    end
end