Class: ForemanDatacenter::Rack
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ForemanDatacenter::Rack
- Includes:
- Authorizable, ScopedSearchExtensions
- Defined in:
- app/models/foreman_datacenter/rack.rb
Instance Method Summary collapse
- #child_devices ⇒ Object
- #device_at(position) ⇒ Object
- #devices_count ⇒ Object
-
#empty_cells ⇒ Object
Finding all empty cells for grid layout.
- #format_for_csv ⇒ Object
- #grid_template_areas ⇒ Object
-
#positioned_devices ⇒ Object
def positioned_devices height.downto(1).map { |position| [position, device_at(position)] } end.
- #unpositioned_devices ⇒ Object
Instance Method Details
#child_devices ⇒ Object
142 143 144 |
# File 'app/models/foreman_datacenter/rack.rb', line 142 def child_devices devices.joins(:device_type).where("device_types.subdevice_role = ?", "Child") end |
#device_at(position) ⇒ Object
27 28 29 |
# File 'app/models/foreman_datacenter/rack.rb', line 27 def device_at(position) devices.where(position: position).to_a end |
#devices_count ⇒ Object
51 52 53 54 55 |
# File 'app/models/foreman_datacenter/rack.rb', line 51 def devices_count @devices_count ||= self.class.where(id: id). joins(:devices). count end |
#empty_cells ⇒ Object
Finding all empty cells for grid layout
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'app/models/foreman_datacenter/rack.rb', line 81 def empty_cells cells = {} # all cells of grid devices_cells = {} # cells with devices height.times.each do |i| cells[i+1] = ["left", "right"] end devices.each do |d| if d.size > 0 if !d.position.nil? && d.position != 0 s = (d.size == 0 || d.size == 1) ? 0 : (d.size - 1) places = [*d.position..d.position+s] places.each do |p| devices_cells[p] = [] unless devices_cells.key?(p) if (d.side == "full" || d.side.nil?) ["left", "right"].each {|side| devices_cells[p] << side} else devices_cells[p] << d.side end end end end end devices_cells.each do |k,v| v.each {|c| cells[k].delete(c)} end cells.each {|k,v| cells.except!(k) if cells[k] == []} return cells end |
#format_for_csv ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/models/foreman_datacenter/rack.rb', line 57 def format_for_csv positioned_devices = self.positioned_devices unpositioned_devices = self.unpositioned_devices csv_string = CSV.generate do |csv| csv << ["positions", "left", "full", "right", "no side"] positioned_devices.each do |i| pos = i[0].map{|p| "#{p}"+"U"}.join(",") if i[1].size > 0 sort = sort_for_csv(i[1]) csv << [pos, sort[0], sort[1], sort[2], sort[3]] else csv << [pos] end end unless unpositioned_devices.empty? csv << [] csv << ["Unpositioned", unpositioned_devices.map(&:name).join(",")] end end File.open("#{self.name}.csv", "w") {|f| f << csv_string} end |
#grid_template_areas ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'app/models/foreman_datacenter/rack.rb', line 110 def grid_template_areas result = {} (height+1).times.each do |i| if i == 0 result[0] = unpositioned_devices.map{|d| "device-#{d.id}"} else dev = {} devices.each do |d| if d.size > 0 if !d.position.nil? && d.position != 0 css_class_name = "device-#{d.id}" s = (d.size == 1 || d.size == 0) ? 0 : (d.size - 1) if [*d.position..(d.position + s)].include?(i) if d.side.nil? dev["full"] = css_class_name else dev["#{d.side}"] = css_class_name end result[i] = dev end end end end end end (height+1).times.each do |i| result[i] = [] if result[i].nil? end # result[0].reject! if result[0].empty? return result.sort_by{|k,v| k}.to_h.to_json end |
#positioned_devices ⇒ Object
def positioned_devices
height.downto(1).map { |position| [position, device_at(position)] }
end
35 36 37 38 39 40 41 42 43 44 45 |
# File 'app/models/foreman_datacenter/rack.rb', line 35 def positioned_devices devs = devices.map{ |d| [d.positions, [d]] } result = [] i = 1 loop do current_device = devs.select{ |d| d[0].include?(i) } current_device == [] ? (result << [[i],[]]; i +=1 ) : (result << merge_devices(current_device, i); i = i + current_device[0][1].last.size) break if i > height end device_sorting(result) end |
#unpositioned_devices ⇒ Object
47 48 49 |
# File 'app/models/foreman_datacenter/rack.rb', line 47 def unpositioned_devices devices.where(position: nil).or(devices.where(position: "0")).to_a end |