Subscribe feed Detya Bozhye

Welcome to my site

My name is Aleksey, I'm just a regular 20.2931506849 year old Evangelical Christian Baptist from California. Any questions?

Tuesday, 16 June 2009

Upcoming downtime

Posted by anime4christ at 04:31 PM

My site will be down while I switch servers during the end of this month. It might take longer because I am planning to redo the whole site in WordPress and create a more artistic design for it, so I can't say for sure how long the downtime will be. See you all in a while.

0 Comments

Tuesday, 28 April 2009

A little PHP CRUD

Posted by anime4christ at 04:09 PM

I had to return to PHP for a small application I had to write for which Rails would have been too big. Being used to Rails RESTful methods and MVC, I didn't want to make another messy 200 line PHP app that I would not be able to read later, so I coded a very small and simple CRUD "router" (as in Rails' routes) and I decided to share it with the world. So far it only works with a "controller" that manages plural resources. The CRUD pattern here is modeled after the way Rails controllers work. It's really simple, but it sure makes coding tiny apps a lot cleaner. (Helpers, clean URLs, MVC, etc. are up to the reader to implement)

crud.php:

Code:

<?php
switch($_SERVER['REQUEST_METHOD']) {
  case 'GET':
    switch($_GET['action']) {
      case 'new':
        cnew();
        break;
      case 'edit':
        edit();
        break;
      default:
        if (preg_match('/^[0-9]+$/', $_GET['id']))
          show();
        else
          index();
        break;
    }
    break;
  case 'POST':
    switch($_POST['_method']) {
      case 'put':
        update();
        break;
      case 'delete':
        destroy();
        break;
      default:
        create();
    }
    break;
  case 'PUT':
    update();
    break;
  case 'DELETE':
    destroy();
    break;
}
?>

index.php (an example "controller"):

Code:

<?php
require 'crud.php';

function index() {
  // grab some data
  require 'views/index.php';
}

function cnew() {
  // new resource
}

function show() {
  // get the resource by id
  echo "You requested resource number " . $_REQUEST['id'];
}

function edit() {
  // edit resource
}

function create() {
  // create something
  header('Location: ./');
}

function destroy() {
  //delete something
  header('Location: ./');
}
?>

0 Comments

Friday, 09 January 2009

Ruby on Rails: Complex forms + auto_complete

Posted by anime4christ at 06:59 PM

I haven't written here in a long time, so as many of you might not know yet, I've been learning Ruby on Rails the past 4 or 5 months. I'll have to say I've run into some pretty big challenges, but overall Ruby on Rails is one awesome framework. My latest challenge has been getting DHH's auto_complete plugin to work with Ryan Bates' complex forms. It might not be the best way to do it, but the solution was to monkey patch ActionView::FormHelper and FormBuilder which I put in a seperate file in the /lib folder of my project. So without further ado, here's the code:

Code:

## lib/auto_complete_form_helper.rb
module ActionView
  module Helpers
    module FormHelper
      def text_field_with_auto_complete_mod(object, method, tag_options = {}, completion_options = {})
        sanitized_object = object.gsub(/\]\[|[^-a-zA-Z0-9:.]/, "_").sub(/_$/, "")
        sanitized_method = method.to_s.sub(/\?$/,"")
        (completion_options[:skip_style] ? "" : auto_complete_stylesheet) +
        text_field(object, method, tag_options) +
        content_tag("div", "", :id => "#{sanitized_object}_#{sanitized_method}_auto_complete", :class => "auto_complete") +
        auto_complete_field("#{sanitized_object}_#{sanitized_method}", { :url => { :action => "auto_complete_for_#{sanitized_object}_#{sanitized_method}" } }.update(completion_options))
      end
    end

    class FormBuilder
      def text_field_with_auto_complete(method, tag_options = {}, completion_options = {})
        @template.text_field_with_auto_complete_mod(@object_name, method,
          objectify_options(tag_options), completion_options)
      end
    end
  end
end

Don't forget to include the file in your environment.rb. If you see any bugs or know of a better way (I couldn't find any solutions on the web), please let me know. smile

0 Comments

Wednesday, 22 October 2008

My first LOLCat

Posted by anime4christ at 12:31 PM

Maybe there's a better caption to go with this photo, but hey, it's only my first. On the photo are two kittens I had about 3 or 4 years ago. Go rate it!

http://images.icanhascheezburger.com/completestore/2008/10/22/128691386979076114.jpg

0 Comments

Tuesday, 30 September 2008

Small Website Update

Posted by anime4christ at 04:20 PM

I've removed the anime, music, and web sections of my website because I never finished them and never will. If I ever need to put something anime, music, or web related, I'll just put it straight on my blog. So all there is left to my website is my blog and the forum (in fact, the forum holds the blog as well, so all I really have is the forum and a bloggy sort of shell on my home page).

When I get some time (hopefully in this decade), I will set up a new blog separate from the forum, and either phase out my forum, or replace it with another one if it's still needed.

2 Comments

« All Blog Posts »