-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[webui] Add Kiwi::Description model.
- Loading branch information
Evan Rolfe
committed
Oct 27, 2017
1 parent
10d63da
commit 7123efd
Showing
9 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/api/db/migrate/20171013103921_create_kiwi_descriptions.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
FactoryGirl.define do | ||
factory :kiwi_description, class: Kiwi::Description do | ||
association :image, factory: :kiwi_image | ||
|
||
description_type { Kiwi::Description.description_types.keys.first } | ||
author 'example_author' | ||
contact 'example_contact' | ||
specification 'example_specification' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Kiwi::Description, type: :model do | ||
let(:kiwi_description) { create(:kiwi_description) } | ||
|
||
describe '#to_xml' do | ||
context 'with full description content' do | ||
let(:expected_xml) do | ||
<<-XML.strip_heredoc | ||
<description type="system"> | ||
<author>example_author</author> | ||
<contact>example_contact</contact> | ||
<specification>example_specification</specification> | ||
</description> | ||
XML | ||
end | ||
|
||
subject { kiwi_description.to_xml } | ||
|
||
it { expect(subject).to eq(expected_xml) } | ||
end | ||
|
||
context 'with empty description content' do | ||
let(:expected_xml) do | ||
<<-XML.strip_heredoc | ||
<description type="system"> | ||
<author/> | ||
<contact/> | ||
<specification/> | ||
</description> | ||
XML | ||
end | ||
|
||
subject { create(:kiwi_description, author: nil, contact: '', specification: nil).to_xml } | ||
|
||
it { expect(subject).to eq(expected_xml) } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters