Class: Amalgam::Tree::Importable

Inherits:
Object
  • Object
show all
Defined in:
lib/amalgam/tree/importable.rb

Class Method Summary collapse

Class Method Details

.import(yaml, force, attributes = []) ⇒ 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
# File 'lib/amalgam/tree/importable.rb', line 4

def self.import(yaml,force,attributes=[])
  Page.delete_all if force == true
  pages = YAML.load(yaml)
  create_tree(pages) do |slug,attrs,parent_id|
    page = ::Page.new
    unless attributes.empty?
      columns = Page.column_names & attributes
      columns.each do |column|
        page.send("#{column}=",attrs[column])if attrs[column] && !['lft','rgt'].include?(column)
      end
    else
      Page.column_names.each do |column|
        page.send("#{column}=",attrs[column])if attrs[column] && !['lft','rgt'].include?(column)
      end
    end
    if page.save
      page
    else
      if parent_id
        page = Page.where("parent_id = ? and slug = ?", parent_id, page.slug).first
      else
        page = Page.where("parent_id is ? and slug = ?", parent_id, page.slug).first
      end
    end
    page
  end
end