pull/6827/head
jwollert 14 years ago
commit 290d89851f
  1. 3
      app/models/report.rb
  2. 19
      app/models/report/chainable.rb
  3. 34
      app/models/report/group_by/base.rb
  4. 3
      app/models/report/group_by/singleton_value.rb
  5. 4
      app/models/report/sql_statement.rb
  6. 4
      app/models/report/validation.rb
  7. 2
      app/models/report/validation/dates.rb
  8. 2
      app/models/report/validation/integers.rb
  9. 4
      lib/tasks/spec.rake

@ -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

@ -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

@ -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<String,Symbol>] 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

@ -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"

@ -171,7 +171,7 @@ class Report::SqlStatement
# @return [Array<String>] 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)

@ -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)

@ -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

@ -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

@ -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/"]
Loading…
Cancel
Save