Class: SnsImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/sns_importer.rb

Defined Under Namespace

Classes: OldTextAsset

Instance Method Summary collapse

Instance Method Details

#add_sheet_content(sheet, attrs) ⇒ Object



62
63
64
65
# File 'lib/sns_importer.rb', line 62

def add_sheet_content(sheet, attrs)
  sheet.part('body').content = attrs.fetch('content','')
  sheet.part('body').filter_id = attrs.fetch('filter_id','')
end

#callObject



29
30
31
32
33
34
# File 'lib/sns_importer.rb', line 29

def call
  sns_assets.each do |ta|
    p "Importing #{ta.class_name} #{ta.name}"
    create_text_asset(ta.attributes)
  end
end

#clean_sns_attributes(sheet, attrs) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/sns_importer.rb', line 54

def clean_sns_attributes(sheet, attrs)
  clean_attrs = attrs.dup
  clean_attrs.delete_if { |att|
    att[0].to_s.match(/^(lock_version|id|content|filter_id|name|class_name)$/) || !sheet.respond_to?("#{att[0]}=")
  }
  clean_attrs
end

#create_text_asset(attrs) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/sns_importer.rb', line 36

def create_text_asset(attrs)
  class_name = attrs.fetch 'class_name', 'Stylesheet'
  klass = (class_name + 'Page').constantize
  sheet = klass.new_with_defaults

  sheet.slug = attrs['name']
  
  unless Page.exists?({:slug => sheet.slug, :parent_id => sheet.parent_id, :class_name => sheet.class_name})
    add_sheet_content(sheet, attrs)
    set_sheet_parent(sheet, class_name)

    clean_attrs = clean_sns_attributes(sheet, attrs)
    sheet.update_attributes(clean_attrs)
  end
  
  sheet
end

#javascript_rootObject



16
17
18
# File 'lib/sns_importer.rb', line 16

def javascript_root
  @javascript_root ||= JavascriptPage.root || JavascriptPage.create_root
end

#root(which) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/sns_importer.rb', line 20

def root(which)
  case which
  when 'stylesheet'
    stylesheet_root
  when 'javascript'
    javascript_root
  end
end

#set_sheet_parent(sheet, class_name) ⇒ Object



67
68
69
# File 'lib/sns_importer.rb', line 67

def set_sheet_parent(sheet, class_name)
  sheet.parent_id = root(class_name.downcase).id
end

#sns_assetsObject



8
9
10
# File 'lib/sns_importer.rb', line 8

def sns_assets
  OldTextAsset.all
end

#stylesheet_rootObject



12
13
14
# File 'lib/sns_importer.rb', line 12

def stylesheet_root
  @stylesheet_root ||= StylesheetPage.root || StylesheetPage.create_root
end