From 8932bf118f2e043dc77a189c66ee5b9bb5a2d718 Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Wed, 27 Aug 2014 10:44:51 -0500 Subject: [PATCH] Pass the proper arguments to boostrap_form_tag. Fixes #39 --- .../concerns/records_controller_behavior.rb | 7 +++-- .../concerns/records_helper_behavior.rb | 2 +- app/views/records/choose_type.html.erb | 9 +++--- config/locales/hydra_editor.yml | 3 +- spec/features/create_record_spec.rb | 28 +++++++++++++++++ spec/features/record_editing_spec.rb | 30 +++++++++---------- 6 files changed, 54 insertions(+), 25 deletions(-) create mode 100644 spec/features/create_record_spec.rb diff --git a/app/controllers/concerns/records_controller_behavior.rb b/app/controllers/concerns/records_controller_behavior.rb index 8796d4a..14fd9fa 100644 --- a/app/controllers/concerns/records_controller_behavior.rb +++ b/app/controllers/concerns/records_controller_behavior.rb @@ -59,13 +59,14 @@ def update protected - def object_as_json + def object_as_json # ActiveFedora::Base#to_json causes a circular reference (before 7.0). Do something easy - resource.terms_for_editing.each_with_object({}) { |term, h| h[term] = resource[term] } + resource.terms_for_editing.each_with_object({}) { |term, h| h[term] = resource[term] } end # Override this method if you want to set different metadata on the object def set_attributes + puts "setting #{collect_form_attributes}" resource.attributes = collect_form_attributes end @@ -93,7 +94,7 @@ def has_valid_type? def initialize_fields resource.terms_for_editing.each do |key| - # if value is empty, we create an one element array to loop over for output + # if value is empty, we create an one element array to loop over for output resource[key] = [''] if resource[key].empty? end end diff --git a/app/helpers/concerns/records_helper_behavior.rb b/app/helpers/concerns/records_helper_behavior.rb index 692c1c0..2e8ea7a 100644 --- a/app/helpers/concerns/records_helper_behavior.rb +++ b/app/helpers/concerns/records_helper_behavior.rb @@ -1,7 +1,7 @@ require 'deprecation' module RecordsHelperBehavior extend Deprecation - + def metadata_help(key) I18n.t("hydra_editor.form.metadata_help.#{key}", default: key.to_s.humanize) end diff --git a/app/views/records/choose_type.html.erb b/app/views/records/choose_type.html.erb index d9c1cec..289b06a 100644 --- a/app/views/records/choose_type.html.erb +++ b/app/views/records/choose_type.html.erb @@ -1,7 +1,6 @@

<%= t('hydra_editor.choose_type.title') %>

-<%= bootstrap_form_tag hydra_editor.new_record_path, :method => :get do |f| %> - <%= bootstrap_select_tag :type, options_for_select(object_type_options) %> - <%= bootstrap_actions do %> - <%= bootstrap_submit_tag 'Next' %> - <% end %> +<%= bootstrap_form_tag url: hydra_editor.new_record_path, method: :get do |f| %> + + <%= select_tag :type, options_for_select(object_type_options) %> + <%= f.primary t('hydra_editor.choose_type.next') %> <% end %> diff --git a/config/locales/hydra_editor.yml b/config/locales/hydra_editor.yml index 9b1593d..518b3d6 100644 --- a/config/locales/hydra_editor.yml +++ b/config/locales/hydra_editor.yml @@ -2,10 +2,11 @@ en: hydra_editor: choose_type: title: 'Create a New Record' + next: 'Next' edit: title: 'Edit %s' form: title: 'Descriptions' required_fields: 'indicates required fields' new: - title: 'Create a New %s Record' \ No newline at end of file + title: 'Create a New %s Record' diff --git a/spec/features/create_record_spec.rb b/spec/features/create_record_spec.rb new file mode 100644 index 0000000..027c23a --- /dev/null +++ b/spec/features/create_record_spec.rb @@ -0,0 +1,28 @@ +require 'spec_helper' + +feature 'User creates an object' do + let(:user) { FactoryGirl.create(:user) } + + before do + HydraEditor.models = ['Audio'] + login_as user + # since we're stubbing save, we won't have an id to redirect to. + allow_any_instance_of(RecordsController).to receive(:redirect_after_create).and_return("/404.html") + end + + scenario 'with a TuftsAudio' do + visit '/records/new' + + select "Audio", from: 'Select an object type' + click_button 'Next' + + fill_in '*Title', with: 'My title' + + expect_any_instance_of(Audio).to receive(:attributes=).with({}) # called when initializing a new object + expect_any_instance_of(Audio).to receive(:attributes=).with(title: ["My title"]) + # Avoid the catalog so we don't have to run Solr + expect_any_instance_of(Audio).to receive(:save).and_return(true) + click_button 'Save' + end + +end diff --git a/spec/features/record_editing_spec.rb b/spec/features/record_editing_spec.rb index 007268d..4b4cf40 100644 --- a/spec/features/record_editing_spec.rb +++ b/spec/features/record_editing_spec.rb @@ -1,33 +1,33 @@ require 'spec_helper' describe "record editing" do + let(:user) { FactoryGirl.create(:user) } + let(:record) { Audio.new(pid: "foo:1", title: "Cool Track") } + # We need a clone to give to the edit view b/c it gets changed by initialize_fields + let(:record_clone) { Audio.new(pid: "foo:1", title: "Cool Track") } + before do HydraEditor.models = ['Audio'] - @user = FactoryGirl.create(:user) - @ability = double(Ability) - @ability.stub(:authorize!).and_return(true) - Ability.stub(:new).with(@user).and_return(@ability) + allow_any_instance_of(Ability).to receive(:authorize!).and_return(true) # Avoid the catalog so we don't have to run Solr allow_any_instance_of(RecordsController).to receive(:redirect_after_update).and_return("/404.html") - allow_any_instance_of(Audio).to receive(:persisted?).and_return(true) allow_any_instance_of(Audio).to receive(:new_record?).and_return(false) allow_any_instance_of(Audio).to receive(:save).and_return(true) - @record = Audio.new(pid: "foo:1", title: "Cool Track") - # We need a clone to give to the edit view b/c it gets changed by initialize_fields - @record_clone = Audio.new(pid: "foo:1", title: "Cool Track") + # We use the original record for the update view to start clean and apply the form data - ActiveFedora::Base.should_receive(:find).with(@record.pid, cast: true).and_return(@record_clone, @record) - login_as @user + expect(ActiveFedora::Base).to receive(:find).with(record.pid, cast: true).and_return(record_clone, record) + login_as user end + after do Warden.test_reset! end it "should be idempotent" do - visit "/records/#{@record.pid}/edit" + visit "/records/#{record.pid}/edit" click_button 'Save' - @record.title.should == ["Cool Track"] - @record.creator.should == [] - @record.description.should == [] - @record.subject.should == [] + expect(record.title).to eq ["Cool Track"] + expect(record.creator).to eq [] + expect(record.description).to eq [] + expect(record.subject).to eq [] end end