From 5863bee83cfaa4e7fa73bbf571b5071b5ff57ea2 Mon Sep 17 00:00:00 2001 From: Siarhei Kavaliou Date: Wed, 8 Feb 2023 16:43:33 +0300 Subject: [PATCH] Fix rubocop offenses --- lib/generators/scenic/materializable.rb | 25 ++++++++-------- .../scenic/model/model_generator.rb | 30 +++++++++++-------- lib/scenic/adapters/postgres.rb | 3 +- lib/scenic/schema_dumper.rb | 7 ++--- lib/scenic/statements.rb | 10 +++---- spec/integration/revert_spec.rb | 2 +- spec/scenic/adapters/postgres_spec.rb | 9 +++--- spec/scenic/command_recorder_spec.rb | 2 +- spec/scenic/schema_dumper_spec.rb | 22 ++++++-------- 9 files changed, 55 insertions(+), 55 deletions(-) diff --git a/lib/generators/scenic/materializable.rb b/lib/generators/scenic/materializable.rb index 2fb1b3fe..7e7a6cdf 100644 --- a/lib/generators/scenic/materializable.rb +++ b/lib/generators/scenic/materializable.rb @@ -6,21 +6,20 @@ module Materializable included do class_option :materialized, - type: :boolean, - required: false, - desc: "Makes the view materialized", - default: false + type: :boolean, + required: false, + desc: "Makes the view materialized", + default: false class_option :no_data, - type: :boolean, - required: false, - desc: "Adds WITH NO DATA when materialized view " \ - "creates/updates", - default: false + type: :boolean, + required: false, + desc: "Adds WITH NO DATA when materialized view creates/updates", + default: false class_option :replace, - type: :boolean, - required: false, - desc: "Uses replace_view instead of update_view", - default: false + type: :boolean, + required: false, + desc: "Uses replace_view instead of update_view", + default: false end private diff --git a/lib/generators/scenic/model/model_generator.rb b/lib/generators/scenic/model/model_generator.rb index d47ac6fe..41627fcc 100644 --- a/lib/generators/scenic/model/model_generator.rb +++ b/lib/generators/scenic/model/model_generator.rb @@ -12,17 +12,16 @@ class ModelGenerator < Rails::Generators::NamedBase def invoke_rails_model_generator invoke "model", - [file_path.singularize], - options.merge( - fixture_replacement: false, - migration: false, - ) + [file_path.singularize], + options.merge( + fixture_replacement: false, + migration: false, + ) end def inject_model_methods if materialized? && generating? - inject_into_class "app/models/#{file_path.singularize}.rb", - class_name do + inject_into_class "app/models/#{file_path.singularize}.rb", class_name do evaluate_template("model.erb") end end @@ -39,12 +38,19 @@ def evaluate_template(source) context = instance_eval("binding", __FILE__, __LINE__) if ERB.instance_method(:initialize).parameters.assoc(:key) # Ruby 2.6+ + erb = ERB.new( + ::File.binread(source), + trim_mode: "-", + eoutvar: "@output_buffer", + ) + else + erb = ERB.new( + ::File.binread(source), + nil, + "-", + "@output_buffer", + ) end - erb = ERB.new( - ::File.binread(source), - trim_mode: "-", - eoutvar: "@output_buffer", - ) erb.result(context) end diff --git a/lib/scenic/adapters/postgres.rb b/lib/scenic/adapters/postgres.rb index 83ff5ada..2da95a93 100644 --- a/lib/scenic/adapters/postgres.rb +++ b/lib/scenic/adapters/postgres.rb @@ -196,7 +196,8 @@ def update_materialized_view(name, sql_definition, no_data: false) def drop_materialized_view(name, if_exists: false) raise_unless_materialized_views_supported definition_if_exists = if_exists ? "IF EXISTS " : "" - execute "DROP MATERIALIZED VIEW #{definition_if_exists}#{quote_table_name(name)};" + execute "DROP MATERIALIZED VIEW #{definition_if_exists}" \ + "#{quote_table_name(name)};" end # Refreshes a materialized view from its SQL schema. diff --git a/lib/scenic/schema_dumper.rb b/lib/scenic/schema_dumper.rb index 319b65f6..68aab1b2 100644 --- a/lib/scenic/schema_dumper.rb +++ b/lib/scenic/schema_dumper.rb @@ -27,8 +27,7 @@ def dumpable_views_in_database end end - unless ActiveRecord::SchemaDumper.private_instance_methods(false) - .include?(:ignored?) + unless ActiveRecord::SchemaDumper.private_instance_methods(false).include?(:ignored?) # This method will be present in Rails 4.2.0 and can be removed then. def ignored?(table_name) ["schema_migrations", ignore_tables].flatten.any? do |ignored| @@ -36,9 +35,7 @@ def ignored?(table_name) when String then remove_prefix_and_suffix(table_name) == ignored when Regexp then remove_prefix_and_suffix(table_name) =~ ignored else - raise StandardError, - "ActiveRecord::SchemaDumper.ignore_tables accepts an array " \ - "of String and / or Regexp values." + raise StandardError, "ActiveRecord::SchemaDumper.ignore_tables accepts an array of String and / or Regexp values." end end end diff --git a/lib/scenic/statements.rb b/lib/scenic/statements.rb index a43af466..e65e974e 100644 --- a/lib/scenic/statements.rb +++ b/lib/scenic/statements.rb @@ -48,7 +48,7 @@ def create_view(name, version: nil, sql_definition: nil, ) else Scenic.database.create_view(name, sql_definition, - if_not_exists: if_not_exists) + if_not_exists: if_not_exists) end end @@ -67,6 +67,7 @@ def create_view(name, version: nil, sql_definition: nil, # @example Drop a view, rolling back to version 3 on rollback # drop_view(:users_who_recently_logged_in, revert_to_version: 3) # + # rubocop:disable Lint/UnusedMethodArgument def drop_view(name, revert_to_version: nil, materialized: false, if_exists: false) if materialized @@ -75,6 +76,7 @@ def drop_view(name, revert_to_version: nil, materialized: false, Scenic.database.drop_view(name, if_exists: if_exists) end end + # rubocop:enable Lint/UnusedMethodArgument # Update a database view to a new version. # @@ -96,8 +98,7 @@ def drop_view(name, revert_to_version: nil, materialized: false, # @example # update_view :engagement_reports, version: 3, revert_to_version: 2 # - def update_view(name, version: nil, sql_definition: nil, - revert_to_version: nil, materialized: false) + def update_view(name, version: nil, sql_definition: nil, revert_to_version: nil, materialized: false) if version.blank? && sql_definition.blank? raise( ArgumentError, @@ -141,8 +142,7 @@ def update_view(name, version: nil, sql_definition: nil, # @example # replace_view :engagement_reports, version: 3, revert_to_version: 2 # - def replace_view(name, version: nil, revert_to_version: nil, - materialized: false) + def replace_view(name, version: nil, revert_to_version: nil, materialized: false) if version.blank? raise ArgumentError, "version is required" end diff --git a/spec/integration/revert_spec.rb b/spec/integration/revert_spec.rb index 572fe84f..2131e16f 100644 --- a/spec/integration/revert_spec.rb +++ b/spec/integration/revert_spec.rb @@ -61,7 +61,7 @@ def migration_class end def run_migration(migration, directions) - silence_stream($stdout) do + silence_stream(STDOUT) do Array.wrap(directions).each do |direction| migration.migrate(direction) end diff --git a/spec/scenic/adapters/postgres_spec.rb b/spec/scenic/adapters/postgres_spec.rb index 78f728b1..fe6828ca 100644 --- a/spec/scenic/adapters/postgres_spec.rb +++ b/spec/scenic/adapters/postgres_spec.rb @@ -12,11 +12,12 @@ module Adapters expect(adapter.views.map(&:name)).to include("greetings") end - it "successfully creates a view with :if_not_exists if view does not exist" do + it "successfully creates a view with :if_not_exists if view " \ + "does not exist" do adapter = Postgres.new adapter.create_view("greetings", "SELECT text 'hi' AS greeting", - if_not_exists: true) + if_not_exists: true) expect(adapter.views.map(&:name)).to include("greetings") end @@ -27,7 +28,7 @@ module Adapters adapter.create_view("greetings", "SELECT text 'hi' AS greeting") expect { adapter.create_view("greetings", "SELECT text 'hi' AS greeting", - if_not_exists: true) + if_not_exists: true) } .not_to raise_error end @@ -146,7 +147,7 @@ module Adapters expect { adapter.drop_materialized_view("greetings", - if_exists: true) + if_exists: true) }.not_to raise_error end diff --git a/spec/scenic/command_recorder_spec.rb b/spec/scenic/command_recorder_spec.rb index 484ab9bd..edc1016f 100644 --- a/spec/scenic/command_recorder_spec.rb +++ b/spec/scenic/command_recorder_spec.rb @@ -24,7 +24,7 @@ recorder.revert { recorder.create_view :greetings, materialized: true } expect(recorder.commands).to eq [ - [:drop_view, [:greetings, { materialized: true }]], + [:drop_view, [:greetings, materialized: true]], ] end end diff --git a/spec/scenic/schema_dumper_spec.rb b/spec/scenic/schema_dumper_spec.rb index 4a138b11..893dd9f9 100644 --- a/spec/scenic/schema_dumper_spec.rb +++ b/spec/scenic/schema_dumper_spec.rb @@ -21,7 +21,7 @@ class SearchInAHaystack < ActiveRecord::Base Search.connection.drop_view :searches - silence_stream($stdout) { eval(output) } + silence_stream(STDOUT) { eval(output) } expect(Search.first.haystack).to eq "needle" end @@ -37,15 +37,14 @@ class SearchInAHaystack < ActiveRecord::Base expect(output).to include "~ '\\\\d+'::text" Search.connection.drop_view :searches - silence_stream($stdout) { eval(output) } + silence_stream(STDOUT) { eval(output) } expect(Search.first.haystack).to eq "needle" end it "dumps a create_view for a materialized view in the database" do view_definition = "SELECT 'needle'::text AS haystack" - Search.connection.create_view :searches, materialized: true, - sql_definition: view_definition + Search.connection.create_view :searches, materialized: true, sql_definition: view_definition stream = StringIO.new ActiveRecord::SchemaDumper.dump(Search.connection, stream) @@ -60,8 +59,7 @@ class SearchInAHaystack < ActiveRecord::Base it "dumps a create_view including namespace for a view in the database" do view_definition = "SELECT 'needle'::text AS haystack" Search.connection.execute "CREATE SCHEMA scenic; SET search_path TO scenic, public" - Search.connection.create_view :"scenic.searches", - sql_definition: view_definition + Search.connection.create_view :"scenic.searches", sql_definition: view_definition stream = StringIO.new ActiveRecord::SchemaDumper.dump(Search.connection, stream) @@ -76,8 +74,7 @@ class SearchInAHaystack < ActiveRecord::Base it "handles active record table name prefixes and suffixes" do with_affixed_tables(prefix: "a_", suffix: "_z") do view_definition = "SELECT 'needle'::text AS haystack" - Search.connection.create_view :a_searches_z, - sql_definition: view_definition + Search.connection.create_view :a_searches_z, sql_definition: view_definition stream = StringIO.new ActiveRecord::SchemaDumper.dump(Search.connection, stream) @@ -105,8 +102,7 @@ class SearchInAHaystack < ActiveRecord::Base context "with views using unexpected characters in name" do it "dumps a create_view for a view in the database" do view_definition = "SELECT 'needle'::text AS haystack" - Search.connection.create_view '"search in a haystack"', - sql_definition: view_definition + Search.connection.create_view '"search in a haystack"', sql_definition: view_definition stream = StringIO.new ActiveRecord::SchemaDumper.dump(Search.connection, stream) @@ -117,7 +113,7 @@ class SearchInAHaystack < ActiveRecord::Base Search.connection.drop_view :'"search in a haystack"' - silence_stream($stdout) { eval(output) } + silence_stream(STDOUT) { eval(output) } expect(SearchInAHaystack.take.haystack).to eq "needle" end @@ -130,7 +126,7 @@ class SearchInAHaystack < ActiveRecord::Base "CREATE SCHEMA scenic; SET search_path TO scenic, public", ) Search.connection.create_view 'scenic."search in a haystack"', - sql_definition: view_definition + sql_definition: view_definition stream = StringIO.new ActiveRecord::SchemaDumper.dump(Search.connection, stream) @@ -141,7 +137,7 @@ class SearchInAHaystack < ActiveRecord::Base Search.connection.drop_view :'scenic."search in a haystack"' - silence_stream($stdout) { eval(output) } + silence_stream(STDOUT) { eval(output) } expect(SearchInAHaystack.take.haystack).to eq "needle" end