Skip to content

Commit

Permalink
Able to add flags in more places; Added very simple global flags index
Browse files Browse the repository at this point in the history
  • Loading branch information
pleary committed Jan 9, 2015
1 parent 06c0408 commit 0253335
Show file tree
Hide file tree
Showing 26 changed files with 244 additions and 135 deletions.
4 changes: 4 additions & 0 deletions app/assets/stylesheets/index.css.scss
Expand Up @@ -1684,3 +1684,7 @@ ul.plain {list-style:none;margin:0;padding:0}
.commentitem .observations.mini .curator_ident .label {
left: 25px;
}

p.spam {
margin-top: 5px;
}
30 changes: 22 additions & 8 deletions app/controllers/flags_controller.rb
Expand Up @@ -2,6 +2,7 @@ class FlagsController < ApplicationController
before_filter :authenticate_user!, :except => [:index, :show]
before_filter :curator_required, :only => [:edit, :update, :destroy]
before_filter :set_model, :except => [:update, :show, :destroy]
before_filter :model_required, :except => [:index, :update, :show, :destroy]
before_filter :load_flag, :only => [:show, :edit, :destroy, :update]

# put the parameters for the foreign keys here
Expand All @@ -13,12 +14,17 @@ class FlagsController < ApplicationController
PARTIALS = %w(dialog)

def index
@object = @model.find(params[@param])
@object = @object.becomes(Photo) if @object.is_a?(Photo)
@flags = @object.flags.paginate(:page => params[:page],
:include => [:user, :resolver], :order => "id desc")
@unresolved = @flags.select {|f| not f.resolved }
@resolved = @flags.select {|f| f.resolved }
if @model
@object = @model.find(params[@param])
@object = @object.becomes(Photo) if @object.is_a?(Photo)
@flags = @object.flags.paginate(:page => params[:page],
:include => [:user, :resolver], :order => "id desc")
@unresolved = @flags.select {|f| not f.resolved }
@resolved = @flags.select {|f| f.resolved }
else
@flags = Flag.where(resolved: false).order("created_at desc").paginate(per_page: 30, page: params[:page])
render :global_index
end
end

def show
Expand Down Expand Up @@ -70,6 +76,9 @@ def create
end

def update
if resolver_id = params[:flag].delete("resolver_id")
params[:flag]["resolver"] = User.find(resolver_id)
end
respond_to do |format|
if @flag.update_attributes(params[:flag])
flash[:notice] = t(:flag_saved)
Expand Down Expand Up @@ -108,7 +117,12 @@ def set_model
if (@model ||= Object.const_get(params[:flag][:flaggable_type]) rescue nil)
return
end
flash[:notice] = t(:you_cant_flag_that)
redirect_to observations_path
end

def model_required
unless @model
flash[:notice] = t(:you_cant_flag_that)
redirect_to observations_path
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/guides_controller.rb
Expand Up @@ -140,7 +140,7 @@ def nav_taxa_for_index
def show
unless @guide.published? || @guide.user_id == current_user.try(:id)
respond_to do |format|
format.any(:html, :mobile) { render(:file => "#{Rails.root}/public/404.html", :status => 404, :layout => false) }
format.any(:html, :mobile) { render_404 }
format.any(:xml, :ngz) { render :status => 404, :text => ""}
format.json { render :json => {:error => "Not found"}, :status => 404 }
end
Expand Down
7 changes: 0 additions & 7 deletions app/models/observation.rb
Expand Up @@ -1331,13 +1331,6 @@ def num_identifications_by_others
num_identification_agreements + num_identification_disagreements
end

#
# Checks whether this observation has been flagged
#
def flagged?
self.flags.select { |f| not f.resolved? }.size > 0
end

def appropriate?
return false if flags.where(:resolved => false).exists?
return false if observation_photos_count > 0 && photos.includes(:flags).where("flags.resolved = ?", false).exists?
Expand Down
4 changes: 0 additions & 4 deletions app/models/photo.rb
Expand Up @@ -223,10 +223,6 @@ def flagged_with(flag, options = {})
end
end
end

def flagged?
flags.detect{|f| !f.resolved?}
end

# Retrieve info about a photo from its native source given its native id.
# Should be implemented by descendents
Expand Down
7 changes: 0 additions & 7 deletions app/models/taxon.rb
Expand Up @@ -638,13 +638,6 @@ def set_scientific_taxon_name
end
end

#
# Checks whether this taxon has been flagged
#
def flagged?
self.flags.select { |f| not f.resolved? }.size > 0
end

# Override assignment method provided by has_many to ensure that all
# callbacks on photos and taxon_photos get called, including after_destroy
def photos=(new_photos)
Expand Down
34 changes: 1 addition & 33 deletions app/models/user.rb
@@ -1,4 +1,5 @@
class User < ActiveRecord::Base
include ActsAsSpammable::User

# If the user has this role, has_role? will always return true
JEDI_MASTER_ROLE = 'admin'
Expand Down Expand Up @@ -125,7 +126,6 @@ class User < ActiveRecord::Base
after_save :update_photo_licenses
after_save :update_sound_licenses
after_save :destroy_messages_by_suspended_user
after_save :suspend_if_spammer
after_update :set_community_taxa_if_pref_changed
after_create :create_default_life_list
after_create :set_uri
Expand Down Expand Up @@ -627,36 +627,4 @@ def self.default_json_options
}
end

def suspend_if_spammer
if self.spammer_changed? && self.spammer
self.suspend!
end
end

# TODO - what to do when the content is deleted? The spam count will go
# down and the user will no longer look like a spammer.
def update_spam_count
self.spam_count = content_flagged_as_spam.length
if self.spam_count >= 3
# anyone with a high spam count is a spammer
self.spammer = true
elsif self.spammer != false
# spammer === false represents known non-spammers. We want to make
# sure to maintain that info and set the rest to nil (=unknown)
self.spammer = nil
end
self.save
end

def content_flagged_as_spam
# The FlagsController is apparently the place to check for what
# models use the acts_as_flaggable module
Rakismet.spammable_models.map{ |klass|
# classes have different ways of getting to user, so just do
# a join and enforce the user_id with a where clause
klass.joins(:user).where(users: { id: self.id }).
joins(:flags).where({ flags: { flag: Flag::SPAM, resolved: false } })
}.compact.flatten.uniq
end

end
8 changes: 5 additions & 3 deletions app/views/flags/_flag_list.html.erb
Expand Up @@ -12,6 +12,8 @@
<% end %>
</ul>
<% end %>
<p class="ui">
<%= link_to t(:add_another_flag), send("new_#{@object.class.name.underscore}_flag_path", @object), :id => "flag_this", :class => "readmore" %>
</p>
<%- if @object %>
<p class="ui">
<%= link_to t(:add_another_flag), send("new_#{@object.class.name.underscore}_flag_path", @object), :id => "flag_this", :class => "readmore" %>
</p>
<%- end %>
12 changes: 12 additions & 0 deletions app/views/flags/global_index.html.haml
@@ -0,0 +1,12 @@
- content_for(:title) do
- @title = t(:flags)

#pageheader.column.span-24
%h2= @title

.description
%p= t(:flags_explanation, site_name: CONFIG.site_name)

#ul
= render :partial => "flag_list", :locals => { :flags => @flags }
= will_paginate @flags
3 changes: 2 additions & 1 deletion app/views/flags/index.html.erb
Expand Up @@ -17,7 +17,8 @@

<div class="description">
<p>
<%=t :flags_explanation, :thing => @model.name.downcase, :site_name => SITE_NAME %>
<%=t :flags_explanation, :site_name => SITE_NAME %>
<%=t :flagged_thing_explanation, :thing => @model.name.downcase %>
</p>
</div>

Expand Down
1 change: 1 addition & 0 deletions app/views/guides/show.html.haml
Expand Up @@ -327,6 +327,7 @@
%p
=t :photo_credits
= references(:linked => true)
= render :partial => "shared/flagging", :locals => { :instance => @guide }

#pdf_dialog.modal.fade.hide
.modal-header
Expand Down
1 change: 1 addition & 0 deletions app/views/lists/show.html.erb
Expand Up @@ -173,6 +173,7 @@
t(:please_hold_on_while_the_file_is_generated),
:rel => "nofollow" %>
</div>
<%= render :partial => "shared/flagging", :locals => { :instance => @list } %>
</div>

<div class="last column span-7">
Expand Down
1 change: 1 addition & 0 deletions app/views/projects/show.html.erb
Expand Up @@ -426,6 +426,7 @@
observations_by_login_path(@project.user.login), :alt => @project.user.login %>
<%= link_to @project.user.login, @project.user %>
<span class="description"><%=t :project_created_on, :date => l(@project.created_at.to_date, :format => :long) %></span>
<%= render :partial => "shared/flagging", :locals => { :instance => @project } %>
<%= separator %>
<center class="small meta">
Expand Down
14 changes: 14 additions & 0 deletions app/views/shared/_flagging.html.haml
@@ -0,0 +1,14 @@
- instance ||= nil
- if instance
- link_path = new_flag_path("#{instance.class.name.downcase}_id" => instance.id)
- if instance.flagged?
%p.description.ui.spam
= link_to t(:view_flags), link_path
\|
%a#flag_this.flag_link{ href: link_path, rel: "nofollow" }
= t(:add_another_flag)
- else
%p.description.ui.spam
= t(:is_this_offensive_html)
%a#flag_this.flag_link{ href: link_path, rel: "nofollow" }
= t(:add_a_flag)
1 change: 1 addition & 0 deletions app/views/trips/show.html.haml
Expand Up @@ -130,3 +130,4 @@
= render :partial => 'comments/comments', :locals => { :parent => @trip, :header_tag => :h2 }
- else
= render :partial => 'comments/comments', :locals => { :parent => @trip, :header_tag => :h2, :hide_form => true }
= render :partial => "shared/flagging", :locals => { :instance => @post }
17 changes: 17 additions & 0 deletions config/initializers/acts_as_flaggable.rb
@@ -0,0 +1,17 @@
module Gonzo
module Acts
module Flaggable
module InstanceMethods

def flagged?
self.flags.where(resolved: false).any?
end

def to_plain_s
s = "#{self.class} #{id} #{self.try_methods(:name, :title)}"
end

end
end
end
end
6 changes: 5 additions & 1 deletion config/locales/en.yml
Expand Up @@ -112,6 +112,7 @@ en:
add_a_batch_of_taxa: Add a Batch of Taxa
add_a_comment: Add a comment
add_a_field: Add a field
add_a_flag: Add a flag
add_a_link_raquo: "Add a link &raquo;"
add_a_name: Add a name
add_a_new_change_group: Add a new change group
Expand Down Expand Up @@ -1031,8 +1032,10 @@ en:
flag_this_photo: Flag this photo
flagged: flagged
flagged_for_curation: Flagged for curation.
flagged_thing_explanation: "Users have flagged this %{thing} because"
flagger: Flagger
flags_explanation: "%{site_name} users flag things when they find something that's not right. Here are some issues that haven't been resolved by the curators yet. Users have flagged this %{thing} because"
flags: Flags
flags_explanation: "%{site_name} users flag things when they find something that's not right. Here are some issues that haven't been resolved by the curators yet."
flags_for: Flags for
flickr_account_information: Your Flickr Account Information
flickr_description: Flickr is an online photo hosting service, maybe you already know about it!
Expand Down Expand Up @@ -1258,6 +1261,7 @@ en:
is_not_following_anyone: is not following anyone.
is_the_organism_wild: "Is the organism wild/naturalized?"
is_this_observation_offensive_html: 'Is this observation <a href="/help#inappropriate">inappropriate</a>, spam, or offensive?'
is_this_offensive_html: 'Is this <a href="/help#inappropriate">inappropriate</a>, spam, or offensive?'
is_this_place_a_duplicate: Is this place a duplicate?
it: It
it_has: It has
Expand Down
11 changes: 9 additions & 2 deletions config/routes.rb
Expand Up @@ -41,6 +41,7 @@
put "add_tags_for_rank/:rank" => "guides#add_tags_for_rank"
put :remove_all_tags
end
resources :flags
end
match '/guides/:id.:layout.pdf' => 'guides#show', :via => :get, :as => "guide_pdf", :constraints => {:format => :pdf}, :defaults => {:format => :pdf}
match 'guides/user/:login' => 'guides#user', :as => :guides_by_login, :constraints => { :login => simplified_login_regex }
Expand Down Expand Up @@ -237,6 +238,7 @@
get :preview_matching, :as => :preview_matching_for
get :invite, :as => :invite_to
end
resources :flags
resources :assessments, :only => [:new, :create, :show, :index, :edit, :update]
end

Expand All @@ -249,6 +251,7 @@
match 'people/:login/followers' => 'users#relationships', :as => :followers_by_login, :constraints => { :login => simplified_login_regex }, :followers => 'followers'
match 'people/:login/following' => 'users#relationships', :as => :following_by_login, :constraints => { :login => simplified_login_regex }, :following => 'following'
resources :lists, :constraints => { :id => id_param_pattern } do
resources :flags
get 'batch_edit'
end
match 'lists/:id/taxa' => 'lists#taxa', :as => :list_taxa, :via => :get
Expand Down Expand Up @@ -335,11 +338,15 @@
match 'journal/:login/archives/' => 'posts#archives', :as => :journal_archives, :constraints => { :login => simplified_login_regex }
match 'journal/:login/archives/:year/:month' => 'posts#archives', :as => :journal_archives_by_month, :constraints => { :month => /\d{1,2}/, :year => /\d{1,4}/, :login => simplified_login_regex }
match 'journal/:login/:id/edit' => 'posts#edit', :as => :edit_journal_post
resources :posts, :except => [:index], :constraints => { :id => id_param_pattern }
resources :posts, :except => [:index], :constraints => { :id => id_param_pattern } do
resources :flags
end
resources :posts,
:as => 'journal_posts',
:path => "/journal/:login",
:constraints => { :login => simplified_login_regex }
:constraints => { :login => simplified_login_regex } do
resources :flags
end
resources :trips, :constraints => { :id => id_param_pattern } do
member do
post :add_taxa_from_observations
Expand Down
3 changes: 3 additions & 0 deletions lib/acts_as_spammable.rb
@@ -1 +1,4 @@
module ActsAsSpammable
end

Dir["#{File.dirname(__FILE__)}/acts_as_spammable/**/*.rb"].each { |f| load(f) }
9 changes: 5 additions & 4 deletions lib/acts_as_spammable/action_controller/base.rb
Expand Up @@ -3,23 +3,24 @@ class Base
class << self

def blocks_spam(options={})
before_filter :block_spammers, :only => options[:only],
:except => options[:except]
before_filter :block_spammers, only: options[:only],
except: options[:except]

# if `obj` is spam, a spammer, or something created by a spammer
# then render the corresponding 4xx page.
# return the value of obj.spam_or_owned_by_spammer?
define_method(:block_if_spam) do |obj|
return unless obj
user_to_check = obj.is_a?(User) ? obj : obj.user
if obj.owned_by_spammer?
if (obj.is_a?(User) && current_user == obj) || current_user == obj.user || (current_user && current_user.is_curator?)
if current_user == user_to_check || (current_user && current_user.is_curator?)
set_spam_flash_error
return false
end
# all spammers are suspended, so show the suspended message page
render(template: "users/_suspended", status: 403, layout: "application")
elsif obj.known_spam?
if current_user == obj.user|| (current_user && current_user.is_curator?)
if current_user == user_to_check|| (current_user && current_user.is_curator?)
set_spam_flash_error
return false
end
Expand Down

0 comments on commit 0253335

Please sign in to comment.