Skip to content

Commit

Permalink
polymorphic change, subscription, slack integration models
Browse files Browse the repository at this point in the history
  • Loading branch information
ivarvong committed Apr 15, 2016
1 parent 964a85e commit f6abbae
Show file tree
Hide file tree
Showing 45 changed files with 772 additions and 4 deletions.
2 changes: 2 additions & 0 deletions app/assets/javascripts/integrations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
2 changes: 2 additions & 0 deletions app/assets/javascripts/slack_integrations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
3 changes: 3 additions & 0 deletions app/assets/stylesheets/integrations.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the integrations controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
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/
4 changes: 4 additions & 0 deletions app/controllers/integrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class IntegrationsController < ApplicationController
def index
end
end
58 changes: 58 additions & 0 deletions app/controllers/slack_integrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class SlackIntegrationsController < ApplicationController
before_action :set_slack_integration, only: [:show, :edit, :update, :destroy]

# GET /slack_integrations
def index
@slack_integrations = SlackIntegration.all
end

# GET /slack_integrations/1
def show
end

# GET /slack_integrations/new
def new
@slack_integration = SlackIntegration.new
end

# GET /slack_integrations/1/edit
def edit
end

# POST /slack_integrations
def create
@slack_integration = SlackIntegration.new(slack_integration_params)

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

# PATCH/PUT /slack_integrations/1
def update
if @slack_integration.update(slack_integration_params)
redirect_to @slack_integration, notice: 'Slack integration was successfully updated.'
else
render :edit
end
end

# DELETE /slack_integrations/1
def destroy
@slack_integration.destroy
redirect_to slack_integrations_url, notice: 'Slack integration was successfully destroyed.'
end

private
# Use callbacks to share common setup or constraints between actions.
def set_slack_integration
@slack_integration = SlackIntegration.find(params[:id])
end

# Only allow a trusted parameter "white list" through.
def slack_integration_params
params.require(:slack_integration).permit(:channel, :webhook_url)
end
end
2 changes: 2 additions & 0 deletions app/helpers/integrations_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module IntegrationsHelper
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
17 changes: 17 additions & 0 deletions app/models/change.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Change < ActiveRecord::Base
belongs_to :before, polymorphic: true
belongs_to :after, polymorphic: true

validate :correct_ordering
def correct_ordering
# delegate order checking to the attached models
before.created_at < after.created_at
end

def notify!
Subscriptions.where(watching: self.after).map do |subscription|
subscription.notify!(change)
end
end

end
2 changes: 2 additions & 0 deletions app/models/page.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Page < ActiveRecord::Base
belongs_to :user
has_many :page_snapshots

def to_param
[id, self.name.parameterize].join('-')
Expand Down Expand Up @@ -39,6 +40,7 @@ def hash
end

def scrape
# TODO
puts "current hash for #{self.url} is #{self.hash}"
end

Expand Down
5 changes: 5 additions & 0 deletions app/models/page_snapshot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class PageSnapshot < ActiveRecord::Base
belongs_to :page
validates :page, presence: true

end
18 changes: 18 additions & 0 deletions app/models/slack_integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class SlackIntegration < ActiveRecord::Base

def subscriptions
Subscription.where(watcher: self)
end

def watching
subscriptions.map(&:watching)
end

def subscribe(watchable)
# TODO: extract this. also in User
Subscription.where(watcher: self, watching: watchable).first_or_create do |subscription|
puts "#{self} subscribed to #{watchable}"
end
end

end
9 changes: 9 additions & 0 deletions app/models/subscription.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Subscription < ActiveRecord::Base
belongs_to :watcher, polymorphic: true
belongs_to :watching, polymorphic: true

def notify!(change)
Notification.send(subscription: self, change: change)
end

end
18 changes: 18 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,22 @@ def display_name
full_name.present? ? full_name : email
end

def subscriptions
Subscription.where(watcher: self)
end

def watching
subscriptions.map(&:watching)
end

def subscribe(watchable)
Subscription.where(watcher: self, watching: watchable).first_or_create do |subscription|
puts "#{self} subscribed to #{watchable}"
end
end

def unsubscribe(watchable)
Subscription.where(watcher: self, watching: watchable).destroy_all
end

end
2 changes: 2 additions & 0 deletions app/views/integrations/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Integrations#index</h1>
<p>Find me in app/views/integrations/index.html.erb</p>
12 changes: 12 additions & 0 deletions app/views/slack_integrations/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<%= simple_form_for(@slack_integration) do |f| %>
<%= f.error_notification %>

<div class="form-inputs">
<%= f.input :channel %>
<%= f.input :webhook_url %>
</div>

<div class="form-actions">
<%= f.button :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 Slack Integration</h1>

<%= render 'form' %>

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

<h1>Listing Slack Integrations</h1>

<table>
<thead>
<tr>
<th>Channel</th>
<th>Webhook url</th>
<th colspan="3"></th>
</tr>
</thead>

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

<br>

<%= link_to 'New Slack integration', new_slack_integration_path %>
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 Slack Integration</h1>

<%= render 'form' %>

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

<p>
<strong>Channel:</strong>
<%= @slack_integration.channel %>
</p>

<p>
<strong>Webhook url:</strong>
<%= @slack_integration.webhook_url %>
</p>

<%= link_to 'Edit', edit_slack_integration_path(@slack_integration) %> |
<%= link_to 'Back', slack_integrations_path %>
2 changes: 2 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class Application < Rails::Application
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de

config.autoload_paths << "#{Rails.root}/lib"

# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true

Expand Down
7 changes: 7 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Rails.application.routes.draw do

get 'integrations/index'

root 'static#feed'

scope '/watching' do
Expand All @@ -12,6 +14,11 @@
get 'iframe' => 'embed#iframe'
end

scope '/integrations' do
get '/' => 'integrations#index'
resources :slack, as: 'slack_integrations', controller: 'slack_integrations'
end

get '/help' => 'static#help', as: :help
get '/feed' => 'static#feed', as: :feed
get '/diff' => 'static#index', as: :diff
Expand Down
17 changes: 17 additions & 0 deletions db/migrate/20160415154706_create_changes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class CreateChanges < ActiveRecord::Migration
def change
create_table :changes do |t|
t.integer :prev_id
t.string :prev_type
t.integer :next_id
t.string :next_type
t.text :summary

t.timestamps null: false
end
add_index :changes, :prev_id
add_index :changes, :prev_type
add_index :changes, :next_id
add_index :changes, :next_type
end
end
10 changes: 10 additions & 0 deletions db/migrate/20160415154722_create_slack_integrations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateSlackIntegrations < ActiveRecord::Migration
def change
create_table :slack_integrations do |t|
t.string :channel
t.text :webhook_url

t.timestamps null: false
end
end
end
16 changes: 16 additions & 0 deletions db/migrate/20160415155125_create_subscriptions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateSubscriptions < ActiveRecord::Migration
def change
create_table :subscriptions do |t|
t.integer :watcher_id
t.string :watcher_type
t.integer :watching_id
t.string :watching_type

t.timestamps null: false
end
add_index :subscriptions, :watcher_id
add_index :subscriptions, :watcher_type
add_index :subscriptions, :watching_id
add_index :subscriptions, :watching_type
end
end
11 changes: 11 additions & 0 deletions db/migrate/20160415171122_create_page_snapshots.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreatePageSnapshots < ActiveRecord::Migration
def change
create_table :page_snapshots do |t|
t.integer :page_id
t.text :s3_url

t.timestamps null: false
end
add_index :page_snapshots, :page_id
end
end
9 changes: 9 additions & 0 deletions db/migrate/20160415181940_rename_cols_on_changes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class RenameColsOnChanges < ActiveRecord::Migration
def change
rename_column :changes, :prev_id, :before_id
rename_column :changes, :prev_type, :before_type

rename_column :changes, :next_id, :after_id
rename_column :changes, :next_type, :after_type
end
end
Loading

0 comments on commit f6abbae

Please sign in to comment.