-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from it3s/flag
Flag
- Loading branch information
Showing
46 changed files
with
758 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.admin { | ||
&.confirm-deletion { | ||
h2 { | ||
text-align: center; | ||
color: $grey; | ||
margin-bottom: 20px; | ||
} | ||
|
||
.delete-btn { | ||
@include red-button; | ||
width: 50%; | ||
margin: 0 25%; | ||
text-align: center; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,4 +66,6 @@ | |
@import "pictures"; | ||
@import "import"; | ||
@import "comments"; | ||
@import "flags"; | ||
@import "admin"; | ||
@import "layers"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
.flags { | ||
h2 { margin-bottom: 10px; } | ||
|
||
select { | ||
width: 355px; | ||
} | ||
|
||
input[type=submit] { | ||
float: right; | ||
} | ||
} | ||
|
||
.flags-list-container { | ||
margin-bottom: 20px; | ||
|
||
h2 { | ||
color: $grey; | ||
padding-bottom: 10px; | ||
border-bottom: solid 1px $soft-grey; | ||
} | ||
|
||
.empty { | ||
font-size: 1.2em; | ||
font-weight: bold; | ||
text-align: center; | ||
color: $grey; | ||
margin-top: 20px; | ||
} | ||
.flag { | ||
background-color: $meppit-light-blue; | ||
border-radius: 4px; | ||
padding: 10px; | ||
margin: 5px 0; | ||
color: $dark-grey; | ||
|
||
a { color: $dark-grey; } | ||
|
||
.flag-headline { | ||
margin-bottom: 5px; | ||
} | ||
|
||
.flag-avatar { | ||
display: inline-block; | ||
position: relative; | ||
margin-right: 5px; | ||
|
||
img { | ||
width: 22px; | ||
height: 22px; | ||
} | ||
} | ||
|
||
.flag-name { | ||
display: inline-block; | ||
color: $grey; | ||
font-weight: bold; | ||
} | ||
|
||
.flag-time { | ||
display: inline-block; | ||
font-size: 0.8em; | ||
color: $grey; | ||
margin-left: 10px; | ||
} | ||
|
||
.flag-user { | ||
display: inline-block; | ||
margin-left: 20px; | ||
font-size: 0.9em; | ||
color: $grey; | ||
|
||
.user-name { font-weight: bold; } | ||
} | ||
|
||
.flag-reason { | ||
display: inline-block; | ||
margin-left: 20px; | ||
background: lighten($meppit-light-blue, 10%); | ||
font-size: 0.9em; | ||
font-weight: bold; | ||
padding: 2px 4px; | ||
border: solid 1px $soft-grey; | ||
border-radius: 4px; | ||
} | ||
|
||
.flag-comment-container { | ||
margin-top: 10px; | ||
margin-left: 20px; | ||
|
||
.comment { | ||
border-left: solid 2px $soft-grey; | ||
padding-left: 10px; | ||
} | ||
} | ||
|
||
.mark-as-solved { | ||
@include blue-button-with-border; | ||
float: right; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class AdminsController < ApplicationController | ||
before_action :require_login | ||
before_action :require_admin | ||
before_action :is_current_user, only: [:show] | ||
before_action :find_flags, only: [:show] | ||
before_action :find_deletable, only: [:confirm_deletion, :delete_object] | ||
|
||
def show | ||
end | ||
|
||
def confirm_deletion | ||
render layout: nil if request.xhr? | ||
end | ||
|
||
def delete_object | ||
@deletable.destroy | ||
flash[:info] = t('admin.deleted', name: @deletable.name) | ||
redirect_to root_path | ||
end | ||
|
||
protected | ||
|
||
def find_flags | ||
@unsolved_flags = Flag.includes(:flaggable, :user).unsolved | ||
@solved_flags = Flag.includes(:flaggable, :user).solved | ||
end | ||
|
||
def is_current_user | ||
redirect_to(root_path, notice: t('access_denied')) if params[:id].to_i != current_user.id | ||
end | ||
|
||
def find_deletable | ||
@deletable = find_object_from_referer | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
class FlagsController < ApplicationController | ||
before_action :require_login | ||
before_action :find_flaggable, only: [:create] | ||
after_action :publish_flagged_event, only: [:create] | ||
before_action :require_admin, only: [:mark_as_solved] | ||
|
||
def new | ||
@flag = Flag.new | ||
render layout: nil if request.xhr? | ||
end | ||
|
||
def create | ||
@flag = Flag.new flag_params | ||
if @flag.valid? && @flag.save | ||
flash[:notice] = t('flags.message_sent') | ||
render json: {redirect: polymorphic_path([@flaggable])} | ||
else | ||
render json: {errors: @flag.errors.messages}, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def mark_as_solved | ||
@flag = Flag.find params[:id] | ||
@flag.solved = true | ||
@flag.save | ||
redirect_to admin_path(current_user) | ||
end | ||
|
||
private | ||
|
||
def find_flaggable | ||
@flaggable = find_object_from_referer | ||
end | ||
|
||
def flag_params | ||
params.require(:flag).permit(:comment, :reason).merge(user: current_user, flaggable: @flaggable) | ||
end | ||
|
||
def publish_flagged_event | ||
EventBus.publish "flagged", object: @flaggable, current_user: current_user, changes: {@flag.reason => ""} if response.ok? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Admin < ActiveRecord::Base | ||
belongs_to :user | ||
|
||
validates :user, presence: true | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module Flaggable | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
has_many :flags, as: :flaggable | ||
|
||
after_destroy :clean_flags_for_destroyed_flaggable! | ||
|
||
private | ||
|
||
def clean_flags_for_destroyed_flaggable! | ||
Flag.where(flaggable: self).destroy_all | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Flag < ActiveRecord::Base | ||
belongs_to :user | ||
belongs_to :flaggable, polymorphic: true | ||
|
||
validates :user, :reason, :flaggable, presence: true | ||
validate :reason_from_list | ||
|
||
scope :solved, -> { where(solved: true).order('created_at desc') } | ||
scope :unsolved, -> { where(solved: false).order('created_at desc') } | ||
|
||
def self.reason_choices | ||
@reasons_ ||= [ | ||
'deletion', | ||
'spam', | ||
'inappropriate', | ||
'tos_violation', | ||
'copyright', | ||
'wrong_info', | ||
'other', | ||
] | ||
end | ||
|
||
private | ||
|
||
def reason_from_list | ||
errors.add :reason, 'Invalid reason' if reason && !self.class.reason_choices.include?(reason) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.