diff --git a/app/controllers/apps_controller.rb b/app/controllers/apps_controller.rb index e874380..27449b8 100644 --- a/app/controllers/apps_controller.rb +++ b/app/controllers/apps_controller.rb @@ -26,7 +26,7 @@ class AppsController < ApplicationController def edit @app.watchers.build if @app.watchers.none? - @app.issue_trackers.build if @app.issue_trackers.none? + @app.issue_tracker = IssueTracker.new if @app.issue_tracker.nil? end def create diff --git a/app/models/app.rb b/app/models/app.rb index b300438..b960121 100644 --- a/app/models/app.rb +++ b/app/models/app.rb @@ -16,7 +16,7 @@ class App embeds_many :watchers embeds_many :deploys - embeds_many :issue_trackers + embeds_one :issue_tracker references_many :errs, :dependent => :destroy before_validation :generate_api_key, :on => :create @@ -25,11 +25,11 @@ class App validates_uniqueness_of :name, :allow_blank => true validates_uniqueness_of :api_key, :allow_blank => true validates_associated :watchers - validate :check_issue_trackers + validate :check_issue_tracker accepts_nested_attributes_for :watchers, :allow_destroy => true, :reject_if => proc { |attrs| attrs[:user_id].blank? && attrs[:email].blank? } - accepts_nested_attributes_for :issue_trackers, :allow_destroy => true, + accepts_nested_attributes_for :issue_tracker, :allow_destroy => true, :reject_if => proc { |attrs| !%w( lighthouseapp ).include?(attrs[:issue_tracker_type]) } # Mongoid Bug: find(id) on association proxies returns an Enumerator @@ -51,12 +51,12 @@ class App self.api_key ||= ActiveSupport::SecureRandom.hex end - def check_issue_trackers - issue_trackers.map(&:valid?) - issue_trackers.each do |tracker| - tracker.errors.full_messages.each do |error| + def check_issue_tracker + if issue_tracker.present? + issue_tracker.valid? + issue_tracker.errors.full_messages.each do |error| errors[:base] << error - end if tracker.errors + end if issue_tracker.errors end end end diff --git a/app/models/issue_tracker.rb b/app/models/issue_tracker.rb index 75025f0..8242fe3 100644 --- a/app/models/issue_tracker.rb +++ b/app/models/issue_tracker.rb @@ -4,7 +4,7 @@ class IssueTracker validate :check_lighthouseapp_params - embedded_in :app, :inverse_of => :issue_trackers + embedded_in :app, :inverse_of => :issue_tracker field :account, :type => String field :api_token, :type => String diff --git a/app/views/apps/_fields.html.haml b/app/views/apps/_fields.html.haml index 2789a80..ef927d2 100644 --- a/app/views/apps/_fields.html.haml +++ b/app/views/apps/_fields.html.haml @@ -23,8 +23,8 @@ = w.text_field :email %fieldset.nested-wrapper - %legend Issue trackers - = f.fields_for :issue_trackers do |w| + %legend Issue tracker + = f.fields_for :issue_tracker do |w| %div.watcher.nested %div.choose = w.radio_button :issue_tracker_type, :lighthouseapp diff --git a/spec/controllers/apps_controller_spec.rb b/spec/controllers/apps_controller_spec.rb index e9f6c3e..d477eab 100644 --- a/spec/controllers/apps_controller_spec.rb +++ b/spec/controllers/apps_controller_spec.rb @@ -179,25 +179,25 @@ describe AppsController do context "setting up issue tracker", :cur => true do context "unknown tracker type" do before(:each) do - put :update, :id => @app.id, :app => { :issue_trackers_attributes => { '0' => { + put :update, :id => @app.id, :app => { :issue_tracker_attributes => { :issue_tracker_type => 'unknown', :project_id => '1234', :api_token => '123123', :account => 'myapp' - } } } + } } @app.reload end it "should not create issue tracker" do - @app.issue_trackers.should be_empty + @app.issue_tracker.should be_nil end end context "lighthouseapp" do it "should save tracker params" do - put :update, :id => @app.id, :app => { :issue_trackers_attributes => { '0' => { + put :update, :id => @app.id, :app => { :issue_tracker_attributes => { :issue_tracker_type => 'lighthouseapp', :project_id => '1234', :api_token => '123123', :account => 'myapp' - } } } + } } @app.reload - tracker = @app.issue_trackers.first + tracker = @app.issue_tracker tracker.issue_tracker_type.should == 'lighthouseapp' tracker.project_id.should == '1234' tracker.api_token.should == '123123' @@ -205,22 +205,22 @@ describe AppsController do end it "should show validation notice when sufficient params are not present" do - put :update, :id => @app.id, :app => { :issue_trackers_attributes => { '0' => { + put :update, :id => @app.id, :app => { :issue_tracker_attributes => { :issue_tracker_type => 'lighthouseapp', :project_id => '1234', :api_token => '123123' - } } } + } } @app.reload - @app.issue_trackers.should be_empty + @app.issue_tracker.should be_nil response.body.should match(/You must specify your Lighthouseapp account, token and project id/) end it "should show validation notice when sufficient params are not present" do - put :update, :id => @app.id, :app => { :issue_trackers_attributes => { '0' => { + put :update, :id => @app.id, :app => { :issue_tracker_attributes => { :issue_tracker_type => 'lighthouseapp', :project_id => '1234', :api_token => '123123' - } } } + } } @app.reload - @app.issue_trackers.should be_empty + @app.issue_tracker.should be_nil response.body.should match(/You must specify your Lighthouseapp account, token and project id/) end end -- libgit2 0.21.2