lib/gooddata/models/metadata/report_definition.rb in gooddata-0.6.10 vs lib/gooddata/models/metadata/report_definition.rb in gooddata-0.6.11

- old
+ new

@@ -70,11 +70,14 @@ elsif item.is_a?(String) x = GoodData::MdObject.get_by_id(item, opts) fail "Object given by id \"#{item}\" could not be found" if x.nil? case x.raw_data.keys.first.to_s when 'attribute' - GoodData::Attribute.new(x.json).display_forms.first + attr = GoodData::Attribute.new(x.json) + attr.client = client + attr.project = opts[:project] + attr.display_forms.first when 'attributeDisplayForm' GoodData::Label.new(x.json) when 'metric' GoodData::Metric.new(x.json) end @@ -115,32 +118,26 @@ left = Array(options[:left]) top = Array(options[:top]) metrics = (left + top).select { |item| item.respond_to?(:metric?) && item.metric? } - unsaved_metrics = metrics.reject { |i| i.saved? } + unsaved_metrics = metrics.reject(&:saved?) unsaved_metrics.each { |m| m.title = 'Untitled metric' unless m.title } begin - unsaved_metrics.each { |m| m.save } + unsaved_metrics.each(&:save) rd = GoodData::ReportDefinition.create(options) data_result(execute_inline(rd, options), options) ensure unsaved_metrics.each { |m| m.delete if m && m.saved? } end end def execute_inline(rd, opts = { :client => GoodData.connection, :project => GoodData.project }) client = opts[:client] - fail ArgumentError, 'No :client specified' if client.nil? + project = opts[:project] - p = opts[:project] - fail ArgumentError, 'No :project specified' if p.nil? - - project = GoodData::Project[p, opts] - fail ArgumentError, 'Wrong :project specified' if project.nil? - rd = rd.respond_to?(:json) ? rd.json : rd data = { report_req: { definitionContent: { content: rd, @@ -189,11 +186,11 @@ left = ReportDefinition.find(left, options) top = ReportDefinition.find(top, options) # TODO: Put somewhere for i18n fail_msg = 'All metrics in report definition must be saved' - fail fail_msg unless (left + top).all? { |i| i.saved? } + fail fail_msg unless (left + top).all?(&:saved?) pars = { 'reportDefinition' => { 'content' => { 'grid' => { @@ -229,13 +226,22 @@ items = cols + rows items.select { |item| item.is_a?(Hash) && item.keys.first == 'attribute' } end def attributes - labels.map { |label| label.attribute } + labels.map(&:attribute) end + # Removes the color mapping from report definition + # + # @return [GoodData::ReportDefinition] Returns self + def reset_color_mapping! + global_chart_options = GoodData::Helpers.get_path(content, %w(chart styles global)) + global_chart_options['colorMapping'] = [] if global_chart_options + self + end + def labels attribute_parts.map { |part| project.labels(part['attribute']['uri']) } end def metric_parts @@ -245,19 +251,24 @@ def metrics metric_parts.map { |i| project.metrics(i['uri']) } end def execute(opts = { :client => GoodData.connection, :project => GoodData.project }) - opts = { - :client => client, - :project => project - } + client = opts[:client] + fail ArgumentError, 'No :client specified' if client.nil? + p = opts[:project] + fail ArgumentError, 'No :project specified' if p.nil? + + project = client.projects(p) + fail ArgumentError, 'Wrong :project specified' if project.nil? + + opts = { client: client, project: project } result = if saved? pars = { 'report_req' => { 'reportDefinition' => uri } } - client.post '/gdc/xtab2/executor', pars + client.post '/gdc/xtab2/executor3', pars else ReportDefinition.execute_inline(self, opts) end ReportDefinition.data_result(result, opts) end