forked from hubertlepicki/KarmaTracker
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor pivotal_tracke angular controller to use integrations_factor…
…y and new unified create action frfom api
- Loading branch information
Bartlomiej Skwira
committed
Feb 9, 2014
1 parent
9e97770
commit 236cb37
Showing
7 changed files
with
48 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 28 additions & 45 deletions
73
app/assets/javascripts/controllers/pivotal_tracker_integrations.js.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,60 @@ | ||
KarmaTracker.controller "PivotalTrackerIntegrationsController", ($scope, $http, $cookieStore, $location) -> | ||
KarmaTracker.controller "PivotalTrackerIntegrationsController", ['$scope', 'Integration', ($scope, Integration) -> | ||
$scope.integrations = [] | ||
$scope.newIntegration = { api_key: null, email: null, password: null } | ||
$scope.addFormShown = false | ||
$scope.errors = {} | ||
$scope.tokenName = 'token' | ||
integrationService = new Integration | ||
|
||
|
||
$scope.updateIntegrations = -> | ||
$http.get( | ||
'/api/v1/integrations?token='+$cookieStore.get($scope.tokenName)+'&service=pivotal_tracker' | ||
).success((data, status, headers, config) -> | ||
$scope.integrations = data | ||
).error((data, status, headers, config) -> | ||
) | ||
integrationService.query('pivotal_tracker').$promise.then (result) -> | ||
$scope.integrations = result | ||
|
||
$scope.remove = (id) -> | ||
answer = confirm("Are you sure to remove Pivotal Tracker Integration?") | ||
if answer | ||
$http.delete( | ||
'/api/v1/integrations/'+id+'?token='+$cookieStore.get($scope.tokenName)+'&service=pivotal_tracker' | ||
).success((data, status, headers, config) -> | ||
$scope.updateIntegrations() | ||
).error((data, status, headers, config) -> | ||
) | ||
$scope.remove = (integration_id) -> | ||
if confirm("Are you sure you want to remove Pivotal Tracker integration?") | ||
integrationService.remove(integration_id).$promise.then (result) -> | ||
index = $scope.integrations.map((integration) -> | ||
integration.pivotal_tracker.id | ||
).indexOf(integration_id) | ||
$scope.integrations.splice(index, 1) if index > -1 | ||
|
||
$scope.formLooksValid = () -> | ||
valid = true | ||
$scope.errors = {} | ||
|
||
unless $scope.newIntegration.api_key? or $scope.newIntegration.api_key == '' | ||
for field in ["email", "password"] | ||
for field in ["username", "password"] | ||
unless $scope.newIntegration[field]? and $scope.newIntegration[field] != '' | ||
$scope.errors[field] = "can't be blank" | ||
valid = false | ||
|
||
valid | ||
|
||
$scope.add = () -> | ||
$scope.add = -> | ||
if $scope.formLooksValid() | ||
if $scope.newIntegration.api_key? and $scope.newIntegration.api_key != '' | ||
$http.post( | ||
'/api/v1/integrations/pivotal_tracker?token='+$cookieStore.get($scope.tokenName)+'&integration[api_key]='+$scope.newIntegration.api_key | ||
).success((data, status, headers, config) -> | ||
$scope.cleanForm() | ||
$scope.openAddForm() | ||
$scope.updateIntegrations() | ||
).error((data, status, headers, config) -> | ||
$scope.errors['api_key'] = data.pivotal_tracker.errors.api_key[0] | ||
) | ||
else | ||
$http.post( | ||
'/api/v1/integrations/pivotal_tracker?token='+$cookieStore.get($scope.tokenName)+'&integration[email]='+$scope.newIntegration.email+'&integration[password]='+$scope.newIntegration.password | ||
).success((data, status, headers, config) -> | ||
$scope.cleanForm() | ||
$scope.newIntegration['type'] = 'pivotal_tracker' | ||
integrationService.save($scope.newIntegration).$promise | ||
.then (result) -> | ||
$scope.openAddForm() | ||
$scope.updateIntegrations() | ||
).error((data, status, headers, config) -> | ||
$scope.newIntegration.email = '' | ||
$scope.newIntegration.password = '' | ||
if data.pivotal_tracker.errors.api_key? | ||
$scope.errors.password = data.pivotal_tracker.errors.api_key[0] | ||
$scope.integrations.push result | ||
.catch (response) -> | ||
data = response.data.pivotal_tracker | ||
if data.errors.api_key? | ||
$scope.errors.api_key = data.errors.api_key[0] | ||
$scope.errors.password = data.errors.api_key[0] | ||
else | ||
$scope.errors.password = data.pivotal_tracker.errors.password[0] | ||
) | ||
$scope.errors.username_token = data.errors.username[0] if data.errors.username? | ||
$scope.errors.password = data.errors.password[0] if data.errors.password? | ||
|
||
$scope.openAddForm = () -> | ||
$scope.openAddForm = -> | ||
$scope.addFormShown = !$scope.addFormShown | ||
$scope.cleanForm() | ||
|
||
$scope.cleanForm = () -> | ||
$scope.cleanForm = -> | ||
$scope.newIntegration.email = '' | ||
$scope.newIntegration.password = '' | ||
$scope.newIntegration.api_key ='' | ||
$scope.errors = {} | ||
|
||
|
||
$scope.updateIntegrations() | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,15 +136,15 @@ def show | |
# => {"pivotal_tracker": {"email": "[email protected]", "password": "wrong_password", | ||
# "errors": { "password": ["does not match email"] }}} | ||
# | ||
def pivotal_tracker | ||
options = (params[:integration] || {}).merge({user_id: @current_user.id}) | ||
@integration = IntegrationsFactory.new(PivotalTrackerIntegration.new, options).create | ||
if @integration.save | ||
render '_show' | ||
else | ||
render '_show', status: 422 | ||
end | ||
end | ||
# def pivotal_tracker | ||
#options = (params[:integration] || {}).merge({user_id: @current_user.id}) | ||
#@integration = IntegrationsFactory.new(PivotalTrackerIntegration.new, options).create | ||
#if @integration.save | ||
#render '_show' | ||
#else | ||
#render '_show', status: 422 | ||
#end | ||
#end | ||
|
||
def create | ||
options = (params[:integration].reject{ |key, value| value.empty?} || {}).merge({user_id: @current_user.id}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters