Monday 2 November 2015

Rails Migrations


This week I came across an issue with a database migration that could easily have been avoided.

I was querying a database that contained a number of boolean NULL values. This is ambiguous, as it could mean false or it could simply mean that the value has never been set.

This scenario is easily avoided by simply setting your boolean to either true or false as a part of your migration. For example;

 add_column :staff, :is_admin, :boolean, null: false, default: false 

That's it! Easy right? It may not change your life but it will surely avoid a headache somewhere down the track.

Sunday 18 October 2015

$LOAD_PATH

What Is $LOAD_PATH (also known as $:)

I have come across $LOAD_PATH many times and have always skipped over it, never quite understanding what it meant. Well, that is, until recently. The project that I am working on forced me to take a closer look. And once I did, like most things, it really wasn’t that difficult at all.

The first thing to understand is what it refers to. Quite simply $LOAD_PATH is what Ruby uses to determine which directories to look in when you use ‘require’ in your code. In other words, $LOAD_PATH contains nothing more than lots of directories. Easy, huh?

The second thing you should know is how to reference it. You can refer to $LOAD_PATH in two ways. The first way is to simply call it by its name, $LOAD_PATH.
The second way is to use the predefined variable, $:
Both mean the exact same thing.

Finally to see it in action, here is an example of adding a directory to $LOAD_PATH


 $: << File.expand_path('../', __FILE__)

Tuesday 28 July 2015

RAILS GIRLS BRISBANE JULY 2015

Created by Sorcha Abel @sabel25 and Rachelle LeQuesne @rachelleonrails

Rails Girls Brisbane 2015 has drawn to a close and what a fantastic weekend it was.

Rails Girls began in Finland a few years ago and since then has become a global phenomenon. It is a free event for women of all ages with little or no programming experience. The aim is to encourage more women into programming and specifically Ruby on Rails.

This was the fourth Rails Girls event to be held in Brisbane and it was a huge success. It was organised by four Rails Girls Brisbane alumnae: myself, Rachelle, Daphne and Jeya.

This year we partnered with Cloud9 for our development environment which removed the local installation headaches and, more importantly, allowed us to start coding right from the get go.

We also changed the guides by adding more detailed explanations. Being relative beginners ourselves, we had a unique perspective on what needed more explanation. That, coupled with regular presentations throughout the event, helped students fully understand and grasp each step of the app they were building.

We rejigged the teaching format and had small tables of four with a mentor permanently assigned to that table. This allowed the participants to get to know their mentor and feel more comfortable asking questions without fear of judgement. Our mentors come from a broad and experienced rails/web background and were all fabulous, giving many students one on one guidance throughout the weekend!

During the event each participant successfully built and deployed their very own Rails app! We had 100% completion this year and their enthusiasm was infectious.

Screenshot 2015-07-28 13.00.01.png

The event was held at River City labs in Fortitude Valley. We are planning to run it again next year so keep watching EventBrite to be a part of the next one. http://railsgirls.com/brisbane

Organisers


Mentors



Sponsors


Ruby AustraliaHerron Todd White

Local Search

New Base

Reinteractive

VPN Solutions


















Thursday 14 May 2015

EMBER MODELS

Today I'm going to expand on a previous post, and have a closer look at how models in Ember work, specifically how they retrieve and display data. An Ember Model tells our application what it needs to know about the data it will be utilising. The attributes and behaviour of the data are defined here. A model in Ember has many similarities to a class in OO programming in that it can have many instances with each instance representing different values. 
# app/models/course.js
 MyDramaSchool.Course = DS.Model.extend({  
  title: DS.attr('string'),   
  cost: DS.attr('number'),   
  description: DS.attr('string'),   
  reviews: DS.hasMany('review'),   
  teacher: DS.belongsTo('teacher')  
 });  
The following data types are available;  

*string 
*number  
*boolean 
*date 

The model can define relationships between other models. Most commonly we will use the 'hasMany' and 'belongsTo'. These are similar to the relationships we might define in a rails model. http://guides.emberjs.com/v1.10.0/models/defining-models/  

Ember models also come with many useful built-in methods. Some of the most popular are;
model.save() #to save changes to the database  
model.rollback() #to reverse any changes made to the database  
model.destroyRecord() #to delete the record from the database  

The data that populates the model can come from two different sources; inside the application or from a connection to another data source. Most commonly you will use data sourced from an api but while still learning, EmberFixtures are a good fit. They are a way to put sample data into an application, 'almost' like hard-coding your data into an array. But for all intents and purposes they act like data we have retrieved from a server. They are ideal to work with when learning Ember due to their simplicity.
 MyDramaSchool.Course.FIXTURES = [{   
 id: 1, title: 'Speech',   
 cost: 99,   
 description: 'Ideal for all ages'   
 },{   
 id: 2,   
 cost: 79,   
 title: 'Drama',   
 description: 'Suitable for beginners'   
 }];   
Ember data has two Application Adapters and their job is to tell our application where it loads the data from and how. 
RESTAdapter: This adapter communicates over HTTP using JSON. You could use this when retrieving data from, perhaps, a rails application that has an api your Ember application consumes. 
FixtureAdapter: This adapter uses fixtures and is what we are using in the application. 
 MyDramaSchool.ApplicationAdapter = DS.FixtureAdapter.extend();  
Models typically use another library called Ember Data. Ember Data has a store, it's like a repository or cached storage for all your records. You can use it to set and retrieve records. Its job is to assist the model in retrieving the data as well as creating and saving new records. It also helps with caching the data for optimum performance. Read more on ember data at https://github.com/emberjs/data

To read more about how ember models work visit the emberjs website http://emberjs.com/

Saturday 11 April 2015

Xero Payroll API

Xero - Payroll API access not authorised

I recently ran into an issue when trying to use the Xero Payroll API. My company, which is comprised of many franchises, was in the process of moving away from MYOB and preparing to go live with Xero. Each franchise in our organisation needed to be set up as a new application. We followed the documentation available to us and undertook the steps outlined below;
  1. Created public/private key pairs for each franchise using openSSL http://developer.xero.com/documentation/advanced-docs/public-private-keypair/
  2. Setup a new private application for each franchise
  3. Uploaded the cer file just created with the openSSL commands
  4. Ensured that the checkbox 'Enable Payroll API for this organisation' was ticked
However each time we tried a call to the Payroll API Previewer (on the Xero site) using any of the payroll endpoints we consistently got this error message 'Payroll API access not authorised'.

A lot of time and research was invested to figure out the issue and in the end the solution was so simple. We discovered by chance that by logging into each organisations Xero, selecting Settings and then Payroll Setting was enough to get Xero to recognise the payroll API. This was done for every franchise and the payroll API for all started to work immediately.

Sunday 22 February 2015

AN INTRODUCTION TO EMBER

Ember is a javascript framework for developing ambitious web applications (http://emberjs.com/)

The objective of this post is to get a simple Ember application up and running. The sample application I will use is based on a fictitious Drama School. Over the coming weeks I will be building on it by using more Ember Components. It is a good place to start if Ember is unfamiliar territory.

A couple of things you need to know and do first;

  1. Ember does not need a webserver to run. Just use a text editor and open in a browser.
  2. You do need to call the Ember.js libraries in your index.html page and these can be downloaded from http://emberjs.com/
  3. Finding ember-data was a little more confusing so I used http://builds.emberjs.com/ and copied the contents of the ember-data.prod.js file to my mac. (I simply created an empty file, called it ember-data.js and copied the contents from the link above into it. I then saved this to the folder where I was developing my web application - /Users/sorchakeane/code/emberDev/js)


STEP 1: Index.html

Create a new index.html page in your editor and in the <head> section do the following;




Note: You will not need all of these libraries initially but I will covering much more than this over the coming weeks so best to include them now.


STEP 2: app.js

Create a new file, I've called mine app.js. Create your Ember application with the following;



Note: LOG_TRANSITIONS is helpful for debugging as it will log out a message to the browser each time a page is accessed.

You need to create an application only once, obviously you can call it whatever suits your purpose but essentially I have just name-spaced my application to MyDramaSchool.


STEP 3: Ember Router

All page requests start here. Every page in the application will be defined in the Router.
It takes what has been entered in the url and renders the correct route.
It then replaces the {{outlet}} found in the application template with the relevant template (will cover templates in step 4).

Say for example the url entered was www.mydramaschool.com/#about. Ember will know to render the 'about' route and load the 'about' template. Be aware that if Ember cannot find the 'about' route object, it will go ahead and create one for us to ensure the application still runs. As I have not explicitly created one that is exactly what will happen for the MyDramaSchool site.

This may sound confusing but will be covered in much more detail in the coming posts. It is just good to know at this point.

In the code below I create a new route called ‘about’ inside the Ember Router.




STEP 4: Handlebars Template

The next task involves setting up some templates using handlebars.
Handlebars templates are just like html however they also give you the option of using expressions.
Handlebar expressions allow us to insert a variable or method result into a html page.
Our Ember application will provide the variables and methods to utilise in these expressions.

Create a main template that will be used to load in the different web pages (templates) in the application. Do this inside the index.html page. It should look similar to this;




The {{outlet}} you see above is called a handlebars expression. Basically the handlebars {{outlet}} gives the code a hint as to where templates should be inserted.

Most of the content of the website will be displayed inside the div with the container class. 

The footer should most likely be static and the navbar div will likely contain the navigation links. 
The {{link-to}} in the navigation is a Ember helper method that generates a link between routes, but is made accessible to our templates.

The tagName is a way to tell Ember that it is a list element.

Finally I will create a new template and call it 'about'. It is this template that will be loaded into the container div above.



You should now be able to open your browser and navigate to your index.html page. I use Chrome and avail of the ember inspector extension https://chrome.google.com/webstore/detail/ember-inspector/bmdblncegkenkacieihfhpjfppoconhi?hl=en
It's really beneficial for debugging.

Next post will cover Ember Controllers and build on what has been covered here. We have only touched on it so far but Ember really is worth getting to know!