#51: some refactoring

This commit is contained in:
Vokhmin Alexey V 2013-04-03 01:59:33 +04:00
parent 2bc580500d
commit 8b6a0eab0f
3 changed files with 19 additions and 9 deletions

View File

@ -41,10 +41,7 @@ class Projects::IssuesController < Projects::BaseController
@assignee_uname = params[:assignee_uname] @assignee_uname = params[:assignee_uname]
@issue.user_id = current_user.id @issue.user_id = current_user.id
unless can?(:write, @project) @issue.can_write_project = can?(:write, @project)
@issue.assignee_id = nil
@issue.labelings = []
end
if @issue.save if @issue.save
@issue.subscribe_creator(current_user.id) @issue.subscribe_creator(current_user.id)
flash[:notice] = I18n.t("flash.issue.saved") flash[:notice] = I18n.t("flash.issue.saved")
@ -60,11 +57,9 @@ class Projects::IssuesController < Projects::BaseController
end end
def update def update
unless can?(:write, @project) can_write_project = can?(:write, @project)
[:labelings, :labelings_attributes, :assignee_id].each{ |k| params[:issue].delete k } @issue.can_write_project = can_write_project
params.delete :update_labels @issue.labelings.destroy_all if can_write_project && params[:update_labels]
end
@issue.labelings.destroy_all if params[:update_labels]
if params[:issue] && status = params[:issue][:status] if params[:issue] && status = params[:issue][:status]
@issue.set_close(current_user) if status == 'closed' @issue.set_close(current_user) if status == 'closed'
@issue.set_open if status == 'open' @issue.set_open if status == 'open'

View File

@ -43,6 +43,7 @@ class Projects::PullRequestsController < Projects::BaseController
@pull.from_project_owner_uname = @pull.from_project.owner.uname @pull.from_project_owner_uname = @pull.from_project.owner.uname
@pull.from_project_name = @pull.from_project.name @pull.from_project_name = @pull.from_project.name
@pull.issue.can_write_project = can?(:write, @project)
if @pull.valid? # FIXME more clean/clever logics if @pull.valid? # FIXME more clean/clever logics
@pull.save # set pull id @pull.save # set pull id
@pull.check(false) # don't make event transaction @pull.check(false) # don't make event transaction
@ -67,6 +68,7 @@ class Projects::PullRequestsController < Projects::BaseController
end end
def update def update
@pull.issue.can_write_project = can?(:write, @project)
if (action = params[:pull_request_action]) && %w(close reopen).include?(params[:pull_request_action]) if (action = params[:pull_request_action]) && %w(close reopen).include?(params[:pull_request_action])
if @pull.send("can_#{action}?") if @pull.send("can_#{action}?")
@pull.set_user_and_time current_user @pull.set_user_and_time current_user

View File

@ -13,6 +13,7 @@ class Issue < ActiveRecord::Base
has_many :labels, :through => :labelings, :uniq => true has_many :labels, :through => :labelings, :uniq => true
has_one :pull_request, :dependent => :destroy has_one :pull_request, :dependent => :destroy
before_validation :sanitize_params_for_current_user
validates :title, :body, :project_id, :presence => true validates :title, :body, :project_id, :presence => true
after_create :set_serial_id after_create :set_serial_id
@ -21,6 +22,7 @@ class Issue < ActiveRecord::Base
attr_accessible :labelings_attributes, :title, :body, :assignee_id attr_accessible :labelings_attributes, :title, :body, :assignee_id
accepts_nested_attributes_for :labelings, :allow_destroy => true accepts_nested_attributes_for :labelings, :allow_destroy => true
attr_accessor :can_write_project
scope :opened, where(:status => 'open') scope :opened, where(:status => 'open')
scope :closed, where(:status => 'closed') scope :closed, where(:status => 'closed')
@ -69,6 +71,17 @@ class Issue < ActiveRecord::Base
protected protected
def sanitize_params_for_current_user
return true if can_write_project
if persisted?
self.assignee_id = self.assignee_id
self.labelings = self.labelings
else
self.assignee_id = nil
self.labelings = []
end
end
def set_serial_id def set_serial_id
self.serial_id = self.project.issues.count self.serial_id = self.project.issues.count
self.save! self.save!