Method: Sibu::SectionsConcern#create_section

Defined in:
app/models/concerns/sibu/sections_concern.rb

#create_section(*ids, after, new_section) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/concerns/sibu/sections_concern.rb', line 79

def create_section(*ids, after, new_section)
  new_section["id"] = "cs#{Time.current.to_i}"
  if ids.length == 1
    parent = sections
  else
    parent = find_or_init(*ids[0..-2])["elements"]
  end
  if new_section["template"].blank?
    nil
  else
    template_defaults = (site_template.templates && site_template.templates[new_section["template"]]) ? site_template.templates[new_section["template"]] : {}
    sec = template_defaults.merge(new_section)
    ref_pos = parent.index {|s| s["id"] == ids.last}
    parent.insert(after.to_s == 'true' ? ref_pos + 1 : ref_pos, sec)
    sec if save
  end
end