Skip to content

Commit

Permalink
Fix rubocop offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
serg-kovalev committed Feb 8, 2023
1 parent 03a9f05 commit 210d552
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 36 deletions.
25 changes: 12 additions & 13 deletions lib/generators/scenic/materializable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 18 additions & 12 deletions lib/generators/scenic/model/model_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/scenic/adapters/postgres.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 2 additions & 5 deletions lib/scenic/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,15 @@ 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|
case ignored
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
Expand Down
10 changes: 5 additions & 5 deletions lib/scenic/statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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.
#
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 210d552

Please sign in to comment.