diff --git a/app/controllers/milestones_controller.rb b/app/controllers/milestones_controller.rb index a0c824e8..57f1e9e6 100644 --- a/app/controllers/milestones_controller.rb +++ b/app/controllers/milestones_controller.rb @@ -12,7 +12,7 @@ class MilestonesController < ProjectResourceController def index @milestones = case params[:f] - when 'all'; @project.milestones.order("closed, due_date DESC") + when 'all'; @project.milestones.order("state, due_date DESC") when 'closed'; @project.milestones.closed.order("due_date DESC") else @project.milestones.active.order("due_date ASC") end diff --git a/app/models/milestone.rb b/app/models/milestone.rb index 457fe18f..23b41446 100644 --- a/app/models/milestone.rb +++ b/app/models/milestone.rb @@ -13,19 +13,32 @@ # class Milestone < ActiveRecord::Base - attr_accessible :title, :description, :due_date, :closed, :author_id_of_changes + attr_accessible :title, :description, :due_date, :state_event, :author_id_of_changes attr_accessor :author_id_of_changes belongs_to :project has_many :issues has_many :merge_requests - scope :active, -> { where(closed: false) } - scope :closed, -> { where(closed: true) } + scope :active, -> { with_state(:active) } + scope :closed, -> { with_state(:closed) } validates :title, presence: true validates :project, presence: true - validates :closed, inclusion: { in: [true, false] } + + state_machine :state, :initial => :active do + event :close do + transition :active => :closed + end + + event :activate do + transition :closed => :active + end + + state :closed + + state :active + end def expired? if due_date @@ -68,17 +81,13 @@ class Milestone < ActiveRecord::Base end def can_be_closed? - open? && issues.opened.count.zero? + active? && issues.opened.count.zero? end def is_empty? total_items_count.zero? end - def open? - !closed - end - def author_id author_id_of_changes end diff --git a/app/views/milestones/_milestone.html.haml b/app/views/milestones/_milestone.html.haml index 00e20117..d48d842a 100644 --- a/app/views/milestones/_milestone.html.haml +++ b/app/views/milestones/_milestone.html.haml @@ -1,6 +1,6 @@ -%li{class: "milestone milestone-#{milestone.closed ? 'closed' : 'open'}", id: dom_id(milestone) } +%li{class: "milestone milestone-#{milestone.closed? ? 'closed' : 'open'}", id: dom_id(milestone) } .pull-right - - if can?(current_user, :admin_milestone, milestone.project) and milestone.open? + - if can?(current_user, :admin_milestone, milestone.project) and milestone.opened? = link_to edit_project_milestone_path(milestone.project, milestone), class: "btn btn-small edit-milestone-link grouped" do %i.icon-edit Edit diff --git a/app/views/milestones/show.html.haml b/app/views/milestones/show.html.haml index 3133c201..f8ed8518 100644 --- a/app/views/milestones/show.html.haml +++ b/app/views/milestones/show.html.haml @@ -9,7 +9,7 @@ ← To milestones list .span6 .pull-right - - unless @milestone.closed + - unless @milestone.closed? = link_to new_project_issue_path(@project, issue: { milestone_id: @milestone.id }), class: "btn btn-small grouped", title: "New Issue" do %i.icon-plus New Issue @@ -25,12 +25,12 @@ %hr %p %span All issues for this milestone are closed. You may close milestone now. - = link_to 'Close Milestone', project_milestone_path(@project, @milestone, milestone: {closed: true }), method: :put, class: "btn btn-small btn-remove" + = link_to 'Close Milestone', project_milestone_path(@project, @milestone, milestone: {state: :closed }), method: :put, class: "btn btn-small btn-remove" .ui-box.ui-box-show .ui-box-head %h4.box-title - - if @milestone.closed + - if @milestone.closed? .error.status_info Closed - elsif @milestone.expired? .error.status_info Expired @@ -63,7 +63,7 @@ %li=link_to('All Issues', '#') %ul.well-list - @issues.each do |issue| - %li{data: {closed: issue.closed}} + %li{data: {closed: issue.closed?}} = link_to [@project, issue] do %span.badge.badge-info ##{issue.id} – diff --git a/lib/api/milestones.rb b/lib/api/milestones.rb index 6aca9d01..eaf0d37c 100644 --- a/lib/api/milestones.rb +++ b/lib/api/milestones.rb @@ -59,14 +59,14 @@ module Gitlab # title (optional) - The title of a milestone # description (optional) - The description of a milestone # due_date (optional) - The due date of a milestone - # closed (optional) - The status of the milestone + # state (optional) - The status of the milestone (close|activate) # Example Request: # PUT /projects/:id/milestones/:milestone_id put ":id/milestones/:milestone_id" do authorize! :admin_milestone, user_project @milestone = user_project.milestones.find(params[:milestone_id]) - attrs = attributes_for_keys [:title, :description, :due_date, :closed] + attrs = attributes_for_keys [:title, :description, :due_date, :state_event] if @milestone.update_attributes attrs present @milestone, with: Entities::Milestone else diff --git a/spec/models/milestone_spec.rb b/spec/models/milestone_spec.rb index 8871bf4a..cdf0715a 100644 --- a/spec/models/milestone_spec.rb +++ b/spec/models/milestone_spec.rb @@ -27,7 +27,6 @@ describe Milestone do describe "Validation" do it { should validate_presence_of(:title) } it { should validate_presence_of(:project) } - it { should ensure_inclusion_of(:closed).in_array([true, false]) } end let(:milestone) { create(:milestone) } @@ -41,7 +40,7 @@ describe Milestone do it "should count closed issues" do IssueObserver.current_user = issue.author - issue.update_attributes(closed: true) + issue.close milestone.issues << issue milestone.percent_complete.should == 100 end @@ -96,7 +95,7 @@ describe Milestone do describe :items_count do before do milestone.issues << create(:issue) - milestone.issues << create(:issue, closed: true) + milestone.issues << create(:closed_issue) milestone.merge_requests << create(:merge_request) end @@ -109,8 +108,4 @@ describe Milestone do describe :can_be_closed? do it { milestone.can_be_closed?.should be_true } end - - describe :open? do - it { milestone.open?.should be_true } - end end diff --git a/spec/requests/api/milestones_spec.rb b/spec/requests/api/milestones_spec.rb index 80696671..d1b5e449 100644 --- a/spec/requests/api/milestones_spec.rb +++ b/spec/requests/api/milestones_spec.rb @@ -44,4 +44,14 @@ describe Gitlab::API do json_response['title'].should == 'updated title' end end + + describe "PUT /projects/:id/milestones/:milestone_id to close milestone" do + it "should update a project milestone" do + put api("/projects/#{project.id}/milestones/#{milestone.id}", user), + state_event: 'close' + response.status.should == 200 + + json_response['state'].should == 'closed' + end + end end