summaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2008-06-27 20:13:56 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2008-06-27 20:13:56 +0000
commitce6cf66f6c3af26383cd25ed012d908be4b40bae (patch)
treedfecb66fd8cca4de280494515e0ad21ec6f32dfd /app/models
parenta4a8b6381e4a162da85319e216a770ee7bd82202 (diff)
downloadredmine-ce6cf66f6c3af26383cd25ed012d908be4b40bae.tar.gz
redmine-ce6cf66f6c3af26383cd25ed012d908be4b40bae.zip
Custom fields refactoring: most of code moved from controllers to models (using new module ActsAsCustomizable).
git-svn-id: http://redmine.rubyforge.org/svn/trunk@1592 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'app/models')
-rw-r--r--app/models/issue.rb14
-rw-r--r--app/models/project.rb13
-rw-r--r--app/models/query.rb4
-rw-r--r--app/models/user.rb8
4 files changed, 18 insertions, 21 deletions
diff --git a/app/models/issue.rb b/app/models/issue.rb
index d83b2ab02..326e234b0 100644
--- a/app/models/issue.rb
+++ b/app/models/issue.rb
@@ -28,13 +28,12 @@ class Issue < ActiveRecord::Base
has_many :journals, :as => :journalized, :dependent => :destroy
has_many :attachments, :as => :container, :dependent => :destroy
has_many :time_entries, :dependent => :delete_all
- has_many :custom_values, :dependent => :delete_all, :as => :customized
- has_many :custom_fields, :through => :custom_values
has_and_belongs_to_many :changesets, :order => "revision ASC"
has_many :relations_from, :class_name => 'IssueRelation', :foreign_key => 'issue_from_id', :dependent => :delete_all
has_many :relations_to, :class_name => 'IssueRelation', :foreign_key => 'issue_to_id', :dependent => :delete_all
+ acts_as_customizable
acts_as_watchable
acts_as_searchable :columns => ['subject', "#{table_name}.description"], :include => :project, :with => {:journal => :issue}
acts_as_event :title => Proc.new {|o| "#{o.tracker.name} ##{o.id}: #{o.subject}"},
@@ -44,7 +43,6 @@ class Issue < ActiveRecord::Base
validates_length_of :subject, :maximum => 255
validates_inclusion_of :done_ratio, :in => 0..100
validates_numericality_of :estimated_hours, :allow_nil => true
- validates_associated :custom_values, :on => :update
def after_initialize
if new_record?
@@ -54,6 +52,11 @@ class Issue < ActiveRecord::Base
end
end
+ # Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
+ def available_custom_fields
+ (project && tracker) ? project.all_issue_custom_fields.select {|c| tracker.custom_fields.include? c } : []
+ end
+
def copy_from(arg)
issue = arg.is_a?(Issue) ? arg : Issue.find(arg)
self.attributes = issue.attributes.dup
@@ -168,11 +171,6 @@ class Issue < ActiveRecord::Base
end
end
- def custom_value_for(custom_field)
- self.custom_values.each {|v| return v if v.custom_field_id == custom_field.id }
- return nil
- end
-
def init_journal(user, notes = "")
@current_journal ||= Journal.new(:journalized => self, :user => user, :notes => notes)
@issue_before_change = self.clone
diff --git a/app/models/project.rb b/app/models/project.rb
index f05ccb2af..a5ba246b1 100644
--- a/app/models/project.rb
+++ b/app/models/project.rb
@@ -22,7 +22,6 @@ class Project < ActiveRecord::Base
has_many :members, :include => :user, :conditions => "#{User.table_name}.status=#{User::STATUS_ACTIVE}"
has_many :users, :through => :members
- has_many :custom_values, :dependent => :delete_all, :as => :customized
has_many :enabled_modules, :dependent => :delete_all
has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position"
has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker]
@@ -38,7 +37,7 @@ class Project < ActiveRecord::Base
has_many :changesets, :through => :repository
has_one :wiki, :dependent => :destroy
# Custom field for the project issues
- has_and_belongs_to_many :custom_fields,
+ has_and_belongs_to_many :issue_custom_fields,
:class_name => 'IssueCustomField',
:order => "#{CustomField.table_name}.position",
:join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
@@ -46,6 +45,7 @@ class Project < ActiveRecord::Base
acts_as_tree :order => "name", :counter_cache => true
+ acts_as_customizable
acts_as_searchable :columns => ['name', 'description'], :project_key => 'id', :permission => nil
acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
:url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}}
@@ -54,7 +54,6 @@ class Project < ActiveRecord::Base
validates_presence_of :name, :identifier
validates_uniqueness_of :name, :identifier
- validates_associated :custom_values, :on => :update
validates_associated :repository, :wiki
validates_length_of :name, :maximum => 30
validates_length_of :homepage, :maximum => 255
@@ -195,12 +194,8 @@ class Project < ActiveRecord::Base
# Returns an array of all custom fields enabled for project issues
# (explictly associated custom fields and custom fields enabled for all projects)
- def custom_fields_for_issues(tracker)
- all_custom_fields.select {|c| tracker.custom_fields.include? c }
- end
-
- def all_custom_fields
- @all_custom_fields ||= (IssueCustomField.for_all + custom_fields).uniq
+ def all_issue_custom_fields
+ @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq
end
def project
diff --git a/app/models/query.rb b/app/models/query.rb
index 4c72e23f2..27ab882c6 100644
--- a/app/models/query.rb
+++ b/app/models/query.rb
@@ -176,7 +176,7 @@ class Query < ActiveRecord::Base
unless @project.active_children.empty?
@available_filters["subproject_id"] = { :type => :list_subprojects, :order => 13, :values => @project.active_children.collect{|s| [s.name, s.id.to_s] } }
end
- add_custom_fields_filters(@project.all_custom_fields)
+ add_custom_fields_filters(@project.all_issue_custom_fields)
else
# global filters for cross project issue list
add_custom_fields_filters(IssueCustomField.find(:all, :conditions => {:is_filter => true, :is_for_all => true}))
@@ -226,7 +226,7 @@ class Query < ActiveRecord::Base
return @available_columns if @available_columns
@available_columns = Query.available_columns
@available_columns += (project ?
- project.all_custom_fields :
+ project.all_issue_custom_fields :
IssueCustomField.find(:all, :conditions => {:is_for_all => true})
).collect {|cf| QueryCustomFieldColumn.new(cf) }
end
diff --git a/app/models/user.rb b/app/models/user.rb
index 5568027d5..a34b96861 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -37,12 +37,13 @@ class User < ActiveRecord::Base
has_many :memberships, :class_name => 'Member', :include => [ :project, :role ], :conditions => "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}", :order => "#{Project.table_name}.name", :dependent => :delete_all
has_many :projects, :through => :memberships
- has_many :custom_values, :dependent => :delete_all, :as => :customized
has_many :issue_categories, :foreign_key => 'assigned_to_id', :dependent => :nullify
has_one :preference, :dependent => :destroy, :class_name => 'UserPreference'
has_one :rss_token, :dependent => :destroy, :class_name => 'Token', :conditions => "action='feeds'"
belongs_to :auth_source
+ acts_as_customizable
+
attr_accessor :password, :password_confirmation
attr_accessor :last_before_login_on
# Prevents unauthorized assignments
@@ -60,7 +61,6 @@ class User < ActiveRecord::Base
validates_length_of :mail, :maximum => 60, :allow_nil => true
validates_length_of :password, :minimum => 4, :allow_nil => true
validates_confirmation_of :password, :allow_nil => true
- validates_associated :custom_values, :on => :update
def before_create
self.mail_notification = false
@@ -280,6 +280,10 @@ class AnonymousUser < User
errors.add_to_base 'An anonymous user already exists.' if AnonymousUser.find(:first)
end
+ def available_custom_fields
+ []
+ end
+
# Overrides a few properties
def logged?; false end
def admin; false end