如何防止非常相似的if / else Ruby代码?

时间:2018-09-04 11:52:07

标签: ruby-on-rails refactoring slim-lang

我有一个Rails 5应用程序,其中有许多if / else语句几乎输出相同的内容。这是一个示例:

- if @url.include?('coaches')
  .page-container
    .container.margin-bottom-xs.spacer-minus
      .row.spacer-md
        .col-md-12
          .margin-bottom-sm
            .row
              - @am_coaches.each do |user|
                .col-md-4
                  .small-card.shadow.margin-bottom-sm
                    = image_tag(user.avatar.url, class: 'medium-img')
                    = link_to [user.team, user] do
                      .small-overlay
                        .row
                          .col-md-11
                            .post-category
                              - if user.city?
                                = user.city
                                |, 
                              - if user.country?
                                = user.country
                            .post-title.text-default
                              = user.name
                            .post-time-ago.text-muted
                              - unless user.age_group.blank?
                                = user.age_group
                                |, 
                              = user.team.name

                          .col-md-1.text-right
                            - if user.facebook_url
                              .post-category
                                = link_to "http://www.facebook.com/#{user.facebook_url}",
                                target: '_blank'
                                  i.icon-facebook
                            - if user.twitter_url
                              .post-category
                                = link_to "http://www.twitter.com/#{user.twitter_url}",
                                target: '_blank'
                                  i.icon-twitter
                            - if user.instagram_handle
                              .post-category
                                = link_to "http://www.instagram.com/#{user.instagram_handle}",
                                target: '_blank'
                                  i.icon-instagram

- elsif @url.include?('users')
  .spacer-lg
    - @skills.uniq.sort.reverse.each do |skill|
      .container.spacer-sm
        span.section-heading.text-semibold
          = skill.humanize

        .margin-bottom-sm
          .row.spacer-md
            - @am_users[skill].each do |user|
              .col-md-4
                .small-card.shadow.margin-bottom-sm
                  = image_tag(user.avatar.url, class: 'medium-img')
                  = link_to [user.team, user] do
                    .small-overlay
                      .row
                        .col-md-11
                          .post-category
                            - if user.city?
                              = user.city
                              |, 
                            - if user.country?
                              = user.country
                          .post-title.text-default
                            = user.name
                          .post-time-ago.text-muted
                            - unless user.age_group.blank?
                              = user.age_group
                              |, 
                            = user.team.name

                        .col-md-1.text-right
                          - if user.facebook_url
                            .post-category
                              = link_to "http://www.facebook.com/#{user.facebook_url}",
                              target: '_blank'
                                i.icon-facebook
                          - if user.twitter_url
                            .post-category
                              = link_to "http://www.twitter.com/#{user.twitter_url}",
                              target: '_blank'
                                i.icon-twitter
                          - if user.instagram_handle
                            .post-category
                              = link_to "http://www.instagram.com/#{user.instagram_handle}",
                              target: '_blank'
                                i.icon-instagram

如您所见,其中80%是重复的代码。我正在尝试改善编写应用程序的方式,很想知道如何使它变得更好。

我最初的想法是采用循环并将其置于共享视图中。但是,如果这样做,则无法识别变量。

关于我现在和将来应该如何处理的任何建议?

0 个答案:

没有答案