Never been to TextSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

About this user

Julik Tarkhanov

« Newer Snippets
Older Snippets »
3 total  XML / RSS feed 

Time travel with Rails fixtures

If you need to test date- and time-dependend data (for example - "posts that were published yesterday") in your application, use ERB to always obtain correct dates and times in your objects relative to the time the tests are being run:

booking:
  id: 2
  client_id: 2
  device_id: 1
  confirmed: 1
  starts: <%= (1.month.from_now).iso8601 %>
  ends: <%= (40.days.from_now).iso8601 %>



Don't forget to do proper casting ( to_date for datetime fields and to_time.to_s(:db) for time fields).

How to configure MySQL for UTF8 when running MovableType as FastCGI


Use the modified dispatcher.

#!/usr/bin/perl -w

use strict;
use MT::Bootstrap;
use CGI::Fast;

# preload app packages
use MT::App::CMS;
use MT::App::Comments;
use MT::App::Trackback;
use MT::App::Search;
## uncomment if necessary, but this adds a lot of
## overhead since it loads up LibXML.
##use MT::AtomServer;


my $handlers = {
    'mt.fcgi' => { class => 'MT::App::CMS', name => 'AdminScript' },
    'mt-comments.fcgi' => { class => 'MT::App::Comments', name => 'CommentScript' },
    'mt-tb.fcgi' => { class => 'MT::App::Trackback', name => 'TrackbackScript' },
    'mt-search.fcgi' => { class => 'MT::App::Search', name => 'SearchScript' },
## See note above about this...
##    'mt-atom.fcgi' => { class => 'MT::AtomServer', name => 'AtomScript' },
};

eval {
    while (my $q = new CGI::Fast) {
        my $cgi = $q->script_name;
        $cgi =~ s!.*/!!;
        my $pkg = $handlers->{$cgi}{class};
        die "Invalid handler for $cgi" unless $pkg;
        my $app = $pkg->new(CGIObject => $q) or die $pkg->errstr;
        local $SIG{__WARN__} = sub { $app->trace($_[0]) };
        $app->init_request(CGIObject => $q) unless $app->{init_request};
        fixup_script_names($app);
        
        # do a little nasty over-the-back jump and setup MySQL on every connection        
        my $driver = MT::Object->driver;
        my $dbh = $driver->{dbh};
              my $names_cmd = $dbh->prepare("SET NAMES UTF8");
        $names_cmd->execute();
    
        $app->run;
        my $mode = $app->mode || '';
        if ("$pkg->$mode" eq 'MT::App::CMS->plugin_control') {
            exit; # allows server to recycle after changing plugin switches
        }
    }
};
if ($@) {
    print "Content-Type: text/htmlnn";
    print "Got an error: $@";
}

sub fixup_script_names {
    my ($app) = @_;
    $app->config($handlers->{$_}{name}, $_) foreach keys %$handlers;
}

Automatically cipher user password

This allows you to cipher the password of the user automatically upon entry. This way you can allow entry of raw ciphered password via web services etc. as well as keep it ciphered in the database (for instance as a char(32))

require 'md5'
class User < ActiveRecord::Base
        before_save :cipher_password!
    def self.login(login, password)
                password = MD5.new(password).to_s unless password.to_s =~ /^[\dabcdef]{32}$/
                self.find_by_login_and_password(login, password)
        end
        
        private
                def cipher_password!
                        unless password.to_s =~ /^[\dabcdef]{32}$/
                                write_attribute("password", MD5.new(password).to_s)
                                # this is needed for virtual validation
                                @password_confirmation = MD5.new(@password_confirmation).to_s if @password_confirmation
                        end
                end
end


« Newer Snippets
Older Snippets »
3 total  XML / RSS feed