diff --git a/app/models/org.rb b/app/models/org.rb index 8739d7f..914df61 100644 --- a/app/models/org.rb +++ b/app/models/org.rb @@ -1,9 +1,11 @@ class Org < ActiveRecord::Base include GlobalHelpers + include FlagShihTzu extend Dragonfly::Model::Validations + ## # Associations - belongs_to :organisation_type + belongs_to :organisation_type # depricated, but cannot be removed until migration run belongs_to :language has_many :guidance_groups has_many :templates @@ -31,7 +33,16 @@ validates_property :format, of: :logo, in: ['jpeg', 'png', 'gif','jpg','bmp'] validates_size_of :logo, maximum: 500.kilobytes - + ## + # Define Bit Field values + # Column org_type + has_flags 1 => :institution, + 2 => :funder, + 3 => :organisation, + 4 => :research_institute, + 5 => :project, + 6 => :school, + column: 'org_type' diff --git a/db/migrate/20161212152020_replacing_organisation_types_with_bitflags.rb b/db/migrate/20161212152020_replacing_organisation_types_with_bitflags.rb new file mode 100644 index 0000000..16be879 --- /dev/null +++ b/db/migrate/20161212152020_replacing_organisation_types_with_bitflags.rb @@ -0,0 +1,40 @@ +class ReplacingOrganisationTypesWithBitflags < ActiveRecord::Migration + # requires flag_shih_tzu for bitfields + def change + # add org_type field to orgs + change_table :orgs do |t| + t.integer :org_type, null: false, default: 0 # flag_shih_tzu-managed bitfield + # Effective booleans which will be stored on the flags column: + # t.boolean :Organisation + # t.boolean :Funder + # t.boolean :Institution + # t.boolean :Reaserch_Institution + # t.boolean :School + # t.boolean :Project + end + # migrate old org_type data to bitfield + Org.includes(:organisation_type).all.each do |org| + unless org.organisation_type.nil? + case org.organisation_type.name + when "Organisation" + org.organisation = true + when "Funder" + org.funder = true + when "Project" + org.project = true + when "School" + org.school = true + when "Institution" + org.institution = true + when "Research Institute" + org.research_institute = true + end + org.save! + end + end + # remove organisation_type_id field from orgs + remove_column :orgs, :organisation_type_id + # remove organisation_type table + drop_table :organisation_types + end +end