Class: Components::DownloadButton

Inherits:
Base
  • Object
show all
Includes:
Phlex::Rails::Helpers::LinkTo
Defined in:
app/components/download_button.rb

Instance Method Summary collapse

Constructor Details

#initialize(model:, format: :zip) ⇒ DownloadButton

Returns a new instance of DownloadButton.



8
9
10
11
# File 'app/components/download_button.rb', line 8

def initialize(model:, format: :zip)
  @model = model
  @format = format
end

Instance Method Details

#before_template ⇒ Object



17
18
19
20
# File 'app/components/download_button.rb', line 17

def before_template
  @extensions = @model.file_extensions.excluding("json")
  @has_supported_and_unsupported = @model.has_supported_and_unsupported?
end


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/components/download_button.rb', line 55

def download_link(selection: nil, file_type: nil, html_class: "dropdown-item")
  downloader = ArchiveDownloadService.new(model: @model, selection: selection || file_type)
  link_options = {
    class: html_class,
    rel: "nofollow",
    download: (downloader.ready? ? "download" : nil)
  }
  if downloader.preparing?
    link_options.merge!(
      disabled: true,
      "aria-disabled": "true",
      tabindex: -1,
      class: html_class + " disabled"
    )
  end
  link_to model_path(@model, format: @format, selection: selection || file_type), link_options do
    if downloader.ready?
      Icon(icon: "cloud-download-fill", label: t("components.download_button.download.ready"))
    elsif downloader.preparing?
      Icon(icon: "hourglass-split", label: t("components.download_button.download.preparing"), effect: "icon-flip")
    else
      Icon(icon: "cloud-download", label: t("components.download_button.download.missing"))
    end
    whitespace
    span do
      if file_type
        t("components.download_button.file_type", type: file_type.upcase)
      elsif selection
        # i18n-tasks-use t('components.download_button.supported')
        # i18n-tasks-use t('components.download_button.unsupported')
        t("components.download_button.%{selection}" % {selection: selection})
      else
        t("components.download_button.label")
      end
    end
  end
end

#render? ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'app/components/download_button.rb', line 13

def render?
  policy(@model).download?
end

#view_template ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/components/download_button.rb', line 22

def view_template
  div class: "btn-group ml-auto mr-auto" do
    download_link html_class: "btn btn-primary"
    button type: "button",
      class: "btn btn-primary dropdown-toggle dropdown-toggle-split",
      data: {
        bs_toggle: "dropdown"
      },
      aria: {
        expanded: false,
        haspopup: "menu",
        controls: "download-menu"
      } do
      span(class: "visually-hidden") { t("components.download_button.menu_header") }
    end
    ul class: "dropdown-menu",
      id: "download-menu",
      aria: {
        role: "menu"
      } do
      DropdownHeader text: t("components.download_button.menu_header")
      if @has_supported_and_unsupported
        li(role: "menuitem") { download_link selection: "supported" }
        li(role: "menuitem") { download_link selection: "unsupported" }
        DropdownDivider
      end
      @extensions&.compact&.map do |type|
        li(role: "menuitem") { download_link file_type: type }
      end
    end
  end
end