Class: ZendeskGuideCategory

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

Class Method Summary collapse

Class Method Details

.find_or_create_categories(options = {}) ⇒ Object



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
47
48
49
# File 'lib/generators/guidepost/templates/zendesk_guide_category.rb', line 7

def self.find_or_create_categories(options={})
    categories = options[:categories]
    category_objects = options[:category_objects]

    allowed_attributes = [
        :category_id,
        :name,
        :description,
        :locale,
        :source_locale,
        :url,
        :html_url,
        :outdated,
        :position,
        :category_created_at,
        :category_updated_at
    ]

    categories.each do |c|
        category_hash = c.clone

        category_hash[:category_id] = category_hash["id"]
        category_hash.delete("id")

        category_hash[:category_created_at] = category_hash["created_at"]
        category_hash.delete("created_at")

        category_hash[:category_updated_at] = category_hash["updated_at"]
        category_hash.delete("updated_at")

        category_hash.symbolize_keys!

        category_hash.each_key do |k|
            category_hash.delete(k) if !allowed_attributes.include?(k)
        end
        
        category = ZendeskGuideCategory.where(category_id: category_hash[:category_id]).first
        category.update(category_hash) if !category.nil?
        category = ZendeskGuideCategory.create(category_hash) if category.nil?
        
        category_objects << category
    end
end