In my views I have to dimensions of commonality: Views related to one controller have in common the kind of object they''re about. More importantly, to me, is the commonality among views pertaining to the same action on different kinds of objects. That is, I''d like to have all list, edit, show, ... views respectively to have the same basic layout. Can this be done with little effort -- POLS-like? Michael -- Michael Schuerig You can twist perceptions mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org Reality won''t budge http://www.schuerig.de/michael/ --Rush, Show Don''t Tell
On Apr 22, 2005, at 12:09 PM, Michael Schuerig wrote:> In my views I have to dimensions of commonality: Views related to one > controller have in common the kind of object they''re about. More > importantly, to me, is the commonality among views pertaining to the > same action on different kinds of objects. That is, I''d like to have > all list, edit, show, ... views respectively to have the same basic > layout. Can this be done with little effort -- POLS-like?Look at Layouts and Partials. For Layouts you''ll have to create your instance variables with the same name (e.g. @item instead of @user or @post) so that the data comes out the same way. For Partials you can pass them in. Another way is to use helper methods. Following is a helper method from my app/helpers/application_helper.rb file that I created for spitting out columns of a record in a table. (I use this on my list pages.) First, in action: <%= create_table_from @roles, :edit => ''role/edit'', :delete => ''role/delete'' %> Produces: <table class="sortable autolinkrows highlightrows"><thead><tr> <th comparemethod="number">Id</th> <th comparemethod="text">Name</th> <th></th><th></th></tr></thead><tbody> <tr><td>2</td><td>Admin Helpers</td><td><a href="/role/edit/2">[edit]</a></td><td><a href="/role/delete/2">[delete]</a></td></tr> <tr><td>1</td><td>Admins</td><td><a href="/role/edit/1">[edit]</a></td><td><a href="/role/delete/1">[delete]</a></td></tr> <tr><td>3</td><td>Franchise Owners</td><td><a href="/role/edit/3">[edit]</a></td><td><a href="/role/delete/3">[delete]</a></td></tr> <tr><td>5</td><td>Job</td><td><a href="/role/edit/5">[edit]</a></td><td><a href="/role/delete/5">[delete]</a></td></tr> <tr><td>4</td><td>Restaurant Owners</td><td><a href="/role/edit/4">[edit]</a></td><td><a href="/role/delete/4">[delete]</a></td></tr> <tr><td>6</td><td>Super-Admin</td><td><a href="/role/edit/6">[edit]</a></td><td><a href="/role/delete/6">[delete]</a></td></tr> </tbody></table> While this: <%= create_table_from @users, :edit => ''edit'', :delete => ''delete'', :columns => [ ''id'', ''login'', ''email'' ] %> Produces: <table class="sortable autolinkrows highlightrows"><thead><tr> <th comparemethod="number">Id</th> <th comparemethod="text">Login</th> <th comparemethod="text">Email</th> <th></th><th></th></tr></thead><tbody> <tr><td>1</td><td>phrogz</td><td>gavin-XtLdkLkwz3ZWk0Htik3J/w@public.gmane.org</td><td><a href="/user/edit/1">[edit]</a></td><td><a href="/user/delete/1">[delete]</a></td></tr> </tbody></table> The code: # _rows_ ::An array of model instances, such as returned by ActiveRecord::Base#find_all # _options_ ::(optional) A hash with one or more of the key/value pairs: # ToDo - document :columns, :edit, and :delete options # # Returns the HTML string of a table for all the records in the _rows_ array, with the # ability to sort by columns, and (if supplied) with links to edit or delete them def create_table_from( rows, options={} ) return ''<p class="ancillary">no rows found</p>'' unless rows.first model = rows.first.class columns_by_name = model.columns_hash #Map ActiveRecord Column types to TableSort.js sort types sort_types = Hash.new(''text'') sort_types[:integer] = sort_types[:float] = ''number'' sort_types[:datetime] = sort_types[:date] = ''date'' sort_types[:timestamp] = sort_types[:time] = ''time'' html = ''<table class="sortable autolinkrows highlightrows">'' col_names = options[:columns] || options[''columns''] if col_names #find the Column objects for each name columns = col_names.collect do |col_name| columns_by_name[ col_name ] end else columns = model.columns end #ToDo: deal with columns that are methods and not actual columns #draw the table header html << "<thead><tr>\r" columns.each do |column| sort_type = column ? sort_types[ column.type ] : sort_types[ :text ] nice_name = column ? column.name.humanize : ''foo'' html << %Q|<th comparemethod="#{ sort_type }">#{ nice_name }</th>\r| end html << ''<th></th>'' if options[ :edit ] html << ''<th></th>'' if options[ :delete ] html << "</tr></thead><tbody>\r" rows.each do |row| html << "<tr>" columns.each do |column| html << %Q|<td>#{ row.send( column.name ) }</td>| end if options[:edit] dest_pair = options[:edit].split(''/'') dest_action = dest_pair.last dest_controller = ( dest_pair.length > 1 ) ? dest_pair.first : @controller.controller_name html << "<td>#{ link_to ''[edit]'', :controller=>dest_controller, :action=>dest_action, :id=>row.id }</td>" end if options[:delete] dest_pair = options[:delete].split(''/'') dest_action = dest_pair.last dest_controller = dest_pair.length > 1 ? dest_pair.first : @controller.controller_name html << "<td>#{ link_to ''[delete]'', :controller=>dest_controller, :action=>dest_action, :id=>row.id }</td>" end html << "</tr>\r" end html << ''</tbody></table>'' end
Michael Schuerig
2005-Apr-23 21:08 UTC
Re: Views: dealing with criss-crossing commonalities?
On Saturday 23 April 2005 03:14, Gavin Kistner wrote:> On Apr 22, 2005, at 12:09 PM, Michael Schuerig wrote: > > In my views I have to dimensions of commonality: Views related to > > one controller have in common the kind of object they''re about. > > More importantly, to me, is the commonality among views pertaining > > to the same action on different kinds of objects. That is, I''d like > > to have all list, edit, show, ... views respectively to have the > > same basic layout. Can this be done with little effort -- > > POLS-like? > > Look at Layouts and Partials.I''m already using both of them in addition to helper methods.> For Layouts you''ll have to create your instance variables with the > same name (e.g. @item instead of @user or @post) so that the data > comes out the same way. > > For Partials you can pass them in.Both of these still require some effort to keep different views for the same object as well as same view kinds for different objects consistent. I was hoping for a way to get consistency more or less automatically. The noteworthy thing about layouts that affords this consistency is the inversion of control (now is that a fashionable concept?), where a structure is defined in a single place and imposed on all other places where it is used. This is hardly a pressing problem for me, currently, more an aesthetic thing... Michael -- Michael Schuerig Face reality and stare it down mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org --Jethro Tull, Silver River Turning http://www.schuerig.de/michael/