diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index c1aab761..15bf99fb 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -41,13 +41,9 @@ class CommentsController < ApplicationController # if the user is internal, set the name @comments.map{|comment| - if(comment.user_type == 'InternalUser') - comment.username = InternalUser.find(comment.user_id).name - elsif(comment.user_type == 'ExternalUser') - comment.username = ExternalUser.find(comment.user_id).name - # alternative: # if the user is external, fetch the displayname from xikolo - # Xikolo::UserClient.get(comment.user_id.to_s)[:display_name] - end + comment.username = comment.user.name + # alternative: # if the user is external, fetch the displayname from xikolo + # Xikolo::UserClient.get(comment.user_id.to_s)[:display_name] } else @comments = Comment.all.limit(0) #we need an empty relation here diff --git a/app/controllers/request_for_comments_controller.rb b/app/controllers/request_for_comments_controller.rb index 9e0a5d49..312dc260 100644 --- a/app/controllers/request_for_comments_controller.rb +++ b/app/controllers/request_for_comments_controller.rb @@ -11,11 +11,15 @@ class RequestForCommentsController < ApplicationController # GET /request_for_comments # GET /request_for_comments.json def index - # @request_for_comments = RequestForComment.all - @request_for_comments = RequestForComment.all.order('created_at DESC').limit(50) + @request_for_comments = RequestForComment.last_per_user(2).paginate(page: params[:page]) authorize! end + def get_my_comment_requests + @request_for_comments = RequestForComment.where(user_id: current_user.id).order('created_at DESC').paginate(page: params[:page]) + render 'index' + end + # GET /request_for_comments/1 # GET /request_for_comments/1.json def show @@ -66,6 +70,6 @@ class RequestForCommentsController < ApplicationController # Never trust parameters from the scary internet, only allow the white list through. def request_for_comment_params - params.require(:request_for_comment).permit(:exercise_id, :file_id, :requested_at).merge(requestor_user_id: current_user.id, user_type: current_user.class.name) + params.require(:request_for_comment).permit(:exercise_id, :file_id, :requested_at).merge(user_id: current_user.id, user_type: current_user.class.name) end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 4cc8b0e2..abbdc2a0 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -4,4 +4,5 @@ class Comment < ActiveRecord::Base attr_accessor :username belongs_to :file, class: CodeOcean::File + belongs_to :user, polymorphic: true end diff --git a/app/models/request_for_comment.rb b/app/models/request_for_comment.rb index a22a7246..3676550d 100644 --- a/app/models/request_for_comment.rb +++ b/app/models/request_for_comment.rb @@ -1,7 +1,20 @@ class RequestForComment < ActiveRecord::Base - before_create :set_requested_timestamp + belongs_to :exercise + belongs_to :file, class: CodeOcean::File + belongs_to :user, polymorphic: true + + before_create :set_requested_timestamp + + def self.last_per_user(n = 5) + from("(#{row_number_user_sql}) as request_for_comments").where("row_number <= ?", n) + end def set_requested_timestamp self.requested_at = Time.now end + + private + def self.row_number_user_sql + select("id, user_id, exercise_id, file_id, requested_at, created_at, updated_at, user_type, row_number() OVER (PARTITION BY user_id ORDER BY created_at DESC) as row_number").to_sql + end end diff --git a/app/views/request_for_comments/_form.html.erb b/app/views/request_for_comments/_form.html.erb index 493e2445..4d4494ce 100644 --- a/app/views/request_for_comments/_form.html.erb +++ b/app/views/request_for_comments/_form.html.erb @@ -12,8 +12,8 @@ <% end %>
- <% - user = nil - if (request_for_comment.user_type == 'InternalUser') - user = InternalUser.find(request_for_comment.requestor_user_id) - else - user = ExternalUser.find(request_for_comment.requestor_user_id) - end - %> - - <%= user %> | <%= request_for_comment.requested_at %> -
- - <% end %> -<% - user = nil - if (@request_for_comment.user_type == 'InternalUser') - user = InternalUser.find(@request_for_comment.requestor_user_id) - else - user = ExternalUser.find(@request_for_comment.requestor_user_id) - end + + user = @request_for_comment.user + %> <%= user %> | <%= @request_for_comment.requested_at %>
diff --git a/app/views/request_for_comments/show.json.jbuilder b/app/views/request_for_comments/show.json.jbuilder index b3962aad..19da6be0 100644 --- a/app/views/request_for_comments/show.json.jbuilder +++ b/app/views/request_for_comments/show.json.jbuilder @@ -1 +1 @@ -json.extract! @request_for_comment, :id, :requestor_user_id, :exercise_id, :file_id, :requested_at, :created_at, :updated_at, :user_type +json.extract! @request_for_comment, :id, :user_id, :exercise_id, :file_id, :requested_at, :created_at, :updated_at, :user_type diff --git a/config/locales/de.yml b/config/locales/de.yml index e7fe1857..cb8ef305 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -73,6 +73,11 @@ de: password: Passwort password_confirmation: Passwort-Bestätigung role: Rolle + request_for_comments: + exercise: Aufgabe + execution_environment: Sprache + username: Benutzername + requested_at: Angefragezeitpunkt submission: cause: Anlass code: Code @@ -176,7 +181,7 @@ de: run: Ausführen run_failure: Ihr Code konnte nicht auf der Plattform ausgeführt werden. run_success: Ihr Code wurde auf der Plattform ausgeführt. - requestComments: Rückmeldung erbitten + requestComments: Kommentare erbitten save: Speichern score: Bewerten start_over: Von vorne anfangen @@ -272,6 +277,9 @@ de: reset_password: body: 'Bitte besuchen Sie %{link}, sofern Sie Ihr Passwort zurücksetzen wollen.' subject: Anweisungen zum Zurücksetzen Ihres Passworts + request_for_comments: + index: + get_my_comment_requests: Meine Kommentaranfragen sessions: create: failure: Fehlerhafte E-Mail oder Passwort. diff --git a/config/locales/en.yml b/config/locales/en.yml index 52d4ceb3..e726557d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -73,6 +73,11 @@ en: password: Password password_confirmation: Passwort Confirmation role: Role + request_for_comments: + exercise: Exercise + execution_environment: Language + username: Username + requested_at: Request Date submission: cause: Cause code: Code @@ -272,6 +277,9 @@ en: reset_password: body: 'Please visit %{link} if you want to reset your password.' subject: Password reset instructions + request_for_comments: + index: + get_my_comment_requests: My Requests for Comments sessions: create: failure: Invalid email or password. diff --git a/config/routes.rb b/config/routes.rb index 7e0b4c33..1616d9b8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,7 @@ FILENAME_REGEXP = /[\w\.]+/ unless Kernel.const_defined?(:FILENAME_REGEXP) Rails.application.routes.draw do resources :request_for_comments + get '/my_request_for_comments', as: 'my_request_for_comments', to: 'request_for_comments#get_my_comment_requests' resources :comments, except: [:destroy] do collection do delete :destroy diff --git a/db/migrate/20150903152727_remove_requestor_from_request_for_comments.rb b/db/migrate/20150903152727_remove_requestor_from_request_for_comments.rb new file mode 100644 index 00000000..f9857f43 --- /dev/null +++ b/db/migrate/20150903152727_remove_requestor_from_request_for_comments.rb @@ -0,0 +1,5 @@ +class RemoveRequestorFromRequestForComments < ActiveRecord::Migration + def change + rename_column :request_for_comments, :requestor_user_id, :user_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 30fb419f..7f3be339 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: 20150818142251) do +ActiveRecord::Schema.define(version: 20150903152727) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -171,9 +171,9 @@ ActiveRecord::Schema.define(version: 20150818142251) do add_index "internal_users_teams", ["team_id"], name: "index_internal_users_teams_on_team_id", using: :btree create_table "request_for_comments", force: true do |t| - t.integer "requestor_user_id", null: false - t.integer "exercise_id", null: false - t.integer "file_id", null: false + t.integer "user_id", null: false + t.integer "exercise_id", null: false + t.integer "file_id", null: false t.datetime "requested_at" t.datetime "created_at" t.datetime "updated_at"