Skip to content
This repository has been archived by the owner on Mar 29, 2019. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'finnlabs/pulls/653/move-query-models-in…
Browse files Browse the repository at this point in the history
…to-separate-files' into unstable
  • Loading branch information
edavis10 committed Oct 14, 2011
2 parents c87738d + c1ecadb commit 061beb4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 62 deletions.
67 changes: 5 additions & 62 deletions app/models/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,7 @@
# 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 StatementInvalid < ::ActiveRecord::StatementInvalid
end

belongs_to :project
belongs_to :user
Expand Down Expand Up @@ -518,7 +461,7 @@ def statement
def issue_count
Issue.count(:include => [:status, :project], :conditions => statement)
rescue ::ActiveRecord::StatementInvalid => e
raise StatementInvalid.new(e.message)
raise Query::StatementInvalid.new(e.message)
end

# Returns the issue count by group or nil if query is not grouped
Expand All @@ -538,7 +481,7 @@ def issue_count_by_group
end
r
rescue ::ActiveRecord::StatementInvalid => e
raise StatementInvalid.new(e.message)
raise Query::StatementInvalid.new(e.message)
end

# Returns the issues
Expand All @@ -553,7 +496,7 @@ def issues(options={})
:limit => options[:limit],
:offset => options[:offset]
rescue ::ActiveRecord::StatementInvalid => e
raise StatementInvalid.new(e.message)
raise Query::StatementInvalid.new(e.message)
end

# Returns the journals
Expand All @@ -565,7 +508,7 @@ def issue_journals(options={})
:limit => options[:limit],
:offset => options[:offset]
rescue ::ActiveRecord::StatementInvalid => e
raise StatementInvalid.new(e.message)
raise Query::StatementInvalid.new(e.message)
end

# Returns the versions
Expand All @@ -574,7 +517,7 @@ def versions(options={})
Version.find :all, :include => :project,
:conditions => Query.merge_conditions(project_statement, options[:conditions])
rescue ::ActiveRecord::StatementInvalid => e
raise StatementInvalid.new(e.message)
raise Query::StatementInvalid.new(e.message)
end

private
Expand Down
2 changes: 2 additions & 0 deletions app/models/query/statement_invalid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Query::StatementInvalid < ActiveRecord::StatementInvalid
end
28 changes: 28 additions & 0 deletions app/models/query_column.rb
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions app/models/query_custom_field_column.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 061beb4

Please sign in to comment.