Skip to content

Commit

Permalink
Merge pull request #98 from projecthydra/deprecate_class_multiple
Browse files Browse the repository at this point in the history
Add an instance method version of multiple?
  • Loading branch information
mjgiarlo committed Sep 30, 2015
2 parents 04f24d5 + 5deff2d commit 88f6a94
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 18 deletions.
10 changes: 9 additions & 1 deletion app/forms/hydra_editor/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ def validators_on(*attributes)
end
end

def multiple?(field)
if reflection = model_class.reflect_on_association(field)
reflection.collection?
else
model_class.multiple?(field)
end
end

# Return a hash of all the parameters from the form as a hash.
# This is typically used by the controller as the main read interface to the form.
# This hash can then be used to create or update an object in the data store.
Expand Down Expand Up @@ -90,7 +98,7 @@ def initialize_fields
# override this method if you need to initialize more complex RDF assertions (b-nodes)
def initialize_field(key)
# if value is empty, we create an one element array to loop over for output
if self.class.multiple?(key)
if multiple?(key)
self[key] = ['']
else
self[key] = ''
Expand Down
15 changes: 15 additions & 0 deletions app/presenters/hydra/presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ module ActiveModelPresenter
extend ActiveSupport::Concern
included do
attr_reader :model

# model_class only needs to be set if you are using the
# deprecated class methods multiple? or unique? or if you
# need to use +model_name+ method or if this class includes Hydra::Editor::Form.
class_attribute :model_class
end

Expand Down Expand Up @@ -51,8 +55,18 @@ def terms
self.class._terms
end

def multiple?(field)
if reflection = model.class.reflect_on_association(field)
reflection.collection?
else
model.class.multiple?(field)
end
end

module ClassMethods
# @deprecated Because if we use an instance method, there will be no need to set self.model_class in most instances. Note, there is a class method multiple? on the form.
def multiple?(field)
Deprecation.warn(ClassMethods, "The class method multiple? has been deprecated. Use the instance method instead. This will be removed in version 2.0")
if reflection = model_class.reflect_on_association(field)
reflection.collection?
else
Expand All @@ -61,6 +75,7 @@ def multiple?(field)
end

def unique?(field)
Deprecation.warn(ClassMethods, "The class method unique? has been deprecated. Use the instance method 'multiple?' instead. This will be removed in version 2.0")
if reflection = model_class.reflect_on_association(field)
!reflection.collection?
else
Expand Down
2 changes: 1 addition & 1 deletion app/views/records/edit_fields/_default.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% if f.object.class.multiple? key %>
<% if f.object.multiple? key %>
<%= f.input key, as: :multi_value, input_html: { class: 'form-control' }, required: f.object.required?(key) %>
<% else %>
<%= f.input key, required: f.object.required?(key) %>
Expand Down
58 changes: 43 additions & 15 deletions spec/presenters/hydra_editor_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,30 +68,58 @@ def count
end

describe "multiple?" do
subject { TestPresenter.multiple?(field) }
describe "instance method" do
subject { presenter.multiple?(field) }

context "for a multivalue string" do
let(:field) { :title }
it { is_expected.to be true }
end
context "for a multivalue string" do
let(:field) { :title }
it { is_expected.to be true }
end

context "for a single value string" do
let(:field) { :creator }
it { is_expected.to be false }
end
context "for a single value string" do
let(:field) { :creator }
it { is_expected.to be false }
end

context "for a multivalue association" do
let(:field) { :contributors }
it { is_expected.to be true }
context "for a multivalue association" do
let(:field) { :contributors }
it { is_expected.to be true }
end

context "for a single value association" do
let(:field) { :publisher }
it { is_expected.to be false }
end
end

context "for a single value association" do
let(:field) { :publisher }
it { is_expected.to be false }
describe "class method" do
before { allow(Deprecation).to receive(:warn) }
subject { TestPresenter.multiple?(field) }

context "for a multivalue string" do
let(:field) { :title }
it { is_expected.to be true }
end

context "for a single value string" do
let(:field) { :creator }
it { is_expected.to be false }
end

context "for a multivalue association" do
let(:field) { :contributors }
it { is_expected.to be true }
end

context "for a single value association" do
let(:field) { :publisher }
it { is_expected.to be false }
end
end
end

describe "unique?" do
before { allow(Deprecation).to receive(:warn) }
subject { TestPresenter.unique?(field) }

context "for a multivalue string" do
Expand Down
3 changes: 2 additions & 1 deletion spec/views/records/edit_fields/_default.html.erb_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require 'spec_helper'

describe 'records/edit_fields/_default' do
let(:form) { SimpleForm::FormBuilder.new(:foo, audio, view, {}) }
let(:form) { SimpleForm::FormBuilder.new(:foo, audio_form, view, {}) }
let(:audio_form) { AudioForm.new(audio) }
let(:audio) { Audio.new }

before do
Expand Down

0 comments on commit 88f6a94

Please sign in to comment.