diff --git a/app/models/role.rb b/app/models/role.rb index 606fb62..fd3883a 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -1,9 +1,19 @@ class Role < ActiveRecord::Base + include FlagShihTzu + ## # Associations belongs_to :user belongs_to :plan + ## + # Define Bit Field Values + # Column access + has_flags 1 => :creator, + 2 => :administrator, + 3 => :editor, + column: 'access' + # EVALUATE CLASS AND INSTANCE METHODS BELOW diff --git a/db/migrate/20161213101804_replacing_plan_roles__with_bitflags.rb b/db/migrate/20161213101804_replacing_plan_roles__with_bitflags.rb new file mode 100644 index 0000000..fa9d143 --- /dev/null +++ b/db/migrate/20161213101804_replacing_plan_roles__with_bitflags.rb @@ -0,0 +1,27 @@ +class ReplacingPlanRolesWithBitflags < ActiveRecord::Migration + def change + # create the field to hold the user's role in plans + add_column :roles, :access, :integer, null: false, default: 0 + # rename creator, editor, and aministrator column names so we can access data + rename_column :roles, :creator, :create + rename_column :roles, :editor, :edit + rename_column :roles, :administrator, :admin + # transfer the data from the other fields to the bitfield + Role.find_each do |role| + if role.admin + role.administrator = true + end + if role.edit + role.editor = true + end + if role.create + role.creator = true + end + role.save! + end + # remove the other columns + remove_column :roles, :create + remove_column :roles, :edit + remove_column :roles, :admin + end +end