Skip to content

Instantly share code, notes, and snippets.

@webmat
Forked from adelevie/admin_gen.rb
Created June 1, 2010 14:16
Show Gist options
  • Save webmat/420990 to your computer and use it in GitHub Desktop.
Save webmat/420990 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'sinatra'
require 'activerecord'
#db settings
ActiveRecord::Base.establish_connection(
:adapter => '',
:encoding => '',
:database => '',
:username => '',
:password => '',
:host => ''
)
#add models here:
models = %w(User Post Comment)
models.each do |k|
eval "class #{k} < ActiveRecord::Base ; end"
end
class Resource
attr_accessor :name, :klass, :instance
def initialize(name)
@name = name.camelcase
@klass = eval(@name)
@instance = @klass.find(:all, :limit => 1).first || @klass.new
end
end
get '/' do
@models = models
erb :menu
end
get '/:model/index' do
@resource = Resource.new(params[:model])
@name = @resource.name.underscore.singularize
erb :index
end
get '/:model/form' do
@resource = Resource.new(params[:model])
@name = @resource.name.underscore.singularize
erb :form
end
def styles
<<-CSS
<style>
textarea {width:100%; height:100%; font-size: 14px;}
body { font-size: 18px }
</style>
CSS
end
__END__
@@ menu
<%= styles %>
<ul>
<% @models.each do |model| %>
<li>
<%= model %>
<a href="/<%= model %>/index">index</a>
<a href="/<%= model %>/form">form</a>
</li>
<% end %>
</ul>
@@ index
<%= styles %>
<a href="/">Back</a>
<textarea style="width:100%;height:100%">
<table>
<tr>
<% @resource.instance.attribute_names.each do |attribute_name| %><td><%=
attribute_name.humanize %></td>
<% end %>
<td></td>
</tr>
<%% @<%= @name.pluralize %>.each do |<%= @name %>| %>
<tr><% @resource.instance.attribute_names.each do |attribute_name| %>
<td><%%= <%= @name %>.<%= attribute_name %> %></td><% end %>
<td>(<%%= link_to 'edit', edit_admin_<%= @name %>_path(<%= @name %>) %>)</td>
</tr>
<%% end %>
</table>
</textarea>
@@ form
<%= styles %>
<a href="/">Back</a>
<textarea style="width:100%;height:100%">
<%% form_for [:admin, @<%= @resource.name.underscore %>] do |f| %>
<% @resource.instance.attribute_names.each do |attribute_name| %>
<div>
<%%= f.label :<%= attribute_name %>, '<%= attribute_name.humanize %>', :class => 'attribute_name' %>
<%%= f.text_field :<%= attribute_name %> %>
</div>
<% end %>
<%% end %>
</textarea>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment