diff --git a/app/models/guidance.rb b/app/models/guidance.rb index 02ffed6..f0ae5da 100644 --- a/app/models/guidance.rb +++ b/app/models/guidance.rb @@ -54,7 +54,7 @@ validates :published, inclusion: { message: INCLUSION_MESSAGE, in: BOOLEAN_VALUES} - validates :themes, presence: { message: PRESENCE_MESSAGE } + validates :themes, presence: { message: PRESENCE_MESSAGE }, if: :published? # Retrieves every guidance associated to an org scope :by_org, -> (org) { diff --git a/app/models/section_sorter.rb b/app/models/section_sorter.rb index dddfb1f..095e480 100644 --- a/app/models/section_sorter.rb +++ b/app/models/section_sorter.rb @@ -72,6 +72,11 @@ end end + # needed for the find_invalid_records rake task + def self.model_name + "SectionSorter" + end + private def modifiable_values diff --git a/app/models/splash_log.rb b/app/models/splash_log.rb deleted file mode 100644 index ea57796..0000000 --- a/app/models/splash_log.rb +++ /dev/null @@ -1,12 +0,0 @@ -# == Schema Information -# -# Table name: splash_logs -# -# id :integer not null, primary key -# destination :string -# created_at :datetime not null -# updated_at :datetime not null -# - -class SplashLog < ActiveRecord::Base -end diff --git a/app/models/stat.rb b/app/models/stat.rb index 18a59e2..acc61db 100644 --- a/app/models/stat.rb +++ b/app/models/stat.rb @@ -1,3 +1,16 @@ +# == Schema Information +# +# Table name: stats +# +# id :integer not null, primary key +# count :integer default(0) +# date :date not null +# type :string not null +# created_at :datetime not null +# updated_at :datetime not null +# org_id :integer +# + class Stat < ActiveRecord::Base belongs_to :org diff --git a/app/models/stat_created_plan.rb b/app/models/stat_created_plan.rb index 4158c7c..43a9f2a 100644 --- a/app/models/stat_created_plan.rb +++ b/app/models/stat_created_plan.rb @@ -1,3 +1,16 @@ +# == Schema Information +# +# Table name: stats +# +# id :integer not null, primary key +# count :integer default(0) +# date :date not null +# type :string not null +# created_at :datetime not null +# updated_at :datetime not null +# org_id :integer +# + class StatCreatedPlan < Stat extend OrgDateRangeable diff --git a/app/models/stat_joined_user.rb b/app/models/stat_joined_user.rb index d9e0601..ec9820b 100644 --- a/app/models/stat_joined_user.rb +++ b/app/models/stat_joined_user.rb @@ -1,3 +1,16 @@ +# == Schema Information +# +# Table name: stats +# +# id :integer not null, primary key +# count :integer default(0) +# date :date not null +# type :string not null +# created_at :datetime not null +# updated_at :datetime not null +# org_id :integer +# + class StatJoinedUser < Stat extend OrgDateRangeable diff --git a/lib/data_cleanup/rules/answer/fix_blank_plan.rb b/lib/data_cleanup/rules/answer/fix_blank_plan.rb index 2aba6b1..705c733 100644 --- a/lib/data_cleanup/rules/answer/fix_blank_plan.rb +++ b/lib/data_cleanup/rules/answer/fix_blank_plan.rb @@ -10,7 +10,7 @@ end def call - ::Answer.where.not(plan_id: ::Plan.all.collect(&:id)).each do |answer| + ::Answer.includes(plan).all.each do |answer| unless answer.plan.present? log("Destroying orphaned Answer##{answer.id}") answer.destroy @@ -20,6 +20,5 @@ end end - end -end \ No newline at end of file +end diff --git a/lib/data_cleanup/rules/guidance/fix_blank_theme.rb b/lib/data_cleanup/rules/guidance/fix_blank_theme.rb new file mode 100644 index 0000000..d9e1116 --- /dev/null +++ b/lib/data_cleanup/rules/guidance/fix_blank_theme.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true +module DataCleanup + module Rules + # Fix blank user on Answer + module Guidance + class FixBlankTheme < Rules::Base + + def description + "Fix blank theme on Published Guidance" + end + + def call + ::Guidance.includes(:themes).each do |g| + if g.themes.blank? && g.published? + g.update!(published: false) + end + end + end + + end + end + end +end diff --git a/lib/data_cleanup/rules/guidance_group/remove_blank_org.rb b/lib/data_cleanup/rules/guidance_group/remove_blank_org.rb new file mode 100644 index 0000000..e46f41a --- /dev/null +++ b/lib/data_cleanup/rules/guidance_group/remove_blank_org.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true +module DataCleanup + module Rules + # Fix blank user on Answer + module GuidanceGroup + class RemoveBlankOrg < Rules::Base + + def description + "Remove GuidanceGroups without an org (and any orphaned guidances)" + end + + def call + ::GuidanceGroup.where(org: nil).each do |group| + group.guidances.destroy_all + group.destroy + end + end + + end + end + end +end diff --git a/lib/data_cleanup/rules/note/remove_orphan.rb b/lib/data_cleanup/rules/note/remove_orphan.rb new file mode 100644 index 0000000..426e9ff --- /dev/null +++ b/lib/data_cleanup/rules/note/remove_orphan.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true +module DataCleanup + module Rules + # Fix blank user on Answer + module Note + class RemoveOrphan < Rules::Base + + def description + "Remove notes missing an answer" + end + + def call + ::Note.where(answer:nil).destroy_all + end + + end + end + end +end diff --git a/lib/data_cleanup/rules/org/fix_blank_abbreviation.rb b/lib/data_cleanup/rules/org/fix_blank_abbreviation.rb index f45aaab..297e5f4 100644 --- a/lib/data_cleanup/rules/org/fix_blank_abbreviation.rb +++ b/lib/data_cleanup/rules/org/fix_blank_abbreviation.rb @@ -22,7 +22,7 @@ else # Raise an exception if there are Orgs with no abbreviation and # no YAML file was defined - if ::Org.where(abbreviation: nil).present? + if ::Org.where(abbreviation: ["", nil]).any? raise "Please create a YAML file at #{YAML_FILE_PATH}" end end diff --git a/lib/data_cleanup/rules/question_format/fix_blank_description.rb b/lib/data_cleanup/rules/question_format/fix_blank_description.rb index 8b99ef3..2cc7d79 100644 --- a/lib/data_cleanup/rules/question_format/fix_blank_description.rb +++ b/lib/data_cleanup/rules/question_format/fix_blank_description.rb @@ -10,7 +10,7 @@ end def call - ::QuestionFormat.where(description: [nil, ""]).each do |qf| + ::QuestionFormat.where(description: ["", nil]).each do |qf| log("Adding default description to QuestionFormat##{qf.id}") qf.update!(description: "#{qf.title} format") end diff --git a/lib/data_cleanup/rules/template/fix_blank_org.rb b/lib/data_cleanup/rules/template/fix_blank_org.rb index 7f952dc..712593f 100644 --- a/lib/data_cleanup/rules/template/fix_blank_org.rb +++ b/lib/data_cleanup/rules/template/fix_blank_org.rb @@ -11,7 +11,7 @@ def call ::Template.where("customization_of is not null and customization_of not in (?)", ::Template.all.pluck(:family_id)).each do |customization| - log("Setting customization_of to NULL for Template without matching family_id: #{ids}") + log("Setting customization_of to NULL for Template without matching family_id: #{customization.family_id}") customization.update_attributes(customization_of: nil) end end diff --git a/lib/data_cleanup/rules/template/fix_blank_title.rb b/lib/data_cleanup/rules/template/fix_blank_title.rb new file mode 100644 index 0000000..e37eea3 --- /dev/null +++ b/lib/data_cleanup/rules/template/fix_blank_title.rb @@ -0,0 +1,18 @@ +module DataCleanup + module Rules + module Template + class FixBlankTitle < Rules::Base + + def description + "Fix blank title on template" + end + + def call + ids = ::Template.where(title: [nil, ""]).ids + log("Setting title to DEFAULT TITLE for Templates #{ids}") + ::Template.where(id: ids).update_all(title: "DEFAULT TITLE") + end + end + end + end +end diff --git a/lib/data_cleanup/rules/user/fix_blank_name.rb b/lib/data_cleanup/rules/user/fix_blank_name.rb new file mode 100644 index 0000000..14217ee --- /dev/null +++ b/lib/data_cleanup/rules/user/fix_blank_name.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true +module DataCleanup + module Rules + # Fix blank user on Note + module User + class FixBlankName < Rules::Base + + def description + "Fix blank Name on User" + end + + def call + surname = Set.new(::User.where(surname: [nil,""])) + firstname = Set.new(::User.where(firstname: [nil,""])) + both = firstname & surname + surname = surname ^ both + firstname = firstname ^ both + either = firstname | surname + log("Setting users without orgs to other_org: #{either.map(&:id)}") + ::User.where(id: both.map(&:id)).update_all(firstname: "PLEASE UPDATE", surname: "YOUR DETAILS") + ::User.where(id: firstname.map(&:id)).update_all(firstname: "PLEASE UPDATE") + ::User.where(id: surname.map(&:id)).update_all(surname: "PLEASE UPDATE") + end + end + end + end +end diff --git a/lib/data_cleanup/rules/user/fix_blank_org.rb b/lib/data_cleanup/rules/user/fix_blank_org.rb new file mode 100644 index 0000000..f05a228 --- /dev/null +++ b/lib/data_cleanup/rules/user/fix_blank_org.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true +module DataCleanup + module Rules + # Fix blank user on Note + module User + class FixBlankOrg < Rules::Base + + def description + "Fix blank Org on User" + end + + def call + users = ::User.joins("LEFT OUTER JOIN orgs ON orgs.id = users.org_id") + .where(orgs: { id: nil }) + log("Setting users without orgs to other_org: #{users.map(&:id)}") + users.update_all(org_id: ::Org.where(is_other: true).first.id) + end + end + end + end +end diff --git a/lib/data_cleanup/rules/user_identifier/fix_blank_user.rb b/lib/data_cleanup/rules/user_identifier/fix_blank_user.rb index f012258..39051b7 100644 --- a/lib/data_cleanup/rules/user_identifier/fix_blank_user.rb +++ b/lib/data_cleanup/rules/user_identifier/fix_blank_user.rb @@ -10,7 +10,8 @@ end def call - ids = ::UserIdentifier.where(user: nil).ids + ids = ::UserIdentifier.joins("LEFT OUTER JOIN users ON users.id = user_identifiers.user_id") + .where( users: {id: nil}).ids log("Destroying UserIdentifier where ids: #{ids} (no user)") ::UserIdentifier.destroy(ids) end