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
72 73 74 75 76 77 78 79 80 81 |
# File 'app/models/concerns/sibu/sections_concern.rb', line 72 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" => "#{element_id}*"}] else parent_elt["elements"] << [{"id" => "#{parent_elt["elements"].last["id"]}§"}] 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"] = element_id + '§' siblings.insert(ref_index + 1, new_elt) save ? new_elt : nil end |
#create_section(*ids, after, new_section) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'app/models/concerns/sibu/sections_concern.rb', line 83 def create_section(*ids, after, new_section) new_section["id"] = "#{new_section["template"]}-#{Time.current.to_i}" if ids.length == 1 parent = sections else parent = find_or_init(*ids[0..-2])["elements"] end ref_pos = parent.index {|s| s["id"] == ids.last} parent.insert(after.to_s == 'true' ? ref_pos + 1 : ref_pos, new_section) save ? new_section : nil end |
#delete_element(*ids, element_id) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 |
# 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"] if siblings.length > 1 ref_index = siblings.index {|s| s["id"] == element_id} siblings.delete_at(ref_index) save else nil end end |
#delete_section(*ids) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'app/models/concerns/sibu/sections_concern.rb', line 95 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
112 113 114 115 |
# File 'app/models/concerns/sibu/sections_concern.rb', line 112 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
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'app/models/concerns/sibu/sections_concern.rb', line 117 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 |