named_redmine_links.patch

Andy Fowler, 2010-01-13 15:40

Download (9.41 KB)

View differences:

app/helpers/application_helper.rb
508 508
    #     export:some/file -> Force the download of the file
509 509
    #  Forum messages:
510 510
    #     message#1218 -> Link to message with id 1218
511
    text = text.gsub(%r{([\s\(,\-\>]|^)(!)?(attachment|document|version|commit|source|export|message)?((#|r)(\d+)|(:)([^"\s<>][^\s<>]*?|"[^"]+?"))(?=(?=[[:punct:]]\W)|,|\s|<|$)}) do |m|
512
      leading, esc, prefix, sep, oid = $1, $2, $3, $5 || $7, $6 || $8
511
    text = text.gsub(%r{([\s\(,\-\>]|^)(!)?("([^"]+)":)?(attachment|document|version|commit|source|export|message)?((#|r)(\d+)|(:)([^"\s<>][^\s<>]*?|"[^"]+?"))(?=(?=[[:punct:]]\W)|,|\s|<|$)}) do |m|
512
      leading, esc, label, prefix, sep, oid = $1, $2, $4, $5, $7 || $9, $8 || $10
513 513
      link = nil
514 514
      if esc.nil?
515 515
        if prefix.nil? && sep == 'r'
516 516
          if project && (changeset = project.changesets.find_by_revision(oid))
517
            link = link_to("r#{oid}", {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => oid},
517
            label = label || "r#{oid}"
518
            link = link_to(label, {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => oid},
518 519
                                      :class => 'changeset',
519 520
                                      :title => truncate_single_line(changeset.comments, :length => 100))
520 521
          end
......
523 524
          case prefix
524 525
          when nil
525 526
            if issue = Issue.visible.find_by_id(oid, :include => :status)
526
              link = link_to("##{oid}", {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid},
527
              title = label ? "##{oid} #{truncate(issue.subject, :length => 100)} (#{issue.status.name})" : "#{truncate(issue.subject, :length => 100)} (#{issue.status.name})"
528
              label = label || "##{oid}"
529
              link = link_to(label, {:only_path => only_path, :controller => 'issues', :action => 'show', :id => oid},
527 530
                                        :class => issue.css_classes,
528
                                        :title => "#{truncate(issue.subject, :length => 100)} (#{issue.status.name})")
531
                                        :title => title)
529 532
            end
530 533
          when 'document'
531 534
            if document = Document.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current))
532
              link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
535
              label = label || h(document.title)
536
              link = link_to label, {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
533 537
                                                :class => 'document'
534 538
            end
535 539
          when 'version'
536 540
            if version = Version.find_by_id(oid, :include => [:project], :conditions => Project.visible_by(User.current))
537
              link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
541
              label = label || h(version.name)
542
              link = link_to label, {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
538 543
                                              :class => 'version'
539 544
            end
540 545
          when 'message'
541 546
            if message = Message.find_by_id(oid, :include => [:parent, {:board => :project}], :conditions => Project.visible_by(User.current))
542
              link = link_to h(truncate(message.subject, :length => 60)), {:only_path => only_path,
547
              label = label || h(truncate(message.subject, :length => 60))
548
              link = link_to label, {:only_path => only_path,
543 549
                                                                :controller => 'messages',
544 550
                                                                :action => 'show',
545 551
                                                                :board_id => message.board,
......
554 560
          case prefix
555 561
          when 'document'
556 562
            if project && document = project.documents.find_by_title(name)
557
              link = link_to h(document.title), {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
563
              label = label || h(document.title)
564
              link = link_to label, {:only_path => only_path, :controller => 'documents', :action => 'show', :id => document},
558 565
                                                :class => 'document'
559 566
            end
560 567
          when 'version'
561 568
            if project && version = project.versions.find_by_name(name)
562
              link = link_to h(version.name), {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
569
              label = label || h(version.name)
570
              link = link_to label, {:only_path => only_path, :controller => 'versions', :action => 'show', :id => version},
563 571
                                              :class => 'version'
564 572
            end
565 573
          when 'commit'
566 574
            if project && (changeset = project.changesets.find(:first, :conditions => ["scmid LIKE ?", "#{name}%"]))
567
              link = link_to h("#{name}"), {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision},
575
              label = label || h("#{name}")
576
              link = link_to label, {:only_path => only_path, :controller => 'repositories', :action => 'revision', :id => project, :rev => changeset.revision},
568 577
                                           :class => 'changeset',
569 578
                                           :title => truncate_single_line(changeset.comments, :length => 100)
570 579
            end
......
572 581
            if project && project.repository
573 582
              name =~ %r{^[/\\]*(.*?)(@([0-9a-f]+))?(#(L\d+))?$}
574 583
              path, rev, anchor = $1, $3, $5
575
              link = link_to h("#{prefix}:#{name}"), {:controller => 'repositories', :action => 'entry', :id => project,
584
              label = label || h("#{prefix}:#{name}")
585
              link = link_to label, {:controller => 'repositories', :action => 'entry', :id => project,
576 586
                                                      :path => to_path_param(path),
577 587
                                                      :rev => rev,
578 588
                                                      :anchor => anchor,
......
581 591
            end
582 592
          when 'attachment'
583 593
            if attachments && attachment = attachments.detect {|a| a.filename == name }
584
              link = link_to h(attachment.filename), {:only_path => only_path, :controller => 'attachments', :action => 'download', :id => attachment},
594
              label = label || h(attachment.filename)
595
              link = link_to label, {:only_path => only_path, :controller => 'attachments', :action => 'download', :id => attachment},
585 596
                                                     :class => 'attachment'
586 597
            end
587 598
          end
test/unit/helpers/application_helper_test.rb
136 136
  def test_redmine_links
137 137
    issue_link = link_to('#3', {:controller => 'issues', :action => 'show', :id => 3}, 
138 138
                               :class => 'issue status-1 priority-1 overdue', :title => 'Error 281 when updating a recipe (New)')
139
                               
140
    named_issue_link = link_to('My Ticket', {:controller => 'issues', :action => 'show', :id => 3}, 
141
                               :class => 'issue status-1 priority-1 overdue', :title => '#3 Error 281 when updating a recipe (New)')
139 142
    
140 143
    changeset_link = link_to('r1', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 1},
141 144
                                   :class => 'changeset', :title => 'My very first commit')
145
    named_changeset_link = link_to('My revision', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 1},
146
                                    :class => 'changeset', :title => 'My very first commit')
142 147
    changeset_link2 = link_to('r2', {:controller => 'repositories', :action => 'revision', :id => 'ecookbook', :rev => 2},
143 148
                                    :class => 'changeset', :title => 'This commit fixes #1, #2 and references #1 & #3')
144 149
    
......
156 161
    to_test = {
157 162
      # tickets
158 163
      '#3, #3 and #3.'              => "#{issue_link}, #{issue_link} and #{issue_link}.",
164
      '"My Ticket":#3, #3 and "My Ticket":#3.' \
165
                                    => "#{named_issue_link}, #{issue_link} and #{named_issue_link}.",
159 166
      # changesets
160 167
      'r1'                          => changeset_link,
168
      '"My revision":r1'            => named_changeset_link,
169
      '"My revision":r1, r2'        => "#{named_changeset_link}, #{changeset_link2}",
161 170
      'r1.'                         => "#{changeset_link}.",
162 171
      'r1, r2'                      => "#{changeset_link}, #{changeset_link2}",
163 172
      'r1,r2'                       => "#{changeset_link},#{changeset_link2}",