Skip to content
This repository has been archived by the owner on Jul 22, 2019. It is now read-only.

Commit

Permalink
[refactor] extract common logic from chat integration controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
levent committed Feb 8, 2015
1 parent 963ef7d commit 32f235c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 50 deletions.
24 changes: 24 additions & 0 deletions app/controllers/chat_integrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class ChatIntegrationsController < AbstractSecurityController
def create
@chat_integration = chat_klass.new(chat_integration_params)
@chat_integration.project = @project
if @chat_integration.save
flash[:notice] = "Settings saved"
redirect_to edit_project_path(@project)
else
flash[:error] = "Settings could not be saved"
redirect_to edit_project_path(@project)
end
end

def update
@chat_integration = @project.send(chat_klass.name.underscore)
if @chat_integration.update_attributes(chat_integration_params)
flash[:notice] = "Settings updated"
redirect_to edit_project_path(@project)
else
flash[:error] = "Settings could not be updated"
redirect_to edit_project_path(@project)
end
end
end
30 changes: 6 additions & 24 deletions app/controllers/hip_chat_integrations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
class HipChatIntegrationsController < AbstractSecurityController
def create
@hip_chat_integration = HipChatIntegration.new(hip_chat_integration_params)
@hip_chat_integration.project = @project
if @hip_chat_integration.save
flash[:notice] = "HipChat settings saved"
redirect_to edit_project_path(@project)
else
flash[:error] = "HipChat settings could not be saved"
redirect_to edit_project_path(@project)
end
end

def update
@hip_chat_integration = @project.hip_chat_integration
if @hip_chat_integration.update_attributes(hip_chat_integration_params)
flash[:notice] = "HipChat settings updated"
redirect_to edit_project_path(@project)
else
flash[:error] = "HipChat settings could not be updated"
redirect_to edit_project_path(@project)
end
end
class HipChatIntegrationsController < ChatIntegrationsController

private

def hip_chat_integration_params
def chat_klass
HipChatIntegration
end

def chat_integration_params
params[:hip_chat_integration].permit(:token, :room, :notify)
end
end
30 changes: 6 additions & 24 deletions app/controllers/slack_integrations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
class SlackIntegrationsController < AbstractSecurityController
def create
@slack_integration = SlackIntegration.new(slack_integration_params)
@slack_integration.project = @project
if @slack_integration.save
flash[:notice] = "Slack settings saved"
redirect_to edit_project_path(@project)
else
flash[:error] = "Slack settings could not be saved"
redirect_to edit_project_path(@project)
end
end

def update
@slack_integration = @project.slack_integration
if @slack_integration.update_attributes(slack_integration_params)
flash[:notice] = "Slack settings updated"
redirect_to edit_project_path(@project)
else
flash[:error] = "Slack settings could not be updated"
redirect_to edit_project_path(@project)
end
end
class SlackIntegrationsController < ChatIntegrationsController

private

def slack_integration_params
def chat_klass
SlackIntegration
end

def chat_integration_params
params[:slack_integration].permit(:team, :token, :channel)
end
end
4 changes: 2 additions & 2 deletions spec/features/update_project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
fill_in 'Room', :with => 'Room1'
check 'Notify'
click_button 'Save HipChat Settings'
expect(page).to have_content "HipChat settings saved"
expect(page).to have_content "Settings saved"
end

it "creates slack settings" do
Expand All @@ -39,6 +39,6 @@
fill_in 'Channel', :with => 'agileista'
check 'Notify'
click_button 'Save Slack Settings'
expect(page).to have_content "Slack settings saved"
expect(page).to have_content "Settings saved"
end
end

0 comments on commit 32f235c

Please sign in to comment.