Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rubocop: Inherit rules from cookpad/styleguide and auto-correct all #330

Merged
merged 4 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
public/packs
.env.docker
coverage
.rubocop-https?--*

.DS_Store
19 changes: 13 additions & 6 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
inherit_from:
- https://raw.githubusercontent.com/cookpad/styleguide/master/.rubocop.yml

inherit_mode:
merge:
- Exclude

require:
- rubocop-capybara
- rubocop-factory_bot
- rubocop-rails
- rubocop-rspec

AllCops:
Exclude:
- 'vendor/**/*'
- 'tmp/**/*'
- 'db/**/*'

Style:
Enabled: false

Layout/EmptyLineAfterGuardClause:
Enabled: false

Layout/LineLength:
Enabled: false

Expand Down
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ group :development do
gem 'ridgepole'
gem 'ruby-prof'
gem 'rubocop', require: false
gem 'rubocop-capybara', require: false
gem 'rubocop-factory_bot', require: false
gem 'rubocop-rails', require: false
gem 'rubocop-rspec', require: false
gem 'brakeman', require: false
gem 'foreman'
end
Expand Down
51 changes: 36 additions & 15 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ GEM
activerecord
kaminari-core (= 1.2.1)
kaminari-core (1.2.1)
language_server-protocol (3.17.0.3)
launchy (2.5.0)
addressable (~> 2.7)
loofah (2.10.0)
Expand Down Expand Up @@ -218,9 +219,10 @@ GEM
actionpack (>= 4.2)
omniauth (~> 2.0)
os (1.1.1)
parallel (1.20.1)
parser (3.0.2.0)
parallel (1.24.0)
parser (3.3.0.5)
ast (~> 2.4.1)
racc
pg (1.2.3)
presto-client (0.6.5)
faraday (~> 0.12)
Expand Down Expand Up @@ -277,21 +279,21 @@ GEM
method_source
rake (>= 0.13)
thor (~> 1.0)
rainbow (3.0.0)
rainbow (3.1.1)
rake (13.0.6)
ransack (2.4.2)
activerecord (>= 5.2.4)
activesupport (>= 5.2.4)
i18n
regexp_parser (2.1.1)
regexp_parser (2.9.0)
representable (3.1.1)
declarative (< 0.1.0)
trailblazer-option (>= 0.1.1, < 0.2.0)
uber (< 0.2.0)
retriable (3.1.2)
revision_plate (0.1.2)
rack
rexml (3.2.5)
rexml (3.2.6)
ridgepole (0.9.5)
activerecord (>= 5.1, < 6.2)
diffy
Expand All @@ -314,19 +316,34 @@ GEM
rspec-mocks (~> 3.10)
rspec-support (~> 3.10)
rspec-support (3.10.2)
rubocop (1.18.3)
rubocop (1.60.2)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.0.0.0)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.7.0, < 2.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.30.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.8.0)
parser (>= 3.0.1.1)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.30.0)
parser (>= 3.2.1.0)
rubocop-capybara (2.20.0)
rubocop (~> 1.41)
rubocop-factory_bot (2.25.1)
rubocop (~> 1.41)
rubocop-rails (2.23.1)
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.33.0, < 2.0)
rubocop-ast (>= 1.30.0, < 2.0)
rubocop-rspec (2.26.1)
rubocop (~> 1.40)
rubocop-capybara (~> 2.17)
rubocop-factory_bot (~> 2.22)
ruby-prof (1.4.3)
ruby-progressbar (1.11.0)
ruby-progressbar (1.13.0)
ruby_parser (3.16.0)
sexp_processor (~> 4.15, >= 4.15.1)
sexp_processor (4.15.3)
Expand Down Expand Up @@ -357,7 +374,7 @@ GEM
tzinfo (2.0.4)
concurrent-ruby (~> 1.0)
uber (0.1.0)
unicode-display_width (2.0.0)
unicode-display_width (2.5.0)
webrick (1.7.0)
websocket-driver (0.7.5)
websocket-extensions (>= 0.1.0)
Expand Down Expand Up @@ -410,10 +427,14 @@ DEPENDENCIES
rouge
rspec-rails
rubocop
rubocop-capybara
rubocop-factory_bot
rubocop-rails
rubocop-rspec
ruby-prof
silencer
simpacker
simplecov

BUNDLED WITH
2.2.3
2.4.22
8 changes: 4 additions & 4 deletions app/batches/import_data_source_definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ def self.run(data_source_name)
db_memo = DatabaseMemo.find_or_create_by!(name: data_source.name)
schema_memos = db_memo.schema_memos
linked_schema_names = schema_memos.where(linked: true).pluck(:name)
schema_memos.each {|memo| memo.linked = false }
schema_memos.each { |memo| memo.linked = false }

all_table_memos = schema_memos.map(&:table_memos).map(&:to_a).flatten
all_table_memos.each {|memo| memo.linked = false }
all_table_memos.each { |memo| memo.linked = false }

data_source_tables.group_by(&:schema_name).each do |schema_name, source_tables|
next unless linked_schema_names.include?(schema_name)
schema_memo = schema_memos.find {|memo| memo.name == schema_name }
schema_memo = schema_memos.find { |memo| memo.name == schema_name }
next if schema_memo.nil?
schema_memo.linked = true
begin
Expand All @@ -26,7 +26,7 @@ def self.run(data_source_name)
end
end

schema_memos.each {|memo| memo.save! if memo.has_changes_to_save? }
schema_memos.each { |memo| memo.save! if memo.has_changes_to_save? }
db_memos.save! if db_memo.has_changes_to_save?
end

Expand Down
6 changes: 3 additions & 3 deletions app/batches/import_schema_definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ def self.run(data_source_name, schema_name)
Rails.logger.info "[Start] Import definition of #{schema_name} schema in #{data_source_name}"

data_source = DataSource.find_by(name: data_source_name)
source_tables = data_source.data_source_tables.select {|table| table.schema_name == schema_name }
source_tables = data_source.data_source_tables.select { |table| table.schema_name == schema_name }

schema_memo = data_source.database_memo.schema_memos.find_by!(name: schema_name, linked: true)
table_memos = schema_memo.table_memos
table_memos.each {|memo| memo.linked = false }
table_memos.each { |memo| memo.linked = false }

if source_tables.empty?
schema_memo.linked = false
else
self.import_table_memos!(source_tables, table_memos)
end

table_memos.each {|memo| memo.save! if memo.has_changes_to_save? }
table_memos.each { |memo| memo.save! if memo.has_changes_to_save? }
schema_memo.save! if schema_memo.has_changes_to_save?

Rails.logger.info "[Update] #{schema_name} schema" if schema_memo.saved_changes?
Expand Down
2 changes: 1 addition & 1 deletion app/batches/import_schema_raw_datasets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def self.run(data_source_name, schema_name)
Rails.logger.info "[Start] Import dataset of #{schema_name} schema in #{data_source_name}"

data_source = DataSource.find_by(name: data_source_name)
source_tables = data_source.data_source_tables.select {|dst| dst.schema_name == schema_name }
source_tables = data_source.data_source_tables.select { |dst| dst.schema_name == schema_name }

schema_memo = data_source.database_memo.schema_memos.find_by!(name: schema_name, linked: true)

Expand Down
6 changes: 3 additions & 3 deletions app/batches/import_table_definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def self.run(data_source_name, schema_name, table_name)
Rails.logger.info "[Start] Import definition of #{schema_name}.#{table_name} table in #{data_source_name}"

data_source = DataSource.find_by(name: data_source_name)
source_table = data_source.data_source_tables.find {|dst| dst.full_table_name == "#{schema_name}.#{table_name}" }
source_table = data_source.data_source_tables.find { |dst| dst.full_table_name == "#{schema_name}.#{table_name}" }

schema_memo = data_source.database_memo.schema_memos.find_by!(name: schema_name, linked: true)
table_memo = schema_memo.table_memos.find_or_create_by!(name: table_name)
Expand All @@ -31,10 +31,10 @@ def self.import_column_memos!(source_table, table_memo)
columns = source_table.columns

column_names = columns.map(&:name)
column_memos.reject {|memo| column_names.include?(memo.name) }.each {|memo| memo.update!(linked: false) }
column_memos.reject { |memo| column_names.include?(memo.name) }.each { |memo| memo.update!(linked: false) }

columns.each_with_index do |column, position|
column_memo = column_memos.find {|memo| memo.name == column.name } || table_memo.column_memos.build(name: column.name)
column_memo = column_memos.find { |memo| memo.name == column.name } || table_memo.column_memos.build(name: column.name)
column_memo.linked = true
column_memo.assign_attributes(sql_type: column.sql_type, default: column.default, nullable: column.null, position: position)
column_memo.save! if column_memo.has_changes_to_save?
Expand Down
4 changes: 2 additions & 2 deletions app/batches/import_table_raw_datasets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def self.run(data_source_name, schema_name, table_name)
Rails.logger.info "[Start] Import dataset of #{schema_name}.#{table_name} table in #{data_source_name}"

data_source = DataSource.find_by(name: data_source_name)
source_table = data_source.data_source_tables.find {|dst| dst.full_table_name == "#{schema_name}.#{table_name}" }
source_table = data_source.data_source_tables.find { |dst| dst.full_table_name == "#{schema_name}.#{table_name}" }

schema_memo = data_source.database_memo.schema_memos.find_by!(name: schema_name, linked: true)
table_memo = schema_memo.table_memos.find_or_create_by!(name: table_name)
Expand Down Expand Up @@ -42,7 +42,7 @@ def self.import_table_memo_raw_dataset_rows!(table_memo, source_table, columns)
end
table_memo.raw_dataset.rows.delete_all
source_table.fetch_rows(DEFAULT_FETCH_ROWS_LIMIT).each do |row|
table_memo.raw_dataset.rows.create!(row: row.map.with_index{|value, i| dataset_columns[i].format_value(value) })
table_memo.raw_dataset.rows.create!(row: row.map.with_index { |value, i| dataset_columns[i].format_value(value) })
end
end
end
20 changes: 10 additions & 10 deletions app/batches/synchronize_data_sources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ def self.run
def self.import_data_source!(data_source)
db_memo = DatabaseMemo.find_or_create_by!(name: data_source.name)
schema_memos = db_memo.schema_memos.includes(table_memos: [:column_memos, :raw_dataset]).to_a
schema_memos.each {|memo| memo.linked = false }
schema_memos.each { |memo| memo.linked = false }

all_table_memos = schema_memos.map(&:table_memos).map(&:to_a).flatten
all_table_memos.each {|memo| memo.linked = false }
all_table_memos.each { |memo| memo.linked = false }

data_source_tables = data_source.data_source_tables

data_source_tables.group_by(&:schema_name).each do |schema_name, source_tables|
schema_memo = schema_memos.find {|memo| memo.name == schema_name } || db_memo.schema_memos.create!(name: schema_name )
schema_memo = schema_memos.find { |memo| memo.name == schema_name } || db_memo.schema_memos.create!(name: schema_name )
schema_memo.linked = true
table_memos = all_table_memos.select {|memo| memo.schema_memo_id == schema_memo.id }
table_memos = all_table_memos.select { |memo| memo.schema_memo_id == schema_memo.id }

source_tables.each do |source_table|
begin
Expand All @@ -30,13 +30,13 @@ def self.import_data_source!(data_source)
end
end
end
schema_memos.each {|memo| memo.save! if memo.changed? }
all_table_memos.each {|memo| memo.save! if memo.changed? }
schema_memos.each { |memo| memo.save! if memo.changed? }
all_table_memos.each { |memo| memo.save! if memo.changed? }
end

def self.import_table_memo!(schema_memo, table_memos, source_table)
table_name = source_table.table_name
table_memo = table_memos.find {|memo| memo.name == table_name } || schema_memo.table_memos.create!(name: table_name )
table_memo = table_memos.find { |memo| memo.name == table_name } || schema_memo.table_memos.create!(name: table_name )
table_memo.linked = true
column_memos = table_memo.column_memos.to_a

Expand All @@ -45,10 +45,10 @@ def self.import_table_memo!(schema_memo, table_memos, source_table)
import_view_query!(table_memo, source_table)

column_names = columns.map(&:name)
column_memos.reject {|memo| column_names.include?(memo.name) }.each {|memo| memo.update!(linked: false) }
column_memos.reject { |memo| column_names.include?(memo.name) }.each { |memo| memo.update!(linked: false) }

columns.each_with_index do |column, position|
column_memo = column_memos.find {|memo| memo.name == column.name } || table_memo.column_memos.build(name: column.name)
column_memo = column_memos.find { |memo| memo.name == column.name } || table_memo.column_memos.build(name: column.name)
column_memo.linked = true
column_memo.assign_attributes(sql_type: column.sql_type, default: column.default, nullable: column.null, position: position)
column_memo.save! if column_memo.changed?
Expand Down Expand Up @@ -79,7 +79,7 @@ def self.import_table_memo_raw_dataset_rows!(table_memo, source_table, columns)
end
table_memo.raw_dataset.rows.delete_all
source_table.fetch_rows(DEFAULT_FETCH_ROWS_LIMIT).each do |row|
table_memo.raw_dataset.rows.create!(row: row.map.with_index{|value, i| dataset_columns[i].format_value(value) })
table_memo.raw_dataset.rows.create!(row: row.map.with_index { |value, i| dataset_columns[i].format_value(value) })
end
end
private_class_method :import_table_memo_raw_dataset_rows!
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/data_sources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ def edit(id)
@imported_schema_memos = @data_source.database_memo.schema_memos
@subscribe_schema_names = @imported_schema_memos.where(linked: true).map(&:name)
@only_dmemo_schema_names = @imported_schema_memos.pluck(:name) - @data_source_schema_names
@only_dmemo_schemas = @only_dmemo_schema_names.map{|s| [s, 'unknown']}
@all_schemas = (@data_source_schemas + @only_dmemo_schemas).sort_by{|s| s[0]} # s[0] is schema name
@only_dmemo_schemas = @only_dmemo_schema_names.map { |s| [s, 'unknown'] }
@all_schemas = (@data_source_schemas + @only_dmemo_schemas).sort_by { |s| s[0] } # s[0] is schema name
rescue NotImplementedError => e
# when not implement fetch_schema_names for adapter
# data_sources/:id/edit page does not view Schema Candidates block
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/masked_data_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def show

def new
@masked_datum = MaskedDatum.new
@database_name_options = (["*"] + DatabaseMemo.pluck(:name)).map {|x| [x, x]}
@database_name_options = (["*"] + DatabaseMemo.pluck(:name)).map { |x| [x, x] }
end

def create(masked_datum)
Expand Down
6 changes: 5 additions & 1 deletion app/controllers/table_memos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ def show(database_name, schema_name, name)
take!
@raw_dataset = @table_memo.raw_dataset
if @raw_dataset
@raw_dataset_columns = @raw_dataset.columns.order(:position)
@masked_columns = MaskedDatum.masked_columns(database_name, name)
@raw_dataset_columns = @raw_dataset.columns.order(:position).map { |column| {
data: column,
masked: @masked_columns.include?(::MaskedDatum::ANY_NAME) || @masked_columns.include?(column.name),
}}
@raw_dataset_rows = @raw_dataset.rows.pluck(:row)
end
@view_meta_data = @table_memo.view_meta_data
Expand Down
2 changes: 1 addition & 1 deletion app/models/data_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def source_table_names

def data_source_table(schema_name, table_name, table_names)
return if ignore?(schema_name, table_name)
schema_name, _ = table_names.find {|schema, table| schema == schema_name && table == table_name }
schema_name, _ = table_names.find { |schema, table| schema == schema_name && table == table_name }
return nil unless schema_name
DataSourceTable.new(self, schema_name, table_name)
end
Expand Down
17 changes: 9 additions & 8 deletions app/models/data_source_adapters/bigquery_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ def bq_dataset
return @bq_dataset if @bq_dataset

config = @data_source.bigquery_config
client = config.credentials ?
Google::Cloud::Bigquery.new(
project_id: config.project_id,
credentials: JSON.parse(config.credentials),
) :
Google::Cloud::Bigquery.new(
project_id: config.project_id,
)
client =
if config.credentials
Google::Cloud::Bigquery.new(
project_id: config.project_id,
credentials: JSON.parse(config.credentials),
)
else
Google::Cloud::Bigquery.new(project_id: config.project_id)
end

@bq_dataset = client.dataset(config.dataset)
raise DataSource::ConnectionBad.new("dataset \"#{config.dataset}\" not found") if @bq_dataset.nil?
Expand Down
Loading
Loading