Created
May 2, 2010 23:54
-
-
Save adelevie/387561 to your computer and use it in GitHub Desktop.
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
require 'sinatra' | |
require 'activerecord' | |
#db settings | |
ActiveRecord::Base.establish_connection( | |
:adapter => '', | |
:encoding => '', | |
:database => '', | |
:username => '', | |
:password => '', | |
:host => '' | |
) | |
#add models here: | |
class User < ActiveRecord::Base | |
end | |
class Resource | |
attr_accessor :name, :klass, :instance | |
def initialize(name) | |
@name = name | |
@klass = eval(name) | |
@instance = @klass.find(:all, :limit => 1).first | |
end | |
def decamelize | |
self.name.to_s.split(/(?=[A-Z])/).join('_').downcase.pluralize | |
end | |
end | |
get '/' do | |
@resource = Resource.new('User') | |
erb :index | |
end | |
get '/form' do | |
@resource = Resource.new('User') | |
erb :form | |
end | |
__END__ | |
@@ index | |
<textarea> | |
<table border="1"> | |
<tr> | |
<% @resource.instance.attribute_names.each do |attribute_name| %> | |
<td><%= attribute_name %></td> | |
<% end %> | |
<td></td> | |
<td></td> | |
</tr> | |
<%% @<%= @resource.decamelize %>.each do |<%= @resource.decamelize.singularize %>| %> | |
<tr> | |
<% @resource.instance.attribute_names.each do |attribute_name| %> | |
<td><%%= <%= @resource.decamelize.singularize %>.<%= attribute_name %> %></td> | |
<% end %> | |
<td>(<a href="/admin/edit/<%= @resource.decamelize.singularize %>/<%%= <%= @resource.decamelize.singularize %>.id %>">edit</a>)</td> | |
<td>(<a href="/admin/view/<%= @resource.decamelize.singularize %>/<%%= <%= @resource.decamelize.singularize %>.id %>">view</a>)</td> | |
</tr> | |
<%% end %> | |
</table> | |
</textarea> | |
@@ form | |
<textarea> | |
<% @resource.instance.attribute_names.each do |attribute_name| %> | |
<p><%= attribute_name %><input type='text' name='<%= @resource.decamelize.singularize %>[<%= attribute_name %>]' value='<%%= @<%= @resource.decamelize.singularize %>.<%= attribute_name %> %>' /></p> | |
<% end %> | |
</textarea> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check this out. I made a version that's a bit easier to customize when adding models.
The main route, /, is now a menu of the configured models, with links to their respective index and form.
gist 420990