Class: BulkSubmissionExcel::Worksheet::DataWorksheet

Inherits:
SequencescapeExcel::Worksheet::Base show all
Includes:
SequencescapeExcel::Helpers::Worksheet
Defined in:
app/bulk_submission_excel/bulk_submission_excel/worksheet/data_worksheet.rb

Overview

DataWorksheet creates a data worksheet to be filled in by a client.

Constant Summary

Constants included from SequencescapeExcel::Helpers::Worksheet

SequencescapeExcel::Helpers::Worksheet::STYLES

Instance Attribute Summary collapse

Attributes inherited from SequencescapeExcel::Worksheet::Base

#axlsx_worksheet, #columns, #name, #password, #ranges, #workbook

Instance Method Summary collapse

Methods included from SequencescapeExcel::Helpers::Worksheet

#add_headers, #create_styles, #styles

Methods inherited from SequencescapeExcel::Worksheet::Base

#add_row, #add_rows, #create_worksheet, #insert_axlsx_worksheet, #protect

Constructor Details

#initialize(attributes = {}) ⇒ DataWorksheet

Returns a new instance of DataWorksheet


14
15
16
17
18
19
20
# File 'app/bulk_submission_excel/bulk_submission_excel/worksheet/data_worksheet.rb', line 14

def initialize(attributes = {})
  super
  create_styles
  add_title_and_description
  add_columns
  freeze_panes
end

Instance Attribute Details

#assetsObject

Returns the value of attribute assets


8
9
10
# File 'app/bulk_submission_excel/bulk_submission_excel/worksheet/data_worksheet.rb', line 8

def assets
  @assets
end

#defaultsObject

Returns the value of attribute defaults


8
9
10
# File 'app/bulk_submission_excel/bulk_submission_excel/worksheet/data_worksheet.rb', line 8

def defaults
  @defaults
end

Instance Method Details

#add_columnsObject

Adds columns with all required data to a worksheet


46
47
48
49
50
51
52
53
# File 'app/bulk_submission_excel/bulk_submission_excel/worksheet/data_worksheet.rb', line 46

def add_columns
  columns.update(first_row, last_row, ranges, axlsx_worksheet)
  add_headers
  assets.each do |asset|
    detail = build_details(asset)
    create_row(detail)
  end
end

#add_title_and_descriptionObject

Adds title and description (study abbreviation, supplier name, number of assets sent) to a worksheet.


37
38
39
# File 'app/bulk_submission_excel/bulk_submission_excel/worksheet/data_worksheet.rb', line 37

def add_title_and_description
  add_row ['Bulk Submissions Form']
end

#build_details(asset) ⇒ Object

Extract the details for the given asset


56
57
58
59
60
61
62
63
# File 'app/bulk_submission_excel/bulk_submission_excel/worksheet/data_worksheet.rb', line 56

def build_details(asset)
  {
    project_name: asset.projects.one? ? asset.projects.first.name : '',
    study_name: asset.studies.one? ? asset.studies.first.name : '',
    barcode: asset.labware_human_barcode,
    plate_well: asset.respond_to?(:map_description) ? asset.map_description : nil
  }.reverse_merge(present_defaults)
end

#create_row(detail) ⇒ Object

Creates row filled in with required column values, also unlocks (adds unlock style) the cells that should be filled in by clients


68
69
70
71
72
73
74
75
76
77
78
# File 'app/bulk_submission_excel/bulk_submission_excel/worksheet/data_worksheet.rb', line 68

def create_row(detail)
  axlsx_worksheet.add_row do |row|
    columns.each do |column|
      if column.unlocked?
        row.add_cell column.attribute_value(detail), type: column.type, style: styles[:unlocked].reference
      else
        row.add_cell column.attribute_value(detail), type: column.type
      end
    end
  end
end

#first_rowObject


22
23
24
# File 'app/bulk_submission_excel/bulk_submission_excel/worksheet/data_worksheet.rb', line 22

def first_row
  3
end

#freeze_after_column(name) ⇒ Object

Finds the column after which the panes should be frozen. If the column was not found freezes the panes after column 0 (basically not frozen vertically)


95
96
97
# File 'app/bulk_submission_excel/bulk_submission_excel/worksheet/data_worksheet.rb', line 95

def freeze_after_column(name)
  columns.find_by(:name, name) ? columns.find_by(:name, name).number : 0
end

#freeze_panes(name = :sanger_sample_id) ⇒ Object

Freezes panes vertically after particular column (sanger_sample_id by default) and horizontally after headings


83
84
85
86
87
88
89
90
# File 'app/bulk_submission_excel/bulk_submission_excel/worksheet/data_worksheet.rb', line 83

def freeze_panes(name = :sanger_sample_id)
  axlsx_worksheet.sheet_view.pane do |pane|
    pane.state = :frozen
    pane.y_split = first_row - 1
    pane.x_split = freeze_after_column(name)
    pane.active_pane = :bottom_right
  end
end

#last_rowObject

The row where the table with data end


100
101
102
# File 'app/bulk_submission_excel/bulk_submission_excel/worksheet/data_worksheet.rb', line 100

def last_row
  @last_row ||= assets.count + first_row - 1
end

#present_defaultsHash

Returns a hash of defaults where a value has been provided

Returns:

  • (Hash)

    Defaults where keys are present?


30
31
32
# File 'app/bulk_submission_excel/bulk_submission_excel/worksheet/data_worksheet.rb', line 30

def present_defaults
  defaults.select { |_k, v| v.present? }
end