Module: ModelsHelper
- Defined in:
- app/helpers/models_helper.rb
Instance Method Summary collapse
- #group(files) ⇒ Object
- #license_select_options(selected: nil) ⇒ Object
- #problems_including_files(model) ⇒ Object
- #t_license(license) ⇒ Object
Instance Method Details
#group(files) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'app/helpers/models_helper.rb', line 2 def group(files) return {} if files.empty? sections = {} min_section_size = [2, (files.count * 0.05).round].max min_prefix_length = 3 names = files.map { it.filename.chomp(File.extname(it.filename)).downcase } slice = names.map(&:length).max while slice > min_prefix_length slice -= 1 candidates = names.map { it.slice(0, slice) } groups = candidates.group_by { it } ready = groups.select { |k, v| v.count >= min_section_size }.map(&:first) ready.each do |r| names.reject! { it.starts_with? r } sections[r], files = files.partition { it.filename.downcase.starts_with? r } end end # Sort and include empty set {nil => files}.merge Naturally.sort_by(sections) { |k, v| k }.to_h end |
#license_select_options(selected: nil) ⇒ Object
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 54 55 56 57 |
# File 'app/helpers/models_helper.rb', line 27 def (selected: nil) # Generate a list of select options for select with a set of useful licenses ( [ "CC-BY-4.0", # i18n-tasks-use t('licenses.CC-BY-40') "CC-BY-NC-4.0", # i18n-tasks-use t('licenses.CC-BY-NC-40') "CC-BY-ND-4.0", # i18n-tasks-use t('licenses.CC-BY-ND-40') "CC-BY-NC-ND-4.0", # i18n-tasks-use t('licenses.CC-BY-NC-ND-40') "CC-BY-NC-SA-4.0", # i18n-tasks-use t('licenses.CC-BY-NC-SA-40') "CC-BY-SA-4.0", # i18n-tasks-use t('licenses.CC-BY-SA-40') "CC-PDDC", # i18n-tasks-use t('licenses.CC-PDDC') "CC0-1.0", # i18n-tasks-use t('licenses.CC0-10') "CERN-OHL-P-2.0", # i18n-tasks-use t('licenses.CERN-OHL-P-20') "CERN-OHL-S-2.0", # i18n-tasks-use t('licenses.CERN-OHL-S-20') "CERN-OHL-W-2.0", # i18n-tasks-use t('licenses.CERN-OHL-W-20') "MIT", # i18n-tasks-use t('licenses.MIT') "GPL-2.0-only", # i18n-tasks-use t('licenses.GPL-20-only') "GPL-3.0-only", # i18n-tasks-use t('licenses.GPL-30-only') "LGPL-2.0-only", # i18n-tasks-use t('licenses.LGPL-20-only') "LGPL-3.0-only", # i18n-tasks-use t('licenses.LGPL-30-only') "0BSD", # i18n-tasks-use t('licenses.0BSD') "LicenseRef-Commercial" # i18n-tasks-use t('licenses.LicenseRef-Commercial') ].map { |id| [ t_license(id), id ] }, selected: selected ) end |
#problems_including_files(model) ⇒ Object
23 24 25 |
# File 'app/helpers/models_helper.rb', line 23 def problems_including_files(model) policy_scope(Problem).where(problematic: policy_scope(model.model_files) + [self]) end |
#t_license(license) ⇒ Object
59 60 61 |
# File 'app/helpers/models_helper.rb', line 59 def t_license(license) t("licenses.%{id}" % {id: license.delete(".")}, default: license) end |