Monday, April 30, 2012

Git, multiple projects with similar features or roots

Todays post is about how I found a blog post that describes how to use git to branch to a new project and then run both projects simultaneously while still being able to pull new features from one to the other.

The essence of all of this is held in this awesome post: http://programblings.com/2008/07/21/setting-up-a-long-term-fork-with-git/

Really I just use that straight up.  The only thing that confused me a bit is how to use it properly.  I was finding that it was hard to maintain the link between the two depots without accidentally bringing changes across that were not meant to be.

For instance, my database connection strings.  After the fork I changed them to point to my new (but similar) projects new DB.  Then I developed a couple of fixes so I pushed them to my "connecting" depot, merged and pushed to my old depot.  I have just now over-written my db connection strings.  Whoops.

My answer? To make any changes that might be a feature I want to bring across in a branch.  It is far easier to cherry pick changes from a branch than to figure out all the different CL's I need to bring across. That and I'm no git expert and I still need to read a good book about it so there are several things that are lost on me or seem far more complex than they really are, for example commits, the hash tag, how to nicely view a list of them, how to use them properly.  Every example I've seen uses bash scripting voodoo (ok, it's not that complex) to get the hash tag to flow from one git command to the next, in perforce it was just an easy to read and distinguish number I could copy and paste and understand as a human, I liked that.

Rant over.

So, for my own reference, the exercise for the user was possibly to create your own working copy.

mkdir working_copy
cd working_copy
git init
git remote add origin /path/to/repo
git pull origin master

That does that, depending on which repo you want, add that path as the origin.

Next, cherry picking without having to work out all the hashes.

git gui

That's pretty much the answer.  I will explain how I personally do it in the next post.

Thursday, April 26, 2012

Google analytics and variable types

I recently went on a mission to change up how we use google analytics events and I wound up having some problems.  I use chrome as my default browser and there is a great extension called google analytics debugger (by google) that helps immensely when debugging analytics code.  I used it the first time around to set everything up.


function gAnalyticsPush(action, name, id, non_interact)
{
  non_interact = typeof non_interaction !== 'undefined' ? non_interact : false;
  push_id = parseInt(id);
  label = id; // this is a string
  _gaq.push(['_trackEvent', name, action, label, push_id, non_interact]);
}

This is my current code.  I tried setting it up so push_id (integer) would be the name (category in the api docs) and the google debugger said everything was working but, alas, when i uploaded it and found I was no longer getting events I realized it was not.

After a while I ditched sending Id's as my category and started using the name (as for me, the two are essentially the same) so I had this:
function gAnalyticsPush(action, name, id, non_interact)
{
  non_interact = typeof non_interaction !== 'undefined' ? non_interact : false;
  push_id = parseInt(id);
  _gaq.push(['_trackEvent', name, action, push_id, push_id, non_interact]);
}

But that wasn't working either.  The problem wound up being that I couldn't send an integer type in the label parameter.  I changed the above code from push_id to just id and it STILL  wasn't working.  I then guaranteed that all id parameters being sent to my gAnalyticsPush function had quotes around them and SHAZZAM, everything worked.

I'm sure that I could solve this in the function but right now there is no need for me to do it.

So I call the above function like so (inside a script tag):

gAnalyticsPush('something_viewed','<%= @model.name %>','<%= @model.id %>',true);

Note that those quotes around the model id are quite important and google analytics debugger did not report any errors even when things were not working.

Monday, April 23, 2012

Rails 3, Ransack and additional query criteria

I'm not exactly sure what the name of what I'm doing is so "additional query criteria" is what it came down to.

I have a table, I want to let users search through it but I only want them to search their records.

I'm using ransack for the searching and it's quite nice, it let me get the search up and running very quickly and it has more power than I really need.  What it didn't have was the ability to require a search term to be a specific value.  I should rephrase, the documentation did not have this.  Without this ability my users will search through everyones data and see things they shouldn't.

Normally I'd use a scope or something like it to make sure user_id = current_user.user_id but how do I do this with ransack?

The answer I'm currently using is to high-jack the query parameter and add my criteria before passing it on to the search method of my model.

That looks something like this:


if params[:q]
  params[:q]["user_id_eq"] = current_user.id
  @q = Customer.search(params[:q])
  @customers = @q.result(:distinct => true)
end

And that's it, now my query has my additional requirements tacked on.

[edit]

OK, so I guess I'm an idiot.  https://github.com/ernie/ransack/wiki/Basic-Searching clearly shows how to do this.  The above could probably be shortened to

  @q = Customer.search(params[:q].merge({"user_id_eq" => current_user.id})

Or something like that anyway

Tuesday, April 17, 2012

An update to the jquery live() function post from before

This isn't a big update.

Just a note.  In a previous post (http://coderambled.blogspot.com.au/2012/03/jquery-coffeescript-and-javascript-html.html) I talked about how my form wasn't answering to click events and whatnot if it was populated using ajax type loading and the answer was the .live function.

Turns out this is deprecated, better to use .on


$(document).on('click', '#form_submit', function() {

    $("#animation").html("<img src='/images/ajax-loader.gif' />");

});


That is my preferred way to do it.  No coffeescript this time because I'm editing a rails 3.0 project.

Also I learned that each release of jquery_ui has a list of where it's hosted on content delivery networks.

http://stackoverflow.com/questions/4944293/jquery-liveclick-vs-click has some good code snippets on the subject

Wednesday, April 11, 2012

Rails, ActiveRecord and using []= operator on string and then saving the record doesn't save!

The whole problem is listed in the title.

Yesterday I was trying to update some fields in my database replacing strings within them and then saving.

records = Model.all
records.each do |record|
  record["somestring"] = otherString
  record.save!
end

This code results in no saves to the database? Why?

From what I found searching the internet (sorry, I'm not really interested in debugging ruby to find out at this point) the assignment operator is used to figure out if a field has changed and needs saving to the database.  Well, I never specifically use the assignment operator and my guess is this is why rails doesn't think it's changed.

The answer (this could likely be nicer but it's just temp code so I bruteforce'd it)


records = Model.all
records.each do |record|
  str = record.field.dup
  record.field["somestring"] = otherString
  record.field = str
  record.save!
end

That did the trick.  It works.  I would call this a bug but it's possible there was a reason for this.  I'm also not certain if it's possible to set the the field to be changed without using the assignment operator.

Tuesday, April 3, 2012

select distinct sql vs select with group by

Yesterday while trying to modify a query I had a bit of a realization about queries with DISTINCT in them and it seems simple and I probably even realized it before and forgotten, it wasn't a ground breaking realization but useful.

SELECT DISTINCT column_name FROM table

is basically the same as

SELECT column_name FROM table GROUP BY column_name

This was taken from one of my favourite query types, the nested query using union logic:

SELECT table.* FROM table WHERE id in (SELECT DISTINCT id FROM maybe_other_table WHERE lots_of_stuff)

That's it, I could probably comment on how they are implemented but you can search the internet for that, I usually just see what stackoverflow has to say about it, here you go http://stackoverflow.com/questions/581521/whats-faster-select-distinct-or-group-by-in-mysql

Peace.