Skip to content

Commit

Permalink
Create Bootstrap version of packages#attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarcoux committed Sep 6, 2018
1 parent c2784ba commit beb9edf
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/api/app/assets/javascripts/webui2/application.js
Expand Up @@ -15,6 +15,7 @@
//= require peek
//= require popper
//= require bootstrap
//= require cocoon
//= require jquery.flot
//= require jquery.flot.resize
//= require jquery.flot.time
Expand All @@ -23,7 +24,7 @@
//= require webui2/datatables.js
//= require webui2/tabs.js
//= require webui2/requests_table.js

//= require webui2/attributes.js
// FIXME refactor these files
//= require webui2/autocomplete.js
//= require webui2/comment.js
Expand Down
9 changes: 9 additions & 0 deletions src/api/app/assets/javascripts/webui2/attributes.js
@@ -0,0 +1,9 @@
$(document).ready(function() {
// TODO: Edge case - When a user is already on the page and does a refresh, the description flickers as it appears. Find a way to fix this...
$('#attribute_type-description-' + $('#attrib_attrib_type_id option:selected').val()).removeClass('d-none');

$('#attrib_attrib_type_id').change(function() {
$("[id^='attribute_type-description-']:visible").addClass('d-none');
$('#attribute_type-description-' + $(this).val()).removeClass('d-none');
});
});
11 changes: 11 additions & 0 deletions src/api/app/assets/stylesheets/webui2/package-attributes.scss
@@ -0,0 +1,11 @@
ul li.value {
&:not(:last-child) {
margin-bottom: 5px;
}

& pre {
background-color: #f6f6f6;
border: 1px dotted #690;
padding: 2px 3px;
}
}
1 change: 1 addition & 0 deletions src/api/app/assets/stylesheets/webui2/webui2.css.scss
Expand Up @@ -30,3 +30,4 @@
@import 'texts';
@import 'build-results';
@import 'autocomplete';
@import 'package-attributes';
18 changes: 15 additions & 3 deletions src/api/app/controllers/webui/attribute_controller.rb
@@ -1,4 +1,6 @@
class Webui::AttributeController < Webui::WebuiController
include Webui2::AttributeController

helper :all
before_action :set_container, only: [:index, :new, :edit]
before_action :set_attribute, only: [:update, :destroy]
Expand All @@ -9,7 +11,9 @@ class Webui::AttributeController < Webui::WebuiController
helper 'webui/project'

def index
@attributes = @container.attribs
@attributes = @container.attribs.includes(:issues, :values, attrib_type: [:attrib_namespace]).sort_by(&:fullname)

switch_to_webui2 if @container.is_a?(Package)
end

def new
Expand All @@ -18,7 +22,10 @@ def new
else
@attribute = Attrib.new(project_id: @project.id)
end

authorize @attribute, :create?

switch_to_webui2 if @container.is_a?(Package)
end

def edit
Expand All @@ -27,8 +34,13 @@ def edit

authorize @attribute

return unless @attribute.attrib_type.value_count && (@attribute.attrib_type.value_count > @attribute.values.length)
(@attribute.attrib_type.value_count - @attribute.values.length).times { @attribute.values.build(attrib: @attribute) }
value_count = @attribute.attrib_type.value_count
values_length = @attribute.values.length
if value_count && (value_count > values_length)
(value_count - values_length).times { @attribute.values.build(attrib: @attribute) }
end

switch_to_webui2 if @container.is_a?(Package)
end

def create
Expand Down
13 changes: 13 additions & 0 deletions src/api/app/controllers/webui2/attribute_controller.rb
@@ -0,0 +1,13 @@
module Webui2::AttributeController
def webui2_new
@attribute_types = AttribType.includes(:attrib_namespace).all.sort_by(&:fullname)
end

def webui2_edit
if @attribute.attrib_type.issue_list
@issue_trackers = IssueTracker.order(:name).all
end

@allowed_values = @attribute.attrib_type.allowed_values.map(&:value)
end
end
@@ -0,0 +1,14 @@
= render partial: 'webui/package/breadcrumb_items'

- if current_page?(index_attribs_path)
%li.breadcrumb-item.active{ 'aria-current' => 'page' }
Attributes
- else
%li.breadcrumb-item
= link_to('Attributes', index_attribs_path)
%li.breadcrumb-item.active{ 'aria-current' => 'page' }
- case request.path
- when new_attribs_path(@project, @package)
Add
- when edit_attribs_path(@project, @package, @attribute.fullname)
= @attribute.fullname
33 changes: 33 additions & 0 deletions src/api/app/views/webui2/webui/attribute/_form.html.haml
@@ -0,0 +1,33 @@
- value_fields_locals = { attribute: attribute, allowed_values: allowed_values }
- issue_fields_locals = { issue_trackers: issue_trackers }

= form_for(attribute) do |form|
= form.hidden_field(:attrib_type_id)

- errors = attribute.errors
- if errors.present?
#error-explanation
%h2 "#{pluralize(errors.count, 'error')} prohibited this attribute from being saved:"
%ul
- errors.full_messages.each do |message|
%li= message

#values
= form.fields_for :values do |value|
= render('value_fields', { f: value }.merge(value_fields_locals))
- if attribute.values_addable?
%p
= link_to_add_association(form, :values, render_options: { locals: value_fields_locals }, title: 'Add a value') do
%i.fas.fa-plus-square
Add a value
- if issue_trackers
#issues
= form.fields_for :issues do |issue|
= render('issue_fields', { f: issue }.merge(issue_fields_locals))
%p
= link_to_add_association(form, :issues, render_options: { locals: issue_fields_locals }, title: 'Add an issue') do
%i.fas.fa-plus-square
Add an issue

= form.submit('Save', class: 'btn btn-primary')
= link_to('Back', index_attribs_path, class: 'btn btn-secondary', role: 'button')
11 changes: 11 additions & 0 deletions src/api/app/views/webui2/webui/attribute/_issue_fields.html.haml
@@ -0,0 +1,11 @@
.nested-fields
.field.form-row.form-group
.col-auto
= f.collection_select(:issue_tracker_id, issue_trackers, :id, :name, {}, class: 'form-control')
.col-auto.col-form-label
= f.label(:id, 'ID:')
.col
= f.text_field(:name, class: 'form-control')
.col-auto.col-form-label
= link_to_remove_association(f, title: 'Delete issue') do
%i.fas.fa-minus-circle
19 changes: 19 additions & 0 deletions src/api/app/views/webui2/webui/attribute/_value_fields.html.haml
@@ -0,0 +1,19 @@
.nested-fields
.field.form-row.form-group
- if attribute.values_addable?
.col-auto.col-form-label
= f.label(:position, 'Position:')
.col-1
= f.number_field(:position, class: 'form-control', min: 1)
.col-auto.col-form-label
= f.label(:value, 'Value:')
- if allowed_values.present?
.col-auto
= f.select(:value, allowed_values, {}, class: 'form-control')
- else
.col
= f.text_area(:value, class: 'form-control')
- if attribute.values_removeable?
.col-auto.col-form-label
= link_to_remove_association(f, title: 'Delete value') do
%i.fas.fa-minus-circle
14 changes: 14 additions & 0 deletions src/api/app/views/webui2/webui/attribute/edit.html.haml
@@ -0,0 +1,14 @@
- @pagetitle = "Edit Attribute #{@attribute.fullname} on #{@container.class} #{@container.name}"

.card
= render(partial: 'webui/package/tabs', locals: { project: @project, package: @package })

.card-body
%h3= @pagetitle
%p
= @attribute.attrib_type.description
- if @attribute.values_editable?
= render(partial: 'form', locals: { attribute: @attribute, issue_trackers: @issue_trackers, allowed_values: @allowed_values })
- else
%p
This attribute has no values to edit
48 changes: 48 additions & 0 deletions src/api/app/views/webui2/webui/attribute/index.html.haml
@@ -0,0 +1,48 @@
- @pagetitle = "Attributes of #{@package} (Project #{@project})"
- package_bread_crumb 'Attributes'

.card
= render(partial: 'webui/package/tabs', locals: { project: @project, package: @package })

.card-body
%h3= @pagetitle
- if @attributes.present?
.table-responsive
%table.table.table-hover#attributes
%thead.thead-light
%tr
%th.col-auto{ scope: 'col' } Attributes
%th.col{ scope: 'col' } Values
- if policy(@package).update?
%th.col-auto{ scope: 'col' } Actions
%tbody
- @attributes.each do |attribute|
%tr
%td= attribute.fullname
%td
%ul
- unless attribute.values.empty?
- attribute.values.each do |v|
%li.value
%pre.d-inline= v.value
- unless attribute.issues.empty?
%li= attribute.issues.map(&:name).to_sentence
- if policy(@package).update?
%td
- if attribute.values_editable?
= link_to(edit_attribs_path(project: @project.name, package: @package, attribute: attribute.fullname), title: 'Edit values') do
%i.fas.fa-edit
= link_to(attrib_path(attribute),
data: { confirm: "Delete attribute '#{attribute.fullname}'?" },
method: :delete,
title: 'Delete attribute') do
%i.fas.fa-minus-circle
- else
%p
%em No attributes set

- if policy(@package).update?
%p
= link_to(new_attribs_path(project: @project.name, package: @package), title: 'Add a new attribute') do
%i.fas.fa-plus-square
Add a new attribute
20 changes: 20 additions & 0 deletions src/api/app/views/webui2/webui/attribute/new.html.haml
@@ -0,0 +1,20 @@
- @pagetitle = 'Add Attribute'

.card
= render(partial: 'webui/package/tabs', locals: { project: @project, package: @package })

.card-body
%h3= @pagetitle
= form_for(@attribute) do |form|
.form-row.form-group
= form.hidden_field(:project_id)
- if @attribute.package_id
= form.hidden_field(:package_id)
.col-auto
= form.collection_select(:attrib_type_id, @attribute_types, :id, :fullname, {}, class: 'form-control')
.col.col-form-label
- @attribute_types.each do |attribute_type|
%p.d-none{ id: "attribute_type-description-#{attribute_type.id}" }
= attribute_type.description.presence || 'Sorry, this attribute has no description'
= form.submit('Add', class: 'btn btn-primary')
= link_to('Back', index_attribs_path, class: 'btn btn-secondary', role: 'button')

0 comments on commit beb9edf

Please sign in to comment.