Monthly Archives: October 2011

Ruby methods after maths

Not quite sure what to call this post…But in any case:

DateTime.now.end_of_week+7.strftime(“%Y-%m-%d”)

This results in an error; the strftime tries to work on the ‘7’ only, for some reason.

You can fix it by expanding the maths into it’s full form (ie, a method):

DateTime.now.end_of_week.+(7).strftime(“%Y-%m-%d”)

Learning Ruby notes

I’ve just finished my first plugin for Redmine, learning Ruby along the way.  It’s a bit messy, but it’s simple and does it’s job.  Some things that I learnt along the way:

When you’re in a view, <% tag is used for codes, and <%= is used to print the result of the codes to the screen.  For example:
<% issues = issues.sort_by {|issue| issue[‘date_relative’]} %> – sorts the array
<%= issues = issues.sort_by {|issue| issue[‘date_relative’]} %> – sorts the array and prints the output

debug and inspect are used for debugging; like so:
debug issues
issues.inspect
debug is a bit more useful, I found

Use .class to figure out the class the variable belongs to; in Ruby, arrays and hashes are classes as well, so it can be quite useful to find out what kind of data you’re looking at.

The output of debug can be confusing, for a long time I was trying to figure out why I couldn’t access some of the attributes of an array I was looking at.  I realised why after doing .count; there was one value in the array, and that array was an array containing the data I was trying to find (this is difficult to figure out from debug if you are using it for the first time).  So:
issue.value returned an error
issue[0].value was ok

It seems lots of RoR applications use ActiveRecords; this page is useful for further methods for debugging/accessing data: http://api.rubyonrails.org/classes/ActiveRecord/Base.html
(In my case, Redmine classes are subclasses of ActiveRecord::Base)

useful mysql commands

Few things I repeatedly look up on the interwebs, keeping here for reference:

GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES ON database1.* TO ‘yourusername’@’localhost’ IDENTIFIED BY ‘yourpassword’;

SHOW GRANTS FOR user@localhost;

SELECT * FROM mysql.user;

Getting git to deploy your site in branch folders via post-receive

Aim:  get the site deployed like so; [webroot]/[branch_name]

I’m using this on our development server, so the devs can view all branches on it.

I first set our server up as described in this previous post on deploying with virtualmin.  Then use the following in hooks-receive:

#!/bin/sh
#get the ref name
while read oldrev newrev ref
do
refbranch=”$ref”
done
#remove ref path</h1>
branch=`basename $refbranch`
#add full path
branchpath=”[your_webroot_full_path]/$branch”
#create directory if it doesn’t exist
if [ ! -d “$branchpath” ]; then
mkdir “$branchpath”
echo “Created new folder for branch $branch”
fi
cd “$branchpath”

git –git-dir=[path_to_git_dir] –work-tree=”$branchpath” checkout -f “$branch”
git –git-dir=[path_to_git_dir] –work-tree=”$branchpath” clean -fd
echo “Updated files in $branch directory”

Once you’ve done that, you may want to add a file to the web root so people know what branches they can view.  I’m using the following very simple php script (index.php, in my webroot):

<?php

$dirs = scandir(‘.’);
$remove = array(‘.’,’..’,’.gitignore’,’.project’,’index.php’,’git’);
$html = ”;
foreach($dirs as $dir){
  if(!in_array($dir,$remove)){
    $date = date(‘jS M Y’,filemtime($dir));
    $html .= ‘<a href=”/’ . $dir . ‘”>’ . $dir . ‘</a> – ‘ . $date . ‘<br />’;
  }
}

?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html>
<head>
<title>Development site</title>
</head>

<body>
<p>Please choose your branch (branch name – last modified):<br />
<?php echo $html; ?>
</p>
</body>

</html>

Add to the $remove array any files/directories you do not want showing up.