diff --git a/app/controllers/admin_controller.rb b/app/controllers/admin_controller.rb index 701a6b2f84..f1c2a2a75f 100644 --- a/app/controllers/admin_controller.rb +++ b/app/controllers/admin_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class AdminController < ApplicationController before_action :require_user, only: %i(spam spam_revisions mark_comment_spam publish_comment spam_comments) @@ -21,7 +23,7 @@ def promote_admin def promote_moderator @user = User.find params[:id] unless @user.nil? - if logged_in_as(['admin', 'moderator']) + if logged_in_as(%w(admin moderator)) @user.role = 'moderator' @user.save flash[:notice] = "User '" + @user.username + "' is now a moderator." @@ -35,7 +37,7 @@ def promote_moderator def demote_basic @user = User.find params[:id] unless @user.nil? - if logged_in_as(['admin', 'moderator']) + if logged_in_as(%w(admin moderator)) @user.role = 'basic' @user.save flash[:notice] = "User '" + @user.username + "' is no longer a moderator." @@ -56,17 +58,17 @@ def reset_user_password PasswordResetMailer.reset_notify(user, key).deliver_later unless user.nil? # respond the same to both successes and failures; security end flash[:notice] = "#{user.name} should receive an email with instructions on how to reset their password. If they do not, please double check that they are using the email they registered with." - redirect_to URI.parse("/profile/" + user.name).path + redirect_to URI.parse('/profile/' + user.name).path end end def useremail - if logged_in_as(['admin', 'moderator']) + if logged_in_as(%w(admin moderator)) if params[:address] # address was submitted. find the username(s) and return. @address = params[:address] @users = User.where(email: params[:address]) - .where(status: [1, 4]) + .where(status: [1, 4]) end else # unauthorized. instead of return ugly 403, just send somewhere else @@ -75,7 +77,7 @@ def useremail end def spam - if logged_in_as(['admin', 'moderator']) + if logged_in_as(%w(admin moderator)) @nodes = Node.paginate(page: params[:page]) .order('nid DESC') @nodes = if params[:type] == 'wiki' @@ -90,7 +92,7 @@ def spam end def spam_revisions - if logged_in_as(['admin', 'moderator']) + if logged_in_as(%w(admin moderator)) @revisions = Revision.paginate(page: params[:page]) .order('timestamp DESC') .where(status: 0) @@ -104,8 +106,8 @@ def spam_revisions def spam_comments if current_user &. can_moderate? @comments = Comment.paginate(page: params[:page]) - .order('timestamp DESC') - .where(status: 0) + .order('timestamp DESC') + .where(status: 0) render template: 'admin/spam' else flash[:error] = 'Only moderators can moderate comments.' @@ -115,7 +117,7 @@ def spam_comments def mark_spam @node = Node.find params[:id] - if logged_in_as(['admin', 'moderator']) + if logged_in_as(%w(admin moderator)) if @node.status == 1 || @node.status == 4 @node.spam @node.author.ban @@ -138,7 +140,7 @@ def mark_spam def mark_comment_spam @comment = Comment.find params[:id] - if logged_in_as(['admin', 'moderator']) + if logged_in_as(%w(admin moderator)) if @comment.status == 1 || @comment.status == 4 @comment.spam user = @comment.author @@ -146,7 +148,7 @@ def mark_comment_spam AdminMailer.notify_moderators_of_comment_spam(@comment, current_user).deliver_later flash[:notice] = "Comment has been marked as spam and comment author has been banned. You can undo this on the spam moderation page." else - flash[:notice] = "Comment already marked as spam." + flash[:notice] = 'Comment already marked as spam.' end else flash[:error] = 'Only moderators can moderate comments.' @@ -155,7 +157,7 @@ def mark_comment_spam end def publish_comment - if logged_in_as(['admin', 'moderator']) + if logged_in_as(%w(admin moderator)) @comment = Comment.find params[:id] if @comment.status == 1 flash[:notice] = 'Comment already published.' @@ -181,7 +183,7 @@ def publish_comment end def publish - if logged_in_as(['admin', 'moderator']) + if logged_in_as(%w(admin moderator)) @node = Node.find params[:id] if @node.status == 1 flash[:notice] = 'Item already published.' @@ -223,7 +225,7 @@ def mark_spam_revision return end - if logged_in_as(['admin', 'moderator']) + if logged_in_as(%w(admin moderator)) if @revision.status == 1 @revision.spam @revision.author.ban @@ -244,7 +246,7 @@ def mark_spam_revision end def publish_revision - if logged_in_as(['admin', 'moderator']) + if logged_in_as(%w(admin moderator)) @revision = Revision.find params[:vid] @revision.publish @revision.author.unban @@ -262,7 +264,7 @@ def publish_revision def moderate user = User.find params[:id] - if logged_in_as(['admin', 'moderator']) + if logged_in_as(%w(admin moderator)) user.moderate flash[:notice] = 'The user has been moderated.' else @@ -273,7 +275,7 @@ def moderate def unmoderate user = User.find params[:id] - if logged_in_as(['admin', 'moderator']) + if logged_in_as(%w(admin moderator)) user.unmoderate flash[:notice] = 'The user has been unmoderated.' else @@ -284,7 +286,7 @@ def unmoderate def ban user = User.find params[:id] - if logged_in_as(['admin', 'moderator']) + if logged_in_as(%w(admin moderator)) user.ban flash[:notice] = 'The user has been banned.' else @@ -295,7 +297,7 @@ def ban def unban user = User.find params[:id] - if logged_in_as(['admin', 'moderator']) + if logged_in_as(%w(admin moderator)) user.unban flash[:notice] = 'The user has been unbanned.' else @@ -305,7 +307,7 @@ def unban end def users - if logged_in_as(['admin', 'moderator']) + if logged_in_as(%w(admin moderator)) @users = User.order('uid DESC').limit(200) else flash[:error] = 'Only moderators can moderate other users.' @@ -314,7 +316,7 @@ def users end def batch - if logged_in_as(['admin', 'moderator']) + if logged_in_as(%w(admin moderator)) nodes = 0 users = [] params[:ids].split(',').uniq.each do |nid| @@ -352,7 +354,7 @@ def migrate end def queue - if logged_in_as(['admin', 'moderator']) + if logged_in_as(%w(admin moderator)) @notes = Node.where(status: 4) .paginate(page: params[:page]) flash[:warning] = "These are notes requiring moderation. Community moderators may approve or reject them." @@ -376,10 +378,10 @@ def smtp_test s.print "RCPT TO: \n" end if line.include? '250 Accepted' - render plain: "Email gateway OK" + render plain: 'Email gateway OK' s.close_write elsif line.include? '550' - render plain: "Email gateway NOT OK" + render plain: 'Email gateway NOT OK' render status: 500 s.close_write end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 700600aabf..0a15359068 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + include ActionView::Helpers::DateHelper # required for time_ago_in_words() class ApplicationController < ActionController::Base protect_from_forgery @@ -35,7 +37,7 @@ def set_sidebar(type = :generic, data = :all, args = {}) hidden_nids = Node.where(type: :note, status: 1).select { |n| n.has_a_tag('hidden:response') }.collect(&:nid) @notes = if params[:controller] == 'questions' Node.questions - .joins(:revision) + .joins(:revision) else Node.research_notes.joins(:revision).order('node.nid DESC').paginate(page: params[:page]) end @@ -43,7 +45,7 @@ def set_sidebar(type = :generic, data = :all, args = {}) @notes = @notes.where('node.nid != (?)', @node.nid) if @node @notes = @notes.where('node_revisions.status = 1 AND node.nid NOT IN (?)', hidden_nids) unless hidden_nids.empty? - @notes = if logged_in_as(['admin', 'moderator']) + @notes = if logged_in_as(%w(admin moderator)) @notes.where('(node.status = 1 OR node.status = 4)') elsif current_user @notes.where('(node.status = 1 OR (node.status = 4 AND node.uid = ?))', current_user.uid) @@ -52,11 +54,11 @@ def set_sidebar(type = :generic, data = :all, args = {}) end @wikis = Node.order('changed DESC') - .joins(:revision) - .where('node_revisions.status = 1 AND node.status = 1 AND type = "page"') - .limit(10) - .group('node_revisions.nid') - .order('node_revisions.timestamp DESC') + .joins(:revision) + .where('node_revisions.status = 1 AND node.status = 1 AND type = "page"') + .limit(10) + .group('node_revisions.nid') + .order('node_revisions.timestamp DESC') end end @@ -94,9 +96,9 @@ def current_user end - cookies.signed["user_token"] = nil + cookies.signed['user_token'] = nil if @current_user - cookies.signed["user_token"] = @current_user.persistence_token + cookies.signed['user_token'] = @current_user.persistence_token end @current_user end @@ -108,7 +110,7 @@ def require_user redirect_to login_url false end - return current_user + current_user end def require_no_user @@ -142,10 +144,10 @@ def redirect_to_node_path?(node) end def alert_and_redirect_moderated - if @node.author.status == User::Status::BANNED && !(logged_in_as(['admin', 'moderator'])) + if @node.author.status == User::Status::BANNED && !logged_in_as(%w(admin moderator)) flash[:error] = I18n.t('application_controller.author_has_been_banned') redirect_to '/' - elsif @node.status == 4 && (logged_in_as(['admin', 'moderator'])) + elsif @node.status == 4 && logged_in_as(%w(admin moderator)) flash.now[:warning] = "First-time poster #{@node.author.name} submitted this #{time_ago_in_words(@node.created_at)} ago and it has not yet been approved by a moderator. Approve Spam" elsif @node.status == 4 && current_user&.id == @node.author.id && !flash[:first_time_post] flash.now[:warning] = "Thank you for contributing open research, and thanks for your patience while your post is approved by community moderators and we'll email you when it is published. In the meantime, if you have more to contribute, feel free to do so." @@ -153,7 +155,7 @@ def alert_and_redirect_moderated flash.now[:warning] = "This is a draft note. Once you're ready, click Publish Draft to make it public. You can share it with collaborators using this private link #{@node.draft_url(request.base_url)}" elsif @node.status == 3 && (params[:token].nil? || (params[:token].present? && @node.slug.split('token:').last != params[:token])) page_not_found - elsif @node.status != 1 && @node.status != 3 && !(logged_in_as(['admin', 'moderator'])) + elsif @node.status != 1 && @node.status != 3 && !logged_in_as(%w(admin moderator)) # if it's spam or a draft # no notification; don't let people easily fish for existing draft titles; we should try to 404 it redirect_to '/' diff --git a/app/controllers/comment_controller.rb b/app/controllers/comment_controller.rb index fd9c32d0af..85ae54ce84 100644 --- a/app/controllers/comment_controller.rb +++ b/app/controllers/comment_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class CommentController < ApplicationController include CommentHelper respond_to :html, :xml, :json @@ -5,9 +7,9 @@ class CommentController < ApplicationController def index comments = Comment.joins(:node, :user) - .order('timestamp DESC') - .where('node.status = ?', 1) - .paginate(page: params[:page], per_page: 30) + .order('timestamp DESC') + .where('node.status = ?', 1) + .paginate(page: params[:page], per_page: 30) @normal_comments = comments.where('comments.status = 1') if logged_in_as(%w(admin moderator)) @@ -44,7 +46,7 @@ def create "#{tagname}" end tagnames = tagnames.join(', ') - tagnames = " Click to subscribe to updates on these tags or topics: " + tagnames unless tagnames.empty? + tagnames = ' Click to subscribe to updates on these tags or topics: ' + tagnames unless tagnames.empty? flash[:notice] = "Comment posted.#{tagnames}" redirect_to @node.path + '#last' # to last comment end @@ -60,7 +62,7 @@ def create_by_token @node = Node.find params[:id] @user = User.find_by(username: params[:username]) @body = params[:body] - @token = request.headers["HTTP_TOKEN"] + @token = request.headers['HTTP_TOKEN'] if @user && @user.token == @token begin @@ -139,9 +141,9 @@ def delete end def like_comment - @comment_id = params["comment_id"].to_i - @user_id = params["user_id"].to_i - @emoji_type = params["emoji_type"] + @comment_id = params['comment_id'].to_i + @user_id = params['user_id'].to_i + @emoji_type = params['emoji_type'] comment = Comment.where(cid: @comment_id).first like = comment.likes.where(user_id: @user_id, emoji_type: @emoji_type) @is_liked = like.count.positive? diff --git a/app/controllers/csvfiles_controller.rb b/app/controllers/csvfiles_controller.rb index d9631ebc22..64ec8fe102 100644 --- a/app/controllers/csvfiles_controller.rb +++ b/app/controllers/csvfiles_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class CsvfilesController < ApplicationController before_action :require_user, only: %i(delete user_files) @@ -7,7 +9,7 @@ def setter filetitle: params[:filetitle], filedescription: params[:filedescription], filepath: params[:object], - filename: "file" + Time.now.to_i.to_s, + filename: 'file' + Time.now.to_i.to_s, filestring: params[:filestring] ) render json: @csvfile if @csvfile.save @@ -28,7 +30,7 @@ def add_graphobject filetitle: params[:filetitle], filedescription: params[:filedescription], filepath: params[:object], - filename: "file" + Time.now.to_i.to_s, + filename: 'file' + Time.now.to_i.to_s, filestring: params[:filestring], graphobject: params[:graphobject] ) @@ -38,11 +40,12 @@ def add_graphobject def delete return unless params[:id] && params[:uid].to_i == current_user.uid + file = Csvfile.where(id: params[:id].to_i) if file.destroy(params[:id].to_i) - flash[:notice] = "Deleted the file" + flash[:notice] = 'Deleted the file' else - flash[:error] = "Could not delete the file" + flash[:error] = 'Could not delete the file' end redirect_to "simple-data-grapher/data/#{params[:uid]}" end diff --git a/app/controllers/editor_controller.rb b/app/controllers/editor_controller.rb index 33bfcf468c..c9b10fbd9f 100644 --- a/app/controllers/editor_controller.rb +++ b/app/controllers/editor_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class EditorController < ApplicationController before_action :require_user, only: %i(post rich legacy editor) @@ -5,7 +7,7 @@ class EditorController < ApplicationController def legacy # /post/?i=http://myurl.com/image.jpg flash.now[:notice] = "This is the legacy editor. For the new rich editor, click here." - flash.now[:warning] = "Deprecation notice: Legacy editor will be discontinued soon, please use rich/markdown editor." + flash.now[:warning] = 'Deprecation notice: Legacy editor will be discontinued soon, please use rich/markdown editor.' image if params[:i] template if params[:n] && !params[:body] # use another node body as a template if params[:tags]&.include?('question:') diff --git a/app/controllers/features_controller.rb b/app/controllers/features_controller.rb index fe8ec5bf72..e169e4d248 100644 --- a/app/controllers/features_controller.rb +++ b/app/controllers/features_controller.rb @@ -1,10 +1,12 @@ +# frozen_string_literal: true + class FeaturesController < ApplicationController before_action :require_user, except: [:embed] def index - @title = "Features" + @title = 'Features' @features = Node.where(type: 'feature') - .paginate(page: params[:page]) + .paginate(page: params[:page]) end def embed @@ -33,17 +35,17 @@ def create flash[:warning] = 'Only admins may edit features.' redirect_to '/features?_=' + Time.now.to_i.to_s else - @node = Node.new(uid: current_user.id, + @node = Node.new(uid: current_user.id, title: params[:title], - type: 'feature') + type: 'feature') if @node.valid? saved = true @revision = false ActiveRecord::Base.transaction do @node.save! - @revision = @node.new_revision(uid: current_user.id, + @revision = @node.new_revision(uid: current_user.id, title: params[:title], - body: params[:body]) + body: params[:body]) if @revision.valid? @revision.save! @node.vid = @revision.vid diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 7aefcbb6dd..a91ba33708 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class HomeController < ApplicationController before_action :require_user, only: %i(subscriptions nearby) @@ -22,21 +24,21 @@ def front # used in front and home methods only def blog @notes = Node.where(status: 1, type: 'note') - .includes(:revision, :tag) - .references(:term_data, :node_revisions) - .where('term_data.name = ?', 'blog') - .order('created DESC') - .paginate(page: params[:page], per_page: 8) + .includes(:revision, :tag) + .references(:term_data, :node_revisions) + .where('term_data.name = ?', 'blog') + .order('created DESC') + .paginate(page: params[:page], per_page: 8) end def dashboard if current_user @note_count = Node.select(%i(created type status)) - .where(type: 'note', status: 1, created: Time.now.to_i - 1.weeks.to_i..Time.now.to_i) - .count(:all) + .where(type: 'note', status: 1, created: Time.now.to_i - 1.weeks.to_i..Time.now.to_i) + .count(:all) @wiki_count = Revision.select(:timestamp) - .where(timestamp: Time.now.to_i - 1.weeks.to_i..Time.now.to_i) - .count + .where(timestamp: Time.now.to_i - 1.weeks.to_i..Time.now.to_i) + .count @user_note_count = Node.where(type: 'note', status: 1, uid: current_user.uid).count @activity, @blog, @notes, @wikis, @revisions, @comments, @answer_comments = activity render template: 'dashboard/dashboard' @@ -50,11 +52,11 @@ def research redirect_to '/dashboard' else @note_count = Node.select(%i(created type status)) - .where(type: 'note', status: 1, created: Time.now.to_i - 1.weeks.to_i..Time.now.to_i) - .count(:all) + .where(type: 'note', status: 1, created: Time.now.to_i - 1.weeks.to_i..Time.now.to_i) + .count(:all) @wiki_count = Revision.select(:timestamp) - .where(timestamp: Time.now.to_i - 1.weeks.to_i..Time.now.to_i) - .count + .where(timestamp: Time.now.to_i - 1.weeks.to_i..Time.now.to_i) + .count @activity, @blog, @notes, @wikis, @revisions, @comments, @answer_comments = activity render template: 'dashboard/dashboard' @title = I18n.t('home_controller.community_research') @@ -67,34 +69,34 @@ def activity blog = Tag.find_nodes_by_type('blog', 'note', 1).first # remove "classroom" postings; also switch to an EXCEPT operator in sql, see https://github.com/publiclab/plots2/issues/375 hidden_nids = Node.joins(:node_tag) - .joins('LEFT OUTER JOIN term_data ON term_data.tid = community_tags.tid') - .select('node.*, term_data.*, community_tags.*') - .where(type: 'note', status: 1) - .where('term_data.name = (?)', 'hidden:response') - .collect(&:nid) + .joins('LEFT OUTER JOIN term_data ON term_data.tid = community_tags.tid') + .select('node.*, term_data.*, community_tags.*') + .where(type: 'note', status: 1) + .where('term_data.name = (?)', 'hidden:response') + .collect(&:nid) notes = Node.where(type: 'note') - .where('node.nid NOT IN (?)', hidden_nids + [0]) # in case hidden_nids is empty - .order('nid DESC') - .page(params[:page]) + .where('node.nid NOT IN (?)', hidden_nids + [0]) # in case hidden_nids is empty + .order('nid DESC') + .page(params[:page]) notes = notes.where('nid != (?)', blog.nid) if blog comments = Comment.joins(:node, :user) - .order('timestamp DESC') - .where('timestamp - node.created > ?', 86_400) # don't report edits within 1 day of page creation - .where('node.status = ?', 1) - .page(params[:page]) - .group(['title', 'comments.cid']) # ONLY_FULL_GROUP_BY, issue #3120 + .order('timestamp DESC') + .where('timestamp - node.created > ?', 86_400) # don't report edits within 1 day of page creation + .where('node.status = ?', 1) + .page(params[:page]) + .group(['title', 'comments.cid']) # ONLY_FULL_GROUP_BY, issue #3120 - if logged_in_as(['admin', 'moderator']) + if logged_in_as(%w(admin moderator)) notes = notes.where('(node.status = 1 OR node.status = 4 OR node.status = 3)') comments = comments.where('comments.status = 1 OR comments.status = 4') elsif current_user coauthor_nids = Node.joins(:node_tag) - .joins('LEFT OUTER JOIN term_data ON term_data.tid = community_tags.tid') - .select('node.*, term_data.*, community_tags.*') - .where(type: 'note', status: 3) - .where('term_data.name = (?)', "with:#{current_user.username}") - .collect(&:nid) + .joins('LEFT OUTER JOIN term_data ON term_data.tid = community_tags.tid') + .select('node.*, term_data.*, community_tags.*') + .where(type: 'note', status: 3) + .where('term_data.name = (?)', "with:#{current_user.username}") + .collect(&:nid) notes = notes.where('(node.nid IN (?) OR node.status = 1 OR ((node.status = 3 OR node.status = 4) AND node.uid = ?))', coauthor_nids, current_user.uid) comments = comments.where('comments.status = 1 OR (comments.status = 4 AND comments.uid = ?)', current_user.uid) else @@ -106,16 +108,16 @@ def activity # include revisions, then mix with new pages: wikis = Node.where(type: 'page', status: 1) - .order('nid DESC') - .limit(10) + .order('nid DESC') + .limit(10) revisions = Revision.joins(:node) - .order('timestamp DESC') - .where('type = (?)', 'page') - .where('node.status = 1') - .where('node_revisions.status = 1') - .where('timestamp - node.created > ?', 300) # don't report edits within 5 mins of page creation - .limit(10) - .group(['node.title', 'node.nid', 'node_revisions.vid']) # ONLY_FULL_GROUP_BY, issue #3120 + .order('timestamp DESC') + .where('type = (?)', 'page') + .where('node.status = 1') + .where('node_revisions.status = 1') + .where('timestamp - node.created > ?', 300) # don't report edits within 5 mins of page creation + .limit(10) + .group(['node.title', 'node.nid', 'node_revisions.vid']) # ONLY_FULL_GROUP_BY, issue #3120 # group by day: http://stackoverflow.com/questions/5970938/group-by-day-from-timestamp revisions = revisions.group('DATE(FROM_UNIXTIME(timestamp))') if Rails.env == 'production' revisions = revisions.to_a # ensure it can be serialized for caching @@ -126,10 +128,10 @@ def activity comments = comments.group('DATE(FROM_UNIXTIME(timestamp))') if Rails.env == 'production' comments = comments.to_a # ensure it can be serialized for caching answer_comments = Comment.joins(:answer, :user) - .order('timestamp DESC') - .where('timestamp - answers.created_at > ?', 86_400) - .limit(20) - .group(['answers.id', 'comments.cid']) # ONLY_FULL_GROUP_BY, issue #3120 + .order('timestamp DESC') + .where('timestamp - answers.created_at > ?', 86_400) + .limit(20) + .group(['answers.id', 'comments.cid']) # ONLY_FULL_GROUP_BY, issue #3120 answer_comments = answer_comments.group('DATE(FROM_UNIXTIME(timestamp))') if Rails.env == 'production' answer_comments = answer_comments.to_a # ensure it can be serialized for caching activity = (notes + wikis + comments + answer_comments).sort_by(&:created_at).reverse diff --git a/app/controllers/images_controller.rb b/app/controllers/images_controller.rb index 62810f9a3d..3abb230185 100644 --- a/app/controllers/images_controller.rb +++ b/app/controllers/images_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'open-uri' class ImagesController < ApplicationController @@ -7,11 +9,11 @@ class ImagesController < ApplicationController def shortlink params[:size] = params[:size] || params[:s] params[:size] = params[:size] || :large - params[:size] = :thumb if (params[:size].to_s == "t") - params[:size] = :thumb if (params[:size].to_s == "thumbnail") - params[:size] = :medium if (params[:size].to_s == "m") - params[:size] = :large if (params[:size].to_s == "l") - params[:size] = :original if (params[:size].to_s == "o") + params[:size] = :thumb if params[:size].to_s == 't' + params[:size] = :thumb if params[:size].to_s == 'thumbnail' + params[:size] = :medium if params[:size].to_s == 'm' + params[:size] = :large if params[:size].to_s == 'l' + params[:size] = :original if params[:size].to_s == 'o' image = Image.find(params[:id]) redirect_to URI.parse(image.path(params[:size])).path end @@ -43,7 +45,7 @@ def create href: @image.shortlink, # Woofmark/PublicLab.Editor title: @image.photo_file_name, results: [{ # Woofmark/PublicLab.Editor - href: @image.shortlink + "." + @image.filetype, + href: @image.shortlink + '.' + @image.filetype, title: @image.photo_file_name }] } diff --git a/app/controllers/legacy_controller.rb b/app/controllers/legacy_controller.rb index 646f4ad333..feec595e04 100644 --- a/app/controllers/legacy_controller.rb +++ b/app/controllers/legacy_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class LegacyController < ApplicationController def notes if params[:id] diff --git a/app/controllers/like_controller.rb b/app/controllers/like_controller.rb index bf29b31ce0..3cc5fa9bb8 100644 --- a/app/controllers/like_controller.rb +++ b/app/controllers/like_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class LikeController < ApplicationController respond_to :html, :xml, :json before_action :require_user, only: %i(create delete) diff --git a/app/controllers/map_controller.rb b/app/controllers/map_controller.rb index 2d4ad29c62..bc602be01a 100644 --- a/app/controllers/map_controller.rb +++ b/app/controllers/map_controller.rb @@ -1,20 +1,22 @@ +# frozen_string_literal: true + class MapController < ApplicationController def index @title = 'Maps' @nodes = Node.paginate(page: params[:page], per_page: 32) - .order('nid DESC') - .where(type: 'map', status: 1) + .order('nid DESC') + .where(type: 'map', status: 1) @map_lat = nil @map_lon = nil - if current_user&.has_power_tag("lat") && current_user&.has_power_tag("lon") - @map_lat = current_user.get_value_of_power_tag("lat").to_f - @map_lon = current_user.get_value_of_power_tag("lon").to_f + if current_user&.has_power_tag('lat') && current_user&.has_power_tag('lon') + @map_lat = current_user.get_value_of_power_tag('lat').to_f + @map_lon = current_user.get_value_of_power_tag('lon').to_f end # I'm not sure if this is actually eager loading the tags... @maps = Node.joins(:tag) - .where('type = "map" AND status = 1 AND (term_data.name LIKE ? OR term_data.name LIKE ?)', 'lat:%', 'lon:%') - .distinct + .where('type = "map" AND status = 1 AND (term_data.name LIKE ? OR term_data.name LIKE ?)', 'lat:%', 'lon:%') + .distinct # This is supposed to eager load the url_aliases, and seems to run, but doesn't actually eager load them...? # @maps = Node.select("node.*,url_alias.dst AS dst").joins(:tag).where('type = "map" AND status = 1 AND (term_data.name LIKE ? OR term_data.name LIKE ?)', 'lat:%', 'lon:%').joins("INNER JOIN url_alias ON url_alias.src = CONCAT('node/',node.nid)") @@ -77,7 +79,7 @@ def update %i(lat lon).each do |coordinate| if coordinate_name = coordinate.to_s + ':' + @node.power_tag(coordinate.to_s) - existing_coordinate_node_tag = NodeTag.where(nid: @node.id).joins(:tag).where("name = ?", coordinate_name).first + existing_coordinate_node_tag = NodeTag.where(nid: @node.id).joins(:tag).where('name = ?', coordinate_name).first existing_coordinate_node_tag.delete end @node.add_tag(coordinate.to_s + ':' + params[coordinate], current_user) @@ -212,8 +214,8 @@ def tag @tagnames = params[:id].split(',') nids = Tag.find_nodes_by_type(params[:id], 'map', 20).collect(&:nid) @notes = Node.paginate(page: params[:page]) - .where('nid in (?)', nids) - .order('nid DESC') + .where('nid in (?)', nids) + .order('nid DESC') @title = @tagnames.join(', ') if @tagnames @unpaginated = true diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb index 6fb956b231..9fdaf4438c 100644 --- a/app/controllers/notes_controller.rb +++ b/app/controllers/notes_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class NotesController < ApplicationController respond_to :html before_action :require_user, only: %i(create edit update delete rsvp publish_draft) @@ -17,14 +19,14 @@ def places @notes = Node.joins('LEFT OUTER JOIN node_revisions ON node_revisions.nid = node.nid LEFT OUTER JOIN community_tags ON community_tags.nid = node.nid LEFT OUTER JOIN term_data ON term_data.tid = community_tags.tid') - .select('*, max(node_revisions.timestamp)') - .where(status: 1, type: %w(page place)) - .includes(:revision, :tag) - .references(:term_data) - .where('term_data.name = ?', 'chapter') - .group('node.nid') - .order(Arel.sql('max(node_revisions.timestamp) DESC, node.nid')) - .paginate(page: params[:page], per_page: 24) + .select('*, max(node_revisions.timestamp)') + .where(status: 1, type: %w(page place)) + .includes(:revision, :tag) + .references(:term_data) + .where('term_data.name = ?', 'chapter') + .group('node.nid') + .order(Arel.sql('max(node_revisions.timestamp) DESC, node.nid')) + .paginate(page: params[:page], per_page: 24) # Arel.sql is used to remove a Deprecation warning while updating to rails 5.2. @@ -54,7 +56,7 @@ def show redirect_to @node.path(:question) return end - + alert_and_redirect_moderated redirect_power_tag_redirect @@ -85,13 +87,13 @@ def create saved, @node, @revision = new_note - if params[:draft] == "true" && current_user.first_time_poster - flash[:notice] = "First-time users are not eligible to create a draft." + if params[:draft] == 'true' && current_user.first_time_poster + flash[:notice] = 'First-time users are not eligible to create a draft.' redirect_to '/' return - elsif params[:draft] == "true" + elsif params[:draft] == 'true' token = SecureRandom.urlsafe_base64(16, false) - @node.slug = @node.slug + " token:" + token + @node.slug = @node.slug + ' token:' + token @node.save! end @@ -119,7 +121,7 @@ def create thanks_for_contribution = I18n.t('notes_controller.thank_you_for_contribution').html_safe flash[:notice] = thanks_for_contribution - elsif params[:draft] != "true" + elsif params[:draft] != 'true' question_note = I18n.t('notes_controller.question_note_published').html_safe research_note = I18n.t('notes_controller.research_note_published').html_safe @@ -262,8 +264,8 @@ def author @user = User.find_by(name: params[:id]) @title = @user.name @notes = Node.paginate(page: params[:page], per_page: 24) - .order('nid DESC') - .where(type: 'note', status: 1, uid: @user.uid) + .order('nid DESC') + .where(type: 'note', status: 1, uid: @user.uid) render template: 'notes/index' end @@ -281,13 +283,13 @@ def author_topic def liked @title = I18n.t('notes_controller.highly_liked_research_notes') @wikis = Node.limit(10) - .where(type: 'page', status: 1) - .order('nid DESC') + .where(type: 'page', status: 1) + .order('nid DESC') @notes = Node.research_notes - .where(status: 1) - .limit(20) - .order('nid DESC') + .where(status: 1) + .limit(20) + .order('nid DESC') @unpaginated = true render template: 'notes/index' end @@ -295,8 +297,8 @@ def liked def recent @title = I18n.t('notes_controller.recent_research_notes') @wikis = Node.limit(10) - .where(type: 'page', status: 1) - .order('nid DESC') + .where(type: 'page', status: 1) + .order('nid DESC') @notes = Node.where(type: 'note', status: 1, created: Time.now.to_i - 1.weeks.to_i..Time.now.to_i) @unpaginated = true render template: 'notes/index' @@ -306,12 +308,12 @@ def recent def popular @title = I18n.t('notes_controller.popular_research_notes') @wikis = Node.limit(10) - .where(type: 'page', status: 1) - .order('nid DESC') + .where(type: 'page', status: 1) + .order('nid DESC') @notes = Node.research_notes - .limit(20) - .where(status: 1) - .order('views DESC') + .limit(20) + .where(status: 1) + .order('views DESC') @unpaginated = true render template: 'notes/index' end @@ -320,12 +322,12 @@ def rss limit = 20 @notes = if params[:moderators] Node.limit(limit) - .order('nid DESC') - .where('type = ? AND status = 4', 'note') + .order('nid DESC') + .where('type = ? AND status = 4', 'note') else Node.limit(limit) - .order('nid DESC') - .where('type = ? AND status = 1', 'note') + .order('nid DESC') + .where('type = ? AND status = 1', 'note') end respond_to do |format| format.rss do @@ -338,8 +340,8 @@ def rss def liked_rss @notes = Node.limit(20) - .order('created DESC') - .where('type = ? AND status = 1 AND cached_likes > 0', 'note') + .order('created DESC') + .where('type = ? AND status = 1 AND cached_likes > 0', 'note') respond_to do |format| format.rss do render layout: false, template: 'notes/rss' @@ -362,10 +364,10 @@ def update_title node = Node.find params[:id].to_i unless current_user && current_user == node.author flash.keep[:error] = I18n.t('notes_controller.author_can_edit_note') - return redirect_to URI.parse(node.path).path + "#comments" + return redirect_to URI.parse(node.path).path + '#comments' end node.update(title: params[:title]) - redirect_to URI.parse(node.path).path + "#comments" + redirect_to URI.parse(node.path).path + '#comments' end def publish_draft @@ -412,7 +414,7 @@ def new_note end def not_draft_and_user_is_first_time_poster? - params[:draft] != "true" && current_user.first_time_poster + params[:draft] != 'true' && current_user.first_time_poster end def show_banned_flash diff --git a/app/controllers/openid_controller.rb b/app/controllers/openid_controller.rb index 968aae67c0..ebddec583a 100644 --- a/app/controllers/openid_controller.rb +++ b/app/controllers/openid_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'pathname' require 'openid' @@ -88,56 +90,55 @@ def index if oidreq - oidresp = nil if oidreq.is_a?(CheckIDRequest) - identity = oidreq.identity - - if oidreq.id_select - if oidreq.immediate - oidresp = oidreq.answer(false) - elsif session[:username] - # The user hasn't logged in. - # show_decision_page(oidreq) # this doesnt make sense... it was in the example though - session[:openid_return_to] = request.env['ORIGINAL_FULLPATH'] - if provider - # provider based authentication - redirect_to '/auth/' + provider - else - # form based authentication - redirect_to '/login' - end + identity = oidreq.identity + + if oidreq.id_select + if oidreq.immediate + oidresp = oidreq.answer(false) + elsif session[:username] + # The user hasn't logged in. + # show_decision_page(oidreq) # this doesnt make sense... it was in the example though + session[:openid_return_to] = request.env['ORIGINAL_FULLPATH'] + if provider + # provider based authentication + redirect_to '/auth/' + provider else - # Else, set the identity to the one the user is using. - identity = url_for_user + # form based authentication + redirect_to '/login' end - + else + # Else, set the identity to the one the user is using. + identity = url_for_user end - if oidresp - nil - elsif is_authorized(identity, oidreq.trust_root) - oidresp = oidreq.answer(true, nil, identity) - - # add the sreg response if requested - add_sreg(oidreq, oidresp) - # ditto pape - add_pape(oidreq, oidresp) - - elsif oidreq.immediate - server_url = url_for action: 'index' - oidresp = oidreq.answer(false, server_url) + end - else - session[:last_oidreq] = oidreq - @oidreq = oidreq - redirect_to action: 'decision' - return - end + if oidresp + nil + elsif is_authorized(identity, oidreq.trust_root) + oidresp = oidreq.answer(true, nil, identity) + + # add the sreg response if requested + add_sreg(oidreq, oidresp) + # ditto pape + add_pape(oidreq, oidresp) + + elsif oidreq.immediate + server_url = url_for action: 'index' + oidresp = oidreq.answer(false, server_url) + + else + session[:last_oidreq] = oidreq + @oidreq = oidreq + redirect_to action: 'decision' + return + end else - oidresp = server.handle_request(oidreq) + oidresp = server.handle_request(oidreq) end render_response(oidresp) @@ -246,7 +247,7 @@ def decision oidresp = oidreq.answer(true, nil, identity) add_sreg(oidreq, oidresp) add_pape(oidreq, oidresp) - return render_response(oidresp) + render_response(oidresp) end protected @@ -308,7 +309,7 @@ def add_sreg(oidreq, oidresp) sreg_data = { 'nickname' => current_user.username, # session[:username], 'email' => current_user.email, - 'fullname' => "status=" + current_user.status.to_s + ":role=" + current_user.role # fullname contains both status and role + 'fullname' => 'status=' + current_user.status.to_s + ':role=' + current_user.role # fullname contains both status and role } sregresp = OpenID::SReg::Response.extract_response(sregreq, sreg_data) diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index c42cd2b2c4..9d29b6f072 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class QuestionsController < ApplicationController private @@ -6,9 +8,9 @@ def filter_questions_by_tag(questions, tagnames) tagnames = tagnames.split(',') nids = questions.collect(&:nid) questions = Node.where(status: 1, type: 'note') - .joins(:tag) - .where('node.nid IN (?)', nids) - .group('node.nid') + .joins(:tag) + .where('node.nid IN (?)', nids) + .group('node.nid') if !tagnames.empty? questions.where('term_data.name IN (?)', tagnames) else @@ -22,30 +24,29 @@ def index @title = 'Questions and Answers' set_sidebar @questions = Node.questions - .where(status: 1) - .order('node.nid DESC') - .paginate(page: params[:page], per_page: 24) + .where(status: 1) + .order('node.nid DESC') + .paginate(page: params[:page], per_page: 24) end def index_shadow @title = 'Questions and Answers' @questions = Node.questions - .where(status: 1) - .order('node.nid DESC') - .paginate(page: params[:page], per_page: 24) - + .where(status: 1) + .order('node.nid DESC') + .paginate(page: params[:page], per_page: 24) + @populartitle = 'Popular Questions' @popularquestions = Node.questions - .where(status: 1) + .where(status: 1) @popularquestions = filter_questions_by_tag(@questions, params[:tagnames]) - .order('views DESC') - .limit(20) + .order('views DESC') + .limit(20) @popularwikis = Node.limit(10) - .where(type: 'page', status: 1) - .order('nid DESC') + .where(type: 'page', status: 1) + .order('nid DESC') @unpaginated = true - end # a form for new questions, at /questions/new @@ -58,7 +59,7 @@ def new end if current_user.nil? redirect_to new_user_session_path(return_to: request.path) - flash[:notice] = "Your question is important and we want to hear from you! Please log in or sign up to post a question" + flash[:notice] = 'Your question is important and we want to hear from you! Please log in or sign up to post a question' else if params[:legacy] render 'editor/question' @@ -94,28 +95,28 @@ def show def answered @title = 'Recently Commented' @questions = Node.questions - .where(status: 1) + .where(status: 1) @questions = filter_questions_by_tag(@questions, params[:tagnames]) - .joins(:comments) - .order('comments.timestamp DESC') - .group('node.nid') - .paginate(page: params[:page], per_page: 24) + .joins(:comments) + .order('comments.timestamp DESC') + .group('node.nid') + .paginate(page: params[:page], per_page: 24) @wikis = Node.limit(10) - .where(type: 'page', status: 1) - .order('nid DESC') + .where(type: 'page', status: 1) + .order('nid DESC') render template: 'questions/index' end def unanswered @title = 'Unanswered questions' @questions = Node.questions - .where(status: 1) - .includes(:answers) - .references(:answers) - .where(answers: { id: nil }) - .order('node.nid DESC') - .group('node.nid') - .paginate(page: params[:page], per_page: 24) + .where(status: 1) + .includes(:answers) + .references(:answers) + .where(answers: { id: nil }) + .order('node.nid DESC') + .group('node.nid') + .paginate(page: params[:page], per_page: 24) render template: 'questions/index' end @@ -131,14 +132,14 @@ def shortlink def popular @title = 'Popular Questions' @questions = Node.questions - .where(status: 1) + .where(status: 1) @questions = filter_questions_by_tag(@questions, params[:tagnames]) - .order('views DESC') - .limit(20) + .order('views DESC') + .limit(20) @wikis = Node.limit(10) - .where(type: 'page', status: 1) - .order('nid DESC') + .where(type: 'page', status: 1) + .order('nid DESC') @unpaginated = true render template: 'questions/index' end @@ -147,12 +148,12 @@ def liked @title = 'Highly liked Questions' @questions = Node.questions.where(status: 1) @questions = filter_questions_by_tag(@questions, params[:tagnames]) - .order('cached_likes DESC') - .limit(20) + .order('cached_likes DESC') + .limit(20) @wikis = Node.limit(10) - .where(type: 'page', status: 1) - .order('nid DESC') + .where(type: 'page', status: 1) + .order('nid DESC') @unpaginated = true render template: 'questions/index' end diff --git a/app/controllers/relationships_controller.rb b/app/controllers/relationships_controller.rb index 37475c7eb6..20b81e2266 100644 --- a/app/controllers/relationships_controller.rb +++ b/app/controllers/relationships_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class RelationshipsController < ApplicationController before_action :require_user @@ -6,14 +8,14 @@ def create respond_to do |format| if !current_user.following?(user) current_user.follow(user) - format.html { redirect_to URI.parse(request.referer || "/").path, notice: "You have started following " + user.username } - format.js { render "create", locals: { following: true, profile_user: user } } + format.html { redirect_to URI.parse(request.referer || '/').path, notice: 'You have started following ' + user.username } + format.js { render 'create', locals: { following: true, profile_user: user } } else - format.html { - flash[:error] = "Error in following user" - redirect_to URI.parse(request.referer || "/").path - } - format.js { render "create", locals: { following: false, profile_user: user } } + format.html do + flash[:error] = 'Error in following user' + redirect_to URI.parse(request.referer || '/').path + end + format.js { render 'create', locals: { following: false, profile_user: user } } end end end @@ -24,14 +26,14 @@ def destroy respond_to do |format| if !relation.nil? current_user.unfollow(user) - format.html { redirect_to URI.parse(request.referer || "/").path, notice: "You have unfollowed " + user.username } - format.js { render "destroy", locals: { unfollowing: true, profile_user: user } } + format.html { redirect_to URI.parse(request.referer || '/').path, notice: 'You have unfollowed ' + user.username } + format.js { render 'destroy', locals: { unfollowing: true, profile_user: user } } else - format.html { - flash[:error] = "Error in unfollowing user" - redirect_to URI.parse(request.referer || "/").path - } - format.js { render "destroy", locals: { unfollowing: false, profile_user: user } } + format.html do + flash[:error] = 'Error in unfollowing user' + redirect_to URI.parse(request.referer || '/').path + end + format.js { render 'destroy', locals: { unfollowing: false, profile_user: user } } end end end diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rb index b2abc405f9..3f13f4c164 100644 --- a/app/controllers/search_controller.rb +++ b/app/controllers/search_controller.rb @@ -1,10 +1,12 @@ +# frozen_string_literal: true + class SearchController < ApplicationController before_action :set_search_criteria, except: %i(notes wikis) def new; end def google - @title = "Search" + @title = 'Search' end # a route to convert /search/_____ to /search?q=______ style search queries @@ -13,20 +15,20 @@ def google_redirect end def notes - @title = "Search notes" + @title = 'Search notes' @notes = SearchService.new.search_notes(params[:query], 15, params[:order].to_s.to_sym, params[:type].to_s.to_sym) - .paginate(page: params[:page], per_page: 24) + .paginate(page: params[:page], per_page: 24) end def wikis - @title = "Search wikis" + @title = 'Search wikis' @wikis = SearchService.new.search_wikis(params[:query], 15, params[:order].to_s.to_sym, params[:type].to_s.to_sym) - .paginate(page: params[:page], per_page: 24) + .paginate(page: params[:page], per_page: 24) end def profiles - @title = "Search profiles" - @search_criteria.sort_by = "recent" + @title = 'Search profiles' + @search_criteria.sort_by = 'recent' if params[:query] @profiles = ExecuteSearch.new.by(:profiles, @search_criteria).paginate(page: params[:page], per_page: 20) else @@ -37,23 +39,23 @@ def profiles end def questions - @title = "Search questions" + @title = 'Search questions' @questions = ExecuteSearch.new.by(:questions, @search_criteria).paginate(page: params[:page], per_page: 20) end def places - @title = "Search maps" + @title = 'Search maps' # it's called nodes because the map/_maps partials expects nodes objects @nodes = ExecuteSearch.new.by(:places, @search_criteria).paginate(page: params[:page], per_page: 20) end def tags - @title = "Search tags" + @title = 'Search tags' @tags = ExecuteSearch.new.by(:tags, @search_criteria).paginate(page: params[:page], per_page: 20) end def all_content - @title = "Search all content" + @title = 'Search all content' @nodes = ExecuteSearch.new.by(:all, @search_criteria) @wikis = wikis @notes = notes diff --git a/app/controllers/settings_controller.rb b/app/controllers/settings_controller.rb index d6a80a0603..b02e563a23 100644 --- a/app/controllers/settings_controller.rb +++ b/app/controllers/settings_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class SettingsController < ApplicationController # Check the locale was passed and if it is a valid one, set the locale in cookies def change_locale diff --git a/app/controllers/stats_controller.rb b/app/controllers/stats_controller.rb index 7f5c48a6a6..1bc71b5eb4 100644 --- a/app/controllers/stats_controller.rb +++ b/app/controllers/stats_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class StatsController < ApplicationController def subscriptions @tags = {} @@ -9,7 +11,7 @@ def subscriptions end def range - flash.now[:notice] = "Data is cached and recalculated daily" + flash.now[:notice] = 'Data is cached and recalculated daily' if params[:options].present? params[:start] = Time.now - to_keyword(params[:options]) params[:end] = Time.now @@ -18,20 +20,20 @@ def range @end = fin Rails.cache.fetch("range-#{@start.to_i}-#{@end.to_i}", expires_in: 1.day) do @notes = Node.published.select(%i(created type)) - .where(type: 'note', created: @start.to_i..@end.to_i) - .size + .where(type: 'note', created: @start.to_i..@end.to_i) + .size @wikis = Revision.published.select(:timestamp) - .where(timestamp: @start.to_i..@end.to_i) - .size - @notes # because notes each have one revision + .where(timestamp: @start.to_i..@end.to_i) + .size - @notes # because notes each have one revision @people = User.where(created_at: @start..@end).where(status: 1) - .size + .size @answers = Answer.where(created_at: @start..@end) - .size + .size @comments = Comment.select(:status, :timestamp) - .where(status: 1, timestamp: @start.to_i..@end.to_i) - .size + .where(status: 1, timestamp: @start.to_i..@end.to_i) + .size @questions = Node.published.questions.where(created: @start.to_i..@end.to_i) - .size + .size @contributors = User.contributor_count_for(@start, @end) @popular_tags = Tag.nodes_frequency(@start, @end) end @@ -40,11 +42,11 @@ def range def index range if @start > @end - flash.now[:warning] = "Start date must come before end date" + flash.now[:warning] = 'Start date must come before end date' end @title = 'Stats' - flash.now[:notice] = "Data is cached and recalculated daily" + flash.now[:notice] = 'Data is cached and recalculated daily' Rails.cache.fetch("stats-index-#{@start.to_i}-#{@end.to_i}", expires_in: 1.day) do @weekly_notes = Node.past_week.select(:type).where(type: 'note').size @weekly_wikis = Revision.past_week.size @@ -76,7 +78,7 @@ def index @all_notes = nids.uniq.length @all_contributors = users.uniq.length end - Rails.cache.fetch("total-contributors-all-time", expires_in: 1.weeks) do + Rails.cache.fetch('total-contributors-all-time', expires_in: 1.weeks) do @all_time_contributors = User.count_all_time_contributor end end @@ -91,8 +93,8 @@ def wikis def users data = User.where(created_at: start..fin) - .where(status: 1) - .select(:username, :role, :bio, :photo_file_name, :id, :created_at) + .where(status: 1) + .select(:username, :role, :bio, :photo_file_name, :id, :created_at) format(data, 'users') end @@ -123,8 +125,8 @@ def node_tags def export_as_json(type) data = Node.published - .where(type: type, created: start.to_i..fin.to_i) - .all + .where(type: type, created: start.to_i..fin.to_i) + .all format(data, type) end diff --git a/app/controllers/subscription_controller.rb b/app/controllers/subscription_controller.rb index d7b1ce5f92..c20f3c63e8 100644 --- a/app/controllers/subscription_controller.rb +++ b/app/controllers/subscription_controller.rb @@ -1,10 +1,12 @@ +# frozen_string_literal: true + class SubscriptionController < ApplicationController respond_to :html, :xml, :json before_action :require_user, only: %i(create delete index digest) def index - @title = "Subscriptions" - render template: "home/subscriptions" + @title = 'Subscriptions' + render template: 'home/subscriptions' end # return a count of subscriptions for a given tag @@ -15,7 +17,7 @@ def tag_count # for the current user, return whether is presently liked or not def followed # may be trouble: there can be multiple tags with the same name, no? We can eliminate that possibility in a migration if so. - result = TagSelection.find_by_user_id_and_tid(current_user.uid, params[:id]) if params[:type] == "tag" + result = TagSelection.find_by_user_id_and_tid(current_user.uid, params[:id]) if params[:type] == 'tag' result = if result.nil? false else @@ -26,7 +28,7 @@ def followed # for the current user, register as liking the given tag def add - if current_user && params[:type] == "tag" + if current_user && params[:type] == 'tag' tag = Tag.find_by(name: params[:name]) @@ -34,7 +36,7 @@ def add tag = Tag.new( vid: 3, name: params[:name], - description: "", + description: '', weight: 0 ) @@ -51,11 +53,11 @@ def add format.html do flash[:error] = "You are already subscribed to '#{params[:name]}'" - redirect_to "/subscriptions" + "?_=" + Time.now.to_i.to_s + redirect_to '/subscriptions' + '?_=' + Time.now.to_i.to_s end format.json do - message = "You already follow this user!" + message = 'You already follow this user!' render json: { status: :precondition_failed, error: message } end @@ -68,26 +70,26 @@ def add # status = "200" else flash[:notice] = "You are now following '#{params[:name]}'." - redirect_to "/subscriptions" + "?_=" + Time.now.to_i.to_s + redirect_to '/subscriptions' + '?_=' + Time.now.to_i.to_s end end end else flash[:warning] = "You must be logged in to subscribe for email updates; please log in or create an account." - redirect_to "/tag/" + params[:name] + redirect_to '/tag/' + params[:name] end end # for the current user, remove the like from the given tag def delete # assume tag, for now - if params[:type] == "tag" + if params[:type] == 'tag' id = Tag.find_by(name: params[:name]).tid end if id.nil? flash[:error] = "You are not subscribed to '#{params[:name]}'" - redirect_to "/subscriptions" + "?_=" + Time.now.to_i.to_s + redirect_to '/subscriptions' + '?_=' + Time.now.to_i.to_s else if !set_following(false, params[:type], id) # should return false if result is that following == false respond_with do |format| @@ -96,29 +98,29 @@ def delete render json: true else flash[:notice] = "You have stopped following '#{params[:name]}'." - redirect_to "/subscriptions" + "?_=" + Time.now.to_i.to_s + redirect_to '/subscriptions' + '?_=' + Time.now.to_i.to_s end end end else - flash[:error] = "Something went wrong!" # silly - redirect_to "/subscriptions" + "?_=" + Time.now.to_i.to_s + flash[:error] = 'Something went wrong!' # silly + redirect_to '/subscriptions' + '?_=' + Time.now.to_i.to_s end end end def digest @wikis = current_user.content_followed_in_period(1.week.ago, Time.now) - .paginate(page: params[:page], per_page: 100) + .paginate(page: params[:page], per_page: 100) @paginated = true - render template: "subscriptions/digest" + render template: 'subscriptions/digest' end def multiple_add if !params[:tagnames] || params[:tagnames] == '' - flash[:notice] = "Please enter tags for subscription in the url." - redirect_to "/subscriptions" + "?_=" + Time.now.to_i.to_s + flash[:notice] = 'Please enter tags for subscription in the url.' + redirect_to '/subscriptions' + '?_=' + Time.now.to_i.to_s return end tag_list = if params[:tagnames].is_a? String @@ -127,7 +129,7 @@ def multiple_add params[:tagnames] end if current_user - if params[:type] == "tag" + if params[:type] == 'tag' tag_list.each do |t| next unless t.length.positive? @@ -136,14 +138,14 @@ def multiple_add tag = Tag.new( vid: 3, # vocabulary id name: t, - description: "", + description: '', weight: 0 ) begin tag.save! rescue ActiveRecord::RecordInvalid flash[:error] = tag.errors.full_messages - redirect_to "/subscriptions" + "?_=" + Time.now.to_i.to_s + redirect_to '/subscriptions' + '?_=' + Time.now.to_i.to_s return false end end @@ -159,14 +161,14 @@ def multiple_add render json: true else flash[:notice] = "You are now following '#{params[:tagnames]}'." - redirect_to "/subscriptions" + "?_=" + Time.now.to_i.to_s + redirect_to '/subscriptions' + '?_=' + Time.now.to_i.to_s end end end end else - flash[:warning] = "You must be logged in to subscribe for email updates!" - redirect_to "/login?return_to=" + request.fullpath + flash[:warning] = 'You must be logged in to subscribe for email updates!' + redirect_to '/login?return_to=' + request.fullpath end end @@ -203,7 +205,7 @@ def set_following(value, type, id) end subscription.following else - flash.now[:error] = "There was an error." + flash.now[:error] = 'There was an error.' false end end diff --git a/app/controllers/tag_controller.rb b/app/controllers/tag_controller.rb index a14d12d44a..2e691eb8a6 100644 --- a/app/controllers/tag_controller.rb +++ b/app/controllers/tag_controller.rb @@ -1,61 +1,63 @@ +# frozen_string_literal: true + class TagController < ApplicationController respond_to :html, :xml, :json, :ics before_action :require_user, only: %i(create delete add_parent) def index - @toggle = params[:sort] || "uses" + @toggle = params[:sort] || 'uses' @title = I18n.t('tag_controller.tags') @paginated = true - @order_type = params[:order] == "desc" ? "asc" : "desc" + @order_type = params[:order] == 'desc' ? 'asc' : 'desc' powertag_clause = params[:powertags] == 'true' ? '' : ['name NOT LIKE ?', '%:%'] if params[:search] keyword = params[:search] @tags = Tag.joins(:node_tag, :node) - .select('node.nid, node.status, term_data.*, community_tags.*') - .where('node.status = ?', 1) - .where('community_tags.date > ?', (DateTime.now - 1.month).to_i) - .where("name LIKE :keyword", keyword: "%#{keyword}%") - .where(powertag_clause) - .group(:name) - .order(order_string) - .paginate(page: params[:page], per_page: 24) - elsif @toggle == "uses" + .select('node.nid, node.status, term_data.*, community_tags.*') + .where('node.status = ?', 1) + .where('community_tags.date > ?', (DateTime.now - 1.month).to_i) + .where('name LIKE :keyword', keyword: "%#{keyword}%") + .where(powertag_clause) + .group(:name) + .order(order_string) + .paginate(page: params[:page], per_page: 24) + elsif @toggle == 'uses' @tags = Tag.joins(:node_tag, :node) - .select('node.nid, node.status, term_data.*, community_tags.*') - .where('node.status = ?', 1) - .where('community_tags.date > ?', (DateTime.now - 1.month).to_i) - .where(powertag_clause) - .group(:name) - .order(order_string) - .paginate(page: params[:page], per_page: 24) - elsif @toggle == "name" + .select('node.nid, node.status, term_data.*, community_tags.*') + .where('node.status = ?', 1) + .where('community_tags.date > ?', (DateTime.now - 1.month).to_i) + .where(powertag_clause) + .group(:name) + .order(order_string) + .paginate(page: params[:page], per_page: 24) + elsif @toggle == 'name' @tags = Tag.joins(:node_tag, :node) - .select('node.nid, node.status, term_data.*, community_tags.*') - .where('node.status = ?', 1) - .where('community_tags.date > ?', (DateTime.now - 1.month).to_i) - .where(powertag_clause) - .group(:name) - .order(order_string) - .paginate(page: params[:page], per_page: 24) - elsif @toggle == "followers" + .select('node.nid, node.status, term_data.*, community_tags.*') + .where('node.status = ?', 1) + .where('community_tags.date > ?', (DateTime.now - 1.month).to_i) + .where(powertag_clause) + .group(:name) + .order(order_string) + .paginate(page: params[:page], per_page: 24) + elsif @toggle == 'followers' raw_tags = Tag.joins(:node_tag, :node) - .select('node.nid, node.status, term_data.*, community_tags.*') - .where('node.status = ?', 1) - .where('community_tags.date > ?', (DateTime.now - 1.month).to_i) - .where(powertag_clause) - .group(:name) + .select('node.nid, node.status, term_data.*, community_tags.*') + .where('node.status = ?', 1) + .where('community_tags.date > ?', (DateTime.now - 1.month).to_i) + .where(powertag_clause) + .group(:name) raw_tags = Tag.sort_according_to_followers(raw_tags, params[:order]) @tags = raw_tags.paginate(page: params[:page], per_page: 24) else tags = Tag.joins(:node_tag, :node) - .select('node.nid, node.status, term_data.*, community_tags.*') - .where('node.status = ?', 1) - .where('community_tags.date > ?', (DateTime.now - 1.month).to_i) - .where(powertag_clause) - .group(:name) - .order(order_string) + .select('node.nid, node.status, term_data.*, community_tags.*') + .where('node.status = ?', 1) + .where('community_tags.date > ?', (DateTime.now - 1.month).to_i) + .where(powertag_clause) + .group(:name) + .order(order_string) followed = [] not_followed = [] @@ -73,15 +75,14 @@ def index end def show - - # Enhancement #6306 - Add counts to `by type` dropdown on tag pages - @counts = {:posts => 0, :questions => 0, :wiki => 0 } + # Enhancement #6306 - Add counts to `by type` dropdown on tag pages + @counts = { posts: 0, questions: 0, wiki: 0 } @counts[:posts] = Tag.find_nodes_by_type([params[:id]], 'note', false).count @counts[:questions] = Tag.find_nodes_by_type("question:#{params[:id]}", 'note', false).count @counts[:wiki] = Tag.find_nodes_by_type([params[:id]], 'page', false).count params[:counts] = @counts # end Enhancement #6306 ============================================ - + if params[:id].is_a? Integer @wiki = Node.find(params[:id])&.first else @@ -118,34 +119,34 @@ def show @wildcard = true @tags = Tag.where('name LIKE (?)', params[:id][0..-2] + '%') nodes = Node.where(status: 1, type: node_type) - .includes(:revision, :tag, :answers) - .references(:term_data, :node_revisions) - .where('term_data.name LIKE (?) OR term_data.parent LIKE (?)', params[:id][0..-2] + '%', params[:id][0..-2] + '%') - .paginate(page: params[:page], per_page: 24) - .order(order_by) + .includes(:revision, :tag, :answers) + .references(:term_data, :node_revisions) + .where('term_data.name LIKE (?) OR term_data.parent LIKE (?)', params[:id][0..-2] + '%', params[:id][0..-2] + '%') + .paginate(page: params[:page], per_page: 24) + .order(order_by) else @tags = Tag.where(name: params[:id]) if @node_type == 'questions' - other_tag = if params[:id].include? "question:" + other_tag = if params[:id].include? 'question:' params[:id].split(':')[1] else - "question:" + params[:id] + 'question:' + params[:id] end nodes = Node.where(status: 1, type: node_type) - .includes(:revision, :tag) - .references(:term_data, :node_revisions) - .where('term_data.name = ? OR term_data.name = ? OR term_data.parent = ?', params[:id], other_tag, params[:id]) - .paginate(page: params[:page], per_page: 24) - .order(order_by) + .includes(:revision, :tag) + .references(:term_data, :node_revisions) + .where('term_data.name = ? OR term_data.name = ? OR term_data.parent = ?', params[:id], other_tag, params[:id]) + .paginate(page: params[:page], per_page: 24) + .order(order_by) else nodes = Node.where(status: 1, type: node_type) - .includes(:revision, :tag) - .references(:term_data, :node_revisions) - .where('term_data.name = ? OR term_data.parent = ?', params[:id], params[:id]) - .paginate(page: params[:page], per_page: 24) - .order(order_by) + .includes(:revision, :tag) + .references(:term_data, :node_revisions) + .where('term_data.name = ? OR term_data.parent = ?', params[:id], params[:id]) + .paginate(page: params[:page], per_page: 24) + .order(order_by) end end nodes = nodes.where(created: @start.to_i..@end.to_i) if @start && @end @@ -221,8 +222,8 @@ def show_for_author @user = User.find_by(name: params[:author]) nodes = Tag.tagged_nodes_by_author(@tagname, @user) - .where(status: 1, type: node_type) - .paginate(page: params[:page], per_page: 24) + .where(status: 1, type: node_type) + .paginate(page: params[:page], per_page: 24) @notes ||= [] @@ -243,7 +244,7 @@ def show_for_author nodes.each do |node| json << node.as_json(except: %i(path tags)) json.last['path'] = 'https://' + request.host - .to_s + node.path + .to_s + node.path json.last['preview'] = node.body_preview(500) json.last['image'] = node.main_image.path(:large) if node.main_image json.last['tags'] = Node.find(node.id).tags.collect(&:name) if node.tags @@ -262,16 +263,16 @@ def widget num = params[:n] || 4 nids = Tag.find_nodes_by_type(params[:id], 'note', num).collect(&:nid) @notes = Node.paginate(page: params[:page], per_page: 24) - .where('status = 1 AND nid in (?)', nids) - .order('nid DESC') + .where('status = 1 AND nid in (?)', nids) + .order('nid DESC') render layout: false end def blog nids = Tag.find_nodes_by_type(params[:id], 'note', nil).collect(&:nid) @notes = Node.paginate(page: params[:page], per_page: 6) - .where('status = 1 AND nid in (?)', nids) - .order('nid DESC') + .where('status = 1 AND nid in (?)', nids) + .order('nid DESC') @tags = Tag.where(name: params[:id]) @tagnames = @tags.collect(&:name).uniq! || [] @title = @tagnames.join(',') + ' Blog' if @tagnames @@ -316,22 +317,22 @@ def create if Tag.exists?(tagname, nid) @output[:errors] << I18n.t('tag_controller.tag_already_exists') - elsif tagname.include?(":") && tagname.split(':').length < 2 - if tagname.split(':')[0] == "barnstar" || tagname.split(':')[0] == "with" + elsif tagname.include?(':') && tagname.split(':').length < 2 + if tagname.split(':')[0] == 'barnstar' || tagname.split(':')[0] == 'with' @output[:errors] << I18n.t('tag_controller.cant_be_empty') end elsif node.can_tag(tagname, current_user) === true || logged_in_as(['admin']) saved, tag = node.add_tag(tagname.strip, current_user) - if tagname.include?(":") && tagname.split(':').length == 2 - if tagname.split(':')[0] == "barnstar" + if tagname.include?(':') && tagname.split(':').length == 2 + if tagname.split(':')[0] == 'barnstar' CommentMailer.notify_barnstar(current_user, node) barnstar_info_link = 'barnstar' node.add_comment(subject: 'barnstar', uid: current_user.uid, body: "@#{current_user.username} awards a #{barnstar_info_link} to #{node.user.name} for their awesome contribution!") - elsif tagname.split(':')[0] == "with" + elsif tagname.split(':')[0] == 'with' user = User.find_by_username_case_insensitive(tagname.split(':')[1]) CommentMailer.notify_coauthor(user, node) node.add_comment(subject: 'co-author', @@ -356,9 +357,11 @@ def create if request.xhr? render json: @output else - flash[:notice] = I18n.t('tag_controller.tags_created_error', + flash[:notice] = I18n.t( + 'tag_controller.tags_created_error', tag_count: @output[:saved].length, - error_count: @output[:errors].length).html_safe + error_count: @output[:errors].length + ).html_safe redirect_to node.path end end @@ -370,14 +373,14 @@ def delete node_tag = NodeTag.where(nid: params[:nid], tid: params[:tid]).first node = Node.where(nid: params[:nid]).first # only admins, mods, and tag authors can delete other peoples' tags - if node_tag.uid == current_user.uid || logged_in_as(['admin', 'moderator']) || node.uid == current_user.uid + if node_tag.uid == current_user.uid || logged_in_as(%w(admin moderator)) || node.uid == current_user.uid tag = Tag.joins(:node_tag) - .select('term_data.name') - .where(tid: params[:tid]) - .first + .select('term_data.name') + .where(tid: params[:tid]) + .first - if (tag.name.split(':')[0] == "lat") || (tag.name.split(':')[0] == "lon") + if (tag.name.split(':')[0] == 'lat') || (tag.name.split(':')[0] == 'lon') node.delete_coord_attribute(tag.name) end @@ -405,7 +408,7 @@ def delete def suggested if !params[:id].empty? && params[:id].length > 2 @suggestions = SearchService.new.search_tags(params[:id]) - render json: @suggestions.collect { |tag| tag.name }.uniq + render json: @suggestions.collect(&:name).uniq else render json: [] end @@ -414,11 +417,11 @@ def suggested def rss @notes = if params[:tagname][-1..-1] == '*' Node.where(status: 1, type: 'note') - .includes(:revision, :tag) - .references(:term_data, :node_revisions) - .where('term_data.name LIKE (?)', params[:tagname][0..-2] + '%') - .limit(20) - .order('node_revisions.timestamp DESC') + .includes(:revision, :tag) + .references(:term_data, :node_revisions) + .where('term_data.name LIKE (?)', params[:tagname][0..-2] + '%') + .limit(20) + .order('node_revisions.timestamp DESC') else Tag.find_nodes_by_type([params[:tagname]], 'note', 20) end @@ -439,8 +442,8 @@ def rss def rss_for_tagged_with_author @user = User.find_by(name: params[:authorname]) @notes = Tag.tagged_nodes_by_author(params[:tagname], @user) - .where(status: 1) - .limit(20) + .where(status: 1) + .limit(20) respond_to do |format| format.rss do response.headers['Content-Type'] = 'application/xml; charset=utf-8' @@ -465,7 +468,7 @@ def contributors # /contributors def contributors_index - @tagnames = ['balloon-mapping', 'spectrometer', 'infragram', 'air-quality', 'water-quality'] + @tagnames = %w(balloon-mapping spectrometer infragram air-quality water-quality) @tagdata = {} @tags = [] @@ -487,13 +490,13 @@ def add_parent @tag = Tag.find_by(name: params[:name]) @tag.update_attribute('parent', params[:parent]) if @tag.save - flash[:notice] = "Tag parent added." + flash[:notice] = 'Tag parent added.' else - flash[:error] = "There was an error adding a tag parent." + flash[:error] = 'There was an error adding a tag parent.' end redirect_to '/tag/' + @tag.name + '?_=' + Time.now.to_i.to_s else - flash[:error] = "Only admins may add tag parents." + flash[:error] = 'Only admins may add tag parents.' end end @@ -508,7 +511,7 @@ def location_modal def gridsEmbed if %w(nodes wikis activities questions upgrades notes).include?(params[:tagname].split(':').first) params[:t] = params[:tagname] - params[:tagname] = "" + params[:tagname] = '' end render layout: false end @@ -537,10 +540,10 @@ def stats private def order_string - if params[:search] || @toggle == "uses" - params[:order] == "asc" ? "count ASC" : "count DESC" + if params[:search] || @toggle == 'uses' + params[:order] == 'asc' ? 'count ASC' : 'count DESC' else - params[:order] == "asc" ? "name ASC" : "name DESC" + params[:order] == 'asc' ? 'name ASC' : 'name DESC' end end end diff --git a/app/controllers/talk_controller.rb b/app/controllers/talk_controller.rb index bcbc331b5f..79026f30c7 100644 --- a/app/controllers/talk_controller.rb +++ b/app/controllers/talk_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class TalkController < ApplicationController def show @node = Node.find_by_path params[:id] diff --git a/app/controllers/user_sessions_controller.rb b/app/controllers/user_sessions_controller.rb index 4a4cf4c5cb..d0b5713d24 100644 --- a/app/controllers/user_sessions_controller.rb +++ b/app/controllers/user_sessions_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class UserSessionsController < ApplicationController before_action :require_no_user, only: [:new] def new @@ -21,10 +23,10 @@ def handle_social_login_flow(auth) return_to = request.env['omniauth.origin'] || root_url return_to += '?_=' + Time.now.to_i.to_s - hash_params = "" + hash_params = '' unless params[:hash_params].to_s.empty? - hash_params = URI.parse("#" + params[:hash_params]).to_s + hash_params = URI.parse('#' + params[:hash_params]).to_s end if signed_in? @@ -35,19 +37,19 @@ def handle_social_login_flow(auth) # associate the identity @identity.user = current_user @identity.save - redirect_to return_to + hash_params, notice: "Successfully linked to your account!" + redirect_to return_to + hash_params, notice: 'Successfully linked to your account!' elsif @identity.user == current_user # User is signed in so they are trying to link an identity with their # account. But we found the identity and the user associated with it # is the current user. So the identity is already associated with # this user. So let's display an error message. - redirect_to return_to + hash_params, notice: "Already linked to your account!" + redirect_to return_to + hash_params, notice: 'Already linked to your account!' else # User is signed in so they are trying to link an identity with their # account. But we found the identity and a different user associated with it # ,which is not the current user. So the identity is already associated with # that user. So let's display an error message. - redirect_to return_to + hash_params, notice: "Already linked to another account!" + redirect_to return_to + hash_params, notice: 'Already linked to another account!' end else # not signed in if @identity&.user.present? @@ -60,10 +62,10 @@ def handle_social_login_flow(auth) session[:openid_return_to] = nil redirect_to return_to + hash_params else - redirect_to return_to + hash_params, notice: "Signed in!" + redirect_to return_to + hash_params, notice: 'Signed in!' end else # identity does not exist so we need to either create a user with identity OR link identity to existing user - if User.where(email: auth["info"]["email"]).empty? + if User.where(email: auth['info']['email']).empty? # Create a new user as email provided is not present in PL database user = User.create_with_omniauth(auth) WelcomeMailer.notify_newcomer(user).deliver_now @@ -77,18 +79,18 @@ def handle_social_login_flow(auth) return_to = session[:openid_return_to] session[:openid_return_to] = nil redirect_to return_to + hash_params - elsif params[:return_to] && params[:return_to].split('/')[0..3] == ["", "subscribe", "multiple", "tag"] + elsif params&.send(:[], :return_to)&.split('/')&.send(:[], 0..3) == ['', 'subscribe', 'multiple', 'tag'] flash[:notice] = "You are now following '#{params[:return_to].split('/')[4]}'." subscribe_multiple_tag(params[:return_to].split('/')[4]) - redirect_to '/dashboard', notice: "You have successfully signed in. Please change your password using the link sent to you via e-mail." - elsif params[:return_to] && params[:return_to] != "/signup" && params[:return_to] != "/login" - flash[:notice] += " " + I18n.t('users_controller.continue_where_you_left_off', url1: params[:return_to].to_s) - redirect_to return_to + hash_params, notice: "You have successfully signed in. Please change your password using the link sent to you via e-mail." + redirect_to '/dashboard', notice: 'You have successfully signed in. Please change your password using the link sent to you via e-mail.' + elsif params[:return_to] && params[:return_to] != '/signup' && params[:return_to] != '/login' + flash[:notice] += ' ' + I18n.t('users_controller.continue_where_you_left_off', url1: params[:return_to].to_s) + redirect_to return_to + hash_params, notice: 'You have successfully signed in. Please change your password using the link sent to you via e-mail.' else - redirect_to return_to + hash_params, notice: "You have successfully signed in. Please change your password using the link sent to you via e-mail." + redirect_to return_to + hash_params, notice: 'You have successfully signed in. Please change your password using the link sent to you via e-mail.' end else # email exists so link the identity with existing user and log in the user - user = User.where(email: auth["info"]["email"]) + user = User.where(email: auth['info']['email']) # If no identity was found, create a brand new one here @identity = UserTag.create_with_omniauth(auth, user.ids.first) # The identity is not associated with the current_user so lets @@ -102,7 +104,7 @@ def handle_social_login_flow(auth) session[:openid_return_to] = nil redirect_to return_to + hash_params else - redirect_to return_to + hash_params, notice: "Successfully linked to your account!" + redirect_to return_to + hash_params, notice: 'Successfully linked to your account!' end end end @@ -114,8 +116,8 @@ def handle_site_login_flow u = User.find_by(username: username) if u && u.password_checker != 0 n = u.password_checker - hash = { 1 => "Facebook", 2 => "Github", 3 => "Google", 4 => "Twitter" } - s = "This account doesn't have a password set. It may be logged in with " + hash[n] + " account, or you can set a new password via Forget password feature" + hash = { 1 => 'Facebook', 2 => 'Github', 3 => 'Google', 4 => 'Twitter' } + s = "This account doesn't have a password set. It may be logged in with " + hash[n] + ' account, or you can set a new password via Forget password feature' flash[:error] = s redirect_to '/' else @@ -127,7 +129,7 @@ def handle_site_login_flow params[:user_session][:username] = @user.username end if @user.nil? - flash[:warning] = "There is nobody in our system by that name, are you sure you have the right username?" + flash[:warning] = 'There is nobody in our system by that name, are you sure you have the right username?' redirect_to params[:return_to] || '/login' elsif params[:user_session].nil? || @user&.status == 1 # an existing Rails user @@ -143,10 +145,10 @@ def handle_site_login_flow remember_me: params[:user_session][:remember_me]) @user_session.save do |result| if result - hash_params = "" + hash_params = '' unless params[:hash_params].to_s.empty? - hash_params = URI.parse("#" + params[:hash_params]).to_s + hash_params = URI.parse('#' + params[:hash_params]).to_s end # replace this with temporarily saving pwd in session, @@ -205,14 +207,14 @@ def destroy @user_session = UserSession.find @user_session.destroy flash[:notice] = I18n.t('user_sessions_controller.logged_out') - prev_uri = URI(request.referer || "").path + prev_uri = URI(request.referer || '').path redirect_to prev_uri + '?_=' + Time.current.to_i.to_s end def logout_remotely current_user&.reset_persistence_token! flash[:notice] = I18n.t('user_sessions_controller.logged_out') - prev_uri = URI(request.referer || "").path + prev_uri = URI(request.referer || '').path redirect_to prev_uri + '?_=' + Time.current.to_i.to_s end @@ -224,32 +226,33 @@ def index def subscribe_multiple_tag(tag_list) if !tag_list || tag_list == '' - flash[:notice] = "Please enter tags for subscription in the url." + flash[:notice] = 'Please enter tags for subscription in the url.' else if tag_list.is_a? String tag_list = tag_list.split(',') end tag_list.each do |t| next unless t.length.positive? + tag = Tag.find_by(name: t) unless tag.present? tag = Tag.new( vid: 3, # vocabulary id name: t, - description: "", + description: '', weight: 0 ) begin tag.save! - rescue ActiveRecord::RecordInvalid + rescue ActiveRecord::RecordInvalid flash[:error] = tag.errors.full_messages - redirect_to "/subscriptions" + "?_=" + Time.now.to_i.to_s + redirect_to '/subscriptions' + '?_=' + Time.now.to_i.to_s return false end end # test for uniqueness unless TagSelection.where(following: true, user_id: current_user.uid, tid: tag.tid).length.positive? - # Successfully we have added subscription + # Successfully we have added subscription if Tag.find_by(tid: tag.tid) # Create the entry if it isn't already created. # assume tag, for now: @@ -261,7 +264,7 @@ def subscribe_multiple_tag(tag_list) subscription.save! end else - flash.now[:error] = "Sorry! There was an error in tag subscriptions. Please try it again." + flash.now[:error] = 'Sorry! There was an error in tag subscriptions. Please try it again.' end end end diff --git a/app/controllers/user_tags_controller.rb b/app/controllers/user_tags_controller.rb index 3938cb7498..0619b72e73 100644 --- a/app/controllers/user_tags_controller.rb +++ b/app/controllers/user_tags_controller.rb @@ -1,34 +1,36 @@ +# frozen_string_literal: true + class UserTagsController < ApplicationController respond_to :html, :xml, :json, :js require 'will_paginate/array' def index - @toggle = params[:sort] || "uses" + @toggle = params[:sort] || 'uses' @title = I18n.t('tag_controller.tags') @paginated = true if params[:search] keyword = params[:search] @user_tags = UserTag - .select('value') - .where("value LIKE :keyword", keyword: "%#{keyword}%") - .group(:value) - .order('value ASC') - .count('value').to_a - .paginate(page: params[:page], per_page: 24) - elsif @toggle == "value" + .select('value') + .where('value LIKE :keyword', keyword: "%#{keyword}%") + .group(:value) + .order('value ASC') + .count('value').to_a + .paginate(page: params[:page], per_page: 24) + elsif @toggle == 'value' @user_tags = UserTag.group(:value) - .select('value') - .order('value ASC') - .count('value').to_a - .paginate(page: params[:page], per_page: 24) + .select('value') + .order('value ASC') + .count('value').to_a + .paginate(page: params[:page], per_page: 24) else # @toggle == "uses" @user_tags = UserTag.group(:value) - .select('value') - .order('count_value DESC') - .count('value').to_a - .paginate(page: params[:page], per_page: 24) + .select('value') + .order('count_value DESC') + .count('value').to_a + .paginate(page: params[:page], per_page: 24) end end @@ -54,13 +56,13 @@ def create next if exist user_tag = user.user_tags.build(value: name) - if tagname.split(':')[1] == "facebook" + if tagname.split(':')[1] == 'facebook' @output[:errors] << "This tag is used for associating a Facebook account. Click here to read more " - elsif tagname.split(':')[1] == "github" + elsif tagname.split(':')[1] == 'github' @output[:errors] << "This tag is used for associating a Github account. Click here to read more " - elsif tagname.split(':')[1] == "google_oauth2" + elsif tagname.split(':')[1] == 'google_oauth2' @output[:errors] << "This tag is used for associating a Google account. Click here to read more " - elsif tagname.split(':')[1] == "twitter" + elsif tagname.split(':')[1] == 'twitter' @output[:errors] << "This tag is used for associating a Twitter account. Click here to read more " elsif user_tag.save @output[:saved] << [name, user_tag.id] diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index aeca48e3f3..bffd192a65 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,40 +1,42 @@ +# frozen_string_literal: true + class UsersController < ApplicationController before_action :require_no_user, only: [:new] before_action :require_user, only: %i(edit update save_settings) - before_action :set_user, only: %i(info followed following followers) + before_action :set_user, only: %i(info followed following followers) def new @user = User.new - @action = "create" # sets the form url + @action = 'create' # sets the form url end def create @user = User.new(user_params) @user.status = 1 - using_recaptcha = !params[:spamaway] && Rails.env == "production" + using_recaptcha = !params[:spamaway] && Rails.env == 'production' recaptcha = verify_recaptcha(model: @user) if using_recaptcha @spamaway = Spamaway.new(spamaway_params) unless using_recaptcha if ((@spamaway&.valid?) || recaptcha) && @user.save if current_user.crypted_password.nil? # the user has not created a pwd in the new site flash[:warning] = I18n.t('users_controller.account_migrated_create_new_password') - redirect_to "/profile/edit" + redirect_to '/profile/edit' else begin WelcomeMailer.notify_newcomer(@user).deliver_now rescue StandardError - flash[:warning] = "We tried and failed to send you a welcome email, but your account was created anyhow. Sorry!" + flash[:warning] = 'We tried and failed to send you a welcome email, but your account was created anyhow. Sorry!' end flash[:notice] = I18n.t('users_controller.registration_successful') - if params[:return_to] && params[:return_to].split('/')[0..3] == ["", "subscribe", "multiple", "tag"] + if params&.send(:[], :return_to)&.split('/')&.send(:[], 0..3) == ['', 'subscribe', 'multiple', 'tag'] flash[:notice] += "You are now following '#{params[:return_to].split('/')[4]}'." subscribe_multiple_tag(params[:return_to].split('/')[4]) - elsif params[:return_to] && params[:return_to] != "/signup" && params[:return_to] != "/login" - flash[:notice] += " " + I18n.t('users_controller.continue_where_you_left_off', url1: params[:return_to].to_s) + elsif params[:return_to] && params[:return_to] != '/signup' && params[:return_to] != '/login' + flash[:notice] += ' ' + I18n.t('users_controller.continue_where_you_left_off', url1: params[:return_to].to_s) end flash[:notice] = flash[:notice].html_safe flash[:warning] = I18n.t('users_controller.spectralworkbench_or_mapknitter', url1: "#{session[:openid_return_to]}'").html_safe if session[:openid_return_to] session[:openid_return_to] = nil - redirect_to "/dashboard" + redirect_to '/dashboard' end else # pipe all spamaway errors into the user error display @@ -46,7 +48,7 @@ def create flash.now[:warning] = "If you're having trouble creating an account, try the alternative signup form, or ask staff for help" end # send all errors to the page so the user can try again - @action = "create" + @action = 'create' render action: 'new' end end @@ -55,7 +57,7 @@ def update @password_verification = user_verification_params @user = current_user @user = User.find_by(username: params[:id]) if params[:id] && logged_in_as(['admin']) - if @user.valid_password?(user_verification_params["current_password"]) || user_verification_params["ui_update"].nil? + if @user.valid_password?(user_verification_params['current_password']) || user_verification_params['ui_update'].nil? # correct password @user.attributes = user_params if @user.save @@ -64,31 +66,31 @@ def update session[:openid_return_to] = nil redirect_to return_to else - flash[:notice] = I18n.t('users_controller.successful_updated_profile') + "" + I18n.t('users_controller.return_profile') + " »" - return redirect_to "/profile/" + @user.username + "/edit" + flash[:notice] = I18n.t('users_controller.successful_updated_profile') + "" + I18n.t('users_controller.return_profile') + ' »' + return redirect_to '/profile/' + @user.username + '/edit' end else render template: 'users/edit' end else # incorrect password - flash[:error] = "Current Password is incorrect!" - return redirect_to "/profile/" + @user.username + "/edit" + flash[:error] = 'Current Password is incorrect!' + return redirect_to '/profile/' + @user.username + '/edit' end end def edit - @action = "update" # sets the form url + @action = 'update' # sets the form url @user = if params[:id] # admin only User.find_by(username: params[:id]) else current_user end if current_user && current_user.uid == @user.uid || logged_in_as(['admin']) - render template: "users/edit" + render template: 'users/edit' else flash[:error] = I18n.t('users_controller.only_user_edit_profile', user: @user.name).html_safe - redirect_to "/profile/" + @user.name + redirect_to '/profile/' + @user.name end end @@ -112,29 +114,29 @@ def list @map_lat = nil @map_lon = nil - if current_user&.has_power_tag("lat") && current_user&.has_power_tag("lon") - @map_lat = current_user.get_value_of_power_tag("lat").to_f - @map_lon = current_user.get_value_of_power_tag("lon").to_f + if current_user&.has_power_tag('lat') && current_user&.has_power_tag('lon') + @map_lat = current_user.get_value_of_power_tag('lat').to_f + @map_lon = current_user.get_value_of_power_tag('lon').to_f end # allow admins to view recent users @users = if params[:id] User.order(order_string) - .where('rusers.role = ?', params[:id]) - .where('rusers.status = 1') - .page(params[:page]) + .where('rusers.role = ?', params[:id]) + .where('rusers.status = 1') + .page(params[:page]) elsif @tagname_param User.where(id: UserTag.where(value: @tagname_param).collect(&:uid)) - .page(params[:page]) + .page(params[:page]) else # recently active User.select('*, rusers.status, MAX(node_revisions.timestamp) AS last_updated') - .joins(:revisions) - .where("node_revisions.status = 1") - .group('rusers.id') - .order(order_string) - .page(params[:page]) + .joins(:revisions) + .where('node_revisions.status = 1') + .group('rusers.id') + .order(order_string) + .page(params[:page]) end @users = @users.where('rusers.status = 1') unless current_user&.can_moderate? @@ -144,50 +146,50 @@ def profile if current_user && params[:id].nil? redirect_to "/profile/#{current_user.username}" elsif !current_user && params[:id].nil? - redirect_to "/" + redirect_to '/' else @profile_user = User.find_by(username: params[:id]) if !@profile_user flash[:error] = I18n.t('users_controller.no_user_found_name', username: params[:id]) - redirect_to "/" + redirect_to '/' else @title = @profile_user.name @notes = Node.research_notes .paginate(page: params[:page], per_page: 24) - .order("nid DESC") + .order('nid DESC') .where(status: 1, uid: @profile_user.uid) if current_user && current_user.uid == @profile_user.uid coauthor_nids = Node.joins(:node_tag) - .joins('LEFT OUTER JOIN term_data ON term_data.tid = community_tags.tid') - .select('node.*, term_data.*, community_tags.*') - .where(type: 'note', status: 3) - .where('term_data.name = (?)', "with:#{@profile_user.username}") - .collect(&:nid) + .joins('LEFT OUTER JOIN term_data ON term_data.tid = community_tags.tid') + .select('node.*, term_data.*, community_tags.*') + .where(type: 'note', status: 3) + .where('term_data.name = (?)', "with:#{@profile_user.username}") + .collect(&:nid) @drafts = Node.where('(nid IN (?) OR (status = 3 AND uid = ?))', coauthor_nids, @profile_user.uid) - .paginate(page: params[:page], per_page: 24) + .paginate(page: params[:page], per_page: 24) end @coauthored = @profile_user.coauthored_notes - .paginate(page: params[:page], per_page: 24) - .order('node_revisions.timestamp DESC') + .paginate(page: params[:page], per_page: 24) + .order('node_revisions.timestamp DESC') @questions = @profile_user.questions - .order('node.nid DESC') - .paginate(page: params[:page], per_page: 24) + .order('node.nid DESC') + .paginate(page: params[:page], per_page: 24) @likes = (@profile_user.liked_notes.includes(%i(tag comments)) + @profile_user.liked_pages) - .paginate(page: params[:page], per_page: 24) + .paginate(page: params[:page], per_page: 24) questions = Node.questions .where(status: 1) .order('node.nid DESC') ans_ques = questions.select { |q| q.comments.collect(&:uid).include?(@profile_user.id) } @commented_questions = ans_ques.paginate(page: params[:page], per_page: 24) - wikis = Revision.order("nid DESC") + wikis = Revision.order('nid DESC') .where('node.type' => 'page', 'node.status' => 1, uid: @profile_user.uid) .joins(:node) .limit(20) @wikis = wikis.collect(&:parent).uniq comments = Comment.limit(20) - .order("timestamp DESC") + .order('timestamp DESC') .where(uid: @profile_user.uid) .paginate(page: params[:page], per_page: 24) @@ -199,17 +201,17 @@ def profile end # User's social links - @github = @profile_user.social_link("github") - @twitter = @profile_user.social_link("twitter") - @facebook = @profile_user.social_link("facebook") - @instagram = @profile_user.social_link("instagram") - @count_activities_posted = Tag.tagged_nodes_by_author("activity:*", @profile_user).count - @count_activities_attempted = Tag.tagged_nodes_by_author("replication:*", @profile_user).count + @github = @profile_user.social_link('github') + @twitter = @profile_user.social_link('twitter') + @facebook = @profile_user.social_link('facebook') + @instagram = @profile_user.social_link('instagram') + @count_activities_posted = Tag.tagged_nodes_by_author('activity:*', @profile_user).count + @count_activities_attempted = Tag.tagged_nodes_by_author('replication:*', @profile_user).count @map_lat = nil @map_lon = nil - if @profile_user.has_power_tag("lat") && @profile_user.has_power_tag("lon") - @map_lat = @profile_user.get_value_of_power_tag("lat").to_f - @map_lon = @profile_user.get_value_of_power_tag("lon").to_f + if @profile_user.has_power_tag('lat') && @profile_user.has_power_tag('lon') + @map_lat = @profile_user.get_value_of_power_tag('lat').to_f + @map_lon = @profile_user.get_value_of_power_tag('lon').to_f @map_blurred = @profile_user.has_tag('blurred:true') end @@ -218,7 +220,7 @@ def profile flash.now[:error] = I18n.t('users_controller.user_has_been_banned') else flash[:error] = I18n.t('users_controller.user_has_been_banned') - redirect_to "/" + redirect_to '/' end elsif @profile_user.status == 5 flash.now[:warning] = I18n.t('users_controller.user_has_been_moderated') @@ -229,7 +231,7 @@ def profile def likes @user = User.find_by(username: params[:id]) - @title = "Liked by " + @user.name + @title = 'Liked by ' + @user.name @notes = @user.liked_notes .includes(%i(tag comments)) .paginate(page: params[:page], per_page: 24) @@ -242,9 +244,9 @@ def rss if params[:author] @author = User.where(username: params[:author], status: 1).first if @author - @notes = Node.order("nid DESC") - .where(type: 'note', status: 1, uid: @author.uid) - .limit(20) + @notes = Node.order('nid DESC') + .where(type: 'note', status: 1, uid: @author.uid) + .limit(20) respond_to do |format| format.rss do render layout: false @@ -254,58 +256,70 @@ def rss end else flash[:error] = I18n.t('users_controller.no_user_found') - redirect_to "/" + redirect_to '/' end end end def reset if params[:key] && !params[:key].nil? - @user = User.find_by(reset_key: params[:key]) - if @user - if params[:user] && params[:user][:password] - if @user.username.casecmp(params[:user][:username].downcase).zero? - @user.password = params[:user][:password] - @user.password_confirmation = params[:user][:password] - @user.reset_key = nil - if @user.changed? && @user.save - flash[:notice] = I18n.t('users_controller.password_change_success') - @user.password_checker = 0 - redirect_to "/dashboard" - else - flash[:error] = I18n.t('users_controller.password_reset_failed').html_safe - redirect_to "/" - end - else - flash[:error] = I18n.t('users_controller.password_change_failed') - end - end - else - flash[:error] = I18n.t('users_controller.password_reset_failed_no_user').html_safe - redirect_to "/" - end - + reset_by_key elsif params[:email] - user = User.find_by(email: params[:email]) - if user - key = user.generate_reset_key - user.save - # send key to user email - PasswordResetMailer.reset_notify(user, key).deliver_now unless user.nil? # respond the same to both successes and failures; security - end - flash[:notice] = I18n.t('users_controller.password_reset_email') - redirect_to "/login" + reset_by_email + end + end + + def reset_by_key + @user = User.find_by(reset_key: params[:key]) + + unless @user + flash[:error] = I18n.t('users_controller.password_reset_failed_no_user').html_safe + redirect_to '/' + return end + + return unless params[:user] && params[:user][:password] + + unless @user.username.casecmp(params[:user][:username].downcase).zero? + flash[:error] = I18n.t('users_controller.password_change_failed') + return + end + + @user.password = params[:user][:password] + @user.password_confirmation = params[:user][:password] + @user.reset_key = nil + + unless @user.changed? && @user.save + flash[:error] = I18n.t('users_controller.password_reset_failed').html_safe + redirect_to '/' + return + end + + flash[:notice] = I18n.t('users_controller.password_change_success') + @user.password_checker = 0 + redirect_to '/dashboard' + end + + def reset_by_email + user = User.find_by(email: params[:email]) + if user + key = user.generate_reset_key + user.save + # send key to user email + PasswordResetMailer.reset_notify(user, key).deliver_now unless user.nil? # respond the same to both successes and failures; security + end + flash[:notice] = I18n.t('users_controller.password_reset_email') + redirect_to '/login' end def comments comments = Comment.limit(20) - .order("timestamp DESC") - .where(uid: params[:id]) - .paginate(page: params[:page], per_page: 24) + .order('timestamp DESC') + .where(uid: params[:id]) + .paginate(page: params[:page], per_page: 24) @normal_comments = comments.where('comments.status = 1') - if logged_in_as(['admin', 'moderator']) + if logged_in_as(%w(admin moderator)) @moderated_comments = comments.where('comments.status = 4') end render template: 'comments/index' @@ -324,11 +338,11 @@ def photo end else flash[:error] = I18n.t('users_controller.image_not_saved') - redirect_to "/images/new" + redirect_to '/images/new' end else flash[:error] = I18n.t('users_controller.image_not_saved') - redirect_to "/images/new" + redirect_to '/images/new' end end @@ -340,20 +354,20 @@ def followed end def following - @title = "Following" + @title = 'Following' @users = @user.following_users.paginate(page: params[:page], per_page: 24) render 'show_follow' end def followers - @title = "Followers" + @title = 'Followers' @users = @user.followers.paginate(page: params[:page], per_page: 24) render 'show_follow' end def test_digest_email DigestMailJob.perform_async(0) - redirect_to "/" + redirect_to '/' end def save_settings @@ -364,7 +378,7 @@ def save_settings ] user_settings.each do |setting| - if params[setting] && params[setting] == "on" + if params[setting] && params[setting] == 'on' UserTag.remove_if_exists(current_user.uid, setting) else UserTag.create_if_absent(current_user.uid, setting) @@ -376,7 +390,7 @@ def save_settings 'digest:daily' ] digest_settings.each do |setting| - if params[setting] == "on" + if params[setting] == 'on' UserTag.create_if_absent(current_user.uid, setting) else UserTag.remove_if_exists(current_user.uid, setting) @@ -390,15 +404,15 @@ def save_settings ] notification_settings.each do |setting| - if params[setting] == "on" + if params[setting] == 'on' UserTag.create_if_absent(current_user.uid, setting) else UserTag.remove_if_exists(current_user.uid, setting) end end - flash[:notice] = "Settings updated successfully!" - render js: "window.location.reload()" + flash[:notice] = 'Settings updated successfully!' + render js: 'window.location.reload()' end def shortlink @@ -412,43 +426,44 @@ def shortlink def verify_email decrypted_user_id = User.validate_token(params[:token]) - action_msg = "Email verification failed" + action_msg = 'Email verification failed' if decrypted_user_id != 0 user_obj = User.find(decrypted_user_id) if user_obj.is_verified - action_msg = "Email already verified" + action_msg = 'Email already verified' else user_obj.update_column(:is_verified, true) - action_msg = "Successfully verified email" + action_msg = 'Successfully verified email' end end - redirect_to "/login", flash: { notice: action_msg } + redirect_to '/login', flash: { notice: action_msg } end private def subscribe_multiple_tag(tag_list) if !tag_list || tag_list == '' - flash[:notice] = "Please enter tags for subscription in the url." + flash[:notice] = 'Please enter tags for subscription in the url.' else if tag_list.is_a? String tag_list = tag_list.split(',') end tag_list.each do |t| next unless t.length.positive? + tag = Tag.find_by(name: t) unless tag.present? tag = Tag.new( vid: 3, # vocabulary id name: t, - description: "", + description: '', weight: 0 ) begin tag.save! - rescue ActiveRecord::RecordInvalid + rescue ActiveRecord::RecordInvalid flash[:error] = tag.errors.full_messages - redirect_to "/subscriptions" + "?_=" + Time.now.to_i.to_s + redirect_to '/subscriptions' + '?_=' + Time.now.to_i.to_s return false end end @@ -466,7 +481,7 @@ def subscribe_multiple_tag(tag_list) subscription.save! end else - flash.now[:error] = "Sorry! There was an error in tag subscriptions. Please try it again." + flash.now[:error] = 'Sorry! There was an error in tag subscriptions. Please try it again.' end end end diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index 319ccead43..c4c6bc30a6 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'rss' class WikiController < ApplicationController @@ -9,7 +11,7 @@ def subdomain when 'new-york-city', 'gulf-coast', 'boston', - 'espana' then + 'espana' redirect_to url + request.subdomain when 'nyc' redirect_to url + 'new-york-city' @@ -130,10 +132,10 @@ def new flash.now[:notice] = I18n.t('wiki_controller.page_does_not_exist_create') title = params[:id].tr('-', ' ') @related = Node.limit(10) - .order('node.nid DESC') - .where('type = "page" AND node.status = 1 AND (node.title LIKE ? OR node_revisions.body LIKE ?)', '%' + title + '%', '%' + title + '%') - .includes(:revision) - .references(:node_revisions) + .order('node.nid DESC') + .where('type = "page" AND node.status = 1 AND (node.title LIKE ? OR node_revisions.body LIKE ?)', '%' + title + '%', '%' + title + '%') + .includes(:revision) + .references(:node_revisions) tag = Tag.find_by(name: params[:id]) # add page name as a tag, too @tags << tag if tag @related += Tag.find_nodes_by_type(@tags.collect(&:name), 'page', 10) @@ -154,9 +156,9 @@ def create # slug = params[:title].parameterize # slug = params[:id].parameterize if params[:id] != "" && !params[:id].nil? # slug = params[:url].parameterize if params[:url] != "" && !params[:url].nil? - saved, @node, @revision = Node.new_wiki(uid: current_user.uid, + saved, @node, @revision = Node.new_wiki(uid: current_user.uid, title: params[:title], - body: params[:body]) + body: params[:body]) if saved flash[:notice] = I18n.t('wiki_controller.wiki_page_created') if params[:main_image] && params[:main_image] != '' @@ -173,7 +175,7 @@ def create node = Node.find(params[:n]) params[:body] = node.body if node end - flash[:error] = "Please enter both body and title" + flash[:error] = 'Please enter both body and title' render template: 'editor/wikiRich' end else @@ -184,9 +186,9 @@ def create def update @node = Node.find(params[:id]) - @revision = @node.new_revision(uid: current_user.uid, + @revision = @node.new_revision(uid: current_user.uid, title: params[:title], - body: params[:body]) + body: params[:body]) if @node.has_tag('locked') && !current_user.can_moderate? flash[:warning] = "This page is locked, and only moderators can update it." @@ -236,7 +238,7 @@ def revert # wiki pages which have a root URL, like /about # also just redirect anything else matching /____ to /wiki/____ def root - @node = Node.find_by(path: "/" + params[:id]) + @node = Node.find_by(path: '/' + params[:id]) return if redirect_to_node_path?(@node) if @node @@ -313,11 +315,11 @@ def index end @wikis = Node.includes(:revision) - .references(:node_revisions) - .group('node_revisions.nid') - .order(order_string) - .where("node_revisions.status = 1 AND node.status = 1 AND (type = 'page' OR type = 'tool' OR type = 'place')") - .page(params[:page]) + .references(:node_revisions) + .group('node_revisions.nid') + .order(order_string) + .where("node_revisions.status = 1 AND node.status = 1 AND (type = 'page' OR type = 'tool' OR type = 'place')") + .page(params[:page]) @paginated = true end @@ -326,11 +328,11 @@ def stale @title = I18n.t('wiki_controller.wiki') @wikis = Node.includes(:revision) - .references(:node_revisions) - .group('node_revisions.nid') - .order('node_revisions.timestamp ASC') - .where("node_revisions.status = 1 AND node.status = 1 AND (type = 'page' OR type = 'tool' OR type = 'place')") - .page(params[:page]) + .references(:node_revisions) + .group('node_revisions.nid') + .order('node_revisions.timestamp ASC') + .where("node_revisions.status = 1 AND node.status = 1 AND (type = 'page' OR type = 'tool' OR type = 'place')") + .page(params[:page]) @paginated = true render template: 'wiki/index' @@ -339,19 +341,19 @@ def stale def popular @title = I18n.t('wiki_controller.popular_wiki_pages') @wikis = Node.limit(40) - .joins(:revision) - .group('node_revisions.nid') - .order('node_revisions.timestamp DESC') - .where("node.status = 1 AND node_revisions.status = 1 AND node.nid != 259 AND (type = 'page' OR type = 'tool' OR type = 'place')") - .sort_by(&:views).reverse + .joins(:revision) + .group('node_revisions.nid') + .order('node_revisions.timestamp DESC') + .where("node.status = 1 AND node_revisions.status = 1 AND node.nid != 259 AND (type = 'page' OR type = 'tool' OR type = 'place')") + .sort_by(&:views).reverse render template: 'wiki/index' end def liked @title = I18n.t('wiki_controller.well_liked_wiki_pages') @wikis = Node.limit(40) - .order('node.cached_likes DESC') - .where("status = 1 AND nid != 259 AND (type = 'page' OR type = 'tool' OR type = 'place') AND cached_likes >= 0") + .order('node.cached_likes DESC') + .where("status = 1 AND nid != 259 AND (type = 'page' OR type = 'tool' OR type = 'place') AND cached_likes >= 0") render template: 'wiki/index' end @@ -389,55 +391,59 @@ def techniques def methods @nodes = Node.where(status: 1, type: %w(page)) - .where('term_data.name = ?', 'method') - .includes(:revision, :tag) - .references(:node_revision) - .order('node_revisions.timestamp DESC') + .where('term_data.name = ?', 'method') + .includes(:revision, :tag) + .references(:node_revision) + .order('node_revisions.timestamp DESC') # deprecating the following in favor of javascript implementation in /app/assets/javascripts/methods.js if params[:topic] nids = @nodes.collect(&:nid) || [] @notes = Node.where(status: 1, type: %w(page)) - .where('node.nid IN (?)', nids) - .where('(type = "note" OR type = "page" OR type = "map") AND node.status = 1 AND (node.title LIKE ? OR node_revisions.title LIKE ? OR node_revisions.body LIKE ? OR term_data.name = ?)', - '%' + params[:topic] + '%', - '%' + params[:topic] + '%', - '%' + params[:topic] + '%', - params[:topic]) - .includes(:revision, :tag) - .references(:node_revision, :term_data) - .order('node_revisions.timestamp DESC') + .where('node.nid IN (?)', nids) + .where( + '(type = "note" OR type = "page" OR type = "map") AND node.status = 1 AND (node.title LIKE ? OR node_revisions.title LIKE ? OR node_revisions.body LIKE ? OR term_data.name = ?)', + '%' + params[:topic] + '%', + '%' + params[:topic] + '%', + '%' + params[:topic] + '%', + params[:topic] + ) + .includes(:revision, :tag) + .references(:node_revision, :term_data) + .order('node_revisions.timestamp DESC') end if params[:topic] nids = @nodes.collect(&:nid) || [] @nodes = Node.where(status: 1, type: %w(page)) - .where('node.nid IN (?)', nids) - .where('(type = "note" OR type = "page" OR type = "map") AND node.status = 1 AND (node.title LIKE ? OR node_revisions.title LIKE ? OR node_revisions.body LIKE ? OR term_data.name = ?)', - '%' + params[:topic] + '%', - '%' + params[:topic] + '%', - '%' + params[:topic] + '%', - params[:topic]) - .includes(:revision, :tag) - .references(:node_revision, :term_data) - .order('node_revisions.timestamp DESC') + .where('node.nid IN (?)', nids) + .where( + '(type = "note" OR type = "page" OR type = "map") AND node.status = 1 AND (node.title LIKE ? OR node_revisions.title LIKE ? OR node_revisions.body LIKE ? OR term_data.name = ?)', + '%' + params[:topic] + '%', + '%' + params[:topic] + '%', + '%' + params[:topic] + '%', + params[:topic] + ) + .includes(:revision, :tag) + .references(:node_revision, :term_data) + .order('node_revisions.timestamp DESC') end @unpaginated = true - @topics = [ - 'agriculture', - 'drinking-water', - 'fracking', - 'indoor-air', - 'chemicals', - 'industry', - 'land-use', - 'land-change', - 'mining', - 'oil-and-gas', - 'transportation', - 'urban-planning', - 'sensors', - 'community-organizing' - ] + @topics = %w( + agriculture + drinking-water + fracking + indoor-air + chemicals + industry + land-use + land-change + mining + oil-and-gas + transportation + urban-planning + sensors + community-organizing + ) render template: 'wiki/methods' end