remove files older than x days
Posted in linux, memotoself on March 23rd, 2010 by paul – Be the first to commentfind .* -mtime +14 -exec rm {} \;
find .* -mtime +14 -exec rm {} \;
The following is one way to handle json responses from a rails application.
So, I have a ajax request in a script in my page. In this example I am using the jquery poll plugin to poll the status of a file import.
$("#<%=import.id%>").poll({
url: "/mis_imports/import_state/<%=import.id%>",
interval: 3000,
type: "GET",
success: function(data){
var return_data = eval('(' + data + ')');
$("#<%=import.id%>").attr("class","mis_import_state_"+return_data.state);
$("#<%=import.id%>_message").html(""+return_data.status+"");
$("#<%=import.id%>_timestamp").html(""+return_data.timestamp+"");
if (return_data.state == 1){
$(this).stop();
}
}
});
And on the server, my rails action method looks like this:
def import_state
import = MisImport.find(params[:id])
render :text => {:state => import.state,:status => import.message,:timestamp => Time.now.strftime("%d:%b:%Y %H:%M:%S")}.to_json
end
The key part to this working is line 6 of the javascript listing, where one ‘evals’ the data returned from the server.
This effectively rehydrates the data structure into a usable javascript object, and that’s the bit I keep forgetting!
There are probably smarter ways of doing this; I know there is a jQuery.getJSON() function, but in this case I couldn’t work out how to use that with jquery poll. Again, probably very simple…
I wanted to use the preInsert() method to do some calculations on a model class before it was saved. But I encountered the error:
Strict Standards: Declaration of Blah::preInsert() should be compatible with that of Doctrine_Record::preInsert() in /PATH/lib/model/doctrine/Klass.class.php on line 14
The code I had in my class file looked something like this:
public function preInsert(Doctrine_Event $event)
{
$this->calculate_status();
}
This seemed correct, but I was getting this strict error.
Anyway, short story shorter, I removed the Doctrine_Event (type hint?) from my method signature and the warning went away.
New code looks like this:
public function preInsert($event)
{
$this->calculate_status();
}
I’ve started using the Symfony PHP framework for my php projects, and while the learning curve has been steeper than I would have liked, I’m getting there.
Anyway, one of the problems I’ve had a few times over the current project is the error
“couldn’t get last insert identifier”
So far, the problem has been down to the object I have been trying to create didn’t have it’s id field set to autoincrement. Or perhaps more accurately, setting that model’s id field to autoincrement has sorted the problem so far.
Hopefully once I get the hang of the schema file and the (what appears to be) the slightly different syntax for migrations, all my models should have autoincrement id fields and this shouldn’t be a problem anymore.
I am using the Image Science library to do some simple thumbnailing of uploaded images in a project.
This library is very simple to use, and by all accounts is nice and lightweight and so seemed right.
However, I hit a few problems getting it installed.
One of the requirements on the site is for Ruby Inline. Now, in my haste the first time through I thought I could just gem install inline .
Hah, not so! The inline gem is not the RubyInline gem as linked to from the Image Science site.
So, one has to install the RubyInline gem to get it to work.
Of course this is clearly signposted on the Image Science page, but I thought I’d note it here.
Assumption being the mother of all wasted evenings etc etc.
I needed to create a ServerAlias for one of my subdomains recently and cPanel was stubbornly refusing to do it via the web interface, something to do with dns servers.
Anyway, I just added a simple ServerAlias subdomain.domain.com entry in a file in
/usr/local/apache/conf/userdata/std/2/domain.com/sub.domain.com/subdomain.conf
Then I ran this command:
/scripts/ensure_vhost_includes --all-users
which gets cPanel to rebuild it’s various config files.
Everything works fine now
A neat way to sort a hash
my_hash.keys.sort_by {|s| s.to_s}.map {|key| [key, my_hash[key]] }
If you need to make sure that some text entered into a text field can be sent as part of a uri, say in an ajax call as a parameter, then the built in javascript function encodeURIComponent() seems to work well.
If we have an array called users which contains a number of User objects sent from our rails app, then looping through this array gives us access to each user object.
for (var i = 0;i<users.length;i++) {
var user = users[i];
alert(user.user.name); //This gives us the name property of the object.
}
Note that each object in the array is preceeded by the name of the class, hence the double user. syntax.
I use the rather excellent Boxy plugin to do some modal style popups.
If you want to close the popup programatically from within the popup, say from a cancel button, you can do it like so…
<input type=button value="Cancel" onClick="cancel_button(this);">
function cancel_button(popup){
if(confirm("All changes will be lost")){
Boxy.get(popup).hide();
}
}