Code Cleanup: Usage of Polymorphic User instead of ExternalUser and InternalUser

Renamed requestor_user_id -> user_id
Index of request_for_comments now returns the n (5?) newest requests per user. Solved via sliding windows in postgres, code added to model.
Added route to /my_request_for_comments/ that shows all requests for the current user.
Changed view from ERB to slim
This commit is contained in:
Ralf Teusner
2015-09-03 19:04:38 +02:00
parent 8e9f19f41d
commit d292fec47c
15 changed files with 78 additions and 47 deletions

View File

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

View File

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

View File

@ -4,4 +4,5 @@ class Comment < ActiveRecord::Base
attr_accessor :username
belongs_to :file, class: CodeOcean::File
belongs_to :user, polymorphic: true
end

View File

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

View File

@ -12,8 +12,8 @@
<% end %>
<div class="field">
<%= f.label :requestor_user_id %><br>
<%= f.number_field :requestor_user_id %>
<%= f.label :user_id %><br>
<%= f.number_field :user_id %>
</div>
<div class="field">
<%= f.label :exercise_id %><br>

View File

@ -1,21 +0,0 @@
<h1><%= t('exercises.implement.comment.listing') %></h1>
<div class="list-group">
<% @request_for_comments.each do |request_for_comment| %>
<a href="<%= request_for_comment_path(request_for_comment) %>" class="list-group-item">
<h4 class="list-group-item-heading"><%= Exercise.find(request_for_comment.exercise_id) %></h4>
<p class="list-group-item-text">
<%
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 %>
</p>
</a>
<% end %>
</div>

View File

@ -0,0 +1,19 @@
h1 = RequestForComment.model_name.human(count: 2)
.table-responsive
table.table
thead
tr
th = t('activerecord.attributes.request_for_comments.exercise')
th = t('activerecord.attributes.request_for_comments.execution_environment')
th = t('activerecord.attributes.request_for_comments.username')
th = t('activerecord.attributes.request_for_comments.requested_at')
tbody
- @request_for_comments.each do |request_for_comment|
tr data-id=request_for_comment.id
td = link_to(request_for_comment.exercise.title, request_for_comment)
td = request_for_comment.exercise.execution_environment
td = request_for_comment.user.name
td = request_for_comment.requested_at
= render('shared/pagination', collection: @request_for_comments)

View File

@ -1,4 +1,4 @@
json.array!(@request_for_comments) do |request_for_comment|
json.extract! request_for_comment, :id, :requestor_user_id, :exercise_id, :file_id, :requested_at, :user_type
json.extract! request_for_comment, :id, :user_id, :exercise_id, :file_id, :requested_at, :user_type
json.url request_for_comment_url(request_for_comment, format: :json)
end

View File

@ -2,12 +2,9 @@
<h4 class="list-group-item-heading"><%= Exercise.find(@request_for_comment.exercise_id) %></h4>
<p class="list-group-item-text">
<%
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 %>
</p>

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,5 @@
class RemoveRequestorFromRequestForComments < ActiveRecord::Migration
def change
rename_column :request_for_comments, :requestor_user_id, :user_id
end
end

View File

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