Skip to content

Commit

Permalink
updates annotation on point manual edit, cleans up dead code, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
madoleary committed Jul 19, 2023
1 parent 187ea86 commit e695bd0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 49 deletions.
63 changes: 23 additions & 40 deletions app/controllers/points_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,24 @@ class PointsController < ApplicationController
include FontAwesome5::Rails::IconHelper

before_action :authenticate_user!, except: [:show]
before_action :set_point, only: [:show, :edit, :update, :review, :post_review, :approve]
before_action :set_topics, only: [:new, :create, :edit, :update, :post_review, :approve]
before_action :check_status, only: [:create, :update]
before_action :set_point, only: %i[show edit update review approve]
before_action :set_topics, only: %i[new create edit update approve]
before_action :check_status, only: %i[create update]

rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized

def index
authorize Point

@points = Point.includes(:service, :case, :user).order("RANDOM()").limit(100)

if @query = params[:query]
@points = Point.includes(:service, :case, :user).search_points_by_multiple(@query)
end
@points = Point.includes(:service, :case, :user).order('RANDOM()').limit(100)
@points = Point.includes(:service, :case, :user).search_points_by_multiple(@query) if @query == params[:query]
end

def new
authorize Point

@point = Point.new
if params[:service_id]
@point.service_id = params[:service_id]
end
@point.service_id = params[:service_id] if params[:service_id]
end

def create
Expand Down Expand Up @@ -57,16 +52,25 @@ def show
if @point.annotation_ref
annotation = Point.retrieve_annotation(@point.annotation_ref)
annotation_json = JSON.parse(annotation['target_selectors'])
@point_text = annotation_json[2]['exact']
@point_text = annotation_json[2] && annotation_json[2]['exact']
end
@versions = @point.versions.includes(:item).reverse
end

def update
authorize @point

if (point_params[:case_id] != @point.case_id) && @point.annotation_ref
case_obj = Case.find(point_params[:case_id])
uuid = StringConverter.new(string: @point.annotation_ref).to_uuid
annotation = Annotation.find(uuid)
annotation.tags = [] << case_obj.title
end

if @point.update(point_params)
create_comment(@point.point_change)
annotation.save!
comment = @point.point_change.present? ? @point.point_change : 'point updated without comment'
create_comment(comment)
redirect_to point_path(@point)
elsif @point.case.nil?
render :edit
Expand All @@ -83,39 +87,18 @@ def approve
authorize @point

if @point.update(status: 'approved')
create_comment(status_badge('approved') + raw('<br>') + 'No comment given')

redirect_to point_path(@point)
else
render :edit
end
end

def post_review
authorize @point

# process a post of the review form
if @point.update(status: point_params['status'])
report_spam(point_params['point_change'], "ham")
create_comment(status_badge(point_params['status']) + raw('<br>') + point_params['point_change'])
comment = status_badge('approved') + raw('<br>') + 'No comment given'
create_comment(comment)
redirect_to point_path(@point)
else
render :edit
end
end

def user_points
@points = current_user.points.includes([:service, :case])

if @query = params[:query]
@points = current_user.points.includes([:service, :case, :user]).search_points_by_multiple(@query)
end
end

private

def user_not_authorized
flash[:alert] = "You are not authorized to perform this action."
flash[:alert] = 'You are not authorized to perform this action.'
redirect_to(request.referrer || root_path)
end

Expand All @@ -126,7 +109,7 @@ def create_comment(comment_text)
def point_create_options(point, path)
if point.save
redirect_to path
flash[:notice] = "You created a point!"
flash[:notice] = 'Point created'
elsif point.case.nil?
render :new
else
Expand All @@ -143,11 +126,11 @@ def set_topics
end

def point_params
params.require(:point).permit(:title, :source, :status, :analysis, :service_id, :query, :point_change, :case_id, :document, :quote_start, :quote_end, :quote_text)
params.require(:point).permit(:title, :source, :status, :analysis, :service_id, :query, :point_change, :case_id, :document)
end

def check_status
if (!['draft', 'pending', 'declined'].include? point_params['status'])
unless %w[draft pending declined].include? point_params['status']
render :edit
end
end
Expand Down
4 changes: 0 additions & 4 deletions app/policies/point_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ def approve?
is_peer_curator?
end

def post_review?
is_peer_curator?
end

def user_points?
!user.nil?
end
Expand Down
10 changes: 6 additions & 4 deletions config/initializers/rack_attack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,11 @@ class Rack::Attack
# If you want to return 503 so that the attacker might be fooled into
# believing that they've successfully broken your app (or you just want to
# customize the response), then uncomment these lines.
self.throttled_responder = lambda do |env|
[503, # status
{}, # headers
["Oops! It looks like you're doing many different things in a short period of time. We check for this to prevent abusive requests or other types of vandalism to our site. Please try again in 10 minutes."]] # body
self.throttled_responder = lambda do |_env|
[
503, # status
{}, # headers
["Oops! It looks like you're doing many different things in a short period of time. We check for this to prevent abusive requests or other types of vandalism to our site. Please try again in 10 minutes."]
] # body
end
end
1 change: 0 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
get 'my_points', to: 'points#user_points', as: :my_points
get 'points/:id/review', to: 'points#review', as: 'review'
get 'points/:id/approve', to: 'points#approve', as: 'approve'
patch 'points/:id/review', to: 'points#post_review'
resources :points, only: :index
resources :points, except: [:index] do
resources :point_comments, only: %i[new create]
Expand Down

0 comments on commit e695bd0

Please sign in to comment.