module Storefront class Dashboard include Storefront::AttributeHelper include Storefront::LocaleHelper attr_reader :content, :template, :key, :report, :keys def initialize(template, record_or_key, hash, options = {}, &block) @template = template @key = record_key(record_or_key) @section_index = -1 @column_index = -1 @row_index = -1 @cols = 0 @section_scope = nil @column_scope = nil @filter_scope = nil @model_names = @key.to_s.camelize.constantize.ancestor_classes.map {|i| i.name.underscore} @filter = Array(options.delete(:filter)).compact @report = hash options[:id] ||= "#{record_or_key.to_s}-section" @keys = options.delete(:keys) || {} @content = template.capture_haml do template.haml_tag :article, options do yield(self) end end end # section :when, :cols => 3 def section(*args, &block) attributes = args.extract_options! @section_scope = args.shift @section_index += 1 @row_index = -1 @column_index = -1 @cols = attributes.delete(:cols) @header = attributes.delete(:title) != false attributes[:"data-filters"] = @filter.join(",") attributes[:"data-section"] = @section_scope.to_s @subtitle = attributes.delete(:subtitle) || report_subtitle merge_class! attributes, index_class(@section_index, @cols), "dashboard-widget", "#{@section_scope}-dashboard-widget" template.capture_haml do template.haml_tag :section, attributes do if @header template.haml_tag :header, :class => "header" do template.haml_tag :h3, t?(:"#{@section_scope}.title", locale_keys.merge(:scope => :"reports.titles")), :class => "title" if @subtitle.present? template.haml_tag :h4, @subtitle, :class => "subtitle" end end end yield end @row_index = 0 @cols = 0 end end def figure(*args, &block) attributes = args.extract_options! @figure_scope = args.shift merge_class! attributes, "figure" attributes[:id] ||= "#{@figure_scope.to_s}-figure" template.capture_haml do template.haml_tag :figure, "", attributes end end def locale_keys hash = {} keys.each do |key, value| hash[key] = value.is_a?(::Symbol) ? t?(:"#{value}", :scope => :"reports.keys", :model_names => @model_names) : value end hash.merge(:model_names => @model_names) end def column(*args, &block) attributes = args.extract_options! @column_scope = args.shift @column_index += 1 @row_data = [] @row_index = -1 @rows = attributes.delete(:rows) keys = attributes.delete(:keys) || @locale_keys merge_class! attributes, index_class(@column_index, @cols), "#{@column_scope}-column", "col#{@cols}", "column" attributes[:"data-column"] = @column_scope.to_s title = attributes.delete(:title) || t?(:"#{@section_scope}.#{@column_scope}.title", locale_keys.merge(:scope => :"reports.titles")) template.capture_haml do template.capture_haml(&block) @rows = @row_data.length template.haml_tag :section, attributes.merge("data-rows" => @rows) do template.haml_tag :header, :class => "header" do template.haml_tag @header ? :h4 : :h3, title, :class => "title" end template.haml_tag :dl, :class => "statistics" do @row_data.map { |row_data| _row(*row_data) } end end @row_index = -1 @rows = 0 end end def row(*args) @row_data << args end # data-type='distance' (microdata for each statistic) def _row(*args, &block) attributes = args.extract_options! @row_scope = args.shift @row_index += 1 @row_tags = [] merge_class! attributes, index_class(@row_index, @rows) template.haml_tag :dt, t?(:"#{@section_scope}.#{@column_scope}.#{@row_scope}", locale_keys.merge(attributes.slice(:past, :present, :future).merge(:scope => :"reports.titles"))), merge_class(attributes, "key") template.haml_tag :dd, report_value, merge_class(attributes.merge(:"data-stat" => @row_scope.to_s), "value") end private def report_value keys = @filter + [@section_scope, @column_scope, @row_scope] result = report begin keys.each { |key| result = result[key] } rescue Exception => e raise keys.inspect end if result.is_a?(::Array) result.compact.blank? ? I18n.t("reports.values.missing") : result else result.blank? ? I18n.t("reports.values.missing") : result end end def report_subtitle keys = @filter + [@section_scope, :subtitle] result = report keys.each { |key| result = result[key] } result end def record_key(record_or_key) if record_or_key.is_a?(String) || record_or_key.is_a?(Symbol) record_or_key.to_s else record_or_key.class.name end end def id_for(type, key, value, row_index = @row_index, column_index = @column_index) [key, type, row_index, column_index].compact.map do |node| node.to_s.gsub(/[\s_]/, "-") end.join("-") end # reports: # titles: # what: # summary: Quick Summary # total: Total Activity # deal: # what: # summary: Quick Summary # total: Total Activity # rewards_deal: # what: # summary: Quick Summary # total: Total Activity end end