Skip to content

Commit

Permalink
Merge pull request #4086 from evanrolfe/feature/kiwi_image_description2
Browse files Browse the repository at this point in the history
Feature/kiwi image description2
  • Loading branch information
Evan Rolfe committed Oct 27, 2017
2 parents 0ca1fac + a35c80d commit 97f9a6c
Show file tree
Hide file tree
Showing 18 changed files with 344 additions and 28 deletions.
44 changes: 44 additions & 0 deletions src/api/app/assets/javascripts/webui/application/kiwi_editor.js
Expand Up @@ -24,6 +24,12 @@ function enableSave(){
$('#kiwi-image-update-form-save, #kiwi-image-update-form-revert').addClass('enabled');
}

function editDescriptionDialog(){
var dialog = $('#kiwi-description').find('.dialog');
dialog.removeClass('hidden');
$('.overlay').show();
}

function editPackageDialog(){
var fields = $(this).parents('.nested-fields');
var dialog = fields.find('.dialog');
Expand Down Expand Up @@ -78,6 +84,39 @@ function addRepositoryErrorMessage(source_path, field) {
field.removeClass('hidden');
}

function closeDescriptionDialog() {
var fields = $(this).parents('.nested-fields');
var dialog = fields.find('.dialog');
var name = dialog.find("[id$='name']");

if (name.val() !== '') {
$('#image-name').text(name.val());
}
else {
fields.find(".ui-state-error").removeClass('hidden');
return false;
}

var elements = fields.find('.fill');
for(var i=0; i < elements.length; i++) {
var object = dialog.find("[id$='" + $(elements[i]).data('tag') + "']");
if ( object.val() != "") {
$(elements[i]).text(object.val());
}
}

addDefault(dialog);

if (!canSave) {
enableSave();
}

fields.find(".ui-state-error").addClass('hidden');

hideOverlay(dialog);
}


function closeDialog() {
var fields = $(this).parents('.nested-fields');
var is_repository = fields.parents('#kiwi-repositories-list').size() == 1;
Expand Down Expand Up @@ -136,6 +175,7 @@ function closeDialog() {
function revertDialog() {
var fields = $(this).parents('.nested-fields');
var dialog = fields.find('.dialog');
dialog.find(".ui-state-error").addClass('hidden');

if(dialog.hasClass('new_element')) {
hideOverlay(dialog);
Expand Down Expand Up @@ -297,6 +337,10 @@ $(document).ready(function(){
// Enable save button
$('.remove_fields').click(enableSave);

// Edit dialog for Description
$('.description_edit').click(editDescriptionDialog);
$('.close-description-dialog').click(closeDescriptionDialog);

// Edit dialog for Repositories and Packages
$('.repository_edit').click(editRepositoryDialog);
$('.package_edit').click(editPackageDialog);
Expand Down
11 changes: 11 additions & 0 deletions src/api/app/assets/stylesheets/webui/application/kiwi_image.scss
Expand Up @@ -87,3 +87,14 @@
margin-top: 3px;
}

#kiwi-description {
h2 { margin-bottom: 0;}

.inline {
li {
display: inline-block;

&:first-child { margin-left: 0; }
}
}
}
14 changes: 14 additions & 0 deletions src/api/app/controllers/webui/kiwi/images_controller.rb
Expand Up @@ -31,7 +31,9 @@ def import_from_package
end

def show
@image.build_description if @image.description.nil? # Because the form needs a Description object instantiated
@package_groups = @image.default_package_group

respond_to do |format|
format.html
format.json { render json: { is_outdated: @image.outdated? } }
Expand Down Expand Up @@ -61,6 +63,14 @@ def autocomplete_binaries
private

def image_params
description_attributes = [
:id,
:author,
:contact,
:description_type,
:specification
]

repositories_attributes = [
:id,
:_destroy,
Expand All @@ -84,6 +94,10 @@ def image_params

params.require(:kiwi_image).permit(
:use_project_repositories,
:name,
:schema_version,
:displayname,
description_attributes: description_attributes,
repositories_attributes: repositories_attributes,
package_groups_attributes: package_groups_attributes
)
Expand Down
35 changes: 35 additions & 0 deletions src/api/app/models/kiwi/description.rb
@@ -0,0 +1,35 @@
class Kiwi::Description < ApplicationRecord
belongs_to :image, inverse_of: :description

enum description_type: %i[system]

validates :description_type, inclusion: { in: description_types.keys }

def to_xml
builder = Nokogiri::XML::Builder.new
builder.description({ type: description_type }) do |description|
description.author(author)
description.contact(contact)
description.specification(specification)
end
builder.to_xml save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION | Nokogiri::XML::Node::SaveOptions::FORMAT
end
end

# == Schema Information
#
# Table name: kiwi_descriptions
#
# id :integer not null, primary key
# image_id :integer indexed
# description_type :integer default("system")
# author :string(255)
# contact :string(255)
# specification :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_kiwi_descriptions_on_image_id (image_id)
#
2 changes: 2 additions & 0 deletions src/api/app/models/kiwi/image.rb
Expand Up @@ -18,6 +18,7 @@ class Image < ApplicationRecord

#### Associations macros (Belongs to, Has one, Has many)
has_one :package, foreign_key: 'kiwi_image_id', class_name: '::Package', dependent: :nullify, inverse_of: :kiwi_image
has_one :description, inverse_of: :image, dependent: :destroy
has_many :repositories, -> { order(order: :asc) }, dependent: :destroy, index_errors: true
has_many :package_groups, -> { order(:id) }, dependent: :destroy, index_errors: true
has_many :kiwi_packages, -> { where(kiwi_package_groups: { kiwi_type: Kiwi::PackageGroup.kiwi_types[:image] }) },
Expand All @@ -31,6 +32,7 @@ class Image < ApplicationRecord
validates :name, presence: true
validate :check_use_project_repositories
validate :check_package_groups
accepts_nested_attributes_for :description
accepts_nested_attributes_for :repositories, allow_destroy: true
accepts_nested_attributes_for :package_groups, allow_destroy: true
accepts_nested_attributes_for :kiwi_packages, allow_destroy: true
Expand Down
25 changes: 25 additions & 0 deletions src/api/app/models/kiwi/image/xml_builder.rb
Expand Up @@ -13,11 +13,36 @@ def build

doc = update_packages(doc)
doc = update_repositories(doc)
doc = update_description(doc)

Nokogiri::XML(doc.to_xml, &:noblanks).to_xml(indent: kiwi_indentation(doc))
end

private

def update_description(document)
return document if @image.description.blank?

if document.xpath('image/description[@type="system"]').first
%w[author contact specification].each do |element_name|
description_xml_element = find_or_create_description_xml_element(document, element_name)
document.xpath('image/description[@type="system"]').first.add_child(description_xml_element)
end
else
document.at_css('image').first_element_child.before(@image.description.to_xml)
end
document
end

def find_or_create_description_xml_element(document, element_name)
element_xml = document.xpath("image/description[@type='system']/#{element_name}").first
unless element_xml
element_xml = Nokogiri::XML::Node.new(element_name, document)
end
element_xml.content = @image.description.send(element_name)
element_xml
end

def update_packages(document)
# for now we only write the default package group
xml_packages = @image.default_package_group.to_xml
Expand Down
17 changes: 17 additions & 0 deletions src/api/app/models/kiwi/image/xml_parser.rb
Expand Up @@ -14,6 +14,7 @@ def parse
new_image.use_project_repositories = use_project_repositories?
new_image.repositories = repositories
new_image.package_groups = package_groups
new_image.description = description

new_image
end
Expand Down Expand Up @@ -86,6 +87,22 @@ def package_groups

package_groups
end

# Return an instance of Kiwi::Description model from the parsed xml
def description
attributes = [xml_hash["description"]].flatten.find do |description|
description['type'] == 'system'
end

return if attributes.blank?

Kiwi::Description.new(
description_type: attributes['type'],
author: attributes['author'],
contact: attributes['contact'],
specification: attributes['specification']
)
end
end
end
end
2 changes: 1 addition & 1 deletion src/api/app/models/kiwi/repository.rb
Expand Up @@ -19,7 +19,7 @@ class Repository < ApplicationRecord
#### Scopes (first the default_scope macro if is used)

#### Validations macros
validates :alias, :source_path, uniqueness: { scope: :image, message: "%{attribute} '%{value}' has already been taken."}
validates :alias, :source_path, uniqueness: { scope: :image, message: "%{attribute} '%{value}' has already been taken."}, allow_blank: true
validates :source_path, presence: { message: '%{attribute} can\'t be nil.'}
validate :source_path_format
validates :priority, numericality: { only_integer: true, allow_nil: true, greater_than_or_equal_to: 0,
Expand Down
44 changes: 44 additions & 0 deletions src/api/app/views/webui/kiwi/images/_description.html.haml
@@ -0,0 +1,44 @@
#kiwi-description
.nested-fields
%h2#image-name
= @image.name
%p
%span.fill{ data: { tag: 'specification' }}= @image.description.specification
%ul.inline
%li
%strong Author:
%span.fill{ data: { tag: 'author' }}= @image.description.author
%li
%strong Contact:
%span.fill{ data: { tag: 'contact' }}= @image.description.contact
%p
= link_to sprited_text('package_edit', 'Edit details'), '#', class: 'description_edit'

.dialog.darkgrey_box.hidden{ style: "width: 500px; left: 45%;" }
.box.box-shadow
%h2.box-header Edit Kiwi Details

.dialog-content
%p
= f.label :name
= f.text_field :name, data: { default: f.object.name }

%p
= f.fields_for :description, f.object.description do |description_fields|
%p
= description_fields.label :author
= description_fields.text_field :author, data: { default: description_fields.object.author }
%p
= description_fields.label :contact
= description_fields.text_field :contact, data: { default: description_fields.object.contact }
%p
= description_fields.label :specification
= description_fields.text_field :specification, data: { default: description_fields.object.specification }

%p#flash-messages
%p.ui-state-error.ui-widget-shadow.hidden
Kiwi Image name can not be empty!

.dialog-buttons
= link_to('Cancel', '#', title: 'Cancel', class: 'revert-dialog')
= link_to('Continue', '#', title: 'Continue', class: 'close-description-dialog')
9 changes: 9 additions & 0 deletions src/api/app/views/webui/kiwi/images/_packages.html.haml
@@ -0,0 +1,9 @@
%h3 Packages
#kiwi-packages-list
%p#no-packages{ class: "#{'hidden' if @package_groups.packages.present?}" }= 'There are no packages.'

= f.fields_for :package_groups, @package_groups do |package_group_fields|
= package_group_fields.fields_for :packages do |kiwi_package_fields|
= render 'package_fields', f: kiwi_package_fields
%p
= link_to_add_association(sprite_tag("package_add", title: 'Add package') + ' Add package', package_group_fields, :packages)
15 changes: 15 additions & 0 deletions src/api/app/views/webui/kiwi/images/_repositories.html.haml
@@ -0,0 +1,15 @@
%h3 Repositories
#kiwi-use-project-repositories
%p
= f.check_box :use_project_repositories
= f.label :use_project_repositories
%p.ui-state-highlight#use-project-repositories-text{ class: "#{'hidden' unless f.object.use_project_repositories?}"}
= sprite_tag("info", title: 'Add package')
This option will use the repositories from the current project. Other repositories set in this Kiwi Image will be REMOVED.
#kiwi-repositories-list{ class: "#{'hidden' if f.object.use_project_repositories?}"}
%hr
%p#no-repositories{ class: "#{'hidden' if @image.repositories.present?}" }= 'There are no repositories.'
= f.fields_for :repositories do |repository_fields|
= render 'repository_fields', f: repository_fields
%p
= link_to_add_association(sprite_tag("drive_add", title: 'Add repository') + ' Add repository', f, :repositories)
30 changes: 4 additions & 26 deletions src/api/app/views/webui/kiwi/images/show.html.haml
Expand Up @@ -3,36 +3,14 @@
= form_for @image, html: { id: 'kiwi-image-update-form' } do |f|
.grid_16.alpha.omega.box.box-shadow
= render partial: 'webui/kiwi/tabs'
%h2
= @image.name

.grid_16.alpha.omega.box.box-shadow
%h3 Repositories
#kiwi-use-project-repositories
%p
= f.check_box :use_project_repositories
= f.label :use_project_repositories
%p.ui-state-highlight#use-project-repositories-text{ class: "#{'hidden' unless f.object.use_project_repositories?}"}
= sprite_tag("info", title: 'Add package')
This option will use the repositories from the current project. Other repositories set in this Kiwi Image will be REMOVED.
#kiwi-repositories-list{ class: "#{'hidden' if f.object.use_project_repositories?}"}
%hr
%p#no-repositories{ class: "#{'hidden' if @image.repositories.present?}" }= 'There are no repositories.'
= f.fields_for :repositories do |repository_fields|
= render 'repository_fields', f: repository_fields
%p
= link_to_add_association(sprite_tag("drive_add", title: 'Add repository') + ' Add repository', f, :repositories)
= render partial: 'webui/kiwi/images/description', locals: { f: f }

.grid_16.alpha.omega.box.box-shadow
%h3 Packages
#kiwi-packages-list
%p#no-packages{ class: "#{'hidden' if @package_groups.packages.present?}" }= 'There are no packages.'
= render partial: 'webui/kiwi/images/repositories', locals: { f: f }

= f.fields_for :package_groups, @package_groups do |package_group_fields|
= package_group_fields.fields_for :packages do |kiwi_package_fields|
= render 'package_fields', f: kiwi_package_fields
%p
= link_to_add_association(sprite_tag("package_add", title: 'Add package') + ' Add package', package_group_fields, :packages)
.grid_16.alpha.omega.box.box-shadow
= render partial: 'webui/kiwi/images/packages', locals: { f: f }

.grid_2.alpha.omega.box.box-shadow.kiwi-button
%h3
Expand Down
13 changes: 13 additions & 0 deletions src/api/db/migrate/20171013103921_create_kiwi_descriptions.rb
@@ -0,0 +1,13 @@
class CreateKiwiDescriptions < ActiveRecord::Migration[5.1]
def change
create_table :kiwi_descriptions, id: :integer do |t|
t.references :image, type: :integer
t.integer :description_type, default: 0
t.string :author
t.string :contact
t.string :specification

t.timestamps
end
end
end

0 comments on commit 97f9a6c

Please sign in to comment.