Navigation Menu

Skip to content

Commit

Permalink
Added helper method for displaying titles
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Wheeler committed Apr 26, 2008
1 parent 64f7a71 commit e668d59
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/merb_autoscaffold/controller.rb
@@ -1,4 +1,5 @@
require 'metaid'
require 'merb_autoscaffold/view_helpers'

module MerbAutoScaffold
class Controller
Expand Down Expand Up @@ -31,7 +32,10 @@ def initialize( model )

module InstanceMethods
def self.included(base)

base.class_eval {
include ::Merb::ViewHelpers

class << self
attr_accessor :native_actions
def native_actions() @native_actions ||= []; end
Expand Down
7 changes: 7 additions & 0 deletions lib/merb_autoscaffold/view_helpers.rb
@@ -0,0 +1,7 @@
module Merb
module ViewHelpers
def scaf_title( obj )
obj.try(:title) || obj.try(:name) || "#{ obj.class.to_s } ##{ obj.id }"
end
end
end
4 changes: 4 additions & 0 deletions lib/merb_autoscaffold/views/edit.html.erb
@@ -1,3 +1,7 @@
<h1>
Edit: <%= scaf_title( @model ) %>
</h1>

<form action="<%= url( self.class.Model.singular_name, @model ) %>" method="POST">
<input type="hidden" name="_method" value="PUT" />
<%= partial( 'form', :model => @model ) %>
Expand Down
6 changes: 5 additions & 1 deletion lib/merb_autoscaffold/views/index.html.erb
@@ -1,4 +1,8 @@
<a href="<%= url("new_#{ self.class.Model.to_s.snake_case }") %>">New</a>
<h1>
All <%= self.class.Model.plural_name %>
</h1>

<a href="<%= url("new_#{ self.class.Model.singular_name }") %>">New</a>

<ul>
<% for model in @models -%>
Expand Down
4 changes: 4 additions & 0 deletions lib/merb_autoscaffold/views/new.html.erb
@@ -1,3 +1,7 @@
<h1>
New <%= self.class.Model.singular_name %>
</h1>

<form action="<%= url( self.class.Model.plural_name ) %>" method="POST">
<%= partial( 'form', :model => @model ) %>
<input type="submit" value="Save" />
Expand Down
8 changes: 6 additions & 2 deletions lib/merb_autoscaffold/views/show.html.erb
Expand Up @@ -12,15 +12,19 @@
assoc_hash = Hash[*belongs_tos.collect {|a| [a.foreign_key_name, a]}.flatten]
%>

<h1>
<%= scaf_title( @model ) %>
</h1>

<ul class="unlist horizontal">
<li><a href="<%= url( self.class.Model.plural_name ) %>">View all</a></li>
<li><a href="<%= url("edit_#{ self.class.Model.singular_name }", @model) %>">Edit</a></li>
<li>
<form action="<%= url( self.class.Model.singular_name, @model ) %>" method="POST">
<input type="hidden" name="_method" value="DELETE" />
<input type="submit" value="Delete" />
</form>
</li>
<li><a href="<%= url( self.class.Model.plural_name ) %>">View all</a></li>
</ul>

<dl>
Expand All @@ -30,7 +34,7 @@
<% if association = assoc_hash[column.name] %>
<% obj = association.constant.get( @model.send( column.name ) ) %>
<a href="<%= url( association.constant.singular_name, obj ) %>">
<%= obj.try(:title) || obj.try(:name) || "#{ obj.class.to_s } ##{ obj.id }" %>
<%= scaf_title( obj ) %>
</a>
<% else %>
<%= @model.send( column.name ) %>
Expand Down

0 comments on commit e668d59

Please sign in to comment.