Skip to content

Commit

Permalink
Merge pull request #14915 from kbrock/miq_shortcut
Browse files Browse the repository at this point in the history
Miq shortcut seeding
  • Loading branch information
isimluk authored May 3, 2017
2 parents 96aac65 + 1ec3644 commit 2a4935d
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions app/models/miq_shortcut.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@ class MiqShortcut < ApplicationRecord
has_many :miq_widgets, :through => :miq_widget_shortcuts

def self.seed
names = []
seed_data.each_with_index do |s, index|
names << s[:name]
db_data = all.index_by(&:name)
seed_records = seed_data

seed_records_by_name = seed_records.group_by { |x| x[:name] }
if seed_records.size != seed_records_by_name.size
names = seed_records_by_name.select { |_n, v| v.size > 1 }.map(&:first)
_log.warn("Duplicate seeds for names: #{names.join(",")}")
end

seed_records.each_with_index do |s, index|
s[:sequence] = index
rec = find_by(:name => s[:name])
rec = db_data[s[:name]]
if rec.nil?
_log.info("Creating #{s.inspect}")
rec = create!(s)
Expand All @@ -20,8 +27,8 @@ def self.seed
end
end

all.each do |rec|
next if names.include?(rec.name)
db_data.each do |name, rec|
next if seed_records_by_name[name]
_log.info("Deleting #{rec.inspect}")
rec.destroy
end
Expand Down

0 comments on commit 2a4935d

Please sign in to comment.