Skip to content

Commit

Permalink
Merge 7358209 into a785027
Browse files Browse the repository at this point in the history
  • Loading branch information
SaravShah committed Sep 6, 2018
2 parents a785027 + 7358209 commit 0735a76
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ class BundleUsageError < StandardError
# back to users of the bin/pre-assemble script.
end

class BundleContext
# This is a temporary BundleConext class that will be replaced with the BundleContext ActiveRecord Model.
class BundleContextTemporary
# Paramaters passed via YAML config files.
YAML_PARAMS = [
:project_style,
Expand Down
2 changes: 1 addition & 1 deletion app/lib/pre_assembly/bundle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Bundle
to: :bundle_context

class << self
delegate :import_csv, to: BundleContext
delegate :import_csv, to: BundleContextTemporary
end

def initialize(bundle_context)
Expand Down
24 changes: 24 additions & 0 deletions app/models/bundle_context.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class BundleContext < ApplicationRecord
belongs_to :user

validates :project_name, presence: true, null: false
validates :content_structure, presence: true, null: false
validates :bundle_dir, presence: true, null: false
validates :staging_style_symlink, inclusion: { in: [ true, false ] }
validates :content_metadata_creation, presence: true, null: false

enum content_structure: {
"simple_image_structure" => 0,
"simple_book_structure" => 1,
"book_as_iamge_structure" => 2,
"file_structure" => 3,
"smpl_structure" => 4
}

enum content_metadata_creation: {
"default_style" => 0,
"filename_style" => 1,
"smpl_style" => 2
}

end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class User < ApplicationRecord
has_many :bundle_contexts
validates :sunet_id, presence: true, uniqueness: true, null: false
end
14 changes: 14 additions & 0 deletions db/migrate/20180905214414_create_bundle_contexts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateBundleContexts < ActiveRecord::Migration[5.2]
def change
create_table :bundle_contexts do |t|
t.string :project_name, null: false
t.integer :content_structure, null: false
t.string :bundle_dir, null: false
t.boolean :staging_style_symlink, default: false, null: false
t.integer :content_metadata_creation, null: false
t.references :user, foreign_key: true, null: false

t.timestamps
end
end
end
14 changes: 13 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,19 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_09_04_183131) do
ActiveRecord::Schema.define(version: 2018_09_05_214414) do

create_table "bundle_contexts", force: :cascade do |t|
t.string "project_name", null: false
t.integer "content_structure", null: false
t.string "bundle_dir", null: false
t.boolean "staging_style_symlink", default: false, null: false
t.integer "content_metadata_creation", null: false
t.integer "user_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_bundle_contexts_on_user_id"
end

create_table "users", force: :cascade do |t|
t.string "sunet_id", null: false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RSpec.describe BundleContext do
RSpec.describe BundleContextTemporary do
let(:revs_context) { context_from_proj(:proj_revs) }

describe "initialize() and other setup" do
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pre_assembly/bundle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
end

it "finds the correct N objects, stageables, and files" do
allow_any_instance_of(BundleContext).to receive(:validate_usage) # req'd for :sohp_files_and_folders
allow_any_instance_of(BundleContextTemporary).to receive(:validate_usage) # req'd for :sohp_files_and_folders
tests.each do |proj, n_dobj, n_stag, n_file|
b = described_class.new(context_from_proj(proj))
b.discover_objects
Expand Down
70 changes: 70 additions & 0 deletions spec/models/bundle_context_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require 'rails_helper'

RSpec.describe BundleContext, type: :model do
context "validation" do
let(:user) { User.new(sunet_id: "Jdoe")}
subject(:bc) { BundleContext.new(project_name: "SmokeTest",
content_structure: 1,
bundle_dir: "spec/test_data/bundle_input_g",
staging_style_symlink: false,
content_metadata_creation: 1,
user: user) }

it "is not valid unless it has all required attributes" do
expect(BundleContext.new).not_to be_valid
expect(bc).to be_valid
end

context "defines enum with expected values" do
it "content_structure enum" do
is_expected.to define_enum_for(:content_structure).with(
"simple_image_structure" => 0,
"simple_book_structure" => 1,
"book_as_iamge_structure" => 2,
"file_structure" => 3,
"smpl_structure" => 4
)
end

it "content_metadata_creation enum" do
is_expected.to define_enum_for(:content_metadata_creation).with(
"default_style" => 0,
"filename_style" => 1,
"smpl_style" => 2
)
end
end

describe "#content_structure=" do
it "validation rejects a value if it does not match the enum" do
expect { described_class.new(content_structure: 654) }
.to raise_error(ArgumentError, "'654' is not a valid content_structure")
expect { described_class.new(content_structure: 'book_as_pdf') }
.to raise_error(ArgumentError, "'book_as_pdf' is not a valid content_structure")
end

it "will accept a symbol, but will always return a string" do
expect(described_class.new(content_structure: :smpl_structure).content_structure).to eq 'smpl_structure'
end
end

describe "#content_metadata_creation=" do
it "validation rejects a value if it does not match the enum" do
expect { described_class.new(content_metadata_creation: 654) }
.to raise_error(ArgumentError, "'654' is not a valid content_metadata_creation")
expect { described_class.new(content_metadata_creation: 'dpg') }
.to raise_error(ArgumentError, "'dpg' is not a valid content_metadata_creation")
end

it "will accept a symbol, but will always return a string" do
expect(described_class.new(content_metadata_creation: :smpl_style).content_metadata_creation).to eq 'smpl_style'
end
end

it { is_expected.to validate_presence_of(:project_name) }
it { is_expected.to validate_presence_of(:content_structure) }
it { is_expected.to validate_presence_of(:bundle_dir) }
it { is_expected.to validate_presence_of(:content_metadata_creation) }
it { is_expected.to belong_to(:user) }
end
end
1 change: 1 addition & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

it { is_expected.to validate_uniqueness_of(:sunet_id) }
it { is_expected.to validate_presence_of(:sunet_id) }
it { is_expected.to have_many(:bundle_contexts) }

describe 'enforces unique constraint on sunet_id' do
let(:required_attributes) do
Expand Down
2 changes: 1 addition & 1 deletion spec/support/bundle_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ def context_from_proj(proj)
filename = "spec/test_data/project_config_files/#{proj}.yaml"
ps = YAML.load(File.read(filename))
ps['config_filename'] = filename
BundleContext.new(ps)
BundleContextTemporary.new(ps)
end

0 comments on commit 0735a76

Please sign in to comment.