Skip to content

Commit

Permalink
Merge pull request #88 from it3s/flag
Browse files Browse the repository at this point in the history
Flag
  • Loading branch information
LuizArmesto committed Nov 11, 2014
2 parents d865389 + 815959c commit 254b237
Show file tree
Hide file tree
Showing 46 changed files with 758 additions and 37 deletions.
6 changes: 3 additions & 3 deletions app/assets/stylesheets/activities.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
float: left;
margin: -3px 6px 0 -22px;
position: relative;

img {
border: $white 1px solid;
width: 22px;
height: 22px;
width: 22px;
height: 22px;
}
}

Expand Down
16 changes: 16 additions & 0 deletions app/assets/stylesheets/admin.css.scss
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;
}
}
}
2 changes: 2 additions & 0 deletions app/assets/stylesheets/application.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,6 @@
@import "pictures";
@import "import";
@import "comments";
@import "flags";
@import "admin";
@import "layers";
101 changes: 101 additions & 0 deletions app/assets/stylesheets/flags.css.scss
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;
}
}
}
37 changes: 37 additions & 0 deletions app/controllers/admins_controller.rb
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


6 changes: 5 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ def not_authenticated
redirect_to login_path
end

private
protected

def require_admin
redirect_to(root_path, notice: t('access_denied')) unless current_user.admin?
end

def set_locale
I18n.locale = (current_user.try(:language) || session[:language] ||
Expand Down
17 changes: 14 additions & 3 deletions app/controllers/concerns/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ def save_object(obj, params_hash)
end

def find_polymorphic_object
resource, id = request.path.split('/')[1..2]
_model = resource.classify.constantize
instance_variable_set "@#{_model.name.underscore}", _model.find(id) # set var and return the obj
_obj = _find_polymorphic_object_from_uri request
instance_variable_set "@#{_obj.class.name.underscore}", _obj # set var and return the obj
end

def find_object_from_referer
_find_polymorphic_object_from_uri URI(request.referer)
end

def find_polymorphic_model
Expand Down Expand Up @@ -126,5 +129,13 @@ def news_feed_results(all=false)
end
activities
end

protected

def _find_polymorphic_object_from_uri(uri)
resource, id = uri.path.split('/')[1..2]
_model = resource.classify.constantize
_model.find(id)
end
end

42 changes: 42 additions & 0 deletions app/controllers/flags_controller.rb
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
4 changes: 4 additions & 0 deletions app/helpers/concerns/components_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ def currency_symbol(curr)
end
end

def flag_reason_options
Flag.reason_choices.map { |reason| [t("flags.reason.#{reason}"), reason] }
end

private

def _hash_with_humanized_keys(hash)
Expand Down
4 changes: 4 additions & 0 deletions app/listeners/activity_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def commented(payload)
save_activity payload, :object, :comment
end

def flagged(payload)
save_activity payload, :object, :flag
end

private

def save_activity(params, key, action)
Expand Down
5 changes: 5 additions & 0 deletions app/models/admin.rb
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
16 changes: 16 additions & 0 deletions app/models/concerns/flaggable.rb
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

28 changes: 28 additions & 0 deletions app/models/flag.rb
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
1 change: 1 addition & 0 deletions app/models/geo_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class GeoData < ActiveRecord::Base
include Exportable
include PublicActivity::Common
include Commentable
include Flaggable

geojson_field :location
searchable_tags :tags
Expand Down
1 change: 1 addition & 0 deletions app/models/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Map < ActiveRecord::Base
include Exportable
include PublicActivity::Common
include Commentable
include Flaggable

belongs_to :administrator, class_name: 'User'

Expand Down
7 changes: 6 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class User < ActiveRecord::Base
include Follower
include Searchable
include PublicActivity::Common
include Flaggable

mount_uploader :avatar, AvatarUploader
process_in_background :avatar
Expand Down Expand Up @@ -60,6 +61,10 @@ def settings
@settings ||= Settings.new(self)
end

def admin?
Admin.where(user_id: id).exists?
end

private

def set_auth_token
Expand All @@ -68,6 +73,6 @@ def set_auth_token
end

def generate_auth_token
SecureRandom.uuid.gsub /\-/, ''
SecureRandom.uuid.gsub(/\-/, '')
end
end
Loading

0 comments on commit 254b237

Please sign in to comment.