-
-
Save webmat/420990 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 '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