Module: Sibu::SectionsConcern
- Includes:
- ActiveSupport::Concern
- Defined in:
- app/models/concerns/sibu/sections_concern.rb
Instance Method Summary collapse
- #child_element(*ids, element_id) ⇒ Object
- #clone_element(*ids, element_id) ⇒ Object
- #create_section(*ids, after, new_section) ⇒ Object
- #delete_element(*ids, element_id) ⇒ Object
- #delete_section(*ids) ⇒ Object
- #element(*ids) ⇒ Object
- #elements(*ids) ⇒ Object
- #elt(siblings, elt_id, default_elt = nil) ⇒ Object
- #find_or_init(*ids) ⇒ Object
- #section(id) ⇒ Object
- #update_element(*ids, value) ⇒ Object
Instance Method Details
#child_element(*ids, element_id) ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'app/models/concerns/sibu/sections_concern.rb', line 68 def child_element(*ids, element_id) siblings = elements(*ids) parent_elt = siblings[siblings.index {|s| s["id"] == element_id}] if parent_elt["elements"].blank? parent_elt["elements"] = [{"id" => "cl#{Time.current.to_i}"}] else parent_elt["elements"] << {"id" => "cl#{Time.current.to_i}"} end save end |
#clone_element(*ids, element_id) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'app/models/concerns/sibu/sections_concern.rb', line 50 def clone_element(*ids, element_id) src_elt = find_or_init(*ids, element_id) siblings = find_or_init(*ids)["elements"] ref_index = siblings.index {|s| s["id"] == element_id} new_elt = siblings[ref_index].deep_dup new_elt["id"] = "cl#{Time.current.to_i}" siblings.insert(ref_index + 1, new_elt) save ? new_elt : nil end |
#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 |
#delete_element(*ids, element_id) ⇒ Object
60 61 62 63 64 65 66 |
# File 'app/models/concerns/sibu/sections_concern.rb', line 60 def delete_element(*ids, element_id) src_elt = find_or_init(*ids, element_id) siblings = find_or_init(*ids)["elements"] ref_index = siblings.index {|s| s["id"] == element_id} siblings.delete_at(ref_index) save end |
#delete_section(*ids) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'app/models/concerns/sibu/sections_concern.rb', line 97 def delete_section(*ids) if ids.length == 1 if sections.length == 1 nil else ref_index = sections.index {|s| s["id"] == ids.first} sections.delete_at(ref_index) save end else parent = find_or_init(*ids[0..-2]) ref_index = parent["elements"].index {|s| s["id"] == ids.last} parent["elements"].delete_at(ref_index) save end end |
#element(*ids) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'app/models/concerns/sibu/sections_concern.rb', line 25 def element(*ids) unless ids.blank? if ids.length == 1 section(ids.first) else elts = elements(*ids[0..-2]) || [] pos = elts.index {|e| e["id"] == ids.last} pos ? elts[pos] : {"id" => ids.last} end end end |
#elements(*ids) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'app/models/concerns/sibu/sections_concern.rb', line 12 def elements(*ids) unless ids.blank? s = section(ids.first) unless s["elements"].blank? ids[1..-1].each do |elt_id| pos = s["elements"].index {|e| e["id"] == elt_id.split('|').last} s = pos ? s["elements"][pos] : {"id" => elt_id, "elements" => []} end s["elements"] end end end |
#elt(siblings, elt_id, default_elt = nil) ⇒ Object
114 115 116 117 |
# File 'app/models/concerns/sibu/sections_concern.rb', line 114 def elt(siblings, elt_id, default_elt = nil) pos = siblings.index {|e| e["id"] == elt_id} pos ? siblings[pos] : default_elt end |
#find_or_init(*ids) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/models/concerns/sibu/sections_concern.rb', line 119 def find_or_init(*ids) node = nil siblings = sections ids.each do |elt_id| node = elt(siblings, elt_id) if node.nil? node = {"id" => elt_id, "elements" => []} siblings << node elsif node["elements"].nil? node["elements"] = [] end siblings = node["elements"] end node end |
#section(id) ⇒ Object
5 6 7 8 9 10 |
# File 'app/models/concerns/sibu/sections_concern.rb', line 5 def section(id) if id pos = sections.index {|s| s["id"] == id} pos ? sections[pos] : {"id" => id} end end |
#update_element(*ids, value) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/models/concerns/sibu/sections_concern.rb', line 37 def update_element(*ids, value) unless ids.blank? parent_section = find_or_init(*ids)["elements"] if parent_section.any? {|elt| elt["id"] == value["id"]} parent_section.map! {|elt| elt["id"] == value["id"] ? value : elt} else parent_section << value.to_h end value if save end end |