Skip to content

Commit

Permalink
Add ability to add slack integration points
Browse files Browse the repository at this point in the history
  • Loading branch information
carsonmcdonald committed Jul 2, 2015
1 parent 026a1f2 commit b282d38
Show file tree
Hide file tree
Showing 21 changed files with 221 additions and 16 deletions.
3 changes: 3 additions & 0 deletions app/assets/javascripts/slack_integrations.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/slack_integrations.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the slack_integrations controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
82 changes: 82 additions & 0 deletions app/controllers/slack_integrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
class SlackIntegrationsController < ApplicationController
before_action :set_integration, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!

def index
if current_user.admin?
@slack_integrations = SlackIntegration.all
else
render_404
end
end

def show
render_404 unless current_user.admin?
end

def new
if current_user.admin?
@slack_integration = SlackIntegration.new
else
render_404
end
end

def edit
render_404 unless current_user.admin?
end

def create
if current_user.admin?
@slack_integration = SlackIntegration.new(slack_integration_params)

if @slack_integration.save
redirect_to @slack_integration, notice: 'Integration was successfully created.'
else
render :new
end

else
render_404
end
end

def update
if current_user.admin?
if @slack_integration.update(slack_integration_params)
redirect_to @slack_integration, notice: 'Integration was successfully updated.'
else
render :edit
end
else
render_404
end
end

def destroy
if current_user.admin?
@slack_integration.destroy
redirect_to uuids_url, notice: 'Integration was successfully destroyed.'
else
render_404
end
end

private
def set_integration
@slack_integration = SlackIntegration.find(params[:id])
end

def slack_integration_params
params.require(:slack_integration).permit(:slack_integration, :hook_url)
end

def render_404
respond_to do |format|
format.html { render :file => "#{Rails.root}/public/404", :layout => false, :status => :not_found }
format.xml { head :not_found }
format.any { head :not_found }
end
end

end
19 changes: 10 additions & 9 deletions app/controllers/slack_posts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class SlackPostsController < ApplicationController
skip_before_filter :verify_authenticity_token
#protect_from_forgery with: :null_session

def index
end
Expand All @@ -22,10 +21,7 @@ def create_entry
render :new, status: 500
end

response = HTTParty.post(
Rails.application.secrets.address,
:body => {"text" => '@%2$s just entered %1$s' % [@slack_post.location, @slack_post.name]
}.to_json)
post_to_integrations('@%2$s just entered %1$s' % [@slack_post.location, @slack_post.name])
end

def create_exit
Expand All @@ -38,15 +34,20 @@ def create_exit
render :new, status: 500
end

response = HTTParty.post(
Rails.application.secrets.address,
:body => {"text" => '@%2$s just left %1$s' % [@slack_post.location, @slack_post.name]
}.to_json)
post_to_integrations('@%2$s just left %1$s' % [@slack_post.location, @slack_post.name])
end


private
def slack_post_params
params.require(:slack_post).permit(:name, :location)
end

def post_to_integrations(message)
SlackIntegration.all.each { |integration|
response = HTTParty.post(
integration.hook_url,
:body => {'text' => message}.to_json)
}
end
end
2 changes: 2 additions & 0 deletions app/helpers/slack_integrations_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module SlackIntegrationsHelper
end
2 changes: 0 additions & 2 deletions app/helpers/welcome_helper.rb

This file was deleted.

2 changes: 2 additions & 0 deletions app/models/slack_integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class SlackIntegration < ActiveRecord::Base
end
1 change: 1 addition & 0 deletions app/views/admin/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

<ul>
<li><a href="/admin/uuids">Manage UUIDs</a></li>
<li><a href="/admin/slack_integrations">Manage Slack Integration</a></li>
</ul>

21 changes: 21 additions & 0 deletions app/views/slack_integrations/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<%= form_for(@slack_integration) do |f| %>
<% if @slack_integration.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@slack_integration.errors.count, "error") %> prohibited this integration from being saved:</h2>

<ul>
<% @slack_integration.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :hook_url %><br>
<%= f.text_field :hook_url %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/slack_integrations/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>Editing Integration</h1>

<%= render 'form' %>

<%= link_to 'Show', @slack_integration %> |
<%= link_to 'Back', slack_integrations_path %>
28 changes: 28 additions & 0 deletions app/views/slack_integrations/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<p id="notice"><%= notice %></p>

<h1>Integrations</h1>

<table>
<thead>
<tr>
<th>Hook URL</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @slack_integrations.each do |integration| %>
<tr>
<td><%= integration.hook_url %></td>
<td><%= link_to 'Show', integration %></td>
<td><%= link_to 'Edit', edit_slack_integration_path(integration) %></td>
<td><%= link_to 'Destroy', integration, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Integration', new_slack_integration_path %>
<%= link_to 'Back', controller: :admin, action: :index %>
5 changes: 5 additions & 0 deletions app/views/slack_integrations/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>New Integration</h1>

<%= render 'form' %>

<%= link_to 'Back', slack_integrations_path %>
9 changes: 9 additions & 0 deletions app/views/slack_integrations/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Integration:</strong>
<%= @slack_integration.hook_url %>
</p>

<%= link_to 'Edit', edit_slack_integration_path(@slack_integration) %> |
<%= link_to 'Back', slack_integrations_path %>
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
get '/' => 'admin#index'
resources :uuids
end
resources :slack_integrations
end
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

# You can have the root of your site routed with "root"
#root 'welcome#index'
#root 'welcome#index'

# Example of regular route:
# get 'products/:id' => 'catalog#view'
Expand Down
3 changes: 0 additions & 3 deletions config/secrets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@

development:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
address: <%= ENV["WEBHOOK"] %>
test:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
address: <%=ENV["WEBHOOK"] %>
production:
secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
address: <%= ENV["WEBHOOK"] %>
9 changes: 9 additions & 0 deletions db/migrate/20150701204201_create_slack_integrations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateSlackIntegrations < ActiveRecord::Migration
def change
create_table :slack_integrations do |t|
t.text :hook_url

t.timestamps null: false
end
end
end
8 changes: 7 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150612181707) do
ActiveRecord::Schema.define(version: 20150701204201) do

create_table "slack_integrations", force: :cascade do |t|
t.text "hook_url"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "slack_posts", force: :cascade do |t|
t.string "name"
Expand Down
5 changes: 5 additions & 0 deletions spec/controllers/slack_integrations_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe SlackIntegrationsController, type: :controller do

end
6 changes: 6 additions & 0 deletions spec/factories/slack_integrations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FactoryGirl.define do
factory :slack_integration do

end

end
15 changes: 15 additions & 0 deletions spec/helpers/slack_integrations_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

# Specs in this file have access to a helper object that includes
# the SlackIntegrationsHelper. For example:
#
# describe SlackIntegrationsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe SlackIntegrationsHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/slack_integration_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe SlackIntegration, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit b282d38

Please sign in to comment.