Skip to content

Commit

Permalink
Update to avoid class-level ingest form vars
Browse files Browse the repository at this point in the history
* Uses metadata-ingest-form 2.0.0
* Fixes all class variable use for new API
* Fixes controller spec to avoid over-stubbing of mostly static data
  • Loading branch information
jechols committed Jan 19, 2014
1 parent c74ee96 commit 52ce7d8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 29 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
GIT
remote: git://github.com/OregonDigital/metadata-ingest-form.git
revision: 87573b7bdb39022173913d9f91946be96aa5e722
revision: 4351ca26da2d34ca3f26670e5bfd5ab197425f6a
specs:
metadata-ingest-form (1.2.1)
metadata-ingest-form (2.0.0)
activemodel (>= 3.0)

GIT
Expand Down
15 changes: 4 additions & 11 deletions app/controllers/ingest_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
require 'metadata/ingest/translators/attributes_to_form'

class IngestController < ApplicationController
before_filter :setup_ingest_map
before_filter :build_controlled_vocabulary_map
before_filter :setup_resources, except: :index

Expand All @@ -11,7 +10,7 @@ def index

# Combined new/edit form handler
def form
for group in Metadata::Ingest::Form.groups
for group in @form.groups
if @form.send(group.pluralize).empty?
@form.send("build_#{group}")
end
Expand All @@ -21,7 +20,7 @@ def form
# Combined create/update form handler
def save
@form.attributes = params[:metadata_ingest_form].to_hash
Metadata::Ingest::Translators::FormToAttributes.from(@form).to(@asset)
Metadata::Ingest::Translators::FormToAttributes.from(@form).using_map(ingest_map).to(@asset)
@asset.save

redirect_to :ingest, :notice => "Ingested new object"
Expand All @@ -36,25 +35,19 @@ def ingest_map
return INGEST_MAP
end

# Sets up the form to use the chosen ingest map
def setup_ingest_map
Metadata::Ingest::Form.internal_groups = ingest_map.keys.collect {|key| key.to_s}
Metadata::Ingest::Translators::FormToAttributes.map = ingest_map
Metadata::Ingest::Translators::AttributesToForm.map = ingest_map
end

# Builds asset and form objects based on incoming data. If an id is not
# passed in, @asset will be a new GenericAsset, and @form will be an empty
# ingest form. If an id is passed in, @asset will represent the asset for
# the given id, and @form will use a translator to pull attributes from the
# asset and put them into the ingest form.
def setup_resources
@form = Metadata::Ingest::Form.new
@form.internal_groups = ingest_map.keys.collect {|key| key.to_s}
@asset = GenericAsset.new
return unless params[:id]

@asset = GenericAsset.find(params[:id])
Metadata::Ingest::Translators::AttributesToForm.from(@asset).to(@form)
Metadata::Ingest::Translators::AttributesToForm.from(@asset).using_map(ingest_map).to(@form)
end

# Iterates over the ingest map, and looks up properties in the datastream
Expand Down
2 changes: 1 addition & 1 deletion app/views/ingest/_internal_group_fields.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% for group in Metadata::Ingest::Form.internal_groups %>
<% for group in @form.internal_groups %>
<div class="multi-fields well">
<h3 class="inverse"><%= I18n.t("ingest_form.#{group}.group_header", :default => group.humanize.pluralize) %></h3>

Expand Down
28 changes: 13 additions & 15 deletions spec/controllers/ingest_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
require 'spec_helper'

# NOTE: This is slightly fragile now in order to simplify testing. If the
# ingest map changes for some reason, the tests may need to be updated to
# either stub the map as a whole or else to traverse the real map instead of
# hard-code attributes.
describe IngestController do
describe "#index" do
# Index doesn't do much yet, so this is just a quick smoke test to verify nothing crashes
Expand All @@ -16,34 +20,28 @@
end

context "for a new asset" do
before(:each) do
# Hack groups to just two simple values
Metadata::Ingest::Form.stub(:groups).and_return(["foo", "bar"])

# Make sure we don't typo a method, since that's a false positive
@build_foo = :build_foo
@build_bar = :build_bar
end

it "should assign a form variable" do
get :form
expect(assigns(:form)).to be_kind_of(Metadata::Ingest::Form)
end

it "should build an empty association for each group" do
expect(@form).to receive(@build_foo).once
expect(@form).to receive(@build_bar).once
for group in @form.groups
expect(@form).to receive(:"build_#{group}").once
end
get :form
end

it "shouldn't build empty associations for groups which have data" do
# Add a single empty object to each group
@form.build_foo
@form.build_bar
for group in @form.groups
@form._build_group(group, {})
end

# Neither builder should be called
expect(@form).not_to receive(@build_foo)
expect(@form).not_to receive(@build_bar)
for group in @form.groups
expect(@form).not_to receive(:"build_#{group}")
end
get :form
end
end
Expand Down

0 comments on commit 52ce7d8

Please sign in to comment.