Michael
2006-Jul-21 16:44 UTC
[Rails] Partial, Table Layout, Multiple Controllers/Models... DRY?
Hi, I am almost embarrased to ask this and I am sure it has been answered previously but the search feature is down on ruby-forum so I am not finding the answer. I have several controllers (leads, orders, activities, etc...). I have a ton of repeated html in each of the views for these controllers. For example, my list view has the same table/tr/td and for..in type of code and it just utilizes different objects based on the controller (@lead, @order, @activity, etc..). I have created partials for the header/footer and also for individual sections of the page (search, list, etc...). However, I would like to further reduce the duplication. Does anyone know the best way to do this? Ideally, I would have a partial (or something similar) that could take in an object from any controller, a list of columns that I want added from that object, table headings, etc.. and produce the output - independent of the controller/model. To further explain: Assume I have a Lead. Lead has first_name, last_name, assigned_to, created_at, phone_number_home, etc.. I want first_name, last_name and created_at to show up in the table as rows and I want "First Name", Last Name" and "Date Created" as the table headings. Even if my object has 50 fields, I only want a select few of them to show up in the list and want to be able to specify the ones to show. Then, I have an activity. For Activity, I have activity_type, start_date, created_at, end_date, assigned_to, etc... I want the headings to be "Activity Type", "Date Created", and "Assigned To". What is the best approach to take here? If I make a change to the table then I want it to be universal - so only one change instead of many as I have now. Make sense? If there are examples of this already then please point me in the right direction. Otherwise, suggestions are greatly appreciated. Regards, Michael mmodica at cox dot net -- Posted via http://www.ruby-forum.com/.
Michael
2006-Jul-22 01:27 UTC
[Rails] Re: Partial, Table Layout, Multiple Controllers/Models... DR
All, I didn''t give it much time, but didn''t get any quick replies to my post. I went ahead and did something and want to know: 1) Is this is okay or is there is a better way to do it. 2) Will this affect performance? Meaning, would it be better to just leave the repeated info in the view files instead of making the helper call every time? What I did: Application Helper: def list_view_td_value(field_value) <<-EOL <td WIDTH="1" class="blackLine" NOWRAP> <img src="/images/blank.gif"> </td> <td style="padding:0px 3px 0px 3px;">#{field_value}</td> EOL end def list_view_td_sort_column(column_name, display_value) list_text <<-EOL <td WIDTH="1" class="blackLine" NOWRAP> <img src="/images/blank.gif"> </td> EOL list_text = list_text + sort_header_tag(column_name, :title => display_value) end Then, in my views, I can use this: Sample Lead: <%= list_view_td_sort_column(''id'', ''Lead Number'') %> <%= list_view_td_sort_column(''lead_type'', ''Lead Type'') %> <%= list_view_td_sort_column(''assigned_to'', ''Assigned To'') %> ... <%= list_view_td_value(lead.id)%> <%= list_view_td_value(lead.lead_type)%> <%= list_view_td_value(lead.assigned_to)%> Sample Order: <%= list_view_td_sort_column(''order_status'', ''Status'') %> <%= list_view_td_sort_column(''customer_first_name'', ''First Name'') %> <%= list_view_td_sort_column(''customer_last_name'', ''Last Name'') %> ... <%= list_view_td_value(order.order_status)%> <%= list_view_td_value(order.customer_first_name)%> <%= list_view_td_value(order.customer_last_name)%> Thoughts? Thanks, Michael mmodica at cox dot net -- Posted via http://www.ruby-forum.com/.