Skip to content

Commit

Permalink
Refactor class methods of MaskedDatum
Browse files Browse the repository at this point in the history
- Avoid using class variables https://github.com/cookpad/styleguide/blob/master/ruby.en.md#variables
    - But class_attribute with the default value which is calculated using an instance method is a bit difficult
- Remove cache and query directly, in order to remove both class variables and class_attributes
    - The calculation cost will be roughly the same
  • Loading branch information
nekketsuuu committed Feb 28, 2024
1 parent 0d21f18 commit e50dc2b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
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
38 changes: 16 additions & 22 deletions app/models/masked_datum.rb
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
class MaskedDatum < ApplicationRecord
validates :database_name, :table_name, :column_name, presence: true

after_save :update_data!
ANY_NAME = "*".freeze

def self.data
@@data ||= update_data!
end
validates :database_name, :table_name, :column_name, presence: true

def self.update_data!
@@data = all.map(&:pack)
end
scope :masking_database, ->(database_name) {
where(database_name: database_name, table_name: ANY_NAME, column_name: ANY_NAME)
}
scope :masking_table, ->(database_name, table_name) {
masking_database(database_name)
.or(where(database_name: database_name, table_name: table_name, column_name: ANY_NAME))
.or(where(database_name: ANY_NAME, table_name: table_name, column_name: ANY_NAME))
}

def self.masked_database?(database_name)
data.include?("#{database_name}/*/*")
self.masking_database(database_name).exists?
end

def self.masked_table?(database_name, table_name)
return true if masked_database?(database_name)
data.include?("#{database_name}/#{table_name}/*")
self.masking_table(database_name, table_name).exists?
end

def self.masked_column?(database_name, table_name, column_name)
return true if masked_database?(database_name)
return true if masked_table?(database_name, table_name)
data.include?("#{database_name}/#{table_name}/#{column_name}") || data.include?("#{database_name}/*/#{column_name}") || data.include?("*/*/#{column_name}")
def self.masked_columns(database_name, table_name)
return [ANY_NAME] if self.masked_table?(database_name, table_name)

Set.new(self.where(database_name: database_name, table_name: table_name).pluck(:column_name))
end

def pack
"#{database_name}/#{table_name}/#{column_name}"
end

private

def update_data!
self.class.update_data!
end
end
4 changes: 2 additions & 2 deletions app/views/table_memos/_raw_dataset.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
%table.table.table-hover.table-bordered.table-striped.text-sm{ role: "grid" }
%tr
- @raw_dataset_columns.each do |column|
%th #{column.name} (#{column.sql_type})
%th #{column.fetch(:data).name} (#{column.fetch(:data).sql_type})
- if @table_memo.masked?
%tr
%td.text-center{ colspan: @raw_dataset_columns.size }
Expand All @@ -18,7 +18,7 @@
%tr
- Array.wrap(row).zip(@raw_dataset_columns).each_with_index do |(value, column), i|
%td
- if MaskedDatum.masked_column?(database_name, table_name, column.name)
- if column.fetch(:masked)
= t("masked_text")
- else
= value
Expand Down

0 comments on commit e50dc2b

Please sign in to comment.