Global Variables in SASS

Using global variables in a SASS stylesheet saves time by eliminating repetitious code. In this example I will show how to declare global color variables that can be referenced by name in multiple partial stylesheets.

First we create an SCSS file called “_variables.scss”. The name isn’t important but the leading underscore is. Inside we declare the variables like so:

Then in the main SCSS file you import the variables file:

The variables should now be accessible from inside other stylesheets provided they are properly imported. Here is an example of a “welcome.scss” file that uses the global variables:

 

Setting Environment Variables in Unix Shell

Setting environment variables allows you to reference sensitive data, like passwords and API keys in an application, without making them publicly visible online or in a public repository.

Example:

becomes…

To do this, first check to make sure you are using a bash shell:

Then edit your “~/.bashrc” file and add:

Save the file and you’re all set. You will have to open a new shell or restart your terminal to continue.

Rails API Basics – Guild Wars 2 Dye Gallery

This is an example showing how to use the Guild Wars 2 API to create a simple dye gallery using Ruby on Rails.

The first step is to generate a “Dye” model with the attributes of “content” as “text” and “color” also as “text”. This will enable the data gathered from the API to be organized for use in the gallery.

Next we want to add the Guild Wars 2 API gem to our gemfile.

Make sure to run “bundle install” after adding the gem.

Heading to the seeds.rb file of our database we put the following code:

Dye.delete_all simply deletes the dyes so that we load a fresh database each time.

The next line calls the API specifying the section Misc>colors.

Then for each ‘color’ it finds the ‘name’ and sets it to the variable ‘name’, it finds the RGB color combination and sets it to the variable ‘color_list’. After that it converts each component of the RGB color array to a string which will then be fed into the css in our gallery view.

Last, each dye is created with name assigned to ‘:content’, and color assigned to ‘:color’.

In the index page of our dyes view we have the following:

For each dye we create a div that displays the name of the dye color and a thumbnail div that is filled with the color itself using the RGB color we setup earlier.

As far as styling goes here are the critical parts:

To display the dyes and sort them we create a “Dyes Controller” with an index method.

The final product looks like this:

Dye Gallery