Reading, Writing, Removing Files and Directories in Ruby
These aren’t all of them, but I think they are some of the most useful.
# making a directory in another directory that doesn't yet exist...
FileUtils.mkdir_p '/path/to/your/directory/that/doesnt/exist/yet'
# recursively remove a directory and the contents
FileUtils.rm_rf("/path/to/directory/you/want/to/delete")
# write a file from the contents of another file...
File.open("/path/to/the/file.ext", "wb") {|f| f.write(@your_other_file.read)}
Adding RSS Graffiti to my Facebook Page
I added the RSS Graffiti application to my Facebook account. It’s a nifty app that lets you add any valid Rss/Atom feed to your profile (includes publishing to your wall) and to your pages.
javascript: $$ code optimization javascript nested prototype tables td
by bseanvt
leave a comment
Accessing Links in Nested TD Cells with Prototype
There must be a better way to do the following with PrototypeJS. I want to loop over nested links inside of table td cells and apply a class to the row that the link is in when the link is clicked. If a user clicks on another link the class will be removed and applied to the current table row.
The code below works but I think it seems hackish. Any thoughts on improving this code?
Here is the link to the pastie http://pastie.org/703316
# This is the table with haml markup
%table
%tr
%td
info
%td
= link_to "be clicked", remote_request_goes_here_path
And the Js
$$("td.filter_link > a").each(function(element){
Event.observe(element, "click", function(event){
$$("td.filter_link").each(function(el){
if(el != this){
el.up().removeClassName("turnwhite");
}
});
// apply the turnwhite class to the table row
this.up().up().addClassName("turnwhite");
});
});
Ruby on Rails: active record array created_at sort_by
by bseanvt
leave a comment
Descending Sort By in Model For Active Record Hash on Created_at attribute
If you have a couple collections from the database and you want to sort it without the help of Active Record, take a look at the sort_by method on Array type. I’ve used this before when I have a couple of collections which are slightly different but I need them in a chronological order.
@posts_group_a = Post.find :all, :conditions => ["user_id = ?", current_user.id]
@posts_group_b = Post.find :all, :conditions => ["user_id = ?", friend_user.id]
#merge the two arrays here
@posts = @posts_group_a + @posts_group_b
# notice the "-" is for descending order and the "to_i" casts the date time to an integer (required)
@posts.sort_by {|post| - post.created_at.to_i}
Ruby on Rails: active record console development logging stdout
by bseanvt
1 comment
Output Logger and SQL to the Rails Console in Development Mode
If you want to take a look at the SQL being generated by active record while your using the console, you can either type this into the console when it loads
ActiveRecord::Base.logger = Logger.new(STDOUT)
Or you can add it to your environment so that it’ll be the default behavior
rails_root/config/environments/development.rb
#... ActiveRecord::Base.logger = Logger.new(STDOUT)
It’s a nice way to keep you away of any expensive queries you may unknowingly be writing!
Wordpress: javascript php plugins text editor tinymce Wordpress
by bseanvt
leave a comment
TinyMCE Rich Text Editor: HELLO EDITOR Plugin Tutorial and Example
I wanted to create a button for the TinyMCE Rich Text Editor for WordPress. It was tough to find good docs on the subject. There are a couple of useful posts out there but in general I found them lacking.
http://codex.wordpress.org/TinyMCE_Custom_Buttons#Creating_an_MCE_Editor_Plugin
The above resource has a good section on the PHP code needed to write your own callbacks but there is no mention of the JavaScript required to create a button and interact with it. Links are available for more reading but why not also post a code sample to get up and running?
Anyway, I made a simple plugin after hobbling a bunch of resources together to get a button in the text editor that when clicked triggers an alert to the window. The code isn’t fancy but it might provide a starting off point for a larger project or be useful as a resource for some of the basic ideas involved. I read through a couple of the sample plugins included with WordPress along with the Viper Video Quicktags Plugin, which makes extensive use of the feature. However, they were too complex for a quick, basic understanding.
This plugin doesn’t do a lot. I’ll probably flesh it out a bit more when I have a clearer understanding of how it all works. For now here are some code samples and at the bottom of this post there is a link to the .zip file for the plugin which you can install on your own site.
In wp-content/plugins/hello_editor/index.php
<?php
/*
Plugin Name: Hello Editor
Plugin URI: http://seanbehan.com/wordpress/tinymce-rich-text-editor-hello-editor-plugin-tutorial-and-example/
Description: A simple plugin showing how to add a button to the rich text editor in WordPress.
The plugin doesn't do anything except place a button and respond to onclick event, with "Hello Editor".
Most of the editor plugins available are too 'functional' to quickly read and see how to do the
basics. This plugin doesn't 'do' anything, rather it makes it simple to read what is going on.
Version: 0.1
Author: Sean Behan
Author URI: http://seanbehan.com
*/
define( "HELLO_EDITOR_PLUGIN_DIR", "hello_editor" );
define( "HELLO_EDITOR_PLUGIN_URL", "/wp-content/plugins/" . HELLO_EDITOR_PLUGIN_DIR );
// Register the external pllugin from the .js file
function hello_editor_register_external_plugin($plugin_array) {
$plugin_array['HELLO_EDITOR'] = HELLO_EDITOR_PLUGIN_URL . '/editor_plugin.js';
return $plugin_array;
}
// Add the button to the array ***NOTE*** The name here "HELLO_EDITOR" must
//match the name in the editor plugin file for the addButton(name_of_button) function!
function hello_editor_register_button($buttons) {
array_push( $buttons, "|", "HELLO_EDITOR" );
return $buttons;
}
// Filters which will call our functions
function hello_editor_init(){
add_filter("mce_external_plugins", "hello_editor_register_external_plugin");
add_filter('mce_buttons', 'hello_editor_register_button');
}
// When the editor is initialized
add_action('init', 'hello_editor_init');
In wp-content/plugins/hello_editor/editor_plugin.js
(function() {
//Init the plugin
tinymce.create('tinymce.plugins.HELLO_EDITOR', {
init : function( ed, url ) {
//addButton name needs to match $plugin_array['HELLO_EDITOR'] in add_filter function
ed.addButton('HELLO_EDITOR', {
title : 'Hello Editor',
image : url + "/buttons/hello.png",
onclick : function(){
alert("HELLO EDITOR!");
}
});
},
//Info about the plugin
getInfo : function() {
return {
longname : "Sean Behan's Sample Hello Editor Plugin",
author : 'Sean Behan',
authorurl : 'http://www.seanbehan.com/',
infourl : 'http://seanbehan.com/wordpress/tinymce-rich-text-editor-hello-editor-plugin-tutorial-and-example/',
version : "0.1"
};
}
});
tinymce.PluginManager.add('HELLO_EDITOR', tinymce.plugins.HELLO_EDITOR);
})();
You’ll also need a directory called buttons inside the hello_editor plugin directory with a hello.png file which will be displayed. Without it you’ll just get a blank button. You can rename this but remember to change the name in the editor_plugin.js file as well.
You can download the sample plugin here http://seanbehan.com/wp-content/uploads/2009/11/hello_editor.zip
Disk Usage Information on Mac OS X
Get disk usage information about the Desktop
$ du -h -d 0 Desktop 14G Desktop
Information about how much free space is available on computer
$ df -lh Filesystem Size Used Avail Capacity Mounted on /dev/disk0s2 111Gi 109Gi 2.3Gi 98% /
You can read more about the flags with the man pages
$ man du $ man df
This is my 100th post!!!
Working with Branches in Git
Show all the branches
git branch
Create a new branch
git branch my_experimental_feature
Use that branch
git checkout my_experimental_feature
Pushing the new branch to a remote server
git push origin my_experimental_feature
Pulling that branch down on another machine
git pull origin my_experimental_feature
Listing all branches on other machine
git branch -a
Updating other machine
git pull origin my_experimental_feature


