회사에서 하고 있는 프로젝트에서 정책 데이터들을 MySQL 데이터베이스로 관리하고 있는데요. 보편적인 MySQL 관리툴인 phpMyAdmin 조차 사용하지 않고 그냥 손으로 관리하고 있길래 RubyOnRails로 간단한 관리 도구를 만들어보았습니다. 정확히 30분 걸렸습니다. 많은 관리 도구들이 CRUD 정도만을 필요로 하다는 것을 고려하면 RubyOnRails의 scaffolding만으로도 거의 충분하다고 볼 수 있죠. 다음에 시간이 나면 authentication/authorization 정도를 붙여볼 생각입니다.
Setting up database configuration
development:
adapter: mysql
database: scheduler_policy_manager_development
username: ***
password: ***
host: ***
Creating a model
Creating a table for the model
class CreateAddressSchedulePolicies < ActiveRecord::Migration
def self.up
create_table :address_schedule_policies do |t|
t.column
:name, :string
t.column
:ip, :int
t.column :period, :int
end
enddef self.down
drop_table :address_schedule_policies
end
end
Creating a scaffolding controller
$ vi app/controllers/admin_controller.rb
class AdminController < ApplicationController
scaffold :address_schedule_policy
end
Running the web server
=> Booting WEBrick…
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with –help for options
[2006-10-09 20:58:38] INFO WEBrick 1.3.1
[2006-10-09 20:58:38] INFO ruby 1.8.4 (2005-12-24) [i486-linux]
[2006-10-09 20:58:38] INFO WEBrick::HTTPServer#start: pid=26079 port=3000
Adding some validation
class AddressSchedulePolicy < ActiveRecord::Base
validates_presence_of :name, :ip, :periodprotected
def validate
errors.add(:period, “should be at
least 5000 (ms)”) if period.nil? || period < 5000 end
end
Generating scaffold
Making a small change to the table
$ vi db/migrate/002_change_type_of_ip_field.rb
class ChangeTypeOfIpField < ActiveRecord::Migration
def self.up
change_column
:address_schedule_policies, :ip, :string
enddef self.down
change_column
:address_schedule_policies, :ip, :int
end
end
Installing streamlined generator
$ wget http://streamlined.relevancellc.com/streamlined_generator-0.0.5.gem
$ sudo gem install streamlined_generator-0.0.5.gem
Generating streamlined scaffold for the model
# should restart WebRick to show streamlined correctly
$ script/server