From f2dbc7f98178412736e93630f154647c060c4f9f Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Fri, 14 Feb 2014 15:54:22 -0500 Subject: [PATCH] Add inital base paste functionality --- app/controllers/pastes_controller.rb | 28 ++++++++++++++++--- app/models/paste.rb | 3 ++ app/views/layouts/_error.html.erb | 3 ++ app/views/layouts/application.html.erb | 3 +- app/views/pastes/_form.html.erb | 7 +++++ app/views/pastes/edit.html.erb | 2 ++ app/views/pastes/index.html.erb | 4 ++- app/views/pastes/new.html.erb | 2 +- app/views/pastes/show.html.erb | 1 + db/migrate/20140214151745_create_pastes.rb | 8 ++++++ .../20140214152056_add_content_to_pastes.rb | 5 ++++ ...214185753_change_content_type_in_pastes.rb | 9 ++++++ db/schema.rb | 22 +++++++++++++++ spec/features/creates_new_paste_spec.rb | 19 +++++++++++++ spec/models/paste_spec.rb | 18 ++++++++++++ 15 files changed, 127 insertions(+), 7 deletions(-) create mode 100644 app/models/paste.rb create mode 100644 app/views/layouts/_error.html.erb create mode 100644 app/views/pastes/_form.html.erb create mode 100644 app/views/pastes/show.html.erb create mode 100644 db/migrate/20140214151745_create_pastes.rb create mode 100644 db/migrate/20140214152056_add_content_to_pastes.rb create mode 100644 db/migrate/20140214185753_change_content_type_in_pastes.rb create mode 100644 db/schema.rb create mode 100644 spec/features/creates_new_paste_spec.rb create mode 100644 spec/models/paste_spec.rb diff --git a/app/controllers/pastes_controller.rb b/app/controllers/pastes_controller.rb index 834c785..5a06c6b 100644 --- a/app/controllers/pastes_controller.rb +++ b/app/controllers/pastes_controller.rb @@ -1,19 +1,39 @@ class PastesController < ApplicationController def index + @pastes = Paste.all end - def create + def show + @paste = Paste.find(params[:id]) end def new - end - - def update + @paste = Paste.new end def edit end + def create + @paste = Paste.new(paste_params) + + if @paste.save + flash[:success] = "Your paste has been added" + redirect_to @paste + else + flash.now[:danger] = "There was an error submitting your form" + render 'new' + end + end + + def update + end + def destroy end + + private + def paste_params + params.require(:paste).permit(:content) + end end diff --git a/app/models/paste.rb b/app/models/paste.rb new file mode 100644 index 0000000..0e0d2c2 --- /dev/null +++ b/app/models/paste.rb @@ -0,0 +1,3 @@ +class Paste < ActiveRecord::Base + validates :content, presence: true +end diff --git a/app/views/layouts/_error.html.erb b/app/views/layouts/_error.html.erb new file mode 100644 index 0000000..f86dc24 --- /dev/null +++ b/app/views/layouts/_error.html.erb @@ -0,0 +1,3 @@ +<% flash.each do |key, value| %> + <%= content_tag(:div, value, class: "alert alert-#{key}") %> +<% end %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 1daf4f4..b97625a 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -13,7 +13,7 @@
+ <%= render partial: 'layouts/error' %> <%= yield %>
diff --git a/app/views/pastes/_form.html.erb b/app/views/pastes/_form.html.erb new file mode 100644 index 0000000..51d6578 --- /dev/null +++ b/app/views/pastes/_form.html.erb @@ -0,0 +1,7 @@ +<%= form_for @paste do |f| %> +
+ <%= f.label :content %> + <%= f.text_area :content, class: 'form-control' %> +
+ <%= f.submit 'Paste it!', class: 'btn btn-primary' %> +<% end %> diff --git a/app/views/pastes/edit.html.erb b/app/views/pastes/edit.html.erb index e99d6e3..3df5e88 100644 --- a/app/views/pastes/edit.html.erb +++ b/app/views/pastes/edit.html.erb @@ -1 +1,3 @@ Edit + +<% render '_form' %> diff --git a/app/views/pastes/index.html.erb b/app/views/pastes/index.html.erb index 8ab686e..91d18f3 100644 --- a/app/views/pastes/index.html.erb +++ b/app/views/pastes/index.html.erb @@ -1 +1,3 @@ -Hello, World! +<% @pastes.each do |paste| %> + <%= link_to paste.id, paste %>
+<% end %> diff --git a/app/views/pastes/new.html.erb b/app/views/pastes/new.html.erb index 96716fb..23997ae 100644 --- a/app/views/pastes/new.html.erb +++ b/app/views/pastes/new.html.erb @@ -1 +1 @@ -New +<%= render partial: 'form' %> diff --git a/app/views/pastes/show.html.erb b/app/views/pastes/show.html.erb new file mode 100644 index 0000000..b8088ab --- /dev/null +++ b/app/views/pastes/show.html.erb @@ -0,0 +1 @@ +<%= @paste.content %> diff --git a/db/migrate/20140214151745_create_pastes.rb b/db/migrate/20140214151745_create_pastes.rb new file mode 100644 index 0000000..565139c --- /dev/null +++ b/db/migrate/20140214151745_create_pastes.rb @@ -0,0 +1,8 @@ +class CreatePastes < ActiveRecord::Migration + def change + create_table :pastes do |t| + + t.timestamps + end + end +end diff --git a/db/migrate/20140214152056_add_content_to_pastes.rb b/db/migrate/20140214152056_add_content_to_pastes.rb new file mode 100644 index 0000000..ead6aa9 --- /dev/null +++ b/db/migrate/20140214152056_add_content_to_pastes.rb @@ -0,0 +1,5 @@ +class AddContentToPastes < ActiveRecord::Migration + def change + add_column :pastes, :content, :string + end +end diff --git a/db/migrate/20140214185753_change_content_type_in_pastes.rb b/db/migrate/20140214185753_change_content_type_in_pastes.rb new file mode 100644 index 0000000..23101d5 --- /dev/null +++ b/db/migrate/20140214185753_change_content_type_in_pastes.rb @@ -0,0 +1,9 @@ +class ChangeContentTypeInPastes < ActiveRecord::Migration + def self.up + change_column :pastes, :content, :text + end + + def self.down + change_column :pastes, :content, :string + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..6ce463f --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,22 @@ +# encoding: UTF-8 +# 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 that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 20140214185753) do + + create_table "pastes", force: true do |t| + t.datetime "created_at" + t.datetime "updated_at" + t.text "content", limit: 255 + end + +end diff --git a/spec/features/creates_new_paste_spec.rb b/spec/features/creates_new_paste_spec.rb new file mode 100644 index 0000000..c2962e9 --- /dev/null +++ b/spec/features/creates_new_paste_spec.rb @@ -0,0 +1,19 @@ +require 'spec_helper' + +feature 'Creates new paste' do + scenario 'with valid content' do + visit new_paste_path + fill_in 'Content', with: 'Test' + click_button 'Paste it!' + + expect(page).to have_content('Your paste has been added') + end + + scenario 'with blank content' do + visit new_paste_path + fill_in 'Content', with: ' ' + click_button 'Paste it!' + + expect(page).to have_content('There was an error submitting your form') + end +end diff --git a/spec/models/paste_spec.rb b/spec/models/paste_spec.rb new file mode 100644 index 0000000..0ab08e4 --- /dev/null +++ b/spec/models/paste_spec.rb @@ -0,0 +1,18 @@ +require 'spec_helper' + +describe Paste do + before do + @paste = Paste.new(content: "Test") + end + + subject { @paste } + + it { should respond_to(:content) } + + it { should be_valid } + + describe "when content is blank" do + before { @paste.content = " " } + it { should_not be_valid } + end +end