changeset 2:42de15334db1

Added the pastes.
author Edho Arief <edho@myconan.net>
date Sun, 26 Aug 2012 11:28:23 -0700
parents 49c0949ee47e
children 04a43fae272c
files app/assets/javascripts/pastes.js app/assets/stylesheets/pastes.css.scss app/assets/stylesheets/scaffolds.css.scss app/controllers/pastes_controller.rb app/helpers/pastes_helper.rb app/models/paste.rb app/views/pastes/_form.html.erb app/views/pastes/edit.html.erb app/views/pastes/index.html.erb app/views/pastes/new.html.erb app/views/pastes/show.html.erb config/routes.rb db/migrate/20120826180414_create_pastes.rb db/migrate/20120826182012_change_column_name_to_conform_ar.rb db/schema.rb
diffstat 15 files changed, 285 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/assets/javascripts/pastes.js	Sun Aug 26 11:28:23 2012 -0700
@@ -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.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/assets/stylesheets/pastes.css.scss	Sun Aug 26 11:28:23 2012 -0700
@@ -0,0 +1,3 @@
+// Place all the styles related to the pastes controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/assets/stylesheets/scaffolds.css.scss	Sun Aug 26 11:28:23 2012 -0700
@@ -0,0 +1,69 @@
+body {
+  background-color: #fff;
+  color: #333;
+  font-family: verdana, arial, helvetica, sans-serif;
+  font-size: 13px;
+  line-height: 18px;
+}
+
+p, ol, ul, td {
+  font-family: verdana, arial, helvetica, sans-serif;
+  font-size: 13px;
+  line-height: 18px;
+}
+
+pre {
+  background-color: #eee;
+  padding: 10px;
+  font-size: 11px;
+}
+
+a {
+  color: #000;
+  &:visited {
+    color: #666;
+  }
+  &:hover {
+    color: #fff;
+    background-color: #000;
+  }
+}
+
+div {
+  &.field, &.actions {
+    margin-bottom: 10px;
+  }
+}
+
+#notice {
+  color: green;
+}
+
+.field_with_errors {
+  padding: 2px;
+  background-color: red;
+  display: table;
+}
+
+#error_explanation {
+  width: 450px;
+  border: 2px solid red;
+  padding: 7px;
+  padding-bottom: 0;
+  margin-bottom: 20px;
+  background-color: #f0f0f0;
+  h2 {
+    text-align: left;
+    font-weight: bold;
+    padding: 5px 5px 5px 15px;
+    font-size: 12px;
+    margin: -7px;
+    margin-bottom: 0px;
+    background-color: #c00;
+    color: #fff;
+  }
+  ul li {
+    font-size: 12px;
+    list-style: square;
+  }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/controllers/pastes_controller.rb	Sun Aug 26 11:28:23 2012 -0700
@@ -0,0 +1,85 @@
+class PastesController < ApplicationController
+  # GET /pastes
+  # GET /pastes.json
+  def index
+    @pastes = Paste.all
+
+    respond_to do |format|
+      format.html # index.html.erb
+      format.json { render :json => @pastes }
+    end
+  end
+
+  # GET /pastes/1
+  # GET /pastes/1.json
+  def show
+    @paste = Paste.find(params[:id])
+
+    respond_to do |format|
+      format.html # show.html.erb
+      format.json { render :json => @paste }
+    end
+  end
+
+  # GET /pastes/new
+  # GET /pastes/new.json
+  def new
+    @paste = Paste.new
+
+    respond_to do |format|
+      format.html # new.html.erb
+      format.json { render :json => @paste }
+    end
+  end
+
+  # GET /pastes/1/edit
+  def edit
+    @paste = Paste.find(params[:id])
+  end
+
+  # POST /pastes
+  # POST /pastes.json
+  def create
+    @paste = Paste.new
+    @paste.paste = params[:paste][:paste]
+    @paste.ip = request.remote_ip
+
+    respond_to do |format|
+      if @paste.save
+        format.html { redirect_to @paste, :notice => 'Paste was successfully created.' }
+        format.json { render :json => @paste, :status => :created, :location => @paste }
+      else
+        format.html { render :action => "new" }
+        format.json { render :json => @paste.errors, :status => :unprocessable_entity }
+      end
+    end
+  end
+
+  # PUT /pastes/1
+  # PUT /pastes/1.json
+  def update
+    @paste = Paste.find(params[:id])
+
+    respond_to do |format|
+      if @paste.update_attributes(params[:paste])
+        format.html { redirect_to @paste, :notice => 'Paste was successfully updated.' }
+        format.json { head :no_content }
+      else
+        format.html { render :action => "edit" }
+        format.json { render :json => @paste.errors, :status => :unprocessable_entity }
+      end
+    end
+  end
+
+  # DELETE /pastes/1
+  # DELETE /pastes/1.json
+  def destroy
+    @paste = Paste.find(params[:id])
+    @paste.destroy
+
+    respond_to do |format|
+      format.html { redirect_to pastes_url }
+      format.json { head :no_content }
+    end
+  end
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/helpers/pastes_helper.rb	Sun Aug 26 11:28:23 2012 -0700
@@ -0,0 +1,2 @@
+module PastesHelper
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/models/paste.rb	Sun Aug 26 11:28:23 2012 -0700
@@ -0,0 +1,10 @@
+class Paste < ActiveRecord::Base
+  attr_accessible :paste
+  before_validation :set_paste_hash
+  validates :paste, :paste_hash, :ip, :presence => true
+  validates :paste, :length => { :maximum => 1_000_000 }
+
+  def set_paste_hash
+    self.paste_hash = Digest::SHA512.hexdigest(paste)
+  end
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/views/pastes/_form.html.erb	Sun Aug 26 11:28:23 2012 -0700
@@ -0,0 +1,21 @@
+<%= form_for(@paste) do |f| %>
+  <% if @paste.errors.any? %>
+    <div id="error_explanation">
+      <h2><%= pluralize(@paste.errors.count, "error") %> prohibited this paste from being saved:</h2>
+
+      <ul>
+      <% @paste.errors.full_messages.each do |msg| %>
+        <li><%= msg %></li>
+      <% end %>
+      </ul>
+    </div>
+  <% end %>
+
+  <div class="field">
+    <%= f.label :paste %><br />
+    <%= f.text_area :paste %>
+  </div>
+  <div class="actions">
+    <%= f.submit %>
+  </div>
+<% end %>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/views/pastes/edit.html.erb	Sun Aug 26 11:28:23 2012 -0700
@@ -0,0 +1,6 @@
+<h1>Editing paste</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @paste %> |
+<%= link_to 'Back', pastes_path %>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/views/pastes/index.html.erb	Sun Aug 26 11:28:23 2012 -0700
@@ -0,0 +1,23 @@
+<h1>Listing pastes</h1>
+
+<table>
+  <tr>
+    <th>Id</th>
+    <th>Paste</th>
+    <th></th>
+    <th></th>
+  </tr>
+
+<% @pastes.each do |paste| %>
+  <tr>
+    <td><%= link_to paste.id, paste %></td>
+    <td><%= paste.paste %></td>
+    <td><%= link_to 'Edit', edit_paste_path(paste) %></td>
+    <td><%= link_to 'Destroy', paste, :method => :delete, :data => { :confirm => 'Are you sure?' } %></td>
+  </tr>
+<% end %>
+</table>
+
+<br />
+
+<%= link_to 'New Paste', new_paste_path %>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/views/pastes/new.html.erb	Sun Aug 26 11:28:23 2012 -0700
@@ -0,0 +1,5 @@
+<h1>New paste</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', pastes_path %>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/views/pastes/show.html.erb	Sun Aug 26 11:28:23 2012 -0700
@@ -0,0 +1,15 @@
+<p id="notice"><%= notice %></p>
+
+<p>
+  <b>Paste:</b>
+  <%= @paste.paste %>
+</p>
+
+<p>
+  <b>Hash:</b>
+  <%= @paste.paste_hash %>
+</p>
+
+
+<%= link_to 'Edit', edit_paste_path(@paste) %> |
+<%= link_to 'Back', pastes_path %>
--- a/config/routes.rb	Sun Aug 26 11:03:59 2012 -0700
+++ b/config/routes.rb	Sun Aug 26 11:28:23 2012 -0700
@@ -1,4 +1,6 @@
 Zeropaste::Application.routes.draw do
+  resources :pastes
+
   # The priority is based upon order of creation:
   # first created -> highest priority.
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/db/migrate/20120826180414_create_pastes.rb	Sun Aug 26 11:28:23 2012 -0700
@@ -0,0 +1,12 @@
+class CreatePastes < ActiveRecord::Migration
+  def change
+    create_table :pastes do |t|
+      t.string :ip, :null => false
+      t.text :paste, :null => false
+      t.string :hash, :null => false
+
+      t.timestamps
+    end
+    add_index :pastes, [:ip, :hash], :unique => true
+  end
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/db/migrate/20120826182012_change_column_name_to_conform_ar.rb	Sun Aug 26 11:28:23 2012 -0700
@@ -0,0 +1,5 @@
+class ChangeColumnNameToConformAr < ActiveRecord::Migration
+  def change
+    rename_column :pastes, :hash, :paste_hash
+  end
+end
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/db/schema.rb	Sun Aug 26 11:28:23 2012 -0700
@@ -0,0 +1,25 @@
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended to check this file into your version control system.
+
+ActiveRecord::Schema.define(:version => 20120826182012) do
+
+  create_table "pastes", :force => true do |t|
+    t.string   "ip",         :null => false
+    t.text     "paste",      :null => false
+    t.string   "paste_hash", :null => false
+    t.datetime "created_at", :null => false
+    t.datetime "updated_at", :null => false
+  end
+
+  add_index "pastes", ["ip", "paste_hash"], :name => "index_pastes_on_ip_and_hash", :unique => true
+
+end