From b7a03032069b4534656ac4e750d1353c5865c5cc Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Sun, 30 Nov 2014 16:42:50 +0100 Subject: [PATCH] Add problem type --- app/assets/javascripts/problems.js.coffee | 3 + app/assets/stylesheets/problems.css.scss | 3 + app/assets/stylesheets/scaffolds.css.scss | 69 ++++++++++++++++++ app/controllers/problems_controller.rb | 74 ++++++++++++++++++++ app/helpers/problems_helper.rb | 2 + app/models/problem.rb | 2 + app/views/problems/_form.html.erb | 25 +++++++ app/views/problems/edit.html.erb | 6 ++ app/views/problems/index.html.erb | 27 +++++++ app/views/problems/index.json.jbuilder | 4 ++ app/views/problems/new.html.erb | 5 ++ app/views/problems/show.html.erb | 14 ++++ app/views/problems/show.json.jbuilder | 1 + config/routes.rb | 2 + db/migrate/20141130153941_create_problems.rb | 10 +++ db/schema.rb | 23 ++++++ test/controllers/problems_controller_test.rb | 49 +++++++++++++ test/fixtures/problems.yml | 9 +++ test/helpers/problems_helper_test.rb | 4 ++ test/models/problem_test.rb | 7 ++ 20 files changed, 339 insertions(+) create mode 100644 app/assets/javascripts/problems.js.coffee create mode 100644 app/assets/stylesheets/problems.css.scss create mode 100644 app/assets/stylesheets/scaffolds.css.scss create mode 100644 app/controllers/problems_controller.rb create mode 100644 app/helpers/problems_helper.rb create mode 100644 app/models/problem.rb create mode 100644 app/views/problems/_form.html.erb create mode 100644 app/views/problems/edit.html.erb create mode 100644 app/views/problems/index.html.erb create mode 100644 app/views/problems/index.json.jbuilder create mode 100644 app/views/problems/new.html.erb create mode 100644 app/views/problems/show.html.erb create mode 100644 app/views/problems/show.json.jbuilder create mode 100644 db/migrate/20141130153941_create_problems.rb create mode 100644 db/schema.rb create mode 100644 test/controllers/problems_controller_test.rb create mode 100644 test/fixtures/problems.yml create mode 100644 test/helpers/problems_helper_test.rb create mode 100644 test/models/problem_test.rb diff --git a/app/assets/javascripts/problems.js.coffee b/app/assets/javascripts/problems.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/problems.js.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/problems.css.scss b/app/assets/stylesheets/problems.css.scss new file mode 100644 index 0000000..440f713 --- /dev/null +++ b/app/assets/stylesheets/problems.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Problems controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/scaffolds.css.scss b/app/assets/stylesheets/scaffolds.css.scss new file mode 100644 index 0000000..6ec6a8f --- /dev/null +++ b/app/assets/stylesheets/scaffolds.css.scss @@ -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; + } +} diff --git a/app/controllers/problems_controller.rb b/app/controllers/problems_controller.rb new file mode 100644 index 0000000..33a0433 --- /dev/null +++ b/app/controllers/problems_controller.rb @@ -0,0 +1,74 @@ +class ProblemsController < ApplicationController + before_action :set_problem, only: [:show, :edit, :update, :destroy] + + # GET /problems + # GET /problems.json + def index + @problems = Problem.all + end + + # GET /problems/1 + # GET /problems/1.json + def show + end + + # GET /problems/new + def new + @problem = Problem.new + end + + # GET /problems/1/edit + def edit + end + + # POST /problems + # POST /problems.json + def create + @problem = Problem.new(problem_params) + + respond_to do |format| + if @problem.save + format.html { redirect_to @problem, notice: 'Problem was successfully created.' } + format.json { render :show, status: :created, location: @problem } + else + format.html { render :new } + format.json { render json: @problem.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /problems/1 + # PATCH/PUT /problems/1.json + def update + respond_to do |format| + if @problem.update(problem_params) + format.html { redirect_to @problem, notice: 'Problem was successfully updated.' } + format.json { render :show, status: :ok, location: @problem } + else + format.html { render :edit } + format.json { render json: @problem.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /problems/1 + # DELETE /problems/1.json + def destroy + @problem.destroy + respond_to do |format| + format.html { redirect_to problems_url, notice: 'Problem was successfully destroyed.' } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_problem + @problem = Problem.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def problem_params + params.require(:problem).permit(:title, :content) + end +end diff --git a/app/helpers/problems_helper.rb b/app/helpers/problems_helper.rb new file mode 100644 index 0000000..6989835 --- /dev/null +++ b/app/helpers/problems_helper.rb @@ -0,0 +1,2 @@ +module ProblemsHelper +end diff --git a/app/models/problem.rb b/app/models/problem.rb new file mode 100644 index 0000000..3a3c932 --- /dev/null +++ b/app/models/problem.rb @@ -0,0 +1,2 @@ +class Problem < ActiveRecord::Base +end diff --git a/app/views/problems/_form.html.erb b/app/views/problems/_form.html.erb new file mode 100644 index 0000000..969825d --- /dev/null +++ b/app/views/problems/_form.html.erb @@ -0,0 +1,25 @@ +<%= form_for(@problem) do |f| %> + <% if @problem.errors.any? %> +
+

<%= pluralize(@problem.errors.count, "error") %> prohibited this problem from being saved:

+ + +
+ <% end %> + +
+ <%= f.label :title %>
+ <%= f.text_field :title %> +
+
+ <%= f.label :content %>
+ <%= f.text_area :content %> +
+
+ <%= f.submit %> +
+<% end %> diff --git a/app/views/problems/edit.html.erb b/app/views/problems/edit.html.erb new file mode 100644 index 0000000..92e7277 --- /dev/null +++ b/app/views/problems/edit.html.erb @@ -0,0 +1,6 @@ +

Editing problem

+ +<%= render 'form' %> + +<%= link_to 'Show', @problem %> | +<%= link_to 'Back', problems_path %> diff --git a/app/views/problems/index.html.erb b/app/views/problems/index.html.erb new file mode 100644 index 0000000..5627ebf --- /dev/null +++ b/app/views/problems/index.html.erb @@ -0,0 +1,27 @@ +

Listing problems

+ + + + + + + + + + + + <% @problems.each do |problem| %> + + + + + + + + <% end %> + +
TitleContent
<%= problem.title %><%= problem.content %><%= link_to 'Show', problem %><%= link_to 'Edit', edit_problem_path(problem) %><%= link_to 'Destroy', problem, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Problem', new_problem_path %> diff --git a/app/views/problems/index.json.jbuilder b/app/views/problems/index.json.jbuilder new file mode 100644 index 0000000..493111a --- /dev/null +++ b/app/views/problems/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@problems) do |problem| + json.extract! problem, :id, :title, :content + json.url problem_url(problem, format: :json) +end diff --git a/app/views/problems/new.html.erb b/app/views/problems/new.html.erb new file mode 100644 index 0000000..1c1b2ee --- /dev/null +++ b/app/views/problems/new.html.erb @@ -0,0 +1,5 @@ +

New problem

+ +<%= render 'form' %> + +<%= link_to 'Back', problems_path %> diff --git a/app/views/problems/show.html.erb b/app/views/problems/show.html.erb new file mode 100644 index 0000000..a2e0a32 --- /dev/null +++ b/app/views/problems/show.html.erb @@ -0,0 +1,14 @@ +

<%= notice %>

+ +

+ Title: + <%= @problem.title %> +

+ +

+ Content: + <%= @problem.content %> +

+ +<%= link_to 'Edit', edit_problem_path(@problem) %> | +<%= link_to 'Back', problems_path %> diff --git a/app/views/problems/show.json.jbuilder b/app/views/problems/show.json.jbuilder new file mode 100644 index 0000000..f9ddbe9 --- /dev/null +++ b/app/views/problems/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @problem, :id, :title, :content, :created_at, :updated_at diff --git a/config/routes.rb b/config/routes.rb index 3f66539..342d016 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ Rails.application.routes.draw do + resources :problems + # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/db/migrate/20141130153941_create_problems.rb b/db/migrate/20141130153941_create_problems.rb new file mode 100644 index 0000000..f85a4ea --- /dev/null +++ b/db/migrate/20141130153941_create_problems.rb @@ -0,0 +1,10 @@ +class CreateProblems < ActiveRecord::Migration + def change + create_table :problems do |t| + t.string :title + t.text :content + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..c07b7d5 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,23 @@ +# 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: 20141130153941) do + + create_table "problems", force: true do |t| + t.string "title" + t.text "content" + t.datetime "created_at" + t.datetime "updated_at" + end + +end diff --git a/test/controllers/problems_controller_test.rb b/test/controllers/problems_controller_test.rb new file mode 100644 index 0000000..32d172d --- /dev/null +++ b/test/controllers/problems_controller_test.rb @@ -0,0 +1,49 @@ +require 'test_helper' + +class ProblemsControllerTest < ActionController::TestCase + setup do + @problem = problems(:one) + end + + test "should get index" do + get :index + assert_response :success + assert_not_nil assigns(:problems) + end + + test "should get new" do + get :new + assert_response :success + end + + test "should create problem" do + assert_difference('Problem.count') do + post :create, problem: { content: @problem.content, title: @problem.title } + end + + assert_redirected_to problem_path(assigns(:problem)) + end + + test "should show problem" do + get :show, id: @problem + assert_response :success + end + + test "should get edit" do + get :edit, id: @problem + assert_response :success + end + + test "should update problem" do + patch :update, id: @problem, problem: { content: @problem.content, title: @problem.title } + assert_redirected_to problem_path(assigns(:problem)) + end + + test "should destroy problem" do + assert_difference('Problem.count', -1) do + delete :destroy, id: @problem + end + + assert_redirected_to problems_path + end +end diff --git a/test/fixtures/problems.yml b/test/fixtures/problems.yml new file mode 100644 index 0000000..19db450 --- /dev/null +++ b/test/fixtures/problems.yml @@ -0,0 +1,9 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + title: MyString + content: MyText + +two: + title: MyString + content: MyText diff --git a/test/helpers/problems_helper_test.rb b/test/helpers/problems_helper_test.rb new file mode 100644 index 0000000..1de2c21 --- /dev/null +++ b/test/helpers/problems_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class ProblemsHelperTest < ActionView::TestCase +end diff --git a/test/models/problem_test.rb b/test/models/problem_test.rb new file mode 100644 index 0000000..2b9ec4e --- /dev/null +++ b/test/models/problem_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class ProblemTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end