Friday, December 26, 2014

An exercise from Eloquent JS

At the moment, I am at Chapter 5 of Eloquent Javascript. It's been a little while since I started slowly going through it, and recently I've decided to accelerate my reading. Attending Women Who Code JS study groups has helped quite a bit as well.

Previously, I was working through some Angular.js tutorials, yet it is definitely helpful to have a more solid understanding of Javascript fundamentals. Thanks to this resource, there are many more books to read, and I definitely feel that I need to work through several before I will feel comfortable with JS functions.

There are several exercises at the end of each chapter of Eloquent Javascript, and I am including a solution to the first one from Chapter 5. The exercise states: "Use the reduce method in combination with the concat method to "flatten" an array of arrays into a single array that has all the elements of the input arrays".

Chapter 5 provides a sample reduce function, however, I have slightly modified it. The final solution is as follows:

var arrays = [[1,2,3], [4,5], [6]];

function reduce(array, combine){
    var current = [];
    for (var i = 0; i < array.length; i++){
        current = combine(current,array[i]);
    }
    return current;
}

console.log(reduce(arrays, function(a, b){
    return a.concat(b);
}));

Wednesday, December 17, 2014

RailsBridge and iOS

Last week I attended a MobileBridge Workshop, where a lot of women participants had a change to enhance their iOS development skills. For me it was the first experience in iOS. We built a simple Tip Calculator and started on a Rotten Tomatoes API app. All tutorials covered Objective-C.

As always, many thanks to organizers and teachers!

Tuesday, December 9, 2014

SQL queries in Rails

Recently, I needed to write a SQL query that would make multiple selections using insensitive search. The query would be done in either raw SQL and/or using Rails helpers.

For this task, I needed to select users if they practiced certain type of law according to the search terms. As such, there are Users who have many Practices through Practice_Users, and Practices have many Users though Practice_Users. I wanted to select Users with Practices if search terms matched the Practices. However, there are several stop words to avoid: for example, if two records in the database list 'Family law' and 'Family', and I want to search for 'family', then both records should be returned.

My initial solution was the following:
scope :by_practice, -> (pract) {joins(:practice_users).where("practice_users.practice_id IN (SELECT id FROM practices where name IN (?))", pract )}

However, this solution would work if only the search terms matched the database records exactly. Thus, searching for 'family' would exclude 'Family law'.

The second solution was the following:
scope :by_practice, -> (pract) {joins(:practice_users).where("practice_users.practice_id IN (SELECT id FROM practices WHERE #{pract})")}

pract = practices.map { |e|  "name iLIKE '%#{e}%'"}.join(" or ")


However, this solution is vulnerable to SQL injection due to the use of #{} in sql string i.e., #{pract}.

The third and final solution was sort of a combination of the first two: it used ? substitution in Rails and a regex expression in Postgres:

scope :by_practice, -> (pract) {joins(:practice_users).where("practice_users.practice_id IN (SELECT id FROM practices WHERE name ~* ?)", pract)}

pract = practices.map {|e| "#{e}"}.join('|')

Thursday, December 4, 2014

Coverage gem

Just a little note that I am quite enjoying working with the simplecov gem. It helps to determine which sections of the app are still to be tested - models, controllers etc. It provides a nice visual interface, where the covered lines are shown in green, and untested lines are highlighted in red. Very helpful.