Refactor: Change the different find_object filters to share a common method.
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3597 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
parent
0f999ef0e0
commit
194dab8e96
|
@ -176,6 +176,20 @@ class ApplicationController < ActionController::Base
|
||||||
render_404
|
render_404
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def find_model_object
|
||||||
|
model = self.class.read_inheritable_attribute('model_object')
|
||||||
|
if model
|
||||||
|
@object = model.find(params[:id])
|
||||||
|
self.instance_variable_set('@' + controller_name.singularize, @object) if @object
|
||||||
|
end
|
||||||
|
rescue ActiveRecord::RecordNotFound
|
||||||
|
render_404
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.model_object(model)
|
||||||
|
write_inheritable_attribute('model_object', model)
|
||||||
|
end
|
||||||
|
|
||||||
# make sure that the user is a member of the project (or admin) if project is private
|
# make sure that the user is a member of the project (or admin) if project is private
|
||||||
# used as a before_filter for actions that do not require any particular permission on the project
|
# used as a before_filter for actions that do not require any particular permission on the project
|
||||||
def check_project_privacy
|
def check_project_privacy
|
||||||
|
|
|
@ -17,8 +17,9 @@
|
||||||
|
|
||||||
class DocumentsController < ApplicationController
|
class DocumentsController < ApplicationController
|
||||||
default_search_scope :documents
|
default_search_scope :documents
|
||||||
|
model_object Document
|
||||||
before_filter :find_project, :only => [:index, :new]
|
before_filter :find_project, :only => [:index, :new]
|
||||||
before_filter :find_document, :except => [:index, :new]
|
before_filter :find_model_object, :except => [:index, :new]
|
||||||
before_filter :find_project_from_association, :except => [:index, :new]
|
before_filter :find_project_from_association, :except => [:index, :new]
|
||||||
before_filter :authorize
|
before_filter :authorize
|
||||||
|
|
||||||
|
@ -82,10 +83,4 @@ private
|
||||||
rescue ActiveRecord::RecordNotFound
|
rescue ActiveRecord::RecordNotFound
|
||||||
render_404
|
render_404
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_document
|
|
||||||
@document = @object = Document.find(params[:id])
|
|
||||||
rescue ActiveRecord::RecordNotFound
|
|
||||||
render_404
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -17,7 +17,8 @@
|
||||||
|
|
||||||
class IssueCategoriesController < ApplicationController
|
class IssueCategoriesController < ApplicationController
|
||||||
menu_item :settings
|
menu_item :settings
|
||||||
before_filter :find_category, :except => :new
|
model_object IssueCategory
|
||||||
|
before_filter :find_model_object, :except => :new
|
||||||
before_filter :find_project_from_association, :except => :new
|
before_filter :find_project_from_association, :except => :new
|
||||||
before_filter :find_project, :only => :new
|
before_filter :find_project, :only => :new
|
||||||
before_filter :authorize
|
before_filter :authorize
|
||||||
|
@ -73,10 +74,11 @@ class IssueCategoriesController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def find_category
|
# Wrap ApplicationController's find_model_object method to set
|
||||||
@category = @object = IssueCategory.find(params[:id])
|
# @category instead of just @issue_category
|
||||||
rescue ActiveRecord::RecordNotFound
|
def find_model_object
|
||||||
render_404
|
super
|
||||||
|
@category = @object
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_project
|
def find_project
|
||||||
|
|
|
@ -16,7 +16,8 @@
|
||||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
|
||||||
class MembersController < ApplicationController
|
class MembersController < ApplicationController
|
||||||
before_filter :find_member, :except => [:new, :autocomplete_for_member]
|
model_object Member
|
||||||
|
before_filter :find_model_object, :except => [:new, :autocomplete_for_member]
|
||||||
before_filter :find_project_from_association, :except => [:new, :autocomplete_for_member]
|
before_filter :find_project_from_association, :except => [:new, :autocomplete_for_member]
|
||||||
before_filter :find_project, :only => [:new, :autocomplete_for_member]
|
before_filter :find_project, :only => [:new, :autocomplete_for_member]
|
||||||
before_filter :authorize
|
before_filter :authorize
|
||||||
|
@ -74,10 +75,4 @@ class MembersController < ApplicationController
|
||||||
render :layout => false
|
render :layout => false
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
def find_member
|
|
||||||
@member = @object = Member.find(params[:id])
|
|
||||||
rescue ActiveRecord::RecordNotFound
|
|
||||||
render_404
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -17,7 +17,8 @@
|
||||||
|
|
||||||
class NewsController < ApplicationController
|
class NewsController < ApplicationController
|
||||||
default_search_scope :news
|
default_search_scope :news
|
||||||
before_filter :find_news, :except => [:new, :index, :preview]
|
model_object News
|
||||||
|
before_filter :find_model_object, :except => [:new, :index, :preview]
|
||||||
before_filter :find_project_from_association, :except => [:new, :index, :preview]
|
before_filter :find_project_from_association, :except => [:new, :index, :preview]
|
||||||
before_filter :find_project, :only => [:new, :preview]
|
before_filter :find_project, :only => [:new, :preview]
|
||||||
before_filter :authorize, :except => [:index, :preview]
|
before_filter :authorize, :except => [:index, :preview]
|
||||||
|
@ -89,12 +90,6 @@ class NewsController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def find_news
|
|
||||||
@news = @object = News.find(params[:id])
|
|
||||||
rescue ActiveRecord::RecordNotFound
|
|
||||||
render_404
|
|
||||||
end
|
|
||||||
|
|
||||||
def find_project
|
def find_project
|
||||||
@project = Project.find(params[:project_id])
|
@project = Project.find(params[:project_id])
|
||||||
rescue ActiveRecord::RecordNotFound
|
rescue ActiveRecord::RecordNotFound
|
||||||
|
|
|
@ -17,7 +17,8 @@
|
||||||
|
|
||||||
class VersionsController < ApplicationController
|
class VersionsController < ApplicationController
|
||||||
menu_item :roadmap
|
menu_item :roadmap
|
||||||
before_filter :find_version, :except => [:new, :close_completed]
|
model_object Version
|
||||||
|
before_filter :find_model_object, :except => [:new, :close_completed]
|
||||||
before_filter :find_project_from_association, :except => [:new, :close_completed]
|
before_filter :find_project_from_association, :except => [:new, :close_completed]
|
||||||
before_filter :find_project, :only => [:new, :close_completed]
|
before_filter :find_project, :only => [:new, :close_completed]
|
||||||
before_filter :authorize
|
before_filter :authorize
|
||||||
|
@ -94,12 +95,6 @@ class VersionsController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def find_version
|
|
||||||
@version = @object = Version.find(params[:id])
|
|
||||||
rescue ActiveRecord::RecordNotFound
|
|
||||||
render_404
|
|
||||||
end
|
|
||||||
|
|
||||||
def find_project
|
def find_project
|
||||||
@project = Project.find(params[:project_id])
|
@project = Project.find(params[:project_id])
|
||||||
rescue ActiveRecord::RecordNotFound
|
rescue ActiveRecord::RecordNotFound
|
||||||
|
|
Loading…
Reference in New Issue