Skip to content

Commit

Permalink
Add immutable configuration for metadata display fields
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Feb 4, 2017
1 parent 31ee1ec commit 827a7e0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 7 deletions.
7 changes: 7 additions & 0 deletions app/models/spotlight/blacklight_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ def blacklight_config
else
set_index_field_defaults(v)
end

v.immutable = Blacklight::OpenStructWithHashAccess.new(v.immutable)
v.merge! v.immutable.to_h.symbolize_keys

v.upstream_if = v.if unless v.if.nil?
v.if = :field_enabled? unless v.if == false

Expand All @@ -114,6 +118,9 @@ def blacklight_config
set_show_field_defaults(v)
end

v.immutable = Blacklight::OpenStructWithHashAccess.new(v.immutable)
v.merge! v.immutable.to_h.symbolize_keys

v.upstream_if = v.if unless v.if.nil?
v.if = :field_enabled? unless v.if == false

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
<%= field.hidden_field :label, value: index_field_label(nil, key), class: 'form-control input-sm', data: {:"edit-field-target" => 'true'} %>
</div>
</td>
<td class="checkbox-cell text-center"><%= field.check_box :show, inline: true, checked: config.show, label: "" %></td>
<td class="checkbox-cell text-center"><%= field.check_box :show, inline: true, checked: config.show, disabled: !config.immutable.show.nil?, label: "" %></td>
<% available_view_fields.keys.each do |type| %>
<td class="checkbox-cell text-center"><%= field.check_box type, inline: true, checked: config.send(type), label: "" %></td>
<td class="checkbox-cell text-center"><%= field.check_box type, inline: true, checked: config.send(type), disabled: !config.immutable.send(type).nil?, label: "" %></td>
<% end %>
<% end %>
</tr>
</tr>
11 changes: 9 additions & 2 deletions spec/models/spotlight/blacklight_configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,17 @@
expect(subject.blacklight_config.index_fields['a'].list).to be_truthy
end

it 'applies immutable values from the configuration' do
blacklight_config.add_index_field 'a', immutable: { something: 'set_in_configuration', another: 'immutable' }
subject.index_fields['a'] = { something: 'set_in_db' }
expect(subject.blacklight_config.index_fields['a'].something).to eq 'set_in_configuration'
expect(subject.blacklight_config.index_fields['a'].another).to eq 'immutable'
end

context 'custom fields' do
it 'includes any custom fields' do
subject.index_fields['a'] = { enabled: true, list: true }
allow(subject).to receive_messages(custom_index_fields: { 'a' => double(if: nil, :if= => true, merge!: true, validate!: true, normalize!: true) })
allow(subject).to receive_messages(custom_index_fields: { 'a' => Blacklight::Configuration::IndexField.new(field: 'a') })
expect(subject.blacklight_config.index_fields).to include('a')
end

Expand Down Expand Up @@ -249,7 +256,7 @@
it 'includes any custom fields' do
subject.index_fields['a'] = { enabled: true, show: true }

allow(subject).to receive_messages(custom_index_fields: { 'a' => double(if: nil, :if= => true, merge!: true, validate!: true, normalize!: true) })
allow(subject).to receive_messages(custom_index_fields: { 'a' => Blacklight::Configuration::IndexField.new(field: 'a') })

expect(subject.blacklight_config.show_fields).to include('a')
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@
)
end

let(:facet_field) { Blacklight::Configuration::FacetField.new }
let(:field) { Blacklight::Configuration::Field.new immutable: OpenStruct.new(another_view_type: false) }
let(:builder) { ActionView::Helpers::FormBuilder.new 'z', nil, view, {} }

it 'uses the index_field_label helper to render the label' do
allow(view).to receive(:index_field_label).with(nil, 'some_key').and_return 'Some label'
render partial: p, locals: { key: 'some_key', config: facet_field, f: builder }
render partial: p, locals: { key: 'some_key', config: field, f: builder }
expect(rendered).to have_selector '.field-label', text: 'Some label'
end

it 'marks views as disabled if they are immutable' do
render partial: p, locals: { key: 'some_key', config: field, f: builder }
expect(rendered).to have_selector 'input[disabled][name="z[some_key][another_view_type]"]'
end
end

0 comments on commit 827a7e0

Please sign in to comment.