Created
May 23, 2014 05:31
-
-
Save memememomo/7a3535eded7fdda322e3 to your computer and use it in GitHub Desktop.
MojocliciousでTengを使う設定 ref: http://qiita.com/uchiko/items/689e33f91cad8c1119be
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package MyApp::DB; | |
use parent 'Teng'; | |
use DateTime; | |
# プラグイン有効 | |
__PACKAGE__->load_plugin('Pager'); | |
__PACKAGE__->load_plugin('Count'); | |
# hookの設定 | |
use Class::Method::Modifiers; | |
# updateメソッド時にupdated_atに現在時刻を自動入力する | |
before update => sub { | |
my ($self, $table_name, $update_row_data, $update_condition) = @_; | |
if ( !$update_row_data->{updated_at} ) { | |
$update_row_data->{updated_at} = DateTime->now->set_time_zone('Asia/Tokyo')->strftime('%Y-%m-%d %H:%M:%S'); | |
} | |
}; | |
# insertメソッド時にupdated_at/created_atに現在時刻を自動入力する | |
before insert => sub { | |
my ($self, $table_name, $row_data) = @_; | |
$row_data->{created_at} = DateTime->now->set_time_zone('Asia/Tokyo')->strftime('%Y-%m-%d %H:%M:%S'); | |
$row_data->{updated_at} = DateTime->now->set_time_zone('Asia/Tokyo')->strftime('%Y-%m-%d %H:%M:%S'); | |
}; | |
# fast_insertメソッド時にupdated_at/created_atに現在時刻を自動入力する | |
before fast_insert => sub { | |
my ($self, $table_name, $row_data) = @_; | |
$row_data->{created_at} = DateTime->now->set_time_zone('Asia/Tokyo')->strftime('%Y-%m-%d %H:%M:%S'); | |
$row_data->{updated_at} = DateTime->now->set_time_zone('Asia/Tokyo')->strftime('%Y-%m-%d %H:%M:%S'); | |
}; | |
1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cpanm Teng Class::Method::Modifiers |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
get '/' => sub { | |
my $self = shift; | |
$self->stash->{users} = [$self->db->search('users')]; | |
$self->render(); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package AM::DB::Schema; | |
use Teng::Schema::Declare; | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment