Commit f73d0b9

mo khan <mo@mokhan.ca>
2015-02-04 03:52:31
add scaffold for dispositions.
1 parent fe7d2f8
app/assets/javascripts/dispositions.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/
app/assets/stylesheets/dispositions.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the Dispositions controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
app/controllers/dispositions_controller.rb
@@ -0,0 +1,76 @@
+class DispositionsController < ApplicationController
+  before_action :set_disposition, only: [:show, :edit, :update, :destroy]
+
+  # GET /dispositions
+  # GET /dispositions.json
+  def index
+    @dispositions = Disposition.all
+  end
+
+  # GET /dispositions/1
+  # GET /dispositions/1.json
+  def show
+  end
+
+  # GET /dispositions/new
+  def new
+    @disposition = Disposition.new
+    @states = Disposition.states
+  end
+
+  # GET /dispositions/1/edit
+  def edit
+    @states = Disposition.states
+  end
+
+  # POST /dispositions
+  # POST /dispositions.json
+  def create
+    @disposition = Disposition.new(disposition_params)
+
+    respond_to do |format|
+      if @disposition.save
+        format.html { redirect_to @disposition, notice: 'Disposition was successfully created.' }
+        format.json { render :show, status: :created, location: @disposition }
+      else
+        format.html { render :new }
+        format.json { render json: @disposition.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+
+  # PATCH/PUT /dispositions/1
+  # PATCH/PUT /dispositions/1.json
+  def update
+    respond_to do |format|
+      if @disposition.update(disposition_params)
+        format.html { redirect_to @disposition, notice: 'Disposition was successfully updated.' }
+        format.json { render :show, status: :ok, location: @disposition }
+      else
+        format.html { render :edit }
+        format.json { render json: @disposition.errors, status: :unprocessable_entity }
+      end
+    end
+  end
+
+  # DELETE /dispositions/1
+  # DELETE /dispositions/1.json
+  def destroy
+    @disposition.destroy
+    respond_to do |format|
+      format.html { redirect_to dispositions_url, notice: 'Disposition was successfully destroyed.' }
+      format.json { head :no_content }
+    end
+  end
+
+  private
+    # Use callbacks to share common setup or constraints between actions.
+    def set_disposition
+      @disposition = Disposition.find_by(fingerprint: params[:id])
+    end
+
+    # Never trust parameters from the scary internet, only allow the white list through.
+    def disposition_params
+      params.require(:disposition).permit(:fingerprint, :state)
+    end
+end
app/helpers/dispositions_helper.rb
@@ -0,0 +1,2 @@
+module DispositionsHelper
+end
app/models/disposition.rb
@@ -0,0 +1,10 @@
+class Disposition < ActiveRecord::Base
+  enum state: [ :clean, :malicious, :unknown ]
+  attr_readonly :fingerprint
+
+  validates_uniqueness_of :fingerprint
+
+  def to_param
+    fingerprint
+  end
+end
app/views/dispositions/_form.html.erb
@@ -0,0 +1,25 @@
+<%= form_for(@disposition) do |f| %>
+  <% if @disposition.errors.any? %>
+    <div id="error_explanation">
+      <h2><%= pluralize(@disposition.errors.count, "error") %> prohibited this disposition from being saved:</h2>
+
+      <ul>
+      <% @disposition.errors.full_messages.each do |message| %>
+        <li><%= message %></li>
+      <% end %>
+      </ul>
+    </div>
+  <% end %>
+
+  <div class="field">
+    <%= f.label :fingerprint %><br>
+    <%= f.text_field :fingerprint %>
+  </div>
+  <div class="field">
+    <%= f.label :state %><br>
+    <%= f.select :state, options_for_select(@states.collect { |s| [s[0].humanize, s[0]] }, selected: @disposition.state) %>
+  </div>
+  <div class="actions">
+    <%= f.submit %>
+  </div>
+<% end %>
app/views/dispositions/edit.html.erb
@@ -0,0 +1,6 @@
+<h1>Editing Disposition</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @disposition %> |
+<%= link_to 'Back', dispositions_path %>
app/views/dispositions/index.html.erb
@@ -0,0 +1,29 @@
+<p id="notice"><%= notice %></p>
+
+<h1>Listing Dispositions</h1>
+
+<table>
+  <thead>
+    <tr>
+      <th>Fingerprint</th>
+      <th>State</th>
+      <th colspan="3"></th>
+    </tr>
+  </thead>
+
+  <tbody>
+    <% @dispositions.each do |disposition| %>
+      <tr>
+        <td><%= disposition.fingerprint %></td>
+        <td><%= disposition.state %></td>
+        <td><%= link_to 'Show', disposition %></td>
+        <td><%= link_to 'Edit', edit_disposition_path(disposition) %></td>
+        <td><%= link_to 'Destroy', disposition, method: :delete, data: { confirm: 'Are you sure?' } %></td>
+      </tr>
+    <% end %>
+  </tbody>
+</table>
+
+<br>
+
+<%= link_to 'New Disposition', new_disposition_path %>
app/views/dispositions/index.json.jbuilder
@@ -0,0 +1,4 @@
+json.array!(@dispositions) do |disposition|
+  json.extract! disposition, :id, :fingerprint, :state
+  json.url disposition_url(disposition, format: :json)
+end
app/views/dispositions/new.html.erb
@@ -0,0 +1,5 @@
+<h1>New Disposition</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', dispositions_path %>
app/views/dispositions/show.html.erb
@@ -0,0 +1,14 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+  <strong>Fingerprint:</strong>
+  <%= @disposition.fingerprint %>
+</p>
+
+<p>
+  <strong>State:</strong>
+  <%= @disposition.state %>
+</p>
+
+<%= link_to 'Edit', edit_disposition_path(@disposition) %> |
+<%= link_to 'Back', dispositions_path %>
app/views/dispositions/show.json.jbuilder
@@ -0,0 +1,1 @@
+json.extract! @disposition, :id, :fingerprint, :state, :created_at, :updated_at
config/routes.rb
@@ -1,4 +1,5 @@
 Rails.application.routes.draw do
+  resources :dispositions
   resources :events, only: [:index, :new, :create, :destroy]
   root 'events#index'
 end
db/migrate/20150204033540_create_dispositions.rb
@@ -0,0 +1,11 @@
+class CreateDispositions < ActiveRecord::Migration
+  def change
+    create_table :dispositions, id: :uuid, default: 'uuid_generate_v4()' do |t|
+      t.string :fingerprint
+      t.integer :state
+
+      t.timestamps null: false
+    end
+    add_index :dispositions, :fingerprint, unique: true
+  end
+end
db/schema.rb
@@ -11,12 +11,19 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20150203043317) do
+ActiveRecord::Schema.define(version: 20150204033540) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
   enable_extension "uuid-ossp"
 
+  create_table "dispositions", id: :uuid, default: "uuid_generate_v4()", force: :cascade do |t|
+    t.string   "fingerprint"
+    t.integer  "state"
+    t.datetime "created_at",  null: false
+    t.datetime "updated_at",  null: false
+  end
+
   create_table "events", id: :uuid, default: "uuid_generate_v4()", force: :cascade do |t|
     t.string   "name"
     t.json     "data"