Skip to content

Commit 515e520

Browse files
committed
Mark hacks
1 parent d0e2066 commit 515e520

6 files changed

Lines changed: 87 additions & 132 deletions

File tree

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ https://github.com/hudmol/user_defined_in_basic/releases
2727
Add an entry to your `config.rb` like this:
2828

2929
AppConfig[:user_defined_in_basic] = {
30-
'accession' => ['boolean_1', 'enum_2', 'real_2'],
31-
'digital_object' => [],
32-
'resource' => ['string_2', 'date_1', 'boolean_1']
30+
'accessions' => ['boolean_1', 'enum_2', 'real_2'],
31+
'digital_objects' => [],
32+
'resources' => ['string_2', 'date_1', 'boolean_1']
3333
}
3434

3535
If you don't have a `:user_defined_in_basic` entry the plugin won't do anything.
3636
It will log a warning at startup.
3737

38-
The three keys shown (`accession`, `resource`, and `digital_object`) are the
38+
The three keys shown (`accessions`, `resources`, and `digital_objects`) are the
3939
record types that can have a `user_defined` subrecord. For each key specified
4040
a `user_defined` subrecord will be automatically added when a new record
4141
is created through the staff UI, or will be added to an existing record when
4242
it is edited if it doesn't already have one. Also, the remove button is disabled.
4343

4444
So, in the example shown, no fields are moved to the `Basic Information` section
45-
for `digital_object` (because the field list is empty), but the presence of the
46-
`digital_object` key guarantees the addition of a `user_defined` subrecord. To
45+
for `digital_objects` (because the field list is empty), but the presence of the
46+
`digital_objects` key guarantees the addition of a `user_defined` subrecord. To
4747
disable this behavior, simply remove the key.
4848

4949
For each record type, specify the `user_defined` fields that you would like to
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function UserDefinedInBasic() {
2+
}
3+
4+
UserDefinedInBasic.prototype.init = function(record_type, fields, read_only_view) {
5+
var bi = $("#basic_information");
6+
7+
if (bi.length == 0) {
8+
// Section not visible yet (e.g. on a Resource edit page). We'll try
9+
// again when the event fires.
10+
return false;
11+
}
12+
13+
// add user defined if necessary, and disable remove button
14+
var user_defined_section = $('section.subrecord-form[data-object-name="user_defined"]');
15+
var remove_btn = user_defined_section.find('.subrecord-form-remove');
16+
if (remove_btn.length == 0) {
17+
user_defined_section.find('.btn-default').filter(':visible').click();
18+
remove_btn.attr('disabled', 'disabled');
19+
window.scrollTo(0,0);
20+
} else {
21+
remove_btn.attr('disabled', 'disabled');
22+
}
23+
24+
fields.map(function (field) {
25+
// Find our fields of interest by their label text
26+
var fld_lab = $('.control-label').filter(function() { return $(this).text() === field });
27+
28+
if (fld_lab.length > 0) {
29+
if (read_only_view) {
30+
var elt_to_move = fld_lab.addClass('col-sm-2').removeClass('col-md-3').parent();
31+
elt_to_move.insertBefore(bi.find("div.audit-display-wide"));
32+
} else {
33+
var elt_to_move = fld_lab.parent();
34+
if (bi.find("fieldset").length > 0) {
35+
elt_to_move.appendTo(bi.find("fieldset"));
36+
} else {
37+
elt_to_move.appendTo(bi);
38+
}
39+
}
40+
}
41+
});
42+
};

frontend/assets/user_defined_in_basic.js.erb

Lines changed: 0 additions & 61 deletions
This file was deleted.

frontend/plugin_init.rb

Lines changed: 6 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,74 +2,18 @@
22

33
# check configuration
44
if AppConfig.has_key?(:user_defined_in_basic)
5-
AppConfig[:user_defined_in_basic].keys.each do |k|
6-
if ['accession', 'resource', 'digital_object'].include?(k)
7-
AppConfig[:user_defined_in_basic][k].each do |fld|
8-
unless JSONModel(:user_defined).schema['properties'].include?(fld)
9-
$stderr.puts "WARNING: user_defined_in_basic plugin configuration includes " +
10-
"a field (#{fld}) in the list for #{k} which is not a user_defined field. " +
11-
"That's ok, we're just concerned you might have intended to refer to an actual field."
12-
end
5+
AppConfig[:user_defined_in_basic].each do |keys, fields|
6+
fields.each do |field|
7+
unless JSONModel(:user_defined).schema['properties'].include?(field)
8+
$stderr.puts "WARNING: user_defined_in_basic plugin configuration includes " +
9+
"a field (#{fld}) in the list for #{k} which is not a user_defined field. " +
10+
"That's ok, we're just concerned you might have intended to refer to an actual field."
1311
end
14-
else
15-
$stderr.puts "WARNING: user_defined_in_basic plugin configuration includes an unexpected key: #{k}. " +
16-
"That's ok, it just occurred to us that it might be a typo. " +
17-
"Supported keys are: accession, resource, and digital_object"
1812
end
1913
end
2014
else
2115
$stderr.puts "WARNING: user_defined_in_basic plugin is active but not configured. " +
2216
"That's ok, it just won't do anything."
2317
end
2418

25-
26-
27-
AspaceFormHelper.class_eval do
28-
29-
PROPERTIES_TO_EXCLUDE_FROM_READ_ONLY_VIEW = ["jsonmodel_type", "lock_version", "_resolved", "uri", "ref", "create_time", "system_mtime", "user_mtime", "created_by", "last_modified_by", "sort_name_auto_generate", "suppressed", "display_string", "file_uri"]
30-
31-
# This is a copy of read_only_view in the AspaceFormHelper
32-
# The only significant difference is that it adds a 'for' attribute to
33-
# the control-label so that the js can tell which field it belongs to
34-
# cut and paste coding - sorry, bad
35-
def awesomer_read_only_view(hash, opts = {})
36-
jsonmodel_type = hash["jsonmodel_type"]
37-
schema = JSONModel(jsonmodel_type).schema
38-
prefix = opts[:plugin] ? 'plugins.' : ''
39-
html = "<div class='form-horizontal'>"
40-
41-
hash.reject {|k,v| PROPERTIES_TO_EXCLUDE_FROM_READ_ONLY_VIEW.include?(k)}.each do |property, value|
42-
43-
if schema and schema["properties"].has_key?(property)
44-
if (schema["properties"][property].has_key?('dynamic_enum'))
45-
value = I18n.t("#{prefix}enumerations.#{schema["properties"][property]["dynamic_enum"]}.#{value}", :default => value)
46-
elsif schema["properties"][property].has_key?("enum")
47-
value = I18n.t("#{prefix}#{jsonmodel_type.to_s}.#{property}_#{value}", :default => value)
48-
elsif schema["properties"][property]["type"] === "boolean"
49-
value = value === true ? "True" : "False"
50-
elsif schema["properties"][property]["type"] === "date"
51-
value = value.blank? ? "" : Date.strptime(value, "%Y-%m-%d")
52-
elsif schema["properties"][property]["type"] === "array"
53-
# this view doesn't support arrays
54-
next
55-
elsif value.kind_of? Hash
56-
# can't display an object either
57-
next
58-
end
59-
end
60-
61-
html << "<div class='form-group'>"
62-
html << "<div class='control-label col-sm-2' for='#{opts['parent']}_#{jsonmodel_type.to_s}__#{property}_'>#{I18n.t("#{prefix}#{jsonmodel_type.to_s}.#{property}")}</div>"
63-
html << "<div class='label-only col-md-9'>#{value}</div>"
64-
html << "</div>"
65-
66-
end
67-
68-
html << "</div>"
69-
70-
html.html_safe
71-
end
72-
73-
end
74-
7519
end
Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
<%
2+
3+
def map_user_defined_to_labels(properties)
4+
properties.map {|property| I18n.t("user_defined.#{property}")}
5+
end
6+
7+
%>
8+
19
<% if AppConfig.has_key?(:user_defined_in_basic) %>
2-
<%= javascript_include_tag "#{AppConfig[:frontend_prefix]}assets/user_defined_in_basic.js" %>
10+
<%= javascript_include_tag "#{AppConfig[:frontend_prefix]}assets/user_defined_in_basic.js" %>
11+
12+
<script>
13+
var record_type = '<%= controller.controller_name %>';
14+
var fields = <%= ASUtils.to_json(map_user_defined_to_labels(AppConfig[:user_defined_in_basic].fetch(controller.controller_name, []))).html_safe %>;
15+
var read_only_view = <%= controller.action_name == 'show' %>;
16+
17+
var init = function () {
18+
setTimeout(function () {
19+
new UserDefinedInBasic().init(record_type, fields, read_only_view);
20+
}, 0);
21+
}
22+
23+
// For Resource record updates
24+
$(document).bind("loadedrecordform.aspace", init);
25+
26+
// For everything else
27+
init();
28+
29+
</script>
30+
331
<% end %>
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
<%
22
section_id = "user_defined" if section_id.blank?
3-
3+
if !defined?(context) || !context
4+
context = {}
5+
end
46
%>
57

68
<section id="<%= section_id %>" class="subrecord-form-dummy">
79
<h3><%= I18n.t("user_defined._singular") %></h3>
810

911
<div class="subrecord-form-container">
1012
<div class="subrecord-form-fields">
11-
<%= awesomer_read_only_view(user_defined, 'parent' => context['jsonmodel_type']) %>
13+
<%= read_only_view(user_defined, 'parent' => context['jsonmodel_type']) %>
1214
</div>
1315
</div>
1416
</section>

0 commit comments

Comments
 (0)