Skip to content

Commit

Permalink
Merge pull request #220 from OregonDigital/edit_labels
Browse files Browse the repository at this point in the history
Edit labels for facets
  • Loading branch information
wickr committed Jul 23, 2015
2 parents aeed62d + 1b84241 commit dbafb11
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ gem 'openseadragon'

gem 'fedora-migrate'

gem 'best_in_place'

group :development, :test do
# Access an IRB console on exception pages or by using <%= console %> in views
gem 'web-console', '~> 2.0'
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ GEM
bcp47 (0.3.3)
i18n
bcrypt (3.1.10)
best_in_place (3.0.3)
actionpack (>= 3.2)
railties (>= 3.2)
binding_of_caller (0.7.2)
debug_inspector (>= 0.0.1)
blacklight (5.13.1)
Expand Down Expand Up @@ -577,6 +580,7 @@ DEPENDENCIES
active-fedora!
active_fedora-noid
attr_extras
best_in_place
blacklight
blacklight-gallery
blacklight_advanced_search
Expand Down
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
// Required by Blacklight
//= require blacklight/blacklight
//= require hydra-editor/hydra-editor
//= require best_in_place
//= require_tree .
4 changes: 4 additions & 0 deletions app/assets/javascripts/enable_best_in_place.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$(document).ready(function() {
/* Activating Best In Place */
jQuery(".best_in_place").best_in_place();
});
16 changes: 15 additions & 1 deletion app/controllers/admin/facets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ def create
end
end

def update
facet = find_facet(params[:id])
respond_to do |format|
if facet.update(facet_field_params)
format.html { redirect_to admin_facets_path }
format.json { respond_with_bip(facet) }
else
flash[:alert] = t('admin.facets.update.fail')
format.html { redirect_to admin_facets_path }
format.json { respond_with_bip(facet) }
end
end
end

def destroy
begin
if find_facet(params[:id]).destroy
Expand All @@ -28,7 +42,7 @@ def find_facet(id)
end

def facet_field_params
params.require(:facet_field).permit(:key)
params.require(:facet_field).permit(:key, :label)
end

end
2 changes: 1 addition & 1 deletion app/facades/facet_configuration_facade.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def all_solr_fields

def solr_field_iterator
@solr_field_iterator ||= iterator_factory.new(all_solr_fields).map do |k, v|
[k, HasFacetField.new(v, grouped_facets[k].try(:first) || FacetField.new)]
[k, HasFacetField.new(v, grouped_facets[k].try(:first) || FacetField.new(:key => k))]
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/blacklight_config/facet_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class FacetFields

def run
facet_fields.each do |field|
configuration.add_facet_field ActiveFedora::SolrQueryBuilder.solr_name(field.key, :symbol), :label => field.key.titleize
configuration.add_facet_field ActiveFedora::SolrQueryBuilder.solr_name(field.key, :symbol), :label => field.view_label
end
configuration.add_facet_fields_to_solr_request!
end
Expand Down
8 changes: 8 additions & 0 deletions app/models/facet_field.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
class FacetField < ActiveRecord::Base
validates :key, :presence => true

def view_label
if label.present?
label
else
key.titleize
end
end
end
2 changes: 1 addition & 1 deletion app/views/admin/facets/_field.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="panel panel-default">
<div class="panel-heading">
<%= property.to_s.titleize %>
<%= best_in_place_if field.facet.persisted?, field.facet, :label, :url => admin_facet_path(:id => field.facet.id || "0", :format => :json), :display_as => :view_label %>
<% unless field.facet.persisted? %>
<%= simple_form_for field.facet, :url => admin_facets_path(field.facet) do |f| %>
<%= f.input :key, :as => :hidden, :input_html => { :value => property.to_s} %>
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ en:
admin:
facets:
field_created: "Facet field configured"
update:
fail: 'Failed to update facet'
destroy:
fail: "Failed to delete facet"
success: "Deleted facet"
5 changes: 5 additions & 0 deletions db/migrate/20150723222437_add_label_to_facet_fields.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddLabelToFacetFields < ActiveRecord::Migration
def change
add_column :facet_fields, :label, :string
end
end
11 changes: 10 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150702213649) do
ActiveRecord::Schema.define(version: 20150723222437) do

create_table "bookmarks", force: :cascade do |t|
t.integer "user_id", null: false
Expand Down Expand Up @@ -49,10 +49,19 @@
t.string "key"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "label"
end

add_index "facet_fields", ["key"], name: "index_facet_fields_on_key", unique: true

create_table "ld_path_configurations", force: :cascade do |t|
t.string "name"
t.text "path"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "roles", force: :cascade do |t|
t.string "name"
end
Expand Down
10 changes: 10 additions & 0 deletions spec/controllers/admin/facets_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@
end
end

describe "#update" do
it "should be able to update a facet" do
facet = FacetField.create(:key => "test", :label => "yo")

put :update, :id => facet.id, :facet_field => {:label => "test"}

expect(facet.reload.label).to eq "test"
end
end

describe "#destroy" do
context "when the field doesn't exist" do
it "should redirect and show a flash" do
Expand Down
3 changes: 3 additions & 0 deletions spec/models/blacklight_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,14 @@

it "should have facets for all the facet fields" do
FacetField.create(:key => "title")
FacetField.create(:key => "test", :label => "Power Rangers")

keys = subject.facet_fields.values.map(&:key)
labels = subject.facet_fields.values.map(&:label)
expect(keys).to include "title_ssim"
expect(labels).to include "Title"
expect(keys).to include "test_ssim"
expect(labels).to include "Power Rangers"
end

it "should have a tool config for edit" do
Expand Down
16 changes: 16 additions & 0 deletions spec/models/facet_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,20 @@
describe "validations" do
it { should validate_presence_of :key }
end

describe "#view_label" do
subject { described_class.new(:key => "awesome_title", :label => label) }
context "when there's no label" do
let(:label) {}
it "should be a titleized key" do
expect(subject.view_label).to eq "Awesome Title"
end
end
context "when there's a label" do
let(:label) { "Testing" }
it "it should be the label" do
expect(subject.view_label).to eq "Testing"
end
end
end
end

0 comments on commit dbafb11

Please sign in to comment.