Merge remote-tracking branch 'finnlabs/pulls/653/move-query-models-into-separate-files' into unstable

This commit is contained in:
Eric Davis 2011-10-14 16:40:08 -07:00
commit 061beb4967
4 changed files with 61 additions and 62 deletions

View File

@ -11,64 +11,7 @@
# See doc/COPYRIGHT.rdoc for more details. # See doc/COPYRIGHT.rdoc for more details.
#++ #++
class QueryColumn
attr_accessor :name, :sortable, :groupable, :default_order
include Redmine::I18n
def initialize(name, options={})
self.name = name
self.sortable = options[:sortable]
self.groupable = options[:groupable] || false
if groupable == true
self.groupable = name.to_s
end
self.default_order = options[:default_order]
@caption_key = options[:caption] || "field_#{name}"
end
def caption
l(@caption_key)
end
# Returns true if the column is sortable, otherwise false
def sortable?
!sortable.nil?
end
def value(issue)
issue.send name
end
end
class QueryCustomFieldColumn < QueryColumn
def initialize(custom_field)
self.name = "cf_#{custom_field.id}".to_sym
self.sortable = custom_field.order_statement || false
if %w(list date bool int).include?(custom_field.field_format)
self.groupable = custom_field.order_statement
end
self.groupable ||= false
@cf = custom_field
end
def caption
@cf.name
end
def custom_field
@cf
end
def value(issue)
cv = issue.custom_values.detect {|v| v.custom_field_id == @cf.id}
cv && @cf.cast_value(cv.value)
end
end
class Query < ActiveRecord::Base class Query < ActiveRecord::Base
class StatementInvalid < ::ActiveRecord::StatementInvalid
end
belongs_to :project belongs_to :project
belongs_to :user belongs_to :user
@ -518,7 +461,7 @@ class Query < ActiveRecord::Base
def issue_count def issue_count
Issue.count(:include => [:status, :project], :conditions => statement) Issue.count(:include => [:status, :project], :conditions => statement)
rescue ::ActiveRecord::StatementInvalid => e rescue ::ActiveRecord::StatementInvalid => e
raise StatementInvalid.new(e.message) raise Query::StatementInvalid.new(e.message)
end end
# Returns the issue count by group or nil if query is not grouped # Returns the issue count by group or nil if query is not grouped
@ -538,7 +481,7 @@ class Query < ActiveRecord::Base
end end
r r
rescue ::ActiveRecord::StatementInvalid => e rescue ::ActiveRecord::StatementInvalid => e
raise StatementInvalid.new(e.message) raise Query::StatementInvalid.new(e.message)
end end
# Returns the issues # Returns the issues
@ -553,7 +496,7 @@ class Query < ActiveRecord::Base
:limit => options[:limit], :limit => options[:limit],
:offset => options[:offset] :offset => options[:offset]
rescue ::ActiveRecord::StatementInvalid => e rescue ::ActiveRecord::StatementInvalid => e
raise StatementInvalid.new(e.message) raise Query::StatementInvalid.new(e.message)
end end
# Returns the journals # Returns the journals
@ -565,7 +508,7 @@ class Query < ActiveRecord::Base
:limit => options[:limit], :limit => options[:limit],
:offset => options[:offset] :offset => options[:offset]
rescue ::ActiveRecord::StatementInvalid => e rescue ::ActiveRecord::StatementInvalid => e
raise StatementInvalid.new(e.message) raise Query::StatementInvalid.new(e.message)
end end
# Returns the versions # Returns the versions
@ -574,7 +517,7 @@ class Query < ActiveRecord::Base
Version.find :all, :include => :project, Version.find :all, :include => :project,
:conditions => Query.merge_conditions(project_statement, options[:conditions]) :conditions => Query.merge_conditions(project_statement, options[:conditions])
rescue ::ActiveRecord::StatementInvalid => e rescue ::ActiveRecord::StatementInvalid => e
raise StatementInvalid.new(e.message) raise Query::StatementInvalid.new(e.message)
end end
private private

View File

@ -0,0 +1,2 @@
class Query::StatementInvalid < ActiveRecord::StatementInvalid
end

View File

@ -0,0 +1,28 @@
class QueryColumn
attr_accessor :name, :sortable, :groupable, :default_order
include Redmine::I18n
def initialize(name, options={})
self.name = name
self.sortable = options[:sortable]
self.groupable = options[:groupable] || false
if groupable == true
self.groupable = name.to_s
end
self.default_order = options[:default_order]
@caption_key = options[:caption] || "field_#{name}"
end
def caption
l(@caption_key)
end
# Returns true if the column is sortable, otherwise false
def sortable?
!sortable.nil?
end
def value(issue)
issue.send name
end
end

View File

@ -0,0 +1,26 @@
class QueryCustomFieldColumn < QueryColumn
def initialize(custom_field)
self.name = "cf_#{custom_field.id}".to_sym
self.sortable = custom_field.order_statement || false
if %w(list date bool int).include?(custom_field.field_format)
self.groupable = custom_field.order_statement
end
self.groupable ||= false
@cf = custom_field
end
def caption
@cf.name
end
def custom_field
@cf
end
def value(issue)
cv = issue.custom_values.detect {|v| v.custom_field_id == @cf.id}
cv && @cf.cast_value(cv.value)
end
end