rename lambda references to use shorthand syntax

This commit is contained in:
Albert Yi
2018-05-10 11:18:02 -07:00
parent 320f1a426e
commit 72f319ccf3
27 changed files with 69 additions and 69 deletions

View File

@@ -24,7 +24,7 @@ class ApplicationController < ActionController::Base
# This is raised on requests to `/blah.js`. Rails has already rendered StaticController#not_found
# here, so calling `rescue_exception` would cause a double render error.
rescue_from ActionController::InvalidCrossOriginRequest, :with => lambda {}
rescue_from ActionController::InvalidCrossOriginRequest, with: -> {}
protected

View File

@@ -14,7 +14,7 @@ module WikiPageVersionsHelper
cbo = Diff::LCS::ContextDiffCallbacks.new
diffs = thisarr.diff(otharr, cbo)
escape_html = lambda {|str| str.gsub(/&/,'&amp;').gsub(/</,'&lt;').gsub(/>/,'&gt;')}
escape_html = ->(str) {str.gsub(/&/,'&amp;').gsub(/</,'&lt;').gsub(/>/,'&gt;')}
output = thisarr
output.each { |q| q.replace(escape_html[q]) }

View File

@@ -16,16 +16,16 @@ class Artist < ApplicationRecord
belongs_to_creator
has_many :members, :class_name => "Artist", :foreign_key => "group_name", :primary_key => "name"
has_many :urls, :dependent => :destroy, :class_name => "ArtistUrl"
has_many :versions, lambda {order("artist_versions.id ASC")}, :class_name => "ArtistVersion"
has_many :versions, -> {order("artist_versions.id ASC")}, :class_name => "ArtistVersion"
has_one :wiki_page, :foreign_key => "title", :primary_key => "name"
has_one :tag_alias, :foreign_key => "antecedent_name", :primary_key => "name"
has_one :tag, :foreign_key => "name", :primary_key => "name"
attribute :notes, :string
scope :active, lambda { where(is_active: true) }
scope :deleted, lambda { where(is_active: false) }
scope :banned, lambda { where(is_banned: true) }
scope :unbanned, lambda { where(is_banned: false) }
scope :active, -> { where(is_active: true) }
scope :deleted, -> { where(is_active: false) }
scope :banned, -> { where(is_banned: true) }
scope :unbanned, -> { where(is_banned: false) }
module UrlMethods
extend ActiveSupport::Concern

View File

@@ -6,8 +6,8 @@ class ArtistCommentary < ApplicationRecord
before_validation :trim_whitespace
validates_uniqueness_of :post_id
belongs_to :post, required: true
has_many :versions, lambda {order("artist_commentary_versions.id ASC")}, :class_name => "ArtistCommentaryVersion", :dependent => :destroy, :foreign_key => :post_id, :primary_key => :post_id
has_one :previous_version, lambda {order(id: :desc)}, :class_name => "ArtistCommentaryVersion", :foreign_key => :post_id, :primary_key => :post_id
has_many :versions, -> {order("artist_commentary_versions.id ASC")}, :class_name => "ArtistCommentaryVersion", :dependent => :destroy, :foreign_key => :post_id, :primary_key => :post_id
has_one :previous_version, -> {order(id: :desc)}, :class_name => "ArtistCommentaryVersion", :foreign_key => :post_id, :primary_key => :post_id
after_save :create_version
after_commit :tag_post

View File

@@ -1,7 +1,7 @@
class ArtistCommentaryVersion < ApplicationRecord
belongs_to :post
belongs_to_updater
scope :for_user, lambda {|user_id| where("updater_id = ?", user_id)}
scope :for_user, ->(user_id) {where("updater_id = ?", user_id)}
def self.search(params)
q = super

View File

@@ -8,7 +8,7 @@ class BulkUpdateRequest < ApplicationRecord
validates_presence_of :user
validates_presence_of :script
validates_presence_of :title, :if => lambda {|rec| rec.forum_topic_id.blank?}
validates_presence_of :title, if: ->(rec) {rec.forum_topic_id.blank?}
validates_inclusion_of :status, :in => %w(pending approved rejected)
validate :script_formatted_correctly
validate :forum_topic_id_not_invalid
@@ -17,10 +17,10 @@ class BulkUpdateRequest < ApplicationRecord
before_validation :normalize_text
after_create :create_forum_topic
scope :pending_first, lambda { order(Arel.sql("(case status when 'pending' then 0 when 'approved' then 1 else 2 end)")) }
scope :pending, ->{where(status: "pending")}
scope :expired, ->{where("created_at < ?", TagRelationship::EXPIRY.days.ago)}
scope :old, ->{where("created_at between ? and ?", TagRelationship::EXPIRY.days.ago, TagRelationship::EXPIRY_WARNING.days.ago)}
scope :pending_first, -> { order(Arel.sql("(case status when 'pending' then 0 when 'approved' then 1 else 2 end)")) }
scope :pending, -> {where(status: "pending")}
scope :expired, -> {where("created_at < ?", TagRelationship::EXPIRY.days.ago)}
scope :old, -> {where("created_at between ? and ?", TagRelationship::EXPIRY.days.ago, TagRelationship::EXPIRY_WARNING.days.ago)}
module SearchMethods
def default_order

View File

@@ -9,17 +9,17 @@ class Comment < ApplicationRecord
belongs_to_updater
has_many :votes, :class_name => "CommentVote", :dependent => :destroy
after_create :update_last_commented_at_on_create
after_update(:if => lambda {|rec| (!rec.is_deleted? || !rec.saved_change_to_is_deleted?) && CurrentUser.id != rec.creator_id}) do |rec|
after_update(:if => ->(rec) {(!rec.is_deleted? || !rec.saved_change_to_is_deleted?) && CurrentUser.id != rec.creator_id}) do |rec|
ModAction.log("comment ##{rec.id} updated by #{CurrentUser.name}",:comment_update)
end
after_save :update_last_commented_at_on_destroy, :if => lambda {|rec| rec.is_deleted? && rec.saved_change_to_is_deleted?}
after_save(:if => lambda {|rec| rec.is_deleted? && rec.saved_change_to_is_deleted? && CurrentUser.id != rec.creator_id}) do |rec|
after_save :update_last_commented_at_on_destroy, :if => ->(rec) {rec.is_deleted? && rec.saved_change_to_is_deleted?}
after_save(:if => ->(rec) {rec.is_deleted? && rec.saved_change_to_is_deleted? && CurrentUser.id != rec.creator_id}) do |rec|
ModAction.log("comment ##{rec.id} deleted by #{CurrentUser.name}",:comment_delete)
end
mentionable(
:message_field => :body,
:title => lambda {|user_name| "#{creator_name} mentioned you in a comment on post ##{post_id}"},
:body => lambda {|user_name| "@#{creator_name} mentioned you in a \"comment\":/posts/#{post_id}#comment-#{id} on post ##{post_id}:\n\n[quote]\n#{DText.excerpt(body, "@"+user_name)}\n[/quote]\n"},
:title => ->(user_name) {"#{creator_name} mentioned you in a comment on post ##{post_id}"},
:body => ->(user_name) {"@#{creator_name} mentioned you in a \"comment\":/posts/#{post_id}#comment-#{id} on post ##{post_id}:\n\n[quote]\n#{DText.excerpt(body, "@"+user_name)}\n[/quote]\n"},
)
module SearchMethods

View File

@@ -1,7 +1,7 @@
class Favorite < ApplicationRecord
belongs_to :post
belongs_to :user
scope :for_user, lambda {|user_id| where("user_id % 100 = #{user_id.to_i % 100} and user_id = #{user_id.to_i}")}
scope :for_user, ->(user_id) {where("user_id % 100 = #{user_id.to_i % 100} and user_id = #{user_id.to_i}")}
def self.add(post:, user:)
Favorite.transaction do

View File

@@ -19,16 +19,16 @@ class ForumPost < ApplicationRecord
validate :topic_is_not_restricted, :on => :create
before_destroy :validate_topic_is_unlocked
after_save :delete_topic_if_original_post
after_update(:if => lambda {|rec| rec.updater_id != rec.creator_id}) do |rec|
after_update(:if => ->(rec) {rec.updater_id != rec.creator_id}) do |rec|
ModAction.log("#{CurrentUser.name} updated forum ##{rec.id}",:forum_post_update)
end
after_destroy(:if => lambda {|rec| rec.updater_id != rec.creator_id}) do |rec|
after_destroy(:if => ->(rec) {rec.updater_id != rec.creator_id}) do |rec|
ModAction.log("#{CurrentUser.name} deleted forum ##{rec.id}",:forum_post_delete)
end
mentionable(
:message_field => :body,
:title => lambda {|user_name| %{#{creator_name} mentioned you in topic ##{topic_id} (#{topic.title})}},
:body => lambda {|user_name| %{@#{creator_name} mentioned you in topic ##{topic_id} ("#{topic.title}":[/forum_topics/#{topic_id}?page=#{forum_topic_page}]):\n\n[quote]\n#{DText.excerpt(body, "@"+user_name)}\n[/quote]\n}},
:title => ->(user_name) {%{#{creator_name} mentioned you in topic ##{topic_id} (#{topic.title})}},
:body => ->(user_name) {%{@#{creator_name} mentioned you in topic ##{topic_id} ("#{topic.title}":[/forum_topics/#{topic_id}?page=#{forum_topic_page}]):\n\n[quote]\n#{DText.excerpt(body, "@"+user_name)}\n[/quote]\n}},
)
module SearchMethods

View File

@@ -13,8 +13,8 @@ class ForumTopic < ApplicationRecord
belongs_to_creator
belongs_to_updater
has_many :posts, lambda {order("forum_posts.id asc")}, :class_name => "ForumPost", :foreign_key => "topic_id", :dependent => :destroy
has_one :original_post, lambda {order("forum_posts.id asc")}, class_name: "ForumPost", foreign_key: "topic_id", inverse_of: :topic
has_many :posts, -> {order("forum_posts.id asc")}, :class_name => "ForumPost", :foreign_key => "topic_id", :dependent => :destroy
has_one :original_post, -> {order("forum_posts.id asc")}, class_name: "ForumPost", foreign_key: "topic_id", inverse_of: :topic
has_many :subscriptions, :class_name => "ForumSubscription"
before_validation :initialize_is_deleted, :on => :create
validates_presence_of :title, :creator_id
@@ -24,7 +24,7 @@ class ForumTopic < ApplicationRecord
validates :title, :length => {:maximum => 255}
accepts_nested_attributes_for :original_post
after_update :update_orignal_post
after_save(:if => lambda {|rec| rec.is_locked? && rec.saved_change_to_is_locked?}) do |rec|
after_save(:if => ->(rec) {rec.is_locked? && rec.saved_change_to_is_locked?}) do |rec|
ModAction.log("locked forum topic ##{id} (title: #{title})",:forum_topic_lock)
end

View File

@@ -3,7 +3,7 @@ class IpBan < ApplicationRecord
belongs_to_creator
validates_presence_of :reason, :creator, :ip_addr
validates_format_of :ip_addr, :with => IP_ADDR_REGEX
validates_uniqueness_of :ip_addr, :if => lambda {|rec| rec.ip_addr =~ IP_ADDR_REGEX}
validates_uniqueness_of :ip_addr, :if => ->(rec) {rec.ip_addr =~ IP_ADDR_REGEX}
after_create do |rec|
ModAction.log("#{CurrentUser.name} created ip ban for #{rec.ip_addr}",:ip_ban_create)
end

View File

@@ -1,5 +1,5 @@
class NewsUpdate < ApplicationRecord
belongs_to_creator
belongs_to_updater
scope :recent, lambda {where("created_at >= ?", 2.weeks.ago).order("created_at desc").limit(5)}
scope :recent, -> {where("created_at >= ?", 2.weeks.ago).order("created_at desc").limit(5)}
end

View File

@@ -7,7 +7,7 @@ class Note < ApplicationRecord
belongs_to :post
belongs_to_creator
belongs_to_updater
has_many :versions, lambda {order("note_versions.id ASC")}, :class_name => "NoteVersion", :dependent => :destroy
has_many :versions, -> {order("note_versions.id ASC")}, :class_name => "NoteVersion", :dependent => :destroy
validates_presence_of :post_id, :creator_id, :updater_id, :x, :y, :width, :height, :body
validate :post_must_exist
validate :note_within_image

View File

@@ -1,6 +1,6 @@
class NoteVersion < ApplicationRecord
belongs_to_updater :counter_cache => "note_update_count"
scope :for_user, lambda {|user_id| where("updater_id = ?", user_id)}
scope :for_user, ->(user_id) {where("updater_id = ?", user_id)}
def self.search(params)
q = super

View File

@@ -52,8 +52,8 @@ class Post < ApplicationRecord
has_many :appeals, :class_name => "PostAppeal", :dependent => :destroy
has_many :votes, :class_name => "PostVote", :dependent => :destroy
has_many :notes, :dependent => :destroy
has_many :comments, lambda {includes(:creator, :updater).order("comments.id")}, :dependent => :destroy
has_many :children, lambda {order("posts.id")}, :class_name => "Post", :foreign_key => "parent_id"
has_many :comments, -> {includes(:creator, :updater).order("comments.id")}, :dependent => :destroy
has_many :children, -> {order("posts.id")}, :class_name => "Post", :foreign_key => "parent_id"
has_many :approvals, :class_name => "PostApproval", :dependent => :destroy
has_many :disapprovals, :class_name => "PostDisapproval", :dependent => :destroy
has_many :favorites
@@ -85,7 +85,7 @@ class Post < ApplicationRecord
end
if PostArchive.enabled?
has_many :versions, lambda {order("post_versions.updated_at ASC")}, :class_name => "PostArchive", :dependent => :destroy
has_many :versions, -> {order("post_versions.updated_at ASC")}, :class_name => "PostArchive", :dependent => :destroy
end
module FileMethods

View File

@@ -7,10 +7,10 @@ class PostDisapproval < ApplicationRecord
validates_uniqueness_of :post_id, :scope => [:user_id], :message => "have already hidden this post"
validates_inclusion_of :reason, :in => %w(legacy breaks_rules poor_quality disinterest)
scope :with_message, lambda {where("message is not null and message <> ''")}
scope :breaks_rules, lambda {where(:reason => "breaks_rules")}
scope :poor_quality, lambda {where(:reason => "poor_quality")}
scope :disinterest, lambda {where(:reason => ["disinterest", "legacy"])}
scope :with_message, -> {where("message is not null and message <> ''")}
scope :breaks_rules, -> {where(:reason => "breaks_rules")}
scope :poor_quality, -> {where(:reason => "poor_quality")}
scope :disinterest, -> {where(:reason => ["disinterest", "legacy"])}
def initialize_attributes
self.user_id ||= CurrentUser.user.id

View File

@@ -19,9 +19,9 @@ class PostFlag < ApplicationRecord
before_save :update_post
attr_accessor :is_deletion
scope :by_users, lambda { where.not(creator: User.system) }
scope :by_system, lambda { where(creator: User.system) }
scope :in_cooldown, lambda { by_users.where("created_at >= ?", COOLDOWN_PERIOD.ago) }
scope :by_users, -> { where.not(creator: User.system) }
scope :by_system, -> { where(creator: User.system) }
scope :in_cooldown, -> { by_users.where("created_at >= ?", COOLDOWN_PERIOD.ago) }
module SearchMethods
def reason_matches(query)

View File

@@ -52,7 +52,7 @@ class SavedSearch < ApplicationRecord
after_save {|rec| Cache.delete(SavedSearch.cache_key(rec.user_id))}
after_destroy {|rec| Cache.delete(SavedSearch.cache_key(rec.user_id))}
before_validation :normalize
scope :labeled, lambda {|label| where("labels @> string_to_array(?, '~~~~')", label)}
scope :labeled, ->(label) { where("labels @> string_to_array(?, '~~~~')", label)}
def self.normalize_label(label)
label.to_s.strip.downcase.gsub(/[[:space:]]/, "_")

View File

@@ -5,15 +5,15 @@ class Tag < ApplicationRecord
SUBQUERY_METATAGS = "commenter|comm|noter|noteupdater|artcomm|flagger|-flagger|appealer|-appealer"
has_one :wiki_page, :foreign_key => "title", :primary_key => "name"
has_one :artist, :foreign_key => "name", :primary_key => "name"
has_one :antecedent_alias, lambda {active}, :class_name => "TagAlias", :foreign_key => "antecedent_name", :primary_key => "name"
has_many :consequent_aliases, lambda {active}, :class_name => "TagAlias", :foreign_key => "consequent_name", :primary_key => "name"
has_many :antecedent_implications, lambda {active}, :class_name => "TagImplication", :foreign_key => "antecedent_name", :primary_key => "name"
has_many :consequent_implications, lambda {active}, :class_name => "TagImplication", :foreign_key => "consequent_name", :primary_key => "name"
has_one :antecedent_alias, -> {active}, :class_name => "TagAlias", :foreign_key => "antecedent_name", :primary_key => "name"
has_many :consequent_aliases, -> {active}, :class_name => "TagAlias", :foreign_key => "consequent_name", :primary_key => "name"
has_many :antecedent_implications, -> {active}, :class_name => "TagImplication", :foreign_key => "antecedent_name", :primary_key => "name"
has_many :consequent_implications, -> {active}, :class_name => "TagImplication", :foreign_key => "consequent_name", :primary_key => "name"
validates :name, uniqueness: true, tag_name: true, on: :create
validates_inclusion_of :category, in: TagCategory.category_ids
after_save :update_category_cache_for_all, if: lambda {|rec| rec.saved_change_to_attribute?(:category)}
after_save :update_category_cache_for_all, if: ->(rec) { rec.saved_change_to_attribute?(:category)}
module ApiMethods
def to_legacy_json

View File

@@ -21,9 +21,9 @@ class TagRelationship < ApplicationRecord
before_validation :normalize_names
validates_format_of :status, :with => /\A(active|deleted|pending|processing|queued|error: .*)\Z/
validates_presence_of :creator_id, :antecedent_name, :consequent_name
validates :creator, presence: { message: "must exist" }, if: lambda { creator_id.present? }
validates :approver, presence: { message: "must exist" }, if: lambda { approver_id.present? }
validates :forum_topic, presence: { message: "must exist" }, if: lambda { forum_topic_id.present? }
validates :creator, presence: { message: "must exist" }, if: -> { creator_id.present? }
validates :approver, presence: { message: "must exist" }, if: -> { approver_id.present? }
validates :forum_topic, presence: { message: "must exist" }, if: -> { forum_topic_id.present? }
def initialize_creator
self.creator_id = CurrentUser.user.id

View File

@@ -68,15 +68,15 @@ class User < ApplicationRecord
after_initialize :initialize_attributes, if: :new_record?
validates :name, user_name: true, on: :create
validates_uniqueness_of :email, :case_sensitive => false, :if => lambda {|rec| rec.email.present? && rec.saved_change_to_email? }
validates_length_of :password, :minimum => 5, :if => lambda {|rec| rec.new_record? || rec.password.present?}
validates_uniqueness_of :email, :case_sensitive => false, :if => ->(rec) { rec.email.present? && rec.saved_change_to_email? }
validates_length_of :password, :minimum => 5, :if => ->(rec) { rec.new_record? || rec.password.present?}
validates_inclusion_of :default_image_size, :in => %w(large original)
validates_inclusion_of :per_page, :in => 1..100
validates_confirmation_of :password
validates_presence_of :email, :if => lambda {|rec| rec.new_record? && Danbooru.config.enable_email_verification?}
validates_presence_of :email, :if => ->(rec) { rec.new_record? && Danbooru.config.enable_email_verification?}
validates_presence_of :comment_threshold
validate :validate_ip_addr_is_not_banned, :on => :create
validate :validate_sock_puppets, :on => :create, :if => lambda { Danbooru.config.enable_sock_puppet_validation? }
validate :validate_sock_puppets, :on => :create, :if => -> { Danbooru.config.enable_sock_puppet_validation? }
before_validation :normalize_blacklisted_tags
before_validation :set_per_page
before_validation :normalize_email
@@ -92,19 +92,19 @@ class User < ApplicationRecord
has_many :post_approvals, :dependent => :destroy
has_many :post_disapprovals, :dependent => :destroy
has_many :post_votes
has_many :bans, lambda {order("bans.id desc")}
has_one :recent_ban, lambda {order("bans.id desc")}, :class_name => "Ban"
has_many :bans, -> {order("bans.id desc")}
has_one :recent_ban, -> {order("bans.id desc")}, :class_name => "Ban"
has_one :api_key
has_one :dmail_filter
has_one :super_voter
has_one :token_bucket
has_many :note_versions, :foreign_key => "updater_id"
has_many :dmails, lambda {order("dmails.id desc")}, :foreign_key => "owner_id"
has_many :dmails, -> {order("dmails.id desc")}, :foreign_key => "owner_id"
has_many :saved_searches
has_many :forum_posts, lambda {order("forum_posts.created_at, forum_posts.id")}, :foreign_key => "creator_id"
has_many :user_name_change_requests, lambda {visible.order("user_name_change_requests.created_at desc")}
has_many :favorite_groups, lambda {order(name: :asc)}, foreign_key: :creator_id
has_many :forum_posts, -> {order("forum_posts.created_at, forum_posts.id")}, :foreign_key => "creator_id"
has_many :user_name_change_requests, -> {visible.order("user_name_change_requests.created_at desc")}
has_many :favorite_groups, -> {order(name: :asc)}, foreign_key: :creator_id
belongs_to :inviter, class_name: "User", optional: true
after_update :create_mod_action
accepts_nested_attributes_for :dmail_filter

View File

@@ -8,10 +8,10 @@ class UserFeedback < ApplicationRecord
validate :creator_is_gold
validate :user_is_not_creator
after_create :create_dmail
after_update(:if => lambda {|rec| CurrentUser.id != rec.creator_id}) do |rec|
after_update(:if => ->(rec) { CurrentUser.id != rec.creator_id}) do |rec|
ModAction.log(%{#{CurrentUser.name} updated user feedback for "#{rec.user_name}":/users/#{rec.user_id}},:user_feedback_update)
end
after_destroy(:if => lambda {|rec| CurrentUser.id != rec.creator_id}) do |rec|
after_destroy(:if => ->(rec) { CurrentUser.id != rec.creator_id}) do |rec|
ModAction.log(%{#{CurrentUser.name} deleted user feedback for "#{rec.user_name}":/users/#{rec.user_id}},:user_feedback_delete)
end

View File

@@ -13,8 +13,8 @@ class WikiPage < ApplicationRecord
validate :validate_not_locked
attr_accessor :skip_secondary_validations
has_one :tag, :foreign_key => "name", :primary_key => "title"
has_one :artist, lambda {where(:is_active => true)}, :foreign_key => "name", :primary_key => "title"
has_many :versions, lambda {order("wiki_page_versions.id ASC")}, :class_name => "WikiPageVersion", :dependent => :destroy
has_one :artist, -> {where(:is_active => true)}, :foreign_key => "name", :primary_key => "title"
has_many :versions, -> {order("wiki_page_versions.id ASC")}, :class_name => "WikiPageVersion", :dependent => :destroy
module SearchMethods
def titled(title)

View File

@@ -19,7 +19,7 @@ class WikiPagePresenter
cbo = Diff::LCS::ContextDiffCallbacks.new
diffs = thisarr.diff(otharr, cbo)
escape_html = lambda {|str| str.gsub(/&/,'&amp;').gsub(/</,'&lt;').gsub(/>/,'&gt;')}
escape_html = ->(str) {str.gsub(/&/,'&amp;').gsub(/</,'&lt;').gsub(/>/,'&gt;')}
output = thisarr;
output.each { |q| q.replace(CGI.escape_html(q)) }