|
| 1 | +Rails.application.config.after_initialize do |
| 2 | + |
| 3 | + AspaceFormHelper.class_eval do |
| 4 | + |
| 5 | + 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"] |
| 6 | + |
| 7 | + # This is a copy of read_only_view in the AspaceFormHelper |
| 8 | + # The only significant difference is that it adds a 'for' attribute to |
| 9 | + # the control-label so that the js can tell which field it belongs to |
| 10 | + # cut and paste coding - sorry, bad |
| 11 | + def awesomer_read_only_view(hash, opts = {}) |
| 12 | + jsonmodel_type = hash["jsonmodel_type"] |
| 13 | + schema = JSONModel(jsonmodel_type).schema |
| 14 | + prefix = opts[:plugin] ? 'plugins.' : '' |
| 15 | + html = "<div class='form-horizontal'>" |
| 16 | + |
| 17 | + hash.reject {|k,v| PROPERTIES_TO_EXCLUDE_FROM_READ_ONLY_VIEW.include?(k)}.each do |property, value| |
| 18 | + |
| 19 | + if schema and schema["properties"].has_key?(property) |
| 20 | + if (schema["properties"][property].has_key?('dynamic_enum')) |
| 21 | + value = I18n.t("#{prefix}enumerations.#{schema["properties"][property]["dynamic_enum"]}.#{value}", :default => value) |
| 22 | + elsif schema["properties"][property].has_key?("enum") |
| 23 | + value = I18n.t("#{prefix}#{jsonmodel_type.to_s}.#{property}_#{value}", :default => value) |
| 24 | + elsif schema["properties"][property]["type"] === "boolean" |
| 25 | + value = value === true ? "True" : "False" |
| 26 | + elsif schema["properties"][property]["type"] === "date" |
| 27 | + value = value.blank? ? "" : Date.strptime(value, "%Y-%m-%d") |
| 28 | + elsif schema["properties"][property]["type"] === "array" |
| 29 | + # this view doesn't support arrays |
| 30 | + next |
| 31 | + elsif value.kind_of? Hash |
| 32 | + # can't display an object either |
| 33 | + next |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + html << "<div class='form-group'>" |
| 38 | + 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>" |
| 39 | + html << "<div class='label-only col-md-9'>#{value}</div>" |
| 40 | + html << "</div>" |
| 41 | + |
| 42 | + end |
| 43 | + |
| 44 | + html << "</div>" |
| 45 | + |
| 46 | + html.html_safe |
| 47 | + end |
| 48 | + |
| 49 | + end |
| 50 | + |
| 51 | +end |
0 commit comments