diff --git a/app/assets/javascripts/slack_integrations.coffee b/app/assets/javascripts/slack_integrations.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/slack_integrations.coffee @@ -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/ diff --git a/app/assets/stylesheets/slack_integrations.scss b/app/assets/stylesheets/slack_integrations.scss new file mode 100644 index 0000000..6bbf5ba --- /dev/null +++ b/app/assets/stylesheets/slack_integrations.scss @@ -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/ diff --git a/app/controllers/slack_integrations_controller.rb b/app/controllers/slack_integrations_controller.rb new file mode 100644 index 0000000..6371b96 --- /dev/null +++ b/app/controllers/slack_integrations_controller.rb @@ -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 diff --git a/app/controllers/slack_posts_controller.rb b/app/controllers/slack_posts_controller.rb index 5486901..fa61ab3 100644 --- a/app/controllers/slack_posts_controller.rb +++ b/app/controllers/slack_posts_controller.rb @@ -1,6 +1,5 @@ class SlackPostsController < ApplicationController skip_before_filter :verify_authenticity_token - #protect_from_forgery with: :null_session def index end @@ -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 @@ -38,10 +34,7 @@ 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 @@ -49,4 +42,12 @@ def create_exit 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 diff --git a/app/helpers/slack_integrations_helper.rb b/app/helpers/slack_integrations_helper.rb new file mode 100644 index 0000000..6a299a6 --- /dev/null +++ b/app/helpers/slack_integrations_helper.rb @@ -0,0 +1,2 @@ +module SlackIntegrationsHelper +end diff --git a/app/helpers/welcome_helper.rb b/app/helpers/welcome_helper.rb deleted file mode 100644 index eeead45..0000000 --- a/app/helpers/welcome_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module WelcomeHelper -end diff --git a/app/models/slack_integration.rb b/app/models/slack_integration.rb new file mode 100644 index 0000000..9e743bb --- /dev/null +++ b/app/models/slack_integration.rb @@ -0,0 +1,2 @@ +class SlackIntegration < ActiveRecord::Base +end diff --git a/app/views/admin/index.html.erb b/app/views/admin/index.html.erb index 78633ae..a34b1fc 100644 --- a/app/views/admin/index.html.erb +++ b/app/views/admin/index.html.erb @@ -2,5 +2,6 @@
diff --git a/app/views/slack_integrations/_form.html.erb b/app/views/slack_integrations/_form.html.erb new file mode 100644 index 0000000..f62e662 --- /dev/null +++ b/app/views/slack_integrations/_form.html.erb @@ -0,0 +1,21 @@ +<%= form_for(@slack_integration) do |f| %> + <% if @slack_integration.errors.any? %> +<%= notice %>
+ +Hook URL | ++ | ||
---|---|---|---|
<%= integration.hook_url %> | +<%= link_to 'Show', integration %> | +<%= link_to 'Edit', edit_slack_integration_path(integration) %> | +<%= link_to 'Destroy', integration, method: :delete, data: { confirm: 'Are you sure?' } %> | +
<%= notice %>
+ ++ Integration: + <%= @slack_integration.hook_url %> +
+ +<%= link_to 'Edit', edit_slack_integration_path(@slack_integration) %> | +<%= link_to 'Back', slack_integrations_path %> diff --git a/config/routes.rb b/config/routes.rb index 96d877f..c912dd7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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' diff --git a/config/secrets.yml b/config/secrets.yml index c4c0919..729427f 100644 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -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"] %> diff --git a/db/migrate/20150701204201_create_slack_integrations.rb b/db/migrate/20150701204201_create_slack_integrations.rb new file mode 100644 index 0000000..457217d --- /dev/null +++ b/db/migrate/20150701204201_create_slack_integrations.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index f31d81b..f1fbbff 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" diff --git a/spec/controllers/slack_integrations_controller_spec.rb b/spec/controllers/slack_integrations_controller_spec.rb new file mode 100644 index 0000000..fbb62f9 --- /dev/null +++ b/spec/controllers/slack_integrations_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe SlackIntegrationsController, type: :controller do + +end diff --git a/spec/factories/slack_integrations.rb b/spec/factories/slack_integrations.rb new file mode 100644 index 0000000..d54ac57 --- /dev/null +++ b/spec/factories/slack_integrations.rb @@ -0,0 +1,6 @@ +FactoryGirl.define do + factory :slack_integration do + + end + +end diff --git a/spec/helpers/slack_integrations_helper_spec.rb b/spec/helpers/slack_integrations_helper_spec.rb new file mode 100644 index 0000000..c3c429a --- /dev/null +++ b/spec/helpers/slack_integrations_helper_spec.rb @@ -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 diff --git a/spec/models/slack_integration_spec.rb b/spec/models/slack_integration_spec.rb new file mode 100644 index 0000000..54a14e5 --- /dev/null +++ b/spec/models/slack_integration_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe SlackIntegration, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end