So I actually do have an application that I developed this year that my current employer is using. It is, in a nutshell, a custom CRM for our company and industry type. The owner of the company wants the simplest and quickest tool available for the employees to be able to check if they can call a certain company or not, and doesn’t want any of the bells or whistles that usually accompany a typical CRM system.
So I need an admin class of user so that we can control who can update the database of clients. I’m also adding some of the more useful features to the application for my own use, but don’t want every user and the owner to see these features and get confused – so I’ll also need to create a beta attribute for certain users that will allow them to access these new features.
rails g migration AddAdminAndBetaColumnsToClients
Now we’ll add our columns in the migration rails just generated for us. We’ll need to add a beta column and an admin column. Since people either “are” or “aren’t” beta users and/or admins, then we’ll make these columns boolean. Finally we’ll make the default value false so that users must manually be added to either category and cannot just sign themselves up.
NewMigrationFile.rb def change add_column :users, :admin, :boolean, :default => false add_column :users, :beta, :boolean, :default => false end
We’ll then make our changes in the database:
$ rake db:migrate
While this sets up our development environment we still need to update our Git and Heroku repositories.
$ git add -A
$ git commit -m "added admin and beta boolean columns to users table, default values for both is false"
$ git push
$ git push heroku master
And we’re ready to actually activate the appropriate attributes for the appropriate user accounts. Now I use Heroku to host my application, so I’ll be using the rails console through there.
$ heroku run rails c
Now we just need to find the appropriate user accounts and update their beta and admin values to true. My account needs to have both values updated while an account for another user “tim” needs to have just his beta value updated.
> myAccount = User.where(email: "email@example.com").first
> myAccount.admin = true
> myAccount.beta = true
We then would go through the same steps for “tim” substituting his email address for my own and then only changing
beta to true before
With this now in place we can enable and disable certain actions and views depending on the user type. I’ll go through several of these methods in future posts.