Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a permission form that supports destroy and id. Fixes #67 #68

Merged
merged 1 commit into from
Jan 20, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/forms/hydra_editor/form.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -50,7 +53,6 @@ def build_permitted_params
permitted << term
end
end
permitted << { permissions_attributes: [:type, :name, :access] }
permitted
end
end
Expand Down
23 changes: 23 additions & 0 deletions app/forms/hydra_editor/form/permissions.rb
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions spec/forms/hydra_editor_form_permissions_spec.rb
Original file line number Diff line number Diff line change
@@ -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' }}) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you setting description when you do not check for it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're checking that it's removed, because its not an authorized attribute.

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
7 changes: 7 additions & 0 deletions spec/forms/hydra_editor_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again on description setting here...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checking it's removed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Now I understand!


it { is_expected.to eq('creator' => 'bob', 'title' => []) }
end
end

let(:object) { TestModel.new(title: ['foo', 'bar'], creator: 'baz') }
Expand Down