From e13b82ad1ff79268ff708788b728a4cf025c1473 Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Sat, 13 Dec 2014 17:00:00 +0100 Subject: [PATCH 01/19] Rename Problem to Translation --- ...blems.js.coffee => translations.js.coffee} | 0 ...roblems.css.scss => translations.css.scss} | 2 +- app/controllers/problems_controller.rb | 74 ------------------- app/controllers/translations_controller.rb | 74 +++++++++++++++++++ app/helpers/problems_helper.rb | 2 - app/helpers/translations_helper.rb | 2 + app/models/{problem.rb => translation.rb} | 2 +- app/views/layouts/application.html.erb | 2 +- .../problems/_problem_pagination.html.erb | 1 - app/views/problems/edit.html.erb | 6 -- app/views/problems/index.html.erb | 24 ------ app/views/problems/index.json.jbuilder | 4 - app/views/problems/new.html.erb | 5 -- app/views/problems/show.html.erb | 17 ----- app/views/problems/show.json.jbuilder | 1 - .../{problems => translations}/_form.html.erb | 8 +- .../_translation_pagination.html.erb | 1 + app/views/translations/edit.html.erb | 6 ++ app/views/translations/index.html.erb | 24 ++++++ app/views/translations/index.json.jbuilder | 4 + app/views/translations/new.html.erb | 5 ++ app/views/translations/show.html.erb | 17 +++++ app/views/translations/show.json.jbuilder | 1 + config/routes.rb | 4 +- ...2_rename_problems_table_to_translations.rb | 5 ++ db/schema.rb | 4 +- db/seeds.rb | 2 +- test/controllers/problems_controller_test.rb | 53 ------------- .../translations_controller_test.rb | 53 +++++++++++++ .../{problems.yml => translations.yml} | 4 +- test/helpers/problems_helper_test.rb | 4 - test/helpers/translations_helper_test.rb | 4 + test/models/problem_test.rb | 34 --------- test/models/translation_test.rb | 34 +++++++++ 34 files changed, 244 insertions(+), 239 deletions(-) rename app/assets/javascripts/{problems.js.coffee => translations.js.coffee} (100%) rename app/assets/stylesheets/{problems.css.scss => translations.css.scss} (63%) delete mode 100644 app/controllers/problems_controller.rb create mode 100644 app/controllers/translations_controller.rb delete mode 100644 app/helpers/problems_helper.rb create mode 100644 app/helpers/translations_helper.rb rename app/models/{problem.rb => translation.rb} (82%) delete mode 100644 app/views/problems/_problem_pagination.html.erb delete mode 100644 app/views/problems/edit.html.erb delete mode 100644 app/views/problems/index.html.erb delete mode 100644 app/views/problems/index.json.jbuilder delete mode 100644 app/views/problems/new.html.erb delete mode 100644 app/views/problems/show.html.erb delete mode 100644 app/views/problems/show.json.jbuilder rename app/views/{problems => translations}/_form.html.erb (60%) create mode 100644 app/views/translations/_translation_pagination.html.erb create mode 100644 app/views/translations/edit.html.erb create mode 100644 app/views/translations/index.html.erb create mode 100644 app/views/translations/index.json.jbuilder create mode 100644 app/views/translations/new.html.erb create mode 100644 app/views/translations/show.html.erb create mode 100644 app/views/translations/show.json.jbuilder create mode 100644 db/migrate/20141213151812_rename_problems_table_to_translations.rb delete mode 100644 test/controllers/problems_controller_test.rb create mode 100644 test/controllers/translations_controller_test.rb rename test/fixtures/{problems.yml => translations.yml} (61%) delete mode 100644 test/helpers/problems_helper_test.rb create mode 100644 test/helpers/translations_helper_test.rb delete mode 100644 test/models/problem_test.rb create mode 100644 test/models/translation_test.rb diff --git a/app/assets/javascripts/problems.js.coffee b/app/assets/javascripts/translations.js.coffee similarity index 100% rename from app/assets/javascripts/problems.js.coffee rename to app/assets/javascripts/translations.js.coffee diff --git a/app/assets/stylesheets/problems.css.scss b/app/assets/stylesheets/translations.css.scss similarity index 63% rename from app/assets/stylesheets/problems.css.scss rename to app/assets/stylesheets/translations.css.scss index 7f0de10..5ede2fe 100644 --- a/app/assets/stylesheets/problems.css.scss +++ b/app/assets/stylesheets/translations.css.scss @@ -1,3 +1,3 @@ -// Place all the styles related to the Problems controller here. +// Place all the styles related to the Translations controller here. // They will automatically be included in application.css.scss. // You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/problems_controller.rb b/app/controllers/problems_controller.rb deleted file mode 100644 index e516ac4..0000000 --- a/app/controllers/problems_controller.rb +++ /dev/null @@ -1,74 +0,0 @@ -class ProblemsController < ApplicationController - before_action :set_problem, only: [:show, :edit, :update, :destroy] - - # GET /problems - # GET /problems.json - def index - @problems = Problem.paginate(page: params[:page]) - 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/controllers/translations_controller.rb b/app/controllers/translations_controller.rb new file mode 100644 index 0000000..cf1a62a --- /dev/null +++ b/app/controllers/translations_controller.rb @@ -0,0 +1,74 @@ +class TranslationsController < ApplicationController + before_action :set_translation, only: [:show, :edit, :update, :destroy] + + # GET /translations + # GET /translations.json + def index + @translations = Translation.paginate(page: params[:page]) + end + + # GET /translations/1 + # GET /translations/1.json + def show + end + + # GET /translations/new + def new + @translation = Translation.new + end + + # GET /translations/1/edit + def edit + end + + # POST /translations + # POST /translations.json + def create + @translation = Translation.new(translation_params) + + respond_to do |format| + if @translation.save + format.html { redirect_to @translation, notice: 'Translation was successfully created.' } + format.json { render :show, status: :created, location: @translation } + else + format.html { render :new } + format.json { render json: @translation.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /translations/1 + # PATCH/PUT /translations/1.json + def update + respond_to do |format| + if @translation.update(translation_params) + format.html { redirect_to @translation, notice: 'Translation was successfully updated.' } + format.json { render :show, status: :ok, location: @translation } + else + format.html { render :edit } + format.json { render json: @translation.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /translations/1 + # DELETE /translations/1.json + def destroy + @translation.destroy + respond_to do |format| + format.html { redirect_to translations_url, notice: 'Translation was successfully destroyed.' } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_translation + @translation = Translation.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def translation_params + params.require(:translation).permit(:title, :content) + end +end diff --git a/app/helpers/problems_helper.rb b/app/helpers/problems_helper.rb deleted file mode 100644 index 6989835..0000000 --- a/app/helpers/problems_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module ProblemsHelper -end diff --git a/app/helpers/translations_helper.rb b/app/helpers/translations_helper.rb new file mode 100644 index 0000000..ed0bd96 --- /dev/null +++ b/app/helpers/translations_helper.rb @@ -0,0 +1,2 @@ +module TranslationsHelper +end diff --git a/app/models/problem.rb b/app/models/translation.rb similarity index 82% rename from app/models/problem.rb rename to app/models/translation.rb index 981dc01..628d87e 100644 --- a/app/models/problem.rb +++ b/app/models/translation.rb @@ -1,4 +1,4 @@ -class Problem < ActiveRecord::Base +class Translation < ActiveRecord::Base validates :title, :content, presence: true validates :title, uniqueness: true diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index a24b5e2..7e0db88 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -13,7 +13,7 @@ <% end %> <%= horizontal do %> <%= nav class: 'navbar-left' do %> - <%= link_to 'Probleme', problems_path %> + <%= link_to 'Translations', translations_path %> <% end %> <% end %> <% end %> diff --git a/app/views/problems/_problem_pagination.html.erb b/app/views/problems/_problem_pagination.html.erb deleted file mode 100644 index 4df9095..0000000 --- a/app/views/problems/_problem_pagination.html.erb +++ /dev/null @@ -1 +0,0 @@ -<%= will_paginate @problems, renderer: BootstrapPagination::Rails %> \ No newline at end of file diff --git a/app/views/problems/edit.html.erb b/app/views/problems/edit.html.erb deleted file mode 100644 index 92e7277..0000000 --- a/app/views/problems/edit.html.erb +++ /dev/null @@ -1,6 +0,0 @@ -

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 deleted file mode 100644 index fd5875b..0000000 --- a/app/views/problems/index.html.erb +++ /dev/null @@ -1,24 +0,0 @@ -

Listing problems

- -<%= render 'problem_pagination' %> - - - - - - - - - - <% @problems.each do |problem| %> - - - - - <% end %> - -
IDTitle
<%= problem.id %><%= link_to problem.title, problem %>
-<%= render 'problem_pagination' %> -
- -<%= link_to 'New Problem', new_problem_path, class: 'btn btn-default' %> diff --git a/app/views/problems/index.json.jbuilder b/app/views/problems/index.json.jbuilder deleted file mode 100644 index 493111a..0000000 --- a/app/views/problems/index.json.jbuilder +++ /dev/null @@ -1,4 +0,0 @@ -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 deleted file mode 100644 index 1c1b2ee..0000000 --- a/app/views/problems/new.html.erb +++ /dev/null @@ -1,5 +0,0 @@ -

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 deleted file mode 100644 index 38e4f41..0000000 --- a/app/views/problems/show.html.erb +++ /dev/null @@ -1,17 +0,0 @@ -<% if notice %> -

<%= notice %>

-<% end %> - - - -<%= panel do %> -
- <%= sanitize @problem.content %> -
-<% end %> -
- <%= link_to 'Dieses Problem auf projecteuler.net', @problem.original_url, target: '_blank' %> -
-<%= link_to 'Edit', edit_problem_path(@problem), class: 'btn btn-default' %> diff --git a/app/views/problems/show.json.jbuilder b/app/views/problems/show.json.jbuilder deleted file mode 100644 index f9ddbe9..0000000 --- a/app/views/problems/show.json.jbuilder +++ /dev/null @@ -1 +0,0 @@ -json.extract! @problem, :id, :title, :content, :created_at, :updated_at diff --git a/app/views/problems/_form.html.erb b/app/views/translations/_form.html.erb similarity index 60% rename from app/views/problems/_form.html.erb rename to app/views/translations/_form.html.erb index 969825d..9656465 100644 --- a/app/views/problems/_form.html.erb +++ b/app/views/translations/_form.html.erb @@ -1,10 +1,10 @@ -<%= form_for(@problem) do |f| %> - <% if @problem.errors.any? %> +<%= form_for(@translation) do |f| %> + <% if @translation.errors.any? %>
-

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

+

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

diff --git a/app/views/translations/_translation_pagination.html.erb b/app/views/translations/_translation_pagination.html.erb new file mode 100644 index 0000000..d6e5b41 --- /dev/null +++ b/app/views/translations/_translation_pagination.html.erb @@ -0,0 +1 @@ +<%= will_paginate @translations, renderer: BootstrapPagination::Rails %> \ No newline at end of file diff --git a/app/views/translations/edit.html.erb b/app/views/translations/edit.html.erb new file mode 100644 index 0000000..b8dc5aa --- /dev/null +++ b/app/views/translations/edit.html.erb @@ -0,0 +1,6 @@ +

Editing translation

+ +<%= render 'form' %> + +<%= link_to 'Show', @translation %> | +<%= link_to 'Back', translations_path %> diff --git a/app/views/translations/index.html.erb b/app/views/translations/index.html.erb new file mode 100644 index 0000000..02764f1 --- /dev/null +++ b/app/views/translations/index.html.erb @@ -0,0 +1,24 @@ +

Listing translations

+ +<%= render 'translation_pagination' %> + + + + + + + + + + <% @translations.each do |translation| %> + + + + + <% end %> + +
IDTitle
<%= translation.id %><%= link_to translation.title, translation %>
+<%= render 'translation_pagination' %> +
+ +<%= link_to 'New Translation', new_translation_path, class: 'btn btn-default' %> diff --git a/app/views/translations/index.json.jbuilder b/app/views/translations/index.json.jbuilder new file mode 100644 index 0000000..890e1ea --- /dev/null +++ b/app/views/translations/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@translations) do |translation| + json.extract! translation, :id, :title, :content + json.url translation_url(translation, format: :json) +end diff --git a/app/views/translations/new.html.erb b/app/views/translations/new.html.erb new file mode 100644 index 0000000..45f1fd9 --- /dev/null +++ b/app/views/translations/new.html.erb @@ -0,0 +1,5 @@ +

New translation

+ +<%= render 'form' %> + +<%= link_to 'Back', translations_path %> diff --git a/app/views/translations/show.html.erb b/app/views/translations/show.html.erb new file mode 100644 index 0000000..a58c437 --- /dev/null +++ b/app/views/translations/show.html.erb @@ -0,0 +1,17 @@ +<% if notice %> +

<%= notice %>

+<% end %> + + + +<%= panel do %> +
+ <%= sanitize @translation.content %> +
+<% end %> +
+ <%= link_to 'Dieses Problem auf projecteuler.net', @translation.original_url, target: '_blank' %> +
+<%= link_to 'Edit', edit_translation_path(@translation), class: 'btn btn-default' %> diff --git a/app/views/translations/show.json.jbuilder b/app/views/translations/show.json.jbuilder new file mode 100644 index 0000000..10b6296 --- /dev/null +++ b/app/views/translations/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @translation, :id, :title, :content, :created_at, :updated_at diff --git a/config/routes.rb b/config/routes.rb index 7b67086..c167245 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,11 +1,11 @@ Rails.application.routes.draw do - resources :problems + resources :translations # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". # You can have the root of your site routed with "root" - root 'problems#index' + root 'translations#index' # Example of regular route: # get 'products/:id' => 'catalog#view' diff --git a/db/migrate/20141213151812_rename_problems_table_to_translations.rb b/db/migrate/20141213151812_rename_problems_table_to_translations.rb new file mode 100644 index 0000000..801a1cc --- /dev/null +++ b/db/migrate/20141213151812_rename_problems_table_to_translations.rb @@ -0,0 +1,5 @@ +class RenameProblemsTableToTranslations < ActiveRecord::Migration + def change + rename_table :problems, :translations + end +end diff --git a/db/schema.rb b/db/schema.rb index c07b7d5..8d34b2c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,9 +11,9 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20141130153941) do +ActiveRecord::Schema.define(version: 20141213151812) do - create_table "problems", force: true do |t| + create_table "translations", force: true do |t| t.string "title" t.text "content" t.datetime "created_at" diff --git a/db/seeds.rb b/db/seeds.rb index 6fe1353..d58d337 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -7,7 +7,7 @@ # Mayor.create(name: 'Emanuel', city: cities.first) for i in 1..103 do - Problem.create( + Translation.create( title: "Problem Nummer #{i}", content: "Das hier ist der Inhalt von Problem #{i}.
Hier ist die zweite Zeile." ) diff --git a/test/controllers/problems_controller_test.rb b/test/controllers/problems_controller_test.rb deleted file mode 100644 index 64d1f2a..0000000 --- a/test/controllers/problems_controller_test.rb +++ /dev/null @@ -1,53 +0,0 @@ -require 'test_helper' - -class ProblemsControllerTest < ActionController::TestCase - setup do - @problem = problems(:one) - @update = { - title: 'New title', - content: 'This is the new content' - } - 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: @update - 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: @update - 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/controllers/translations_controller_test.rb b/test/controllers/translations_controller_test.rb new file mode 100644 index 0000000..61c7455 --- /dev/null +++ b/test/controllers/translations_controller_test.rb @@ -0,0 +1,53 @@ +require 'test_helper' + +class TranslationsControllerTest < ActionController::TestCase + setup do + @translation = translations(:one) + @update = { + title: 'New title', + content: 'This is the new content' + } + end + + test "should get index" do + get :index + assert_response :success + assert_not_nil assigns(:translations) + end + + test "should get new" do + get :new + assert_response :success + end + + test "should create translation" do + assert_difference('Translation.count') do + post :create, translation: @update + end + + assert_redirected_to translation_path(assigns(:translation)) + end + + test "should show translation" do + get :show, id: @translation + assert_response :success + end + + test "should get edit" do + get :edit, id: @translation + assert_response :success + end + + test "should update translation" do + patch :update, id: @translation, translation: @update + assert_redirected_to translation_path(assigns(:translation)) + end + + test "should destroy translation" do + assert_difference('Translation.count', -1) do + delete :destroy, id: @translation + end + + assert_redirected_to translations_path + end +end diff --git a/test/fixtures/problems.yml b/test/fixtures/translations.yml similarity index 61% rename from test/fixtures/problems.yml rename to test/fixtures/translations.yml index 0e8e4a7..68398c1 100644 --- a/test/fixtures/problems.yml +++ b/test/fixtures/translations.yml @@ -2,8 +2,8 @@ one: title: First title - content: The content of the problem + content: The content of the translation two: title: Second title - content: The content of the second problem + content: The content of the second translation diff --git a/test/helpers/problems_helper_test.rb b/test/helpers/problems_helper_test.rb deleted file mode 100644 index 1de2c21..0000000 --- a/test/helpers/problems_helper_test.rb +++ /dev/null @@ -1,4 +0,0 @@ -require 'test_helper' - -class ProblemsHelperTest < ActionView::TestCase -end diff --git a/test/helpers/translations_helper_test.rb b/test/helpers/translations_helper_test.rb new file mode 100644 index 0000000..346380b --- /dev/null +++ b/test/helpers/translations_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class TranslationsHelperTest < ActionView::TestCase +end diff --git a/test/models/problem_test.rb b/test/models/problem_test.rb deleted file mode 100644 index 83e1d8c..0000000 --- a/test/models/problem_test.rb +++ /dev/null @@ -1,34 +0,0 @@ -require 'test_helper' - -class ProblemTest < ActiveSupport::TestCase - test "should not save problem without title" do - problem = Problem.new(content: 'This is some content') - assert_not problem.save - end - - test "should not save problem without content" do - problem = Problem.new(title: 'Problem title') - assert_not problem.save - end - - test "should not save problem with duplicate title" do - problem = Problem.new( - title: problems(:one).title, - content: 'This is some content' - ) - assert_not problem.save - end - - test "should save correct problem" do - problem = Problem.new( - title: 'A unique title', - content: 'Some content' - ) - assert problem.save - end - - test "should have correct original url" do - first = Problem.first - assert_equal "https://projecteuler.net/problem=#{first.id}", first.original_url - end -end diff --git a/test/models/translation_test.rb b/test/models/translation_test.rb new file mode 100644 index 0000000..57c79d9 --- /dev/null +++ b/test/models/translation_test.rb @@ -0,0 +1,34 @@ +require 'test_helper' + +class TranslationTest < ActiveSupport::TestCase + test "should not save translation without title" do + translation = Translation.new(content: 'This is some content') + assert_not translation.save + end + + test "should not save translation without content" do + translation = Translation.new(title: 'Translation title') + assert_not translation.save + end + + test "should not save translation with duplicate title" do + translation = Translation.new( + title: translations(:one).title, + content: 'This is some content' + ) + assert_not translation.save + end + + test "should save correct translation" do + translation = Translation.new( + title: 'A unique title', + content: 'Some content' + ) + assert translation.save + end + + test "should have correct original url" do + first = Translation.first + assert_equal "https://projecteuler.net/problem=#{first.id}", first.original_url + end +end From 79ed82cfd8c93a983d2db5dc6d7f6c1d5c408ad6 Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Sun, 14 Dec 2014 23:11:40 +0100 Subject: [PATCH 02/19] Create Problem model with reference to current translation --- app/models/problem.rb | 3 +++ db/migrate/20141214214958_create_problems_again.rb | 7 +++++++ .../20141214220056_add_translation_to_problem.rb | 5 +++++ db/schema.rb | 10 +++++++++- test/fixtures/problems.yml | 7 +++++++ test/models/problem_test.rb | 7 +++++++ 6 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 app/models/problem.rb create mode 100644 db/migrate/20141214214958_create_problems_again.rb create mode 100644 db/migrate/20141214220056_add_translation_to_problem.rb create mode 100644 test/fixtures/problems.yml create mode 100644 test/models/problem_test.rb diff --git a/app/models/problem.rb b/app/models/problem.rb new file mode 100644 index 0000000..4740c0b --- /dev/null +++ b/app/models/problem.rb @@ -0,0 +1,3 @@ +class Problem < ActiveRecord::Base + has_one :translation +end diff --git a/db/migrate/20141214214958_create_problems_again.rb b/db/migrate/20141214214958_create_problems_again.rb new file mode 100644 index 0000000..578d123 --- /dev/null +++ b/db/migrate/20141214214958_create_problems_again.rb @@ -0,0 +1,7 @@ +class CreateProblemsAgain < ActiveRecord::Migration + def change + create_table :problems do |t| + t.timestamps + end + end +end diff --git a/db/migrate/20141214220056_add_translation_to_problem.rb b/db/migrate/20141214220056_add_translation_to_problem.rb new file mode 100644 index 0000000..02302e0 --- /dev/null +++ b/db/migrate/20141214220056_add_translation_to_problem.rb @@ -0,0 +1,5 @@ +class AddTranslationToProblem < ActiveRecord::Migration + def change + add_reference :problems, :translation, index: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 8d34b2c..4e5eff5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,15 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20141213151812) do +ActiveRecord::Schema.define(version: 20141214220056) do + + create_table "problems", force: true do |t| + t.datetime "created_at" + t.datetime "updated_at" + t.integer "translation_id" + end + + add_index "problems", ["translation_id"], name: "index_problems_on_translation_id" create_table "translations", force: true do |t| t.string "title" diff --git a/test/fixtures/problems.yml b/test/fixtures/problems.yml new file mode 100644 index 0000000..d3043bc --- /dev/null +++ b/test/fixtures/problems.yml @@ -0,0 +1,7 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + Translation_id: + +two: + Translation_id: 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 From bbe80b1409c97a6d2032818dde8c0cc517f9747a Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Mon, 15 Dec 2014 00:38:29 +0100 Subject: [PATCH 03/19] Create association of translations with their problems --- app/models/problem.rb | 1 + app/models/translation.rb | 2 ++ db/migrate/20141214221259_add_problem_to_translation.rb | 5 +++++ db/schema.rb | 5 ++++- 4 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20141214221259_add_problem_to_translation.rb diff --git a/app/models/problem.rb b/app/models/problem.rb index 4740c0b..ee70fe3 100644 --- a/app/models/problem.rb +++ b/app/models/problem.rb @@ -1,3 +1,4 @@ class Problem < ActiveRecord::Base has_one :translation + has_many :translations, inverse_of: :problem end diff --git a/app/models/translation.rb b/app/models/translation.rb index 628d87e..c598631 100644 --- a/app/models/translation.rb +++ b/app/models/translation.rb @@ -1,4 +1,6 @@ class Translation < ActiveRecord::Base + belongs_to :problem, inverse_of: :translations + validates :title, :content, presence: true validates :title, uniqueness: true diff --git a/db/migrate/20141214221259_add_problem_to_translation.rb b/db/migrate/20141214221259_add_problem_to_translation.rb new file mode 100644 index 0000000..c598504 --- /dev/null +++ b/db/migrate/20141214221259_add_problem_to_translation.rb @@ -0,0 +1,5 @@ +class AddProblemToTranslation < ActiveRecord::Migration + def change + add_reference :translations, :problem, index: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 4e5eff5..f0321ed 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20141214220056) do +ActiveRecord::Schema.define(version: 20141214221259) do create_table "problems", force: true do |t| t.datetime "created_at" @@ -26,6 +26,9 @@ ActiveRecord::Schema.define(version: 20141214220056) do t.text "content" t.datetime "created_at" t.datetime "updated_at" + t.integer "problem_id" end + add_index "translations", ["problem_id"], name: "index_translations_on_problem_id" + end From fa86abc06d0f7179ed5fb28388e1d60d8813866c Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Mon, 29 Dec 2014 15:29:01 +0100 Subject: [PATCH 04/19] Add presence of problem to translation --- app/controllers/translations_controller.rb | 2 +- app/models/translation.rb | 2 +- test/controllers/translations_controller_test.rb | 3 ++- test/fixtures/translations.yml | 2 ++ test/models/translation_test.rb | 3 ++- 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/app/controllers/translations_controller.rb b/app/controllers/translations_controller.rb index cf1a62a..5436b33 100644 --- a/app/controllers/translations_controller.rb +++ b/app/controllers/translations_controller.rb @@ -69,6 +69,6 @@ class TranslationsController < ApplicationController # Never trust parameters from the scary internet, only allow the white list through. def translation_params - params.require(:translation).permit(:title, :content) + params.require(:translation).permit(:title, :content, :problem_id) end end diff --git a/app/models/translation.rb b/app/models/translation.rb index c598631..f487ca5 100644 --- a/app/models/translation.rb +++ b/app/models/translation.rb @@ -1,7 +1,7 @@ class Translation < ActiveRecord::Base belongs_to :problem, inverse_of: :translations - validates :title, :content, presence: true + validates :title, :content, :problem, presence: true validates :title, uniqueness: true self.per_page = 50 diff --git a/test/controllers/translations_controller_test.rb b/test/controllers/translations_controller_test.rb index 61c7455..603d0e7 100644 --- a/test/controllers/translations_controller_test.rb +++ b/test/controllers/translations_controller_test.rb @@ -5,7 +5,8 @@ class TranslationsControllerTest < ActionController::TestCase @translation = translations(:one) @update = { title: 'New title', - content: 'This is the new content' + content: 'This is the new content', + problem_id: Problem.first } end diff --git a/test/fixtures/translations.yml b/test/fixtures/translations.yml index 68398c1..0fa0201 100644 --- a/test/fixtures/translations.yml +++ b/test/fixtures/translations.yml @@ -1,9 +1,11 @@ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: + problem: one title: First title content: The content of the translation two: + problem: two title: Second title content: The content of the second translation diff --git a/test/models/translation_test.rb b/test/models/translation_test.rb index 57c79d9..34bab0a 100644 --- a/test/models/translation_test.rb +++ b/test/models/translation_test.rb @@ -22,7 +22,8 @@ class TranslationTest < ActiveSupport::TestCase test "should save correct translation" do translation = Translation.new( title: 'A unique title', - content: 'Some content' + content: 'Some content', + problem: Problem.first ) assert translation.save end From d1470f391ef1c3799267b8afb1f345ee1c204143 Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Mon, 29 Dec 2014 15:41:56 +0100 Subject: [PATCH 05/19] Rename translation fixtures, explicitly use problem ids --- test/controllers/translations_controller_test.rb | 4 ++-- test/fixtures/problems.yml | 6 ++++-- test/fixtures/translations.yml | 8 ++++---- test/models/translation_test.rb | 4 ++-- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/test/controllers/translations_controller_test.rb b/test/controllers/translations_controller_test.rb index 603d0e7..a1cbc04 100644 --- a/test/controllers/translations_controller_test.rb +++ b/test/controllers/translations_controller_test.rb @@ -2,11 +2,11 @@ require 'test_helper' class TranslationsControllerTest < ActionController::TestCase setup do - @translation = translations(:one) + @translation = translations(:translation_one) @update = { title: 'New title', content: 'This is the new content', - problem_id: Problem.first + problem_id: 1 } end diff --git a/test/fixtures/problems.yml b/test/fixtures/problems.yml index d3043bc..1914f9b 100644 --- a/test/fixtures/problems.yml +++ b/test/fixtures/problems.yml @@ -1,7 +1,9 @@ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: - Translation_id: + id: 1 + translation_id: <%= ActiveRecord::FixtureSet.identify(:translation_one) %> two: - Translation_id: + id: 2 + translation_id: <%= ActiveRecord::FixtureSet.identify(:translation_two) %> diff --git a/test/fixtures/translations.yml b/test/fixtures/translations.yml index 0fa0201..f8bcb86 100644 --- a/test/fixtures/translations.yml +++ b/test/fixtures/translations.yml @@ -1,11 +1,11 @@ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html -one: - problem: one +translation_one: + problem_id: 1 title: First title content: The content of the translation -two: - problem: two +translation_two: + problem_id: 2 title: Second title content: The content of the second translation diff --git a/test/models/translation_test.rb b/test/models/translation_test.rb index 34bab0a..088628e 100644 --- a/test/models/translation_test.rb +++ b/test/models/translation_test.rb @@ -13,7 +13,7 @@ class TranslationTest < ActiveSupport::TestCase test "should not save translation with duplicate title" do translation = Translation.new( - title: translations(:one).title, + title: translations(:translation_one).title, content: 'This is some content' ) assert_not translation.save @@ -23,7 +23,7 @@ class TranslationTest < ActiveSupport::TestCase translation = Translation.new( title: 'A unique title', content: 'Some content', - problem: Problem.first + problem_id: 1 ) assert translation.save end From 06ab2e4cabdf164e8a4c99e3e13f874e076f3ea5 Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Mon, 29 Dec 2014 16:16:57 +0100 Subject: [PATCH 06/19] Add controller for Problems with index and show actions --- app/assets/javascripts/problems.js.coffee | 3 +++ app/assets/stylesheets/problems.css.scss | 3 +++ app/controllers/problems_controller.rb | 21 +++++++++++++++++++ app/helpers/problems_helper.rb | 2 ++ app/views/problems/_problem_pagination.erb | 1 + app/views/problems/index.html.erb | 22 ++++++++++++++++++++ app/views/problems/show.html.erb | 16 ++++++++++++++ config/routes.rb | 1 + db/seeds.rb | 9 ++++++-- test/controllers/problems_controller_test.rb | 14 +++++++++++++ test/helpers/problems_helper_test.rb | 4 ++++ 11 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 app/assets/javascripts/problems.js.coffee create mode 100644 app/assets/stylesheets/problems.css.scss create mode 100644 app/controllers/problems_controller.rb create mode 100644 app/helpers/problems_helper.rb create mode 100644 app/views/problems/_problem_pagination.erb create mode 100644 app/views/problems/index.html.erb create mode 100644 app/views/problems/show.html.erb create mode 100644 test/controllers/problems_controller_test.rb create mode 100644 test/helpers/problems_helper_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..e08096e --- /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/controllers/problems_controller.rb b/app/controllers/problems_controller.rb new file mode 100644 index 0000000..b341960 --- /dev/null +++ b/app/controllers/problems_controller.rb @@ -0,0 +1,21 @@ +class ProblemsController < ApplicationController + before_action :set_problem, only: [:show] + + def index + @problems = Problem.paginate(page: params[:page]) + end + + def show + 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) + 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/views/problems/_problem_pagination.erb b/app/views/problems/_problem_pagination.erb new file mode 100644 index 0000000..4df9095 --- /dev/null +++ b/app/views/problems/_problem_pagination.erb @@ -0,0 +1 @@ +<%= will_paginate @problems, renderer: BootstrapPagination::Rails %> \ No newline at end of file diff --git a/app/views/problems/index.html.erb b/app/views/problems/index.html.erb new file mode 100644 index 0000000..6aa7ecf --- /dev/null +++ b/app/views/problems/index.html.erb @@ -0,0 +1,22 @@ +

Listing Problems

+ +<%= render 'problem_pagination' %> + + + + + + + + + + <% @problems.each do |problem| %> + + + + + <% end %> + +
IDTitle
<%= problem.id %><%= link_to problem.title, problem %>
+<%= render 'problem_pagination' %> +
diff --git a/app/views/problems/show.html.erb b/app/views/problems/show.html.erb new file mode 100644 index 0000000..e165f6a --- /dev/null +++ b/app/views/problems/show.html.erb @@ -0,0 +1,16 @@ +<% if notice %> +

<%= notice %>

+<% end %> + + + +<%= panel do %> +
+ <%= sanitize @problem.content %> +
+<% end %> +
+ <%= link_to 'Dieses Problem auf projecteuler.net', @problem.translation.original_url, target: '_blank' %> +
\ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index c167245..65e9153 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :problems, only: [:index, :show] resources :translations # The priority is based upon order of creation: first created -> highest priority. diff --git a/db/seeds.rb b/db/seeds.rb index d58d337..ed60ace 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -6,9 +6,14 @@ # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) # Mayor.create(name: 'Emanuel', city: cities.first) +Translation.delete_all +Problem.delete_all + for i in 1..103 do - Translation.create( + translation = Translation.create( + problem_id: i, title: "Problem Nummer #{i}", content: "Das hier ist der Inhalt von Problem #{i}.
Hier ist die zweite Zeile." ) -end \ No newline at end of file + Problem.create(id: i, translation: translation) +end diff --git a/test/controllers/problems_controller_test.rb b/test/controllers/problems_controller_test.rb new file mode 100644 index 0000000..57a7e39 --- /dev/null +++ b/test/controllers/problems_controller_test.rb @@ -0,0 +1,14 @@ +require 'test_helper' + +class ProblemsControllerTest < ActionController::TestCase + test "should get index" do + get :index + assert_response :success + end + + test "should get show" do + get :show, id: 1 + assert_response :success + end + +end 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 From cbf17cfce07221844c86e189108c03fea21e45cc Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Mon, 29 Dec 2014 16:18:20 +0100 Subject: [PATCH 07/19] Add delegation and per_page to problem --- app/models/problem.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/models/problem.rb b/app/models/problem.rb index ee70fe3..1405074 100644 --- a/app/models/problem.rb +++ b/app/models/problem.rb @@ -1,4 +1,8 @@ class Problem < ActiveRecord::Base has_one :translation + delegate :title, :content, to: :translation + has_many :translations, inverse_of: :problem + + self.per_page = 50 end From 4c16813c92eb61f79b2cba9e1ccf8358db8fa04b Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Mon, 29 Dec 2014 20:28:33 +0100 Subject: [PATCH 08/19] Add check whether problem has a translation --- app/controllers/problems_controller.rb | 3 +++ app/models/problem.rb | 5 +++++ app/views/problems/index.html.erb | 9 ++++++++- app/views/problems/untranslated.html.erb | 1 + db/seeds.rb | 4 +++- test/controllers/problems_controller_test.rb | 5 +++++ test/fixtures/problems.yml | 4 ++++ test/models/problem_test.rb | 15 ++++++++++++--- 8 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 app/views/problems/untranslated.html.erb diff --git a/app/controllers/problems_controller.rb b/app/controllers/problems_controller.rb index b341960..4cd4dab 100644 --- a/app/controllers/problems_controller.rb +++ b/app/controllers/problems_controller.rb @@ -6,6 +6,9 @@ class ProblemsController < ApplicationController end def show + unless @problem.is_translated? + render action: "untranslated" + end end private diff --git a/app/models/problem.rb b/app/models/problem.rb index 1405074..a1adcd9 100644 --- a/app/models/problem.rb +++ b/app/models/problem.rb @@ -5,4 +5,9 @@ class Problem < ActiveRecord::Base has_many :translations, inverse_of: :problem self.per_page = 50 + + def is_translated? + !!self.translation + end + end diff --git a/app/views/problems/index.html.erb b/app/views/problems/index.html.erb index 6aa7ecf..48632e2 100644 --- a/app/views/problems/index.html.erb +++ b/app/views/problems/index.html.erb @@ -13,7 +13,14 @@ <% @problems.each do |problem| %> <%= problem.id %> - <%= link_to problem.title, problem %> + + <% if problem.is_translated? %> + <%= link_to problem.title, problem %> + <% else %> + Dieses Problem wurde noch nicht übersetzt. + <% end %> + + <% end %> diff --git a/app/views/problems/untranslated.html.erb b/app/views/problems/untranslated.html.erb new file mode 100644 index 0000000..3f6dcd5 --- /dev/null +++ b/app/views/problems/untranslated.html.erb @@ -0,0 +1 @@ +Dieses Problem wurde noch nicht übersetzt. \ No newline at end of file diff --git a/db/seeds.rb b/db/seeds.rb index ed60ace..9e7aaf9 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -9,7 +9,7 @@ Translation.delete_all Problem.delete_all -for i in 1..103 do +for i in 1..10 do translation = Translation.create( problem_id: i, title: "Problem Nummer #{i}", @@ -17,3 +17,5 @@ for i in 1..103 do ) Problem.create(id: i, translation: translation) end + +Problem.create(id: 11) \ No newline at end of file diff --git a/test/controllers/problems_controller_test.rb b/test/controllers/problems_controller_test.rb index 57a7e39..7f202d3 100644 --- a/test/controllers/problems_controller_test.rb +++ b/test/controllers/problems_controller_test.rb @@ -11,4 +11,9 @@ class ProblemsControllerTest < ActionController::TestCase assert_response :success end + test "should get untranslated problem" do + get :show, id: 3 + assert_response :success + end + end diff --git a/test/fixtures/problems.yml b/test/fixtures/problems.yml index 1914f9b..95b9482 100644 --- a/test/fixtures/problems.yml +++ b/test/fixtures/problems.yml @@ -7,3 +7,7 @@ one: two: id: 2 translation_id: <%= ActiveRecord::FixtureSet.identify(:translation_two) %> + +three: + id: 3 + diff --git a/test/models/problem_test.rb b/test/models/problem_test.rb index 2b9ec4e..0888bd7 100644 --- a/test/models/problem_test.rb +++ b/test/models/problem_test.rb @@ -1,7 +1,16 @@ require 'test_helper' class ProblemTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + test "should save correct problem" do + problem = Problem.new + assert problem.save + end + + test "is_translated? should return false for missing translation" do + assert_not problems(:three).is_translated? + end + + test "is_translated? should return true for existing translation" do + assert problems(:one).is_translated? + end end From 2b812f297dff43637f1ae704da0ba2d05aa45229 Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Mon, 29 Dec 2014 20:33:30 +0100 Subject: [PATCH 09/19] Move original_url from translation to problem --- app/models/problem.rb | 4 ++++ app/models/translation.rb | 4 ---- app/views/problems/show.html.erb | 2 +- app/views/translations/show.html.erb | 4 ++-- test/models/problem_test.rb | 4 ++++ test/models/translation_test.rb | 5 ----- 6 files changed, 11 insertions(+), 12 deletions(-) diff --git a/app/models/problem.rb b/app/models/problem.rb index a1adcd9..4652b51 100644 --- a/app/models/problem.rb +++ b/app/models/problem.rb @@ -10,4 +10,8 @@ class Problem < ActiveRecord::Base !!self.translation end + + def original_url + "https://projecteuler.net/problem=#{self.id}" + end end diff --git a/app/models/translation.rb b/app/models/translation.rb index f487ca5..04227f5 100644 --- a/app/models/translation.rb +++ b/app/models/translation.rb @@ -5,8 +5,4 @@ class Translation < ActiveRecord::Base validates :title, uniqueness: true self.per_page = 50 - - def original_url - "https://projecteuler.net/problem=#{id}" - end end diff --git a/app/views/problems/show.html.erb b/app/views/problems/show.html.erb index e165f6a..4ad7873 100644 --- a/app/views/problems/show.html.erb +++ b/app/views/problems/show.html.erb @@ -12,5 +12,5 @@
<% end %>
- <%= link_to 'Dieses Problem auf projecteuler.net', @problem.translation.original_url, target: '_blank' %> + <%= link_to 'Dieses Problem auf projecteuler.net', @problem.original_url, target: '_blank' %>
\ No newline at end of file diff --git a/app/views/translations/show.html.erb b/app/views/translations/show.html.erb index a58c437..c7fb342 100644 --- a/app/views/translations/show.html.erb +++ b/app/views/translations/show.html.erb @@ -3,7 +3,7 @@ <% end %> <%= panel do %> @@ -12,6 +12,6 @@ <% end %>
- <%= link_to 'Dieses Problem auf projecteuler.net', @translation.original_url, target: '_blank' %> + <%= link_to 'Dieses Problem auf projecteuler.net', @translation.problem.original_url, target: '_blank' %>
<%= link_to 'Edit', edit_translation_path(@translation), class: 'btn btn-default' %> diff --git a/test/models/problem_test.rb b/test/models/problem_test.rb index 0888bd7..595651f 100644 --- a/test/models/problem_test.rb +++ b/test/models/problem_test.rb @@ -13,4 +13,8 @@ class ProblemTest < ActiveSupport::TestCase test "is_translated? should return true for existing translation" do assert problems(:one).is_translated? end + + test "should have correct original url" do + assert_equal "https://projecteuler.net/problem=1", problems(:one).original_url + end end diff --git a/test/models/translation_test.rb b/test/models/translation_test.rb index 088628e..b2c3ebc 100644 --- a/test/models/translation_test.rb +++ b/test/models/translation_test.rb @@ -27,9 +27,4 @@ class TranslationTest < ActiveSupport::TestCase ) assert translation.save end - - test "should have correct original url" do - first = Translation.first - assert_equal "https://projecteuler.net/problem=#{first.id}", first.original_url - end end From 3cfb19a0096efe63715c7d0d44a078c0c3c08404 Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Tue, 30 Dec 2014 11:32:06 +0100 Subject: [PATCH 10/19] Remove edit, update, destroy actions for translations --- app/controllers/translations_controller.rb | 30 +------------------ app/views/translations/show.html.erb | 1 - config/routes.rb | 2 +- .../translations_controller_test.rb | 18 ----------- 4 files changed, 2 insertions(+), 49 deletions(-) diff --git a/app/controllers/translations_controller.rb b/app/controllers/translations_controller.rb index 5436b33..7d63d70 100644 --- a/app/controllers/translations_controller.rb +++ b/app/controllers/translations_controller.rb @@ -1,5 +1,5 @@ class TranslationsController < ApplicationController - before_action :set_translation, only: [:show, :edit, :update, :destroy] + before_action :set_translation, only: :show # GET /translations # GET /translations.json @@ -17,10 +17,6 @@ class TranslationsController < ApplicationController @translation = Translation.new end - # GET /translations/1/edit - def edit - end - # POST /translations # POST /translations.json def create @@ -37,30 +33,6 @@ class TranslationsController < ApplicationController end end - # PATCH/PUT /translations/1 - # PATCH/PUT /translations/1.json - def update - respond_to do |format| - if @translation.update(translation_params) - format.html { redirect_to @translation, notice: 'Translation was successfully updated.' } - format.json { render :show, status: :ok, location: @translation } - else - format.html { render :edit } - format.json { render json: @translation.errors, status: :unprocessable_entity } - end - end - end - - # DELETE /translations/1 - # DELETE /translations/1.json - def destroy - @translation.destroy - respond_to do |format| - format.html { redirect_to translations_url, notice: 'Translation was successfully destroyed.' } - format.json { head :no_content } - end - end - private # Use callbacks to share common setup or constraints between actions. def set_translation diff --git a/app/views/translations/show.html.erb b/app/views/translations/show.html.erb index c7fb342..8b01959 100644 --- a/app/views/translations/show.html.erb +++ b/app/views/translations/show.html.erb @@ -14,4 +14,3 @@
<%= link_to 'Dieses Problem auf projecteuler.net', @translation.problem.original_url, target: '_blank' %>
-<%= link_to 'Edit', edit_translation_path(@translation), class: 'btn btn-default' %> diff --git a/config/routes.rb b/config/routes.rb index 65e9153..ba38df9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,6 @@ Rails.application.routes.draw do resources :problems, only: [:index, :show] - resources :translations + resources :translations, only: [:index, :show, :new, :create] # 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/test/controllers/translations_controller_test.rb b/test/controllers/translations_controller_test.rb index a1cbc04..4314187 100644 --- a/test/controllers/translations_controller_test.rb +++ b/test/controllers/translations_controller_test.rb @@ -33,22 +33,4 @@ class TranslationsControllerTest < ActionController::TestCase get :show, id: @translation assert_response :success end - - test "should get edit" do - get :edit, id: @translation - assert_response :success - end - - test "should update translation" do - patch :update, id: @translation, translation: @update - assert_redirected_to translation_path(assigns(:translation)) - end - - test "should destroy translation" do - assert_difference('Translation.count', -1) do - delete :destroy, id: @translation - end - - assert_redirected_to translations_path - end end From c29e77b2381f2f2b7cb4ce8726f08f34fdb3cf7b Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Tue, 30 Dec 2014 11:35:10 +0100 Subject: [PATCH 11/19] Make problems index the root page --- config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index ba38df9..756ec6d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,7 +6,7 @@ Rails.application.routes.draw do # See how all your routes lay out with "rake routes". # You can have the root of your site routed with "root" - root 'translations#index' + root 'problems#index' # Example of regular route: # get 'products/:id' => 'catalog#view' From 37f4eefff2c3367e540f85672b5cc32fcd6abbf9 Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Tue, 30 Dec 2014 11:35:25 +0100 Subject: [PATCH 12/19] Add problems to navbar --- app/views/layouts/application.html.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 7e0db88..5f18bdf 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -13,6 +13,7 @@ <% end %> <%= horizontal do %> <%= nav class: 'navbar-left' do %> + <%= link_to 'Problems', problems_path %> <%= link_to 'Translations', translations_path %> <% end %> <% end %> From b16a04973a1089450ae9a75acf64cef20236519a Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Tue, 30 Dec 2014 17:12:50 +0100 Subject: [PATCH 13/19] Make translations a nested resource for each problem --- app/controllers/translations_controller.rb | 11 ++++++++--- app/models/translation.rb | 2 +- app/views/translations/_form.html.erb | 2 +- app/views/translations/index.html.erb | 4 +--- app/views/translations/new.html.erb | 2 +- config/routes.rb | 6 ++++-- test/controllers/translations_controller_test.rb | 5 ++--- 7 files changed, 18 insertions(+), 14 deletions(-) diff --git a/app/controllers/translations_controller.rb b/app/controllers/translations_controller.rb index 7d63d70..3134b7b 100644 --- a/app/controllers/translations_controller.rb +++ b/app/controllers/translations_controller.rb @@ -1,5 +1,6 @@ class TranslationsController < ApplicationController before_action :set_translation, only: :show + before_action :set_problem, only: [:new, :create] # GET /translations # GET /translations.json @@ -14,13 +15,13 @@ class TranslationsController < ApplicationController # GET /translations/new def new - @translation = Translation.new + @translation = @problem.translations.build end # POST /translations # POST /translations.json def create - @translation = Translation.new(translation_params) + @translation = @problem.translations.new(translation_params) respond_to do |format| if @translation.save @@ -41,6 +42,10 @@ class TranslationsController < ApplicationController # Never trust parameters from the scary internet, only allow the white list through. def translation_params - params.require(:translation).permit(:title, :content, :problem_id) + params.require(:translation).permit(:title, :content) + end + + def set_problem + @problem = Problem.find(params[:problem_id]) end end diff --git a/app/models/translation.rb b/app/models/translation.rb index 04227f5..8253535 100644 --- a/app/models/translation.rb +++ b/app/models/translation.rb @@ -1,7 +1,7 @@ class Translation < ActiveRecord::Base belongs_to :problem, inverse_of: :translations - validates :title, :content, :problem, presence: true + validates :title, :content, :problem_id, presence: true validates :title, uniqueness: true self.per_page = 50 diff --git a/app/views/translations/_form.html.erb b/app/views/translations/_form.html.erb index 9656465..19910fd 100644 --- a/app/views/translations/_form.html.erb +++ b/app/views/translations/_form.html.erb @@ -1,4 +1,4 @@ -<%= form_for(@translation) do |f| %> +<%= form_for([@problem, @translation]) do |f| %> <% if @translation.errors.any? %>

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

diff --git a/app/views/translations/index.html.erb b/app/views/translations/index.html.erb index 02764f1..ad98e8d 100644 --- a/app/views/translations/index.html.erb +++ b/app/views/translations/index.html.erb @@ -19,6 +19,4 @@ <%= render 'translation_pagination' %> -
- -<%= link_to 'New Translation', new_translation_path, class: 'btn btn-default' %> +
\ No newline at end of file diff --git a/app/views/translations/new.html.erb b/app/views/translations/new.html.erb index 45f1fd9..47224da 100644 --- a/app/views/translations/new.html.erb +++ b/app/views/translations/new.html.erb @@ -1,4 +1,4 @@ -

New translation

+

New translation for problem <%= @problem.id %>

<%= render 'form' %> diff --git a/config/routes.rb b/config/routes.rb index 756ec6d..a0f3fe3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,8 @@ Rails.application.routes.draw do - resources :problems, only: [:index, :show] - resources :translations, only: [:index, :show, :new, :create] + resources :problems, only: [:index, :show] do + resources :translations, only: [:index, :new, :create] + end + resources :translations, only: [:index, :show] # 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/test/controllers/translations_controller_test.rb b/test/controllers/translations_controller_test.rb index 4314187..41b3e18 100644 --- a/test/controllers/translations_controller_test.rb +++ b/test/controllers/translations_controller_test.rb @@ -6,7 +6,6 @@ class TranslationsControllerTest < ActionController::TestCase @update = { title: 'New title', content: 'This is the new content', - problem_id: 1 } end @@ -17,13 +16,13 @@ class TranslationsControllerTest < ActionController::TestCase end test "should get new" do - get :new + get :new, problem_id: 1 assert_response :success end test "should create translation" do assert_difference('Translation.count') do - post :create, translation: @update + post :create, problem_id: 1, translation: @update end assert_redirected_to translation_path(assigns(:translation)) From d07f9bb0591b4863b971c43c513028c263a28a57 Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Tue, 30 Dec 2014 17:34:14 +0100 Subject: [PATCH 14/19] Add missing attribute in test --- test/models/translation_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/models/translation_test.rb b/test/models/translation_test.rb index b2c3ebc..5549ff0 100644 --- a/test/models/translation_test.rb +++ b/test/models/translation_test.rb @@ -14,7 +14,8 @@ class TranslationTest < ActiveSupport::TestCase test "should not save translation with duplicate title" do translation = Translation.new( title: translations(:translation_one).title, - content: 'This is some content' + content: 'This is some content', + problem_id: 3 ) assert_not translation.save end From 63bd0193ce0c5bbde01ffd77f53e90db2d9e4852 Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Tue, 30 Dec 2014 17:46:55 +0100 Subject: [PATCH 15/19] Remove index action for nested translation --- config/routes.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/routes.rb b/config/routes.rb index a0f3fe3..444ebcc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,6 @@ Rails.application.routes.draw do resources :problems, only: [:index, :show] do - resources :translations, only: [:index, :new, :create] + resources :translations, only: [:new, :create] end resources :translations, only: [:index, :show] From d20b3e0fef8c9b1711001a357517adcd97d74f58 Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Tue, 30 Dec 2014 17:59:39 +0100 Subject: [PATCH 16/19] Prevent N+1 query on problem index --- app/controllers/problems_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/problems_controller.rb b/app/controllers/problems_controller.rb index 4cd4dab..43adad4 100644 --- a/app/controllers/problems_controller.rb +++ b/app/controllers/problems_controller.rb @@ -2,7 +2,7 @@ class ProblemsController < ApplicationController before_action :set_problem, only: [:show] def index - @problems = Problem.paginate(page: params[:page]) + @problems = Problem.paginate(page: params[:page]).includes(:translation) end def show From d2f12927241530dc5b8fdd35e078d76d0bb7e18e Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Tue, 30 Dec 2014 19:50:35 +0100 Subject: [PATCH 17/19] Add links for problem translation and improvement --- app/views/problems/index.html.erb | 5 ++++- app/views/problems/show.html.erb | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/app/views/problems/index.html.erb b/app/views/problems/index.html.erb index 48632e2..a46d613 100644 --- a/app/views/problems/index.html.erb +++ b/app/views/problems/index.html.erb @@ -17,7 +17,10 @@ <% if problem.is_translated? %> <%= link_to problem.title, problem %> <% else %> - Dieses Problem wurde noch nicht übersetzt. + Dieses Problem wurde noch nicht übersetzt. + <%= link_to new_problem_translation_path(problem), class: 'btn btn-default btn-xs' do %> + <%= icon :pencil %> Übersetzung vorschlagen + <% end %> <% end %> diff --git a/app/views/problems/show.html.erb b/app/views/problems/show.html.erb index 4ad7873..c070457 100644 --- a/app/views/problems/show.html.erb +++ b/app/views/problems/show.html.erb @@ -5,7 +5,9 @@ - +<%= link_to new_problem_translation_path(@problem), class: 'btn btn-default btn-sm pull-right' do %> + <%= icon :pencil %> Übersetzung verbessern +<% end %> <%= panel do %>
<%= sanitize @problem.content %> From 5b7fd1fe270608f435e5b5ca7af353cb84b3ebe7 Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Tue, 30 Dec 2014 23:32:17 +0100 Subject: [PATCH 18/19] Fix association from problem to its current translation --- app/models/problem.rb | 3 ++- test/fixtures/problems.yml | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/models/problem.rb b/app/models/problem.rb index 4652b51..7747ddd 100644 --- a/app/models/problem.rb +++ b/app/models/problem.rb @@ -1,5 +1,6 @@ class Problem < ActiveRecord::Base - has_one :translation + # TODO Check whether there is a better relation for this + belongs_to :translation delegate :title, :content, to: :translation has_many :translations, inverse_of: :problem diff --git a/test/fixtures/problems.yml b/test/fixtures/problems.yml index 95b9482..48dcf94 100644 --- a/test/fixtures/problems.yml +++ b/test/fixtures/problems.yml @@ -2,11 +2,11 @@ one: id: 1 - translation_id: <%= ActiveRecord::FixtureSet.identify(:translation_one) %> + translation: translation_one two: id: 2 - translation_id: <%= ActiveRecord::FixtureSet.identify(:translation_two) %> + translation: translation_two three: id: 3 From 2cb4e0a2806ea70c64c87e0317c830350a436935 Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Tue, 30 Dec 2014 23:33:05 +0100 Subject: [PATCH 19/19] Fix id problem in seed data --- db/seeds.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/seeds.rb b/db/seeds.rb index 9e7aaf9..9bca30f 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -15,7 +15,7 @@ for i in 1..10 do title: "Problem Nummer #{i}", content: "Das hier ist der Inhalt von Problem #{i}.
Hier ist die zweite Zeile." ) - Problem.create(id: i, translation: translation) + Problem.create(id: i, translation_id: translation.id) end Problem.create(id: 11) \ No newline at end of file