From e0ea6386dce7e8ee2f1705a4a8c47820670342a1 Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Mon, 19 Jan 2015 23:43:52 -0600 Subject: [PATCH] Added a permission form that supports destroy and id. Fixes #67 --- app/forms/hydra_editor/form.rb | 4 ++- app/forms/hydra_editor/form/permissions.rb | 23 ++++++++++++++ .../hydra_editor_form_permissions_spec.rb | 31 +++++++++++++++++++ spec/forms/hydra_editor_form_spec.rb | 7 +++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 app/forms/hydra_editor/form/permissions.rb create mode 100644 spec/forms/hydra_editor_form_permissions_spec.rb diff --git a/app/forms/hydra_editor/form.rb b/app/forms/hydra_editor/form.rb index eabd9fa..888c54b 100644 --- a/app/forms/hydra_editor/form.rb +++ b/app/forms/hydra_editor/form.rb @@ -1,6 +1,9 @@ module HydraEditor module Form + extend ActiveSupport::Autoload + autoload :Permissions extend ActiveSupport::Concern + include Hydra::Presenter included do class_attribute :required_fields @@ -50,7 +53,6 @@ def build_permitted_params permitted << term end end - permitted << { permissions_attributes: [:type, :name, :access] } permitted end end diff --git a/app/forms/hydra_editor/form/permissions.rb b/app/forms/hydra_editor/form/permissions.rb new file mode 100644 index 0000000..715aab3 --- /dev/null +++ b/app/forms/hydra_editor/form/permissions.rb @@ -0,0 +1,23 @@ +module HydraEditor + module Form + module Permissions + extend ActiveSupport::Concern + + module ClassMethods + + def build_permitted_params + permitted = super + permitted << { permissions_attributes: [:type, :name, :access, :id, :_destroy] } + permitted + end + end + + # This is required so that fields_for will draw a nested form. + # See ActionView::Helpers#nested_attributes_association? + # https://github.com/rails/rails/blob/a04c0619617118433db6e01b67d5d082eaaa0189/actionview/lib/action_view/helpers/form_helper.rb#L1890 + def permissions_attributes= attributes + model.permissions_attributes= attributes + end + end + end +end diff --git a/spec/forms/hydra_editor_form_permissions_spec.rb b/spec/forms/hydra_editor_form_permissions_spec.rb new file mode 100644 index 0000000..f5d1bfc --- /dev/null +++ b/spec/forms/hydra_editor_form_permissions_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe HydraEditor::Form::Permissions do + class TestModel < ActiveFedora::Base + property :title, predicate: ::RDF::DC.title + property :creator, predicate: ::RDF::DC.creator, multiple: false + end + + class TestForm + include HydraEditor::Form + include HydraEditor::Form::Permissions + self.model_class = TestModel + # Terms is the list of fields displayed by app/views/records/_form.html.erb + self.terms = [:title, :creator] + end + + describe "model_attributes" do + let(:params) { ActionController::Parameters.new(title: [''], creator: 'bob', description: ['huh'], permissions_attributes: {'0' => { id: '123', _destroy: 'true' }}) } + subject { TestForm.model_attributes(params) } + + it { is_expected.to eq('creator' => 'bob', 'title' => [], + 'permissions_attributes' => { '0' => { 'id' => '123', '_destroy' => 'true' } }) } + end + + describe "permissions_attributes=" do + subject { TestForm.new(TestModel.new) } + it "should respond to permissions_attributes=" do + expect(subject).to respond_to(:permissions_attributes=) + end + end +end diff --git a/spec/forms/hydra_editor_form_spec.rb b/spec/forms/hydra_editor_form_spec.rb index 07064db..df542da 100644 --- a/spec/forms/hydra_editor_form_spec.rb +++ b/spec/forms/hydra_editor_form_spec.rb @@ -16,6 +16,13 @@ class TestForm describe "class methods" do subject { TestForm.model_name } it { is_expected.to eq 'TestModel' } + + describe "model_attributes" do + let(:params) { ActionController::Parameters.new(title: [''], creator: 'bob', description: ['huh']) } + subject { TestForm.model_attributes(params) } + + it { is_expected.to eq('creator' => 'bob', 'title' => []) } + end end let(:object) { TestModel.new(title: ['foo', 'bar'], creator: 'baz') }