changeset 499:d2ef59c9a4b4

Use rails rubocop
author nanaya <me@nanaya.net>
date Sun, 15 Dec 2024 22:33:27 +0900
parents 300bac944674
children 062723d1f66c
files .rubocop.yml Gemfile Gemfile.lock app/controllers/pastes_controller.rb app/models/paste.rb config/config_init.rb config/initializers/session_store.rb config/initializers/wrap_parameters.rb config/routes.rb db/migrate/20120826180414_create_pastes.rb db/migrate/20120905072325_set_limit_on_paste.rb db/migrate/20120905074204_return_not_null_to_paste.rb db/migrate/20150916161622_strict_character_lengths.rb db/migrate/20180625074738_convert_timestamps_to_with_time_zone.rb test/controllers/pastes_controller_test.rb
diffstat 15 files changed, 91 insertions(+), 55 deletions(-) [+]
line wrap: on
line diff
--- a/.rubocop.yml	Sun Dec 15 22:31:14 2024 +0900
+++ b/.rubocop.yml	Sun Dec 15 22:33:27 2024 +0900
@@ -1,11 +1,3 @@
-AllCops:
-  Include:
-    - Gemfile
-    - config/local_config.rb.example
-  Exclude:
-    - bin/*
-    - config/local_config.rb
-    - db/schema.rb
-
-Rails:
-  Enabled: true
+# Omakase Ruby styling for Rails
+inherit_gem:
+  rubocop-rails-omakase: rubocop.yml
--- a/Gemfile	Sun Dec 15 22:31:14 2024 +0900
+++ b/Gemfile	Sun Dec 15 22:33:27 2024 +0900
@@ -9,7 +9,7 @@
 # Bundle edge Rails instead:
 # gem "rails", :git => "git://github.com/rails/rails.git"
 
-gem "pg", :platforms => :ruby
+gem "pg", platforms: :ruby
 
 # To use ActiveModel has_secure_password
 # gem "bcrypt-ruby", "~> 3.0.0"
@@ -23,4 +23,6 @@
 # To use debugger
 # gem "ruby-debug"
 
-gem "puma", :require => false
+gem "puma", require: false
+
+gem "rubocop-rails-omakase", require: false, group: [ :development ]
--- a/Gemfile.lock	Sun Dec 15 22:31:14 2024 +0900
+++ b/Gemfile.lock	Sun Dec 15 22:33:27 2024 +0900
@@ -72,6 +72,7 @@
       securerandom (>= 0.3)
       tzinfo (~> 2.0, >= 2.0.5)
       uri (>= 0.13.1)
+    ast (2.4.2)
     autoprefixer-rails (10.4.19.0)
       execjs (~> 2)
     base64 (0.2.0)
@@ -96,6 +97,8 @@
       reline (>= 0.4.2)
     jsbundling-rails (1.3.1)
       railties (>= 6.0.0)
+    json (2.9.0)
+    language_server-protocol (3.17.0.3)
     logger (1.6.3)
     loofah (2.23.1)
       crass (~> 1.0.2)
@@ -122,6 +125,10 @@
     nokogiri (1.17.2)
       mini_portile2 (~> 2.8.2)
       racc (~> 1.4)
+    parallel (1.26.3)
+    parser (3.3.6.0)
+      ast (~> 2.4.1)
+      racc
     pg (1.5.9)
     psych (5.2.1)
       date
@@ -165,11 +172,42 @@
       rake (>= 12.2)
       thor (~> 1.0, >= 1.2.2)
       zeitwerk (~> 2.6)
+    rainbow (3.1.1)
     rake (13.2.1)
     rdoc (6.9.0)
       psych (>= 4.0.0)
+    regexp_parser (2.9.3)
     reline (0.5.12)
       io-console (~> 0.5)
+    rubocop (1.69.2)
+      json (~> 2.3)
+      language_server-protocol (>= 3.17.0)
+      parallel (~> 1.10)
+      parser (>= 3.3.0.2)
+      rainbow (>= 2.2.2, < 4.0)
+      regexp_parser (>= 2.9.3, < 3.0)
+      rubocop-ast (>= 1.36.2, < 2.0)
+      ruby-progressbar (~> 1.7)
+      unicode-display_width (>= 2.4.0, < 4.0)
+    rubocop-ast (1.37.0)
+      parser (>= 3.3.1.0)
+    rubocop-minitest (0.36.0)
+      rubocop (>= 1.61, < 2.0)
+      rubocop-ast (>= 1.31.1, < 2.0)
+    rubocop-performance (1.23.0)
+      rubocop (>= 1.48.1, < 2.0)
+      rubocop-ast (>= 1.31.1, < 2.0)
+    rubocop-rails (2.27.0)
+      activesupport (>= 4.2.0)
+      rack (>= 1.1)
+      rubocop (>= 1.52.0, < 2.0)
+      rubocop-ast (>= 1.31.1, < 2.0)
+    rubocop-rails-omakase (1.0.0)
+      rubocop
+      rubocop-minitest
+      rubocop-performance
+      rubocop-rails
+    ruby-progressbar (1.13.0)
     sass-rails (6.0.0)
       sassc-rails (~> 2.1, >= 2.1.1)
     sassc (2.4.0)
@@ -194,6 +232,9 @@
     timeout (0.4.2)
     tzinfo (2.0.6)
       concurrent-ruby (~> 1.0)
+    unicode-display_width (3.1.2)
+      unicode-emoji (~> 4.0, >= 4.0.4)
+    unicode-emoji (4.0.4)
     uri (1.0.2)
     useragent (0.16.11)
     websocket-driver (0.7.6)
@@ -210,6 +251,7 @@
   pg
   puma
   rails (~> 8.0.0)
+  rubocop-rails-omakase
   sass-rails
   sprockets-rails
 
--- a/app/controllers/pastes_controller.rb	Sun Dec 15 22:31:14 2024 +0900
+++ b/app/controllers/pastes_controller.rb	Sun Dec 15 22:33:27 2024 +0900
@@ -6,9 +6,9 @@
     return head :not_found unless @paste
 
     if params[:id] != @paste.to_param
-      redirect_to :action => :show,
-        :id => @paste.to_param,
-        :format => params[:format]
+      redirect_to action: :show,
+        id: @paste.to_param,
+        format: params[:format]
       return
     end
 
@@ -33,21 +33,21 @@
   # POST /pastes.json
   # POST /pastes.txt
   def create
-    created, @paste, @fresh = Paste.graceful_create paste_params.merge(:ip => request.remote_ip)
+    created, @paste, @fresh = Paste.graceful_create paste_params.merge(ip: request.remote_ip)
 
     respond_to do |format|
       if created
         if @fresh
-          format.html { redirect_to @paste, :notice => "Paste was successfully created." }
-          format.json { render :json => @paste, :status => :created, :location => @paste }
+          format.html { redirect_to @paste, notice: "Paste was successfully created." }
+          format.json { render json: @paste, status: :created, location: @paste }
         else
           format.html { redirect_to paste_path(@paste) }
-          format.json { render :json => @paste }
+          format.json { render json: @paste }
         end
       else
         flash.now[:alert] = @paste.errors.full_messages.to_sentence
-        format.html { render :action => "new" }
-        format.json { render :json => @paste.errors, :status => :unprocessable_entity }
+        format.html { render action: "new" }
+        format.json { render json: @paste.errors, status: :unprocessable_entity }
       end
       format.txt
     end
@@ -56,7 +56,7 @@
   def destroy
     @paste = Paste.safe_find(params[:id])
     if @paste.safe_destroy(params[:paste][:key])
-      redirect_to root_path, :notice => "Paste ##{params[:id]} deleted"
+      redirect_to root_path, notice: "Paste ##{params[:id]} deleted"
     else
       flash.now[:alert] = @paste.errors.full_messages.to_sentence
       render :show
--- a/app/models/paste.rb	Sun Dec 15 22:31:14 2024 +0900
+++ b/app/models/paste.rb	Sun Dec 15 22:33:27 2024 +0900
@@ -1,14 +1,14 @@
 class Paste < ApplicationRecord
   attr_accessor :is_private
-  after_initialize :set_privacy, :if => :new_record?
+  after_initialize :set_privacy, if: :new_record?
 
   before_validation :paste_limit
   before_validation :convert_newlines
   before_validation :set_paste_hash
   before_validation :set_paste_key
   before_validation :set_paste_secret
-  validates :paste, :paste_hash, :key, :ip, :presence => true
-  validates :paste, :length => { :maximum => 1_000_000 }
+  validates :paste, :paste_hash, :key, :ip, presence: true
+  validates :paste, length: { maximum: 1_000_000 }
 
   def to_param
     path
@@ -17,7 +17,7 @@
   def self.safe_find(raw_id)
     /\A(?<id>[0-9]+)(?:-(?<secret>[0-9a-f]+))?\z/i =~ raw_id.to_s
 
-    find_by(:secret => secret.try(:downcase), :id => id)
+    find_by(secret: secret.try(:downcase), id: id)
   end
 
   def self.graceful_create(params)
@@ -28,11 +28,11 @@
     begin
       created = paste.save
     rescue ActiveRecord::RecordNotUnique
-      paste = find_by(:ip => paste.ip, :paste_hash => paste.paste_hash)
+      paste = find_by(ip: paste.ip, paste_hash: paste.paste_hash)
       fresh = false
     end
 
-    [created, paste, fresh]
+    [ created, paste, fresh ]
   end
 
   def paste_gzip=(paste)
@@ -55,7 +55,7 @@
   end
 
   def path
-    [id, secret.presence].compact.join("-")
+    [ id, secret.presence ].compact.join("-")
   end
 
   def set_paste_hash
@@ -79,7 +79,7 @@
   end
 
   def paste_limit
-    ip_post_recent_count = self.class.where(:ip => ip).where("created_at > ?", Time.zone.now - 1.hour).count
+    ip_post_recent_count = self.class.where(ip: ip).where("created_at > ?", Time.zone.now - 1.hour).count
     errors.add :base, :limit if ip_post_recent_count > 100
   end
 
--- a/config/config_init.rb	Sun Dec 15 22:31:14 2024 +0900
+++ b/config/config_init.rb	Sun Dec 15 22:33:27 2024 +0900
@@ -9,7 +9,7 @@
 $cfg[:standalone] = true if $cfg[:standalone].nil?
 $cfg[:log_to_stdout] = true if $cfg[:log_to_stdout].nil?
 
-$cfg[:bundler_groups] ||= [:default, env]
+$cfg[:bundler_groups] ||= [ :default, env ]
 $cfg[:bundler_groups] << "standalone" if $cfg[:standalone]
 
 $cfg.freeze
--- a/config/initializers/session_store.rb	Sun Dec 15 22:31:14 2024 +0900
+++ b/config/initializers/session_store.rb	Sun Dec 15 22:33:27 2024 +0900
@@ -1,3 +1,3 @@
 # Be sure to restart your server when you modify this file.
 
-Rails.application.config.session_store :cookie_store, :key => "_zeropaste_session"
+Rails.application.config.session_store :cookie_store, key: "_zeropaste_session"
--- a/config/initializers/wrap_parameters.rb	Sun Dec 15 22:31:14 2024 +0900
+++ b/config/initializers/wrap_parameters.rb	Sun Dec 15 22:33:27 2024 +0900
@@ -5,7 +5,7 @@
 
 # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
 ActiveSupport.on_load(:action_controller) do
-  wrap_parameters format: [:json]
+  wrap_parameters format: [ :json ]
 end
 
 # To enable root element in JSON for ActiveRecord objects.
--- a/config/routes.rb	Sun Dec 15 22:31:14 2024 +0900
+++ b/config/routes.rb	Sun Dec 15 22:33:27 2024 +0900
@@ -1,5 +1,5 @@
 Rails.application.routes.draw do
   post "(pastes)" => "pastes#create", :format => /(html|json|xml|txt)/, :as => "pastes"
-  resources :pastes, :only => [:show, :destroy], :path => "/"
-  root :to => "pastes#new"
+  resources :pastes, only: [ :show, :destroy ], path: "/"
+  root to: "pastes#new"
 end
--- a/db/migrate/20120826180414_create_pastes.rb	Sun Dec 15 22:31:14 2024 +0900
+++ b/db/migrate/20120826180414_create_pastes.rb	Sun Dec 15 22:33:27 2024 +0900
@@ -2,12 +2,12 @@
 class CreatePastes < ActiveRecord::Migration[4.2]
   def change
     create_table :pastes do |t|
-      t.string :ip, :null => false
-      t.text :paste, :null => false
-      t.string :hash, :null => false
+      t.string :ip, null: false
+      t.text :paste, null: false
+      t.string :hash, null: false
 
       t.timestamps
     end
-    add_index :pastes, [:ip, :hash], :unique => true
+    add_index :pastes, [ :ip, :hash ], unique: true
   end
 end
--- a/db/migrate/20120905072325_set_limit_on_paste.rb	Sun Dec 15 22:31:14 2024 +0900
+++ b/db/migrate/20120905072325_set_limit_on_paste.rb	Sun Dec 15 22:33:27 2024 +0900
@@ -2,8 +2,8 @@
 class SetLimitOnPaste < ActiveRecord::Migration[4.2]
   def change
     change_table :pastes do |t|
-      t.change :ip, :string, :limit => 50
-      t.change :paste_hash, :string, :limit => 150
+      t.change :ip, :string, limit: 50
+      t.change :paste_hash, :string, limit: 150
     end
   end
 end
--- a/db/migrate/20120905074204_return_not_null_to_paste.rb	Sun Dec 15 22:31:14 2024 +0900
+++ b/db/migrate/20120905074204_return_not_null_to_paste.rb	Sun Dec 15 22:33:27 2024 +0900
@@ -2,9 +2,9 @@
 class ReturnNotNullToPaste < ActiveRecord::Migration[4.2]
   def change
     change_table :pastes do |t|
-      t.change :ip, :string, :limit => 50, :null => false
-      t.change :paste_hash, :string, :limit => 150, :null => false
-      t.change :paste, :text, :null => false
+      t.change :ip, :string, limit: 50, null: false
+      t.change :paste_hash, :string, limit: 150, null: false
+      t.change :paste, :text, null: false
     end
   end
 end
--- a/db/migrate/20150916161622_strict_character_lengths.rb	Sun Dec 15 22:31:14 2024 +0900
+++ b/db/migrate/20150916161622_strict_character_lengths.rb	Sun Dec 15 22:33:27 2024 +0900
@@ -1,9 +1,9 @@
 class StrictCharacterLengths < ActiveRecord::Migration[4.2]
   def change
     change_table :pastes do |t|
-      t.change :key, :string, :limit => 100
-      t.change :secret, :string, :limit => 100
-      t.change :language, :string, :limit => 100
+      t.change :key, :string, limit: 100
+      t.change :secret, :string, limit: 100
+      t.change :language, :string, limit: 100
     end
   end
 end
--- a/db/migrate/20180625074738_convert_timestamps_to_with_time_zone.rb	Sun Dec 15 22:31:14 2024 +0900
+++ b/db/migrate/20180625074738_convert_timestamps_to_with_time_zone.rb	Sun Dec 15 22:33:27 2024 +0900
@@ -1,7 +1,7 @@
 class ConvertTimestampsToWithTimeZone < ActiveRecord::Migration[5.2]
   COLUMNS = {
-    ar_internal_metadata: [:created_at, :updated_at],
-    pastes: [:created_at, :updated_at],
+    ar_internal_metadata: [ :created_at, :updated_at ],
+    pastes: [ :created_at, :updated_at ]
   }
 
   def up
--- a/test/controllers/pastes_controller_test.rb	Sun Dec 15 22:31:14 2024 +0900
+++ b/test/controllers/pastes_controller_test.rb	Sun Dec 15 22:33:27 2024 +0900
@@ -3,7 +3,7 @@
 class PastesControllerTest < ActionController::TestCase
   test "creates paste from plaintext" do
     assert_difference("Paste.count", 1) do
-      post :create, :params => { "paste" => { "paste" => "here be paste" } }
+      post :create, params: { "paste" => { "paste" => "here be paste" } }
     end
 
     assert_response :redirect
@@ -11,7 +11,7 @@
 
   test "creates paste from gzip" do
     assert_difference("Paste.count", 1) do
-      post :create, :params => { "paste" => { "paste_gzip" => fixture_file_upload('hello_world.txt.gz') } }
+      post :create, params: { "paste" => { "paste_gzip" => fixture_file_upload("hello_world.txt.gz") } }
     end
 
     assert_response :redirect
@@ -19,7 +19,7 @@
 
   test "creates paste from base64 gzip" do
     assert_difference("Paste.count", 1) do
-      post :create, :params => { "paste" => { "paste_gzip_base64" => Base64.encode64(ActiveSupport::Gzip.compress("here be paste")) } }
+      post :create, params: { "paste" => { "paste_gzip_base64" => Base64.encode64(ActiveSupport::Gzip.compress("here be paste")) } }
     end
 
     assert_response :redirect
@@ -27,14 +27,14 @@
 
   test "filters spam" do
     assert_no_difference("Paste.count") do
-      post :create, :params => { "url1" => "http://hello.com", "paste" => { "paste" => "here be paste", "key" => "12341234" } }
+      post :create, params: { "url1" => "http://hello.com", "paste" => { "paste" => "here be paste", "key" => "12341234" } }
     end
 
     assert_response :success
   end
 
   test "shows paste" do
-    get :show, :params => { :id => pastes(:basic).id }
+    get :show, params: { id: pastes(:basic).id }
 
     assert_response :success
   end