diff --git a/app/models/report.rb b/app/models/report.rb index fa9a88375d..fd6112dc94 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -1,7 +1,8 @@ require 'forwardable' class Report < ActiveRecord::Base - InheritedNamespace.activate + InheritedNamespace.activate unless defined? Rails && Rails.version =~ /^3./ + extend Forwardable include Enumerable #belongs_to :user diff --git a/app/models/report/chainable.rb b/app/models/report/chainable.rb index 3b0f469779..6f5ba107ec 100644 --- a/app/models/report/chainable.rb +++ b/app/models/report/chainable.rb @@ -1,5 +1,5 @@ -# Proviedes convinience layer and logic shared between GroupBy::Base and Filter::Base. -# Implements a dubble linked list (FIXME: is that the correct term?). +# Provides convinience layer and logic shared between GroupBy::Base and Filter::Base. +# Implements a double linked list (FIXME: is that the correct term?). class Report < ActiveRecord::Base class Chainable include Enumerable @@ -63,6 +63,12 @@ class Report < ActiveRecord::Base name.demodulize.underscore end + def self.put_sql_table_names(table_prefix_placement = {}) + @table_prefix_placement ||= {} + @table_prefix_placement.merge! table_prefix_placement + @table_prefix_placement + end + ## # The given block is called when a new chain is created for a report. # The query will be given to the block as a parameter. @@ -72,7 +78,7 @@ class Report < ActiveRecord::Base engine.chain_initializer.push block end - inherited_attribute :label + inherited_attribute :label, :default => :translation_needed inherited_attribute :properties, :list => true class << self @@ -101,7 +107,7 @@ class Report < ActiveRecord::Base end def to_a - returning([to_hash]) { |a| a.unshift(*child.to_a) unless bottom? } + [to_hash].tap { |a| a.unshift(*child.to_a) unless bottom? } end def top @@ -285,7 +291,10 @@ class Report < ActiveRecord::Base end def with_table(fields) - fields.map { |f| field_name_for f, self } + fields.map do |f| + place_field_name = self.class.put_sql_table_names[f] || self.class.put_sql_table_names[f].nil? + place_field_name ? (field_name_for f, self) : f + end end def field @@ -293,4 +302,4 @@ class Report < ActiveRecord::Base end end -end \ No newline at end of file +end diff --git a/app/models/report/group_by/base.rb b/app/models/report/group_by/base.rb index 0d90117bd8..b8c99b1aeb 100644 --- a/app/models/report/group_by/base.rb +++ b/app/models/report/group_by/base.rb @@ -32,8 +32,35 @@ class Report::GroupBy end.uniq end + def self.select_fields(*fields) + unless fields.empty? + @select_fields ||= [] + @select_fields += fields + end + @select_fields + end + + def select_fields + if self.class.select_fields + (parent ? parent.select_fields : []) + self.class.select_fields + else + group_fields + end + end + + ## + # @param [FalseClass, TrueClass] prefix Whether or not add a table prefix the field names + # @return [Array] List of select fields corresponding to self and all parents' + def all_select_fields(prefix = true) + @all_select_fields ||= [] + @all_select_fields[prefix ? 0 : 1] ||= begin + fields = select_fields.reject { |c| c.blank? or c == 'base' } + (parent ? parent.all_select_fields(prefix) : []) + (prefix ? with_table(fields) : fields) + end.uniq + end + def clear - @all_group_fields = nil + @all_group_fields = @all_select_fields = nil super end @@ -58,9 +85,8 @@ class Report::GroupBy end def define_group(sql) - fields = all_group_fields - sql.group_by fields - sql.select fields + sql.select all_select_fields + sql.group_by all_group_fields end end end \ No newline at end of file diff --git a/app/models/report/group_by/singleton_value.rb b/app/models/report/group_by/singleton_value.rb index 197b9fcf80..68deb502a4 100644 --- a/app/models/report/group_by/singleton_value.rb +++ b/app/models/report/group_by/singleton_value.rb @@ -2,6 +2,9 @@ class Report::GroupBy class SingletonValue < Base dont_display! + put_sql_table_names "singleton_value" => false + select_fields "1 as singleton_value" + def define_group(sql) sql.select "1 as singleton_value" sql.group_by "singleton_value" diff --git a/app/models/report/sql_statement.rb b/app/models/report/sql_statement.rb index 3aded2e7ef..c2f415b64a 100644 --- a/app/models/report/sql_statement.rb +++ b/app/models/report/sql_statement.rb @@ -171,7 +171,7 @@ class Report::SqlStatement # @return [Array] All fields/statements for select part def select(*fields) return(@select || default_select) if fields.empty? - returning(@select ||= []) do + (@select ||= []).tap do @sql = nil fields.each do |f| case f @@ -200,7 +200,7 @@ class Report::SqlStatement # @param [Array, String, Symbol] fields Fields to add def group_by(*fields) @sql = nil unless fields.empty? - returning(@group_by ||= []) do + (@group_by ||= []).tap do fields.each do |e| if e.is_a? Array and (e.size != 2 or !e.first.respond_to? :table_name) group_by(*e) diff --git a/app/models/report/validation.rb b/app/models/report/validation.rb index a4d0b7dc96..bc8e1cd3d8 100644 --- a/app/models/report/validation.rb +++ b/app/models/report/validation.rb @@ -4,7 +4,7 @@ module Report::Validation register_validation(val_method) end end - + def register_validation(val_method) const_name = val_method.to_s.camelize begin @@ -23,13 +23,11 @@ module Report::Validation end def errors - @errors ||= [] - @errors + @errors ||= Hash.new { |h,k| h[k] = [] } end def validations @validations ||= [] - @validations end def validate(*values) diff --git a/app/models/report/validation/dates.rb b/app/models/report/validation/dates.rb index 79e3ae9fde..8f07138603 100644 --- a/app/models/report/validation/dates.rb +++ b/app/models/report/validation/dates.rb @@ -7,7 +7,7 @@ module Report::Validation begin !!val.to_dateish rescue ArgumentError - errors << "\'#{val}\' " + l(:validation_failure_date) + errors[:date] << val validate_dates(values - [val]) false end diff --git a/app/models/report/validation/integers.rb b/app/models/report/validation/integers.rb index 23a1b96356..c710965cb1 100644 --- a/app/models/report/validation/integers.rb +++ b/app/models/report/validation/integers.rb @@ -5,7 +5,7 @@ module Report::Validation return true if values.empty? values.flatten.all? do |val| if val.to_i.to_s != val.to_s - errors << "\'#{val}\'" + l(:validation_failure_integer) + errors[:int] << val validate_integers(values - [val]) false else diff --git a/tasks/spec.rake b/lib/tasks/spec.rake similarity index 88% rename from tasks/spec.rake rename to lib/tasks/spec.rake index cac94a68ba..aa6dad5f96 100644 --- a/tasks/spec.rake +++ b/lib/tasks/spec.rake @@ -4,13 +4,13 @@ begin namespace :plugins do desc "Runs the examples for reporting_engine" Spec::Rake::SpecTask.new(:reporting_engine) do |t| - t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""] + t.spec_opts = ['--options', "\"#{Rails.root}/spec/spec.opts\""] t.spec_files = FileList['vendor/plugins/reporting_engine/spec/**/*_spec.rb'] end desc "Runs the examples for reporting_engine" Spec::Rake::SpecTask.new(:"reporting_engine:rcov") do |t| - t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""] + t.spec_opts = ['--options', "\"#{Rails.root}/spec/spec.opts\""] t.spec_files = FileList['vendor/plugins/reporting_engine/spec/**/*_spec.rb'] t.rcov = true t.rcov_opts = ['-x', "\.rb,spec", '-i', "reporting_engine/app/,redmine_reporting/lib/"]