Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm ashamed to ask such an elementary question but I tried to DRY up this template a couple ways and couldn't come up with anything I was satisfied with. Can you help?

  %ul.dropdown-menu(aria-labelledby="single-button" role="menu" uib-dropdown-menu)
    %li(role="menuitem")
      %a(href="/legal-forms/admin/document_templates/{{ documentTemplate.id }}/edit") Edit
    %li(role="menuitem")
      %a(href="/legal-forms/admin/document_templates/{{ documentTemplate.id }}/open_in_wizard") Create Document
    %li(role="menuitem")
      %a(href="/legal-forms/admin/document_templates/{{ documentTemplate.id }}/preview" document-template-preview="true" id="documentTemplate.id") Preview
    %li(role="menuitem" ng-hide="documentTemplate.parent_id")
      %a(href=true ng-click="copy(documentTemplate)") Copy
    %li(role="menuitem")
      %a(href=true ng-click="delete(documentTemplate)") Delete
share|improve this question

I think this is solid code. All I can think of is a repeat pointing to an array in the controller. I do this on some of our web apps that have high-freq of changes to menu items.

I don't know if this would really be a good refactor otherwise as it just abstracts away more of the template but it definitely DRY's it up.

BTW I'm not familiar with that templating lib so I apologize if there are any problems with the syntax.

template

 %ul.dropdown-menu(aria-labelledby="single-button" role="menu" uib-dropdown-menu)
        %li(role="menuitem" ng-repeat="item in doc_dropdown")
          %a(
             href={{ ( item.is_path ) ? item.pathLocation : true }} 
             ng-click={{ item.clickTrigger }}
             ng-hide={{ ( item.can_hide ) ? "documentTemplate.parent_id" : "" }}
            ) 
                {{item.text}}

somewhere in the controller

doc_dropdown = [
    {
        is_path: true,
        pathLocation: "/legal-forms/admin/document_templates/{{ documentTemplate.id }}/edit", 
        text: "Edit", 
        click_trigger: null,
        can_hide: false 
    },{
        is_path: true,
        pathLocation: "/legal-forms/admin/document_templates/{{ documentTemplate.id }}/open_in_wizard", 
        text: "Create Document", 
        click_trigger: null,
        can_hide: false 
    },{
        is_path: true,
        pathLocation: "/legal-forms/admin/document_templates/{{ documentTemplate.id }}/preview", 
        text: "Preview", 
        click_trigger: null,
        can_hide: false 
    },{
        is_path: false,
        pathLocation: null, 
        text: "Copy", 
        click_trigger: "copy(documentTemplate)",
        can_hide: true  
    },{
        is_path: false,
        pathLocation: null, 
        text: "Delete", 
        click_trigger: "delete(documentTemplate)",
        can_hide: false 
    },
];
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.