1
0
mirror of https://github.com/projekteuler/projekteuler.git synced 2025-12-10 00:36:42 +01:00

Add problem type

This commit is contained in:
Philipp Fischbeck 2014-11-30 16:42:50 +01:00
parent bbbadc5184
commit b7a0303206
20 changed files with 339 additions and 0 deletions

View File

@ -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/

View File

@ -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/

View File

@ -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;
}
}

View File

@ -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

View File

@ -0,0 +1,2 @@
module ProblemsHelper
end

2
app/models/problem.rb Normal file
View File

@ -0,0 +1,2 @@
class Problem < ActiveRecord::Base
end

View File

@ -0,0 +1,25 @@
<%= form_for(@problem) do |f| %>
<% if @problem.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@problem.errors.count, "error") %> prohibited this problem from being saved:</h2>
<ul>
<% @problem.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :content %><br>
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

View File

@ -0,0 +1,6 @@
<h1>Editing problem</h1>
<%= render 'form' %>
<%= link_to 'Show', @problem %> |
<%= link_to 'Back', problems_path %>

View File

@ -0,0 +1,27 @@
<h1>Listing problems</h1>
<table>
<thead>
<tr>
<th>Title</th>
<th>Content</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @problems.each do |problem| %>
<tr>
<td><%= problem.title %></td>
<td><%= problem.content %></td>
<td><%= link_to 'Show', problem %></td>
<td><%= link_to 'Edit', edit_problem_path(problem) %></td>
<td><%= link_to 'Destroy', problem, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Problem', new_problem_path %>

View File

@ -0,0 +1,4 @@
json.array!(@problems) do |problem|
json.extract! problem, :id, :title, :content
json.url problem_url(problem, format: :json)
end

View File

@ -0,0 +1,5 @@
<h1>New problem</h1>
<%= render 'form' %>
<%= link_to 'Back', problems_path %>

View File

@ -0,0 +1,14 @@
<p id="notice"><%= notice %></p>
<p>
<strong>Title:</strong>
<%= @problem.title %>
</p>
<p>
<strong>Content:</strong>
<%= @problem.content %>
</p>
<%= link_to 'Edit', edit_problem_path(@problem) %> |
<%= link_to 'Back', problems_path %>

View File

@ -0,0 +1 @@
json.extract! @problem, :id, :title, :content, :created_at, :updated_at

View File

@ -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".

View File

@ -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

23
db/schema.rb Normal file
View File

@ -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

View File

@ -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

9
test/fixtures/problems.yml vendored Normal file
View File

@ -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

View File

@ -0,0 +1,4 @@
require 'test_helper'
class ProblemsHelperTest < ActionView::TestCase
end

View File

@ -0,0 +1,7 @@
require 'test_helper'
class ProblemTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end