Skip to content

Instantly share code, notes, and snippets.

@maiha
Created December 25, 2008 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maiha/39830 to your computer and use it in GitHub Desktop.
Save maiha/39830 to your computer and use it in GitHub Desktop.
module Inspector
def self.root
File.join Merb.root, "inspector"
end
module HtmlInspectors
def self.lookup(object)
inspectors = {
DataMapper::Collection => DataMapperCollectionInspector,
}
inspectors[object.class] || Base
end
class Base
def initialize(object, caller, context = nil)
@object = object
@caller = caller
@context = context || binding
end
def execute
@object.inspect
end
private
def template_for(name)
File.join Inspector.root, "templates", name
end
def render(name)
name = template_for(name)
@caller.send :render, options.merge(:template=>name)
end
def partial(name)
name = template_for(name)
@caller.send :partial, name, options
end
def options
{}
end
end
class DataMapperCollectionInspector < Base
def execute
partial "index"
end
private
def model
@object.query.model
end
def resource_name
model.name.plural.snake_case
end
def link?(action)
@caller.resource(resource_name, action)
rescue Merb::Router::GenerationError
false
end
def link_to_new(label = 'New')
if (url = link?(:new))
link_to label, url
end
end
def options
{:model=>model, :records=>@object, :list_columns=>list_columns}
end
def list_columns
model.properties
end
end
end
module Helper
def inspect(object)
inspector = HtmlInspectors.lookup(object)
inspector.new(object, self).execute
end
def column_header(p)
label = p.name.to_s
link_to label, "#", :onclick=>"return false;"
end
def column_value(record, p)
record.send p.name
end
def link?(action)
@caller.resource(resource_name, action)
rescue Merb::Router::GenerationError
false
end
def link_to_new(label = 'New')
if (url = link?(:new))
link_to label, url
end
end
end
end
__END__
# Erubis::TinyEruby.new(str).result(binding)
def inspector_render(name)
path = inspector_path + "templates" + name.to_s
partial(path)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment