From ea878bfae6e1c1da48fea4d025a89f4388f53643 Mon Sep 17 00:00:00 2001 From: Todd Eichel Date: Tue, 3 Feb 2015 23:55:25 -0800 Subject: [PATCH 1/2] Add Upmin::AttributeValue class so that we can render values for display using different partials than the normal attribute partial (which usually has form elements). --- .../attribute_values/_boolean.html.haml | 1 + .../partials/attribute_values/_enum.html.haml | 1 + .../attribute_values/_unknown.html.haml | 1 + lib/upmin/admin.rb | 1 + lib/upmin/attribute.rb | 6 ++++ lib/upmin/attribute_value.rb | 10 ++++++ lib/upmin/railties/render.rb | 4 +++ lib/upmin/railties/render_helpers.rb | 35 +++++++++++++++++++ 8 files changed, 59 insertions(+) create mode 100644 app/views/upmin/partials/attribute_values/_boolean.html.haml create mode 100644 app/views/upmin/partials/attribute_values/_enum.html.haml create mode 100644 app/views/upmin/partials/attribute_values/_unknown.html.haml create mode 100644 lib/upmin/attribute_value.rb diff --git a/app/views/upmin/partials/attribute_values/_boolean.html.haml b/app/views/upmin/partials/attribute_values/_boolean.html.haml new file mode 100644 index 0000000..7393aa0 --- /dev/null +++ b/app/views/upmin/partials/attribute_values/_boolean.html.haml @@ -0,0 +1 @@ +%span.badge= attribute.value ? 'Yes' : 'No' diff --git a/app/views/upmin/partials/attribute_values/_enum.html.haml b/app/views/upmin/partials/attribute_values/_enum.html.haml new file mode 100644 index 0000000..3c5abe5 --- /dev/null +++ b/app/views/upmin/partials/attribute_values/_enum.html.haml @@ -0,0 +1 @@ +%span.badge= attribute.value diff --git a/app/views/upmin/partials/attribute_values/_unknown.html.haml b/app/views/upmin/partials/attribute_values/_unknown.html.haml new file mode 100644 index 0000000..78eaffd --- /dev/null +++ b/app/views/upmin/partials/attribute_values/_unknown.html.haml @@ -0,0 +1 @@ += attribute.value diff --git a/lib/upmin/admin.rb b/lib/upmin/admin.rb index e90867c..59ab39d 100644 --- a/lib/upmin/admin.rb +++ b/lib/upmin/admin.rb @@ -8,6 +8,7 @@ require "upmin/model" require "upmin/attribute" +require "upmin/attribute_value" require "upmin/association" require "upmin/action" require "upmin/parameter" diff --git a/lib/upmin/attribute.rb b/lib/upmin/attribute.rb index 5d68001..9fbdf68 100644 --- a/lib/upmin/attribute.rb +++ b/lib/upmin/attribute.rb @@ -13,6 +13,12 @@ def value return model.model.send(name) end + def display_value + return @display_value if defined?(@display_value) + @display_value = Upmin::AttributeValue.new(self) + return @display_value + end + def type # TODO(jon): Add a way to override with widgets? return @type if defined?(@type) diff --git a/lib/upmin/attribute_value.rb b/lib/upmin/attribute_value.rb new file mode 100644 index 0000000..43a3583 --- /dev/null +++ b/lib/upmin/attribute_value.rb @@ -0,0 +1,10 @@ +module Upmin + class AttributeValue + attr_reader :attribute + + def initialize(attribute, options = {}) + @attribute = attribute + end + + end +end diff --git a/lib/upmin/railties/render.rb b/lib/upmin/railties/render.rb index 2ef8832..b1bcee3 100644 --- a/lib/upmin/railties/render.rb +++ b/lib/upmin/railties/render.rb @@ -12,6 +12,10 @@ def up_render(data, options = {}) options = RenderHelpers.attribute_options(data, options) partials = RenderHelpers.attribute_partials(data, options) + elsif data.is_a?(Upmin::AttributeValue) + options = RenderHelpers.attribute_value_options(data, options) + partials = RenderHelpers.attribute_value_partials(data, options) + elsif data.is_a?(Upmin::Association) options = RenderHelpers.association_options(data, options) partials = RenderHelpers.association_partials(data, options) diff --git a/lib/upmin/railties/render_helpers.rb b/lib/upmin/railties/render_helpers.rb index b312c02..70a41e5 100644 --- a/lib/upmin/railties/render_helpers.rb +++ b/lib/upmin/railties/render_helpers.rb @@ -61,6 +61,41 @@ def RenderHelpers.build_attribute_path(partial) + def RenderHelpers.attribute_value_partials(attribute_value, options = {}) + attribute = attribute_value.attribute + partials = [] + # + # _, eg: user_name + # _, eg: user_string + # , eg: string + # unknown + + model_name = attribute.model.underscore_name + attr_type = attribute.type + + partials << build_attribute_value_path(options[:as]) if options[:as] + partials << build_attribute_value_path("#{model_name}_#{attribute.name}") + partials << build_attribute_value_path("#{model_name}_#{attr_type}") + partials << build_attribute_value_path(attribute.name) + partials << build_attribute_value_path(attr_type) + partials << build_attribute_value_path(:unknown) + return partials + end + + def RenderHelpers.attribute_value_options(attribute_value, options = {}) + attribute = attribute_value.attribute + options[:locals] ||= {} + options[:locals][:model] ||= attribute.model + options[:locals][:attribute] = attribute + return options + end + + def RenderHelpers.build_attribute_value_path(partial) + return build_path("attribute_values", partial) + end + + + # NOTE: assoc_type is sketchy at best. It tries to determine it, but in some cases it has to be guessed at, so if you have polymorphic associations it will choose the data type of the first association it finds - eg if user.things returns [Order, Product, Review] it will use the type of "order" def RenderHelpers.association_partials(association, options = {}) partials = [] From 2cada399bc7574a69919bfa5c768cc7abbba9c1f Mon Sep 17 00:00:00 2001 From: Todd Eichel Date: Tue, 3 Feb 2015 23:59:58 -0800 Subject: [PATCH 2/2] Render AttributeValues rather than raw attributes where applicable, specifically in search results. --- app/views/upmin/partials/attributes/_datetime.html.haml | 2 +- app/views/upmin/partials/search_results/_results.html.haml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/upmin/partials/attributes/_datetime.html.haml b/app/views/upmin/partials/attributes/_datetime.html.haml index 0af4994..abe3db9 100644 --- a/app/views/upmin/partials/attributes/_datetime.html.haml +++ b/app/views/upmin/partials/attributes/_datetime.html.haml @@ -35,4 +35,4 @@ - else %p.well -# TODO(jon): Make this show an uneditable date and time field. - = iso8601 + = up_render(attribute.display_value) diff --git a/app/views/upmin/partials/search_results/_results.html.haml b/app/views/upmin/partials/search_results/_results.html.haml index 20fd48a..c8e48c8 100644 --- a/app/views/upmin/partials/search_results/_results.html.haml +++ b/app/views/upmin/partials/search_results/_results.html.haml @@ -6,4 +6,4 @@ %dt = attribute.label_name %dd - = attribute.value + = up_render(attribute.display_value)