diff --git a/app/admin/org.rb b/app/admin/org.rb new file mode 100644 index 0000000..0f92a58 --- /dev/null +++ b/app/admin/org.rb @@ -0,0 +1,110 @@ +# [+Project:+] DMPRoadmap +# [+Description:+] +# +# [+Created:+] 03/09/2014 +# [+Copyright:+] Digital Curation Centre and University of California Curation Center + +ActiveAdmin.register Org do + permit_params :abbreviation, :banner_file_id, :description, :logo_file_id, :name, :target_url, :organisation_type_id, :wayfless_entity, :parent_id + + menu :priority => 14, :label => proc{I18n.t('admin.org')}, :parent => "Organisations management" + + index do + column I18n.t('admin.org_title'), :sortable => :name do |ggn| + link_to ggn.name, [:admin, ggn] + end + column I18n.t('admin.abbrev'), :sortable => :abbreviation do |ggn| + if !ggn.abbreviation.nil? + link_to ggn.abbreviation, [:admin, ggn] + else + '-' + end + end + column I18n.t('admin.org_type'), :sortable => :organisation_type_id do |org_type| + if !org_type.organisation_type_id.nil? then + link_to org_type.organisation_type.name, [:admin, org_type] + end + end + + actions + end + + + #show details of an organisation + show do + resource.check_api_credentials + attributes_table do + row I18n.t('admin.org_title'), :sortable => :name do |gn| + if !gn.name.nil? then + link_to gn.name, [:admin, gn] + end + end + row I18n.t('admin.abbrev'), :abbreviation do |ggn| + if !ggn.abbreviation.nil? + link_to ggn.abbreviation, [:admin, ggn] + else + '-' + end + end + row :sort_name + row I18n.t('admin.org_type'), :organisation_type_id do |org_type| + if !org_type.organisation_type_id.nil? then + link_to org_type.organisation_type.name, [:admin, org_type] + end + end + row :banner_text do |banner| + if !banner.banner_text.nil? then + banner.banner_text.html_safe + end + end + # row :target_url + row :logo_file_name + row :wayfless_entity + row I18n.t('admin.token_permission_type') do + (organisation.token_permission_types.map{|tpt| link_to tpt.token_type, [:admin, tpt]}).join(', ').html_safe + end + row :created_at + row :updated_at + end + end + + #templates sidebar + sidebar I18n.t('admin.templates'), :only => :show, :if => proc { organisation.dmptemplates.count >= 1} do + table_for organisation.dmptemplates.order("title asc") do |temp| + column :title do |dmptemp| + link_to dmptemp.title, [:admin, dmptemp] + end + column :published + end + end + + #form + form do |f| + f.inputs "Details" do + f.input :name + f.input :abbreviation + f.input :sort_name + f.input :organisation_type_id, :label => I18n.t('admin.org_type'), :as => :select, :collection => OrganisationType.order('name').map{|orgt|[orgt.name, orgt.id]} + # f.input :target_url + f.input :banner_text + f.input :logo_file_name + f.input :wayfless_entity + f.input :token_permission_types, label: I18n.t('admin.token_permission_type'), + as: :select, multiple: true, include_blank: I18n.t('helpers.none'), + collection: TokenPermissionType.order(:token_type).map{|token| [token.token_type, token.id]}, + hint: I18n.t('admin.choose_api_permissions') + # f.input :parent_id, :label => I18n.t('admin.org_parent'), :as => :select, :collection => Organisation.find(:all, :order => 'name ASC').map{|orgp|[orgp.name, orgp.id]} + # f.input :stylesheet_file_id + end + f.actions + end + + + controller do + def permitted_params + params.permit! + end + end + + +end diff --git a/app/admin/organisation.rb b/app/admin/organisation.rb deleted file mode 100644 index 2357eb3..0000000 --- a/app/admin/organisation.rb +++ /dev/null @@ -1,110 +0,0 @@ -# [+Project:+] DMPRoadmap -# [+Description:+] -# -# [+Created:+] 03/09/2014 -# [+Copyright:+] Digital Curation Centre and University of California Curation Center - -ActiveAdmin.register Organisation do - permit_params :abbreviation, :banner_file_id, :description, :logo_file_id, :name, :target_url, :organisation_type_id, :wayfless_entity, :parent_id - - menu :priority => 14, :label => proc{I18n.t('admin.org')}, :parent => "Organisations management" - - index do - column I18n.t('admin.org_title'), :sortable => :name do |ggn| - link_to ggn.name, [:admin, ggn] - end - column I18n.t('admin.abbrev'), :sortable => :abbreviation do |ggn| - if !ggn.abbreviation.nil? - link_to ggn.abbreviation, [:admin, ggn] - else - '-' - end - end - column I18n.t('admin.org_type'), :sortable => :organisation_type_id do |org_type| - if !org_type.organisation_type_id.nil? then - link_to org_type.organisation_type.name, [:admin, org_type] - end - end - - actions - end - - - #show details of an organisation - show do - resource.check_api_credentials - attributes_table do - row I18n.t('admin.org_title'), :sortable => :name do |gn| - if !gn.name.nil? then - link_to gn.name, [:admin, gn] - end - end - row I18n.t('admin.abbrev'), :abbreviation do |ggn| - if !ggn.abbreviation.nil? - link_to ggn.abbreviation, [:admin, ggn] - else - '-' - end - end - row :sort_name - row I18n.t('admin.org_type'), :organisation_type_id do |org_type| - if !org_type.organisation_type_id.nil? then - link_to org_type.organisation_type.name, [:admin, org_type] - end - end - row :banner_text do |banner| - if !banner.banner_text.nil? then - banner.banner_text.html_safe - end - end - # row :target_url - row :logo_file_name - row :wayfless_entity - row I18n.t('admin.token_permission_type') do - (organisation.token_permission_types.map{|tpt| link_to tpt.token_type, [:admin, tpt]}).join(', ').html_safe - end - row :created_at - row :updated_at - end - end - - #templates sidebar - sidebar I18n.t('admin.templates'), :only => :show, :if => proc { organisation.dmptemplates.count >= 1} do - table_for organisation.dmptemplates.order("title asc") do |temp| - column :title do |dmptemp| - link_to dmptemp.title, [:admin, dmptemp] - end - column :published - end - end - - #form - form do |f| - f.inputs "Details" do - f.input :name - f.input :abbreviation - f.input :sort_name - f.input :organisation_type_id, :label => I18n.t('admin.org_type'), :as => :select, :collection => OrganisationType.order('name').map{|orgt|[orgt.name, orgt.id]} - # f.input :target_url - f.input :banner_text - f.input :logo_file_name - f.input :wayfless_entity - f.input :token_permission_types, label: I18n.t('admin.token_permission_type'), - as: :select, multiple: true, include_blank: I18n.t('helpers.none'), - collection: TokenPermissionType.order(:token_type).map{|token| [token.token_type, token.id]}, - hint: I18n.t('admin.choose_api_permissions') - # f.input :parent_id, :label => I18n.t('admin.org_parent'), :as => :select, :collection => Organisation.find(:all, :order => 'name ASC').map{|orgp|[orgp.name, orgp.id]} - # f.input :stylesheet_file_id - end - f.actions - end - - - controller do - def permitted_params - params.permit! - end - end - - -end diff --git a/app/models/guidance_group.rb b/app/models/guidance_group.rb index d9102a4..c5cdbe2 100644 --- a/app/models/guidance_group.rb +++ b/app/models/guidance_group.rb @@ -2,7 +2,7 @@ include GlobalHelpers #associations between tables - belongs_to :organisation + belongs_to :org has_and_belongs_to_many :guidances, join_table: "guidance_in_group" diff --git a/app/models/new_answer.rb b/app/models/new_answer.rb deleted file mode 100644 index e8dd797..0000000 --- a/app/models/new_answer.rb +++ /dev/null @@ -1,7 +0,0 @@ -class NewAnswer < ActiveRecord::Base - belongs_to :new_plan - belongs_to :new_question - has_and_belongs_to_many :question_options, join_table: "new_answers_question_options" - has_many :notes - belongs_to :user -end diff --git a/app/models/new_phase.rb b/app/models/new_phase.rb deleted file mode 100644 index 1919787..0000000 --- a/app/models/new_phase.rb +++ /dev/null @@ -1,4 +0,0 @@ -class NewPhase < ActiveRecord::Base - belongs_to :template - has_many :new_sections -end diff --git a/app/models/new_plan.rb b/app/models/new_plan.rb deleted file mode 100644 index 70b8ca2..0000000 --- a/app/models/new_plan.rb +++ /dev/null @@ -1,4 +0,0 @@ -class NewPlan < ActiveRecord::Base - has_one :template - has_many :roles -end diff --git a/app/models/new_question.rb b/app/models/new_question.rb deleted file mode 100644 index 3935fba..0000000 --- a/app/models/new_question.rb +++ /dev/null @@ -1,8 +0,0 @@ -class NewQuestion < ActiveRecord::Base - belongs_to :new_section - has_one :new_answer - has_one :note - has_many :question_options - belongs_to :question_format - has_and_belongs_to_many :themes, join_table: :new_questions_themes -end diff --git a/app/models/new_section.rb b/app/models/new_section.rb deleted file mode 100644 index 4c5c155..0000000 --- a/app/models/new_section.rb +++ /dev/null @@ -1,4 +0,0 @@ -class NewSection < ActiveRecord::Base - belongs_to :new_phase - has_many :new_questions -end diff --git a/app/models/new_suggested_answer.rb b/app/models/new_suggested_answer.rb deleted file mode 100644 index 6578f1d..0000000 --- a/app/models/new_suggested_answer.rb +++ /dev/null @@ -1,4 +0,0 @@ -class NewSuggestedAnswer < ActiveRecord::Base - belongs_to :new_question - belongs_to :organisation -end diff --git a/app/models/note.rb b/app/models/note.rb index 681fee3..66c835f 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -1,4 +1,4 @@ class Note < ActiveRecord::Base - belongs_to :new_answer + belongs_to :answer belongs_to :user end diff --git a/app/models/org.rb b/app/models/org.rb new file mode 100644 index 0000000..8d81f61 --- /dev/null +++ b/app/models/org.rb @@ -0,0 +1,178 @@ +class Org < ActiveRecord::Base + include GlobalHelpers + + extend Dragonfly::Model::Validations + + #associations between tables + belongs_to :organisation_type + has_many :guidance_groups + has_many :templates + has_many :users + has_many :suggested_answers + has_and_belongs_to_many :token_permission_types, join_table: "org_token_permissions" + + belongs_to :parent, :class_name => 'Organisation' + + belongs_to :language + + has_many :children, :class_name => 'Organisation', :foreign_key => 'parent_id' + + accepts_nested_attributes_for :templates + accepts_nested_attributes_for :token_permission_types + + attr_accessible :abbreviation, :banner_text, :logo, :remove_logo, + :logo_file_name, :name, :target_url, + :organisation_type_id, :wayfless_entity, :parent_id, :sort_name, + :token_permission_type_ids, :language_id, :contact_email + + validates :contact_email, email: true, allow_nil: true + validates :name, presence: true, uniqueness: true + + # allow validations for logo upload + dragonfly_accessor :logo do + after_assign :resize_image + end + + validates_property :height, of: :logo, in: (0..100) + validates_property :format, of: :logo, in: ['jpeg', 'png', 'gif','jpg','bmp'] + validates_size_of :logo, maximum: 500.kilobytes + + ## + # returns the name of the organisation + # + # @return [String] + def to_s + name + end + + ## + # returns the abbreviation for the organisation if it exists, or the name if not + # + # @return [String] name or abbreviation of the organisation + def short_name + if abbreviation.nil? then + return name + else + return abbreviation + end + end + + ## + # finds all organisations who have a parent of the passed organisation type + # + # @param [String] the name of an organisation type + # @return [Array] + def self.orgs_with_parent_of_type(org_type) + parents = OrganisationType.find_by_name(org_type).organisations + children = Array.new + parents.each do |parent| + children += parent.children + end + return children + end + + ## + # returns a list of all guidance groups belonging to other organisations + # + # @return [Array] + def self.other_organisations + org_types = [GlobalHelpers.constant("organisation_types.funder")] + organisations_list = [] + org_types.each do |ot| + new_org_obejct = OrganisationType.find_by_name(ot) + + org_with_guidance = GuidanceGroup.joins(new_org_obejct.organisations) + + organisations_list = organisations_list + org_with_guidance + end + return organisations_list + end + + + ## + # returns a list of all guidance groups belonging to other organisations + # + # @return [Array] + def self.other_organisations + org_types = [GlobalHelpers.constant("organisation_types.funder")] + organisations_list = [] + org_types.each do |ot| + new_org_obejct = OrganisationType.find_by_name(ot) + + org_with_guidance = GuidanceGroup.joins(new_org_obejct.organisations) + + organisations_list = organisations_list + org_with_guidance + end + return organisations_list + end + + ## + # returns a list of all sections of a given version from this organisation and it's parents + # + # @param version_id [Integer] version number of the section + # @return [Array
] list of sections + def all_sections(version_id) + if parent.nil? + secs = sections.where("version_id = ?", version_id) + if secs.nil? then + secs = Array.new + end + return secs + else + return sections.where("version_id = ? ", version_id).all + parent.all_sections(version_id) + end + end + + ## + # returns the guidance groups of this organisation and all of it's children + # + # @return [Array] list of guidance groups + def all_guidance_groups + ggs = guidance_groups + children.each do |c| + ggs = ggs + c.all_guidance_groups + end + return ggs + end + + ## + # returns the highest parent organisation in the tree + # + # @return [organisation] the root organisation + def root + if parent.nil? + return self + else + return parent.root + end + end + + ## + # returns all published templates belonging to the organisation + # + # @return [Array] published dmptemplates + def published_templates + return dmptemplates.where("published = ?", true) + end + + def check_api_credentials + if token_permission_types.count == 0 + users.each do |user| + user.api_token = "" + user.save! + end + end + end + + private + ## + # checks size of logo and resizes if necessary + # + def resize_image + unless logo.nil? + if logo.height != 100 + self.logo = logo.thumb('x100') # resize height and maintain aspect ratio + end + end + end +end diff --git a/app/models/organisation.rb b/app/models/organisation.rb deleted file mode 100644 index 14c5672..0000000 --- a/app/models/organisation.rb +++ /dev/null @@ -1,179 +0,0 @@ -class Organisation < ActiveRecord::Base - include GlobalHelpers - - extend Dragonfly::Model::Validations - - #associations between tables - belongs_to :organisation_type - has_many :guidance_groups - has_many :dmptemplates - has_many :sections - has_many :users - has_many :suggested_answers - has_and_belongs_to_many :token_permission_types, join_table: "org_token_permissions" - - belongs_to :parent, :class_name => 'Organisation' - - has_one :language - - has_many :children, :class_name => 'Organisation', :foreign_key => 'parent_id' - - accepts_nested_attributes_for :dmptemplates - accepts_nested_attributes_for :token_permission_types - - attr_accessible :abbreviation, :banner_text, :logo, :remove_logo, - :logo_file_name, :name, :target_url, - :organisation_type_id, :wayfless_entity, :parent_id, :sort_name, - :token_permission_type_ids, :language_id, :contact_email - - validates :contact_email, email: true, allow_nil: true - validates :name, presence: true, uniqueness: true - - # allow validations for logo upload - dragonfly_accessor :logo do - after_assign :resize_image - end - - validates_property :height, of: :logo, in: (0..100) - validates_property :format, of: :logo, in: ['jpeg', 'png', 'gif','jpg','bmp'] - validates_size_of :logo, maximum: 500.kilobytes - - ## - # returns the name of the organisation - # - # @return [String] - def to_s - name - end - - ## - # returns the abbreviation for the organisation if it exists, or the name if not - # - # @return [String] name or abbreviation of the organisation - def short_name - if abbreviation.nil? then - return name - else - return abbreviation - end - end - - ## - # finds all organisations who have a parent of the passed organisation type - # - # @param [String] the name of an organisation type - # @return [Array] - def self.orgs_with_parent_of_type(org_type) - parents = OrganisationType.find_by_name(org_type).organisations - children = Array.new - parents.each do |parent| - children += parent.children - end - return children - end - - ## - # returns a list of all guidance groups belonging to other organisations - # - # @return [Array] - def self.other_organisations - org_types = [GlobalHelpers.constant("organisation_types.funder")] - organisations_list = [] - org_types.each do |ot| - new_org_obejct = OrganisationType.find_by_name(ot) - - org_with_guidance = GuidanceGroup.joins(new_org_obejct.organisations) - - organisations_list = organisations_list + org_with_guidance - end - return organisations_list - end - - - ## - # returns a list of all guidance groups belonging to other organisations - # - # @return [Array] - def self.other_organisations - org_types = [GlobalHelpers.constant("organisation_types.funder")] - organisations_list = [] - org_types.each do |ot| - new_org_obejct = OrganisationType.find_by_name(ot) - - org_with_guidance = GuidanceGroup.joins(new_org_obejct.organisations) - - organisations_list = organisations_list + org_with_guidance - end - return organisations_list - end - - ## - # returns a list of all sections of a given version from this organisation and it's parents - # - # @param version_id [Integer] version number of the section - # @return [Array
] list of sections - def all_sections(version_id) - if parent.nil? - secs = sections.where("version_id = ?", version_id) - if secs.nil? then - secs = Array.new - end - return secs - else - return sections.where("version_id = ? ", version_id).all + parent.all_sections(version_id) - end - end - - ## - # returns the guidance groups of this organisation and all of it's children - # - # @return [Array] list of guidance groups - def all_guidance_groups - ggs = guidance_groups - children.each do |c| - ggs = ggs + c.all_guidance_groups - end - return ggs - end - - ## - # returns the highest parent organisation in the tree - # - # @return [organisation] the root organisation - def root - if parent.nil? - return self - else - return parent.root - end - end - - ## - # returns all published templates belonging to the organisation - # - # @return [Array] published dmptemplates - def published_templates - return dmptemplates.where("published = ?", true) - end - - def check_api_credentials - if token_permission_types.count == 0 - users.each do |user| - user.api_token = "" - user.save! - end - end - end - - private - ## - # checks size of logo and resizes if necessary - # - def resize_image - unless logo.nil? - if logo.height != 100 - self.logo = logo.thumb('x100') # resize height and maintain aspect ratio - end - end - end -end diff --git a/app/models/phase.rb b/app/models/phase.rb index 155a86f..c56d091 100644 --- a/app/models/phase.rb +++ b/app/models/phase.rb @@ -8,14 +8,11 @@ extend FriendlyId #associations between tables - belongs_to :dmptemplate - - has_many :versions, :dependent => :destroy - has_many :sections, :through => :versions, :dependent => :destroy + belongs_to :template + has_many :sections, :dependent => :destroy has_many :questions, :through => :sections, :dependent => :destroy #Link the child's data - accepts_nested_attributes_for :versions, :allow_destroy => true # accepts_nested_attributes_for :dmptemplate attr_accessible :description, :number, :title, :dmptemplate_id, :as => [:default, :admin] diff --git a/app/models/role.rb b/app/models/role.rb index ea6573c..02caf1b 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -1,4 +1,4 @@ class Role < ActiveRecord::Base belongs_to :user - belongs_to :new_plan + belongs_to :plan end diff --git a/app/models/suggested_answer.rb b/app/models/suggested_answer.rb index 3bbefa8..56376db 100644 --- a/app/models/suggested_answer.rb +++ b/app/models/suggested_answer.rb @@ -1,6 +1,6 @@ class SuggestedAnswer < ActiveRecord::Base - belongs_to :organisation + belongs_to :org belongs_to :question # accepts_nested_attributes_for :question diff --git a/app/models/template.rb b/app/models/template.rb index cb586f6..b3deec8 100644 --- a/app/models/template.rb +++ b/app/models/template.rb @@ -1,5 +1,5 @@ class Template < ActiveRecord::Base - belongs_to :organisation + belongs_to :org has_many :new_phases has_many :new_plans end diff --git a/app/models/user.rb b/app/models/user.rb index c0595e9..6a14c60 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -13,7 +13,7 @@ has_many :user_role_types, through: :user_org_roles belongs_to :language - belongs_to :organisation + belongs_to :org has_many :projects, through: :project_groups do def filter(query) diff --git a/app/models/users_perm.rb b/app/models/users_perm.rb new file mode 100644 index 0000000..38a6cd3 --- /dev/null +++ b/app/models/users_perm.rb @@ -0,0 +1,4 @@ +class UsersPerm < ActiveRecord::Base + belongs_to :user + belongs_to :perm +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 78c801a..925af56 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -42,7 +42,7 @@ #post 'contact_form' => 'contacts', as: 'localized_contact_creation' #get 'contact_form' => 'contacts#new', as: 'localized_contact_form' - resources :organisations, :path => 'org/admin', only: [] do + resources :orgs, :path => 'org/admin', only: [] do member do get 'children' get 'templates' diff --git a/db/migrate/20161205095623_removing_intermediary_tables.rb b/db/migrate/20161205095623_removing_intermediary_tables.rb index 18a698e..79f2f4f 100644 --- a/db/migrate/20161205095623_removing_intermediary_tables.rb +++ b/db/migrate/20161205095623_removing_intermediary_tables.rb @@ -1,5 +1,5 @@ class RemovingIntermediaryTables < ActiveRecord::Migration - def change + def up # removing intermediary structures drop_table :dmptemplates @@ -43,7 +43,7 @@ end drop_table :options - change_table :answers_question_options do |t| + change_table :new_answers_question_options do |t| t.rename :new_answer_id, :answer_id end drop_table :answers_options @@ -66,5 +66,87 @@ change_table :new_suggested_answers do |t| t.rename :new_question_id, :question_id end + drop_table :suggested_answers + rename_table :new_suggested_answers, :suggested_answers + + #drop_table :projects + + #rename_table :organisations, :orgs + rename_column :guidance_groups, :organisation_id, :org_id + rename_column :suggested_answers, :organisation_id, :org_id + rename_column :org_token_permissions, :organisation_id, :org_id + rename_column :projects, :organisation_id, :org_id + rename_column :templates, :organisation_id, :org_id + rename_column :users, :organisation_id, :org_id + + end + + def down + create_table :dmptemplates + change_table :phases do |t| + t.integer :vid + end + rename_table :phases, :new_phases + create_table :phases + + + change_table :sections do |t| + t.rename :phase_id, :new_phase_id + end + rename_table :sections, :new_sections + create_table :sections + + + change_table :questions do |t| + t.rename :section_id, :new_section_id + t.integer :question_id + end + rename_table :questions, :new_questions + create_table :questions + + change_table :questions_themes do |t| + t.rename :question_id, :new_question_id + end + rename_table :questions_themes, :new_questions_themes + create_table :questions_themes + + change_table :answers do |t| + t.rename :plan_id, :new_plan_id + t.rename :question_id, :new_question_id + end + rename_table :answers, :new_answers + create_table :answers + + change_table :question_options do |t| + t.rename :question_id, :new_question_id + t.integer :option_id + end + create_table :options + + change_table :answers_question_options do |t| + t.rename :answer_id, :new_answer_id + end + rename_table :answers_question_options, :new_answers_question_options + create_table :answers_options + + change_table :notes do |t| + t.rename :answer_id, :new_answer_id + end + create_table :comments + + change_table :plans do |t| + end + rename_table :plans, :new_plans + create_table :plans + + change_table :roles do |t| + t.rename :plan_id, :new_plan_id + end + + rename_table :suggested_answers, :new_suggested_answers + create_table :suggested_answers + change_table :new_suggested_answers do |t| + t.rename :question_id, :new_question_id + end end end diff --git a/db/migrate/20161206122926_add_foreign_keys.rb b/db/migrate/20161206122926_add_foreign_keys.rb new file mode 100644 index 0000000..d071ad5 --- /dev/null +++ b/db/migrate/20161206122926_add_foreign_keys.rb @@ -0,0 +1,227 @@ +class AddForeignKeys < ActiveRecord::Migration + # this migraiton modifies data in tables, and thus depends on being at a specific + # point in the history of the codebase. Specificly the models must reflect the + # relationships expected within this code + def change + # remove broken references + scrub_references + # answers + add_foreign_key :answers, :users + add_foreign_key :answers, :plans + add_foreign_key :answers, :questions + + # answers_question_options + add_foreign_key :answers_question_options, :answers + add_foreign_key :answers_question_options, :question_options + + # notes + add_foreign_key :notes, :answers + add_foreign_key :notes, :users + + # templates + add_foreign_key :templates, :orgs + + # guidance_groups + add_foreign_key :guidance_groups, :orgs + + # guidance_in_group + add_foreign_key :guidance_in_group, :guidance_groups + add_foreign_key :guidance_in_group, :guidances + + # guidances + add_foreign_key :guidances, :guidance_groups + + # question_options + add_foreign_key :question_options, :questions + + # org_token_permissions + add_foreign_key :org_token_permissions, :orgs + add_foreign_key :org_token_permissions, :token_permission_types + + # orgs + add_foreign_key :orgs, :regions + add_foreign_key :orgs, :languages + + # phases + add_foreign_key :phases, :templates + + # plans + add_foreign_key :plans, :templates + + # roles + add_foreign_key :roles, :plans + add_foreign_key :roles, :users + + # questions + add_foreign_key :questions, :sections + add_foreign_key :questions, :question_formats + + # questions_themes + add_foreign_key :questions_themes, :questions + add_foreign_key :questions_themes, :themes + + # sections + add_foreign_key :sections, :phases + + # suggested_answers + add_foreign_key :suggested_answers, :orgs + add_foreign_key :suggested_answers, :questions + + # themes_in_guidance + add_foreign_key :themes_in_guidance, :themes + add_foreign_key :themes_in_guidance, :guidances + + # users + add_foreign_key :users, :orgs + add_foreign_key :users, :languages + + # users_perms + add_foreign_key :users_perms, :users + add_foreign_key :users_perms, :perms + end + + def scrub_references + # answers + i = 0 + Answer.includes(:user, :plan, :question).find_each do |ans| + if ans.user.nil? && ans.user_id.present? + ans.user_id = nil + i += 1 + end + if ans.plan.nil? && ans.plan_id.present? + ans.plan_id = nil + i += 1 + end + if ans.question.nil? && ans.question_id.present? + ans.question_id = nil + i += 1 + end + ans.save! + end + puts "#{i} answers scrubbed" + + # notes + i = 0 + Note.includes(:answer, :user).find_each do |note| + if note.answer.nil? && note.answer_id.present? + note.answer_id = nil + i += 1 + end + if note.user.nil? && note.user_id.present? + note.user_id = nil + i += 1 + end + note.save! + end + puts "#{i} notes scrubbed" + + # templates + i = 0 + Template.includes(:org).find_each do |temp| + if temp.org.nil? && temp.org_id.present? + temp.org_id = nil + i += 1 + end + temp.save! + end + puts "#{i} templates scrubbed" + + # guidance_groups + # i = 0 + # GuidanceGroup.includes(:org).find_each do |gg| + # if gg.org.nil? && gg.org_id.present? + # gg.org_id = nil + # i += 1 + # end + # gg.save! + # end + # puts "#{i} guidance groups scrubbed" + + # # question_options + # i = 0 + # QuestionOption.includes(:question).find_each do |opt| + # if opt.question.nil? && opt.question_id.present? + # opt.question_id = nil + # i += 1 + # end + # opt.save! + # end + # puts "#{i} question_options scrubbed" + + # # orgs + # i = 0 + # Org.includes( :language).find_each do |org| + # if org.language.nil? && org.language_id.present? + # org.language_id = nil + # i += 1 + # end + # org.save! + # end + # puts "#{i} orgs scrubbed" + + # # phases + # # new structure so it's fine + + # # plans + # # new structure so it's fine + + # # roles + # i = 0 + # Role.includes(:user, :plan).find_each do |role| + # if role.user.nil? && role.user_id.present? + # role.delete! + # i += 1 + # next + # end + # if role.plan.nil? && role.plan_id.present? + # role.delete! + # i += 1 + # end + # end + # puts "#{i} roles scrubbed" + + # # questions + + # # sections + + # # suggested_answers + # i = 0 + # SuggestedAnswer.includes(:org, :question).find_each do |sa| + # if sa.org.nil? && sa.org_id.present? + # sa.org_id = nil + # i += 1 + # end + # if sa.question.nil? + # sa.delete! + # i += 1 + # next + # end + # sa.save! + # end + # puts "#{i} suggested answers scrubbed" + + # # themes_in_guidance + + # # users + # i = 0 + # User.includes(:org, :language).find_each do |u| + # if u.org.nil? && u.org_id.present? + # u.org_id = nil + # i += 1 + # end + # if u.language.nil? && u.language_id.present? + # u.language_id = nil + # i += 1 + # end + # u.save! + # end + # puts "#{i} users scrubbed" + + # users_perms + UsersPerm.includes(:user).all.each do |u| + if u.user.nil? + UsersPerm.delete_all(user_id: u.user_id) + end + end + end +end