Skip to content
Open
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ Annotate model options:
--without-table-comments exclude table comments in model annotations
--classes-default-to-s class Custom classes to be represented with `to_s`, may be used multiple times
--nested-position Place annotations directly above nested classes or modules instead of at the top of the file.
--load-columns-from-schema Load columns from schema to avoid ActiveRecord ignored/only column configuration
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This naming seems fine to me 👍


Annotate routes options:
Usage: annotaterb routes [options]
Expand Down
7 changes: 6 additions & 1 deletion lib/annotate_rb/model_annotator/model_wrapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ def database_name

# Returns the unmodified model columns
def raw_columns
@raw_columns ||= @klass.columns
@raw_columns ||= if @options[:load_columns_from_schema]
@klass.load_schema
@klass.schema_cache.columns_hash(@klass.table_name).values.freeze
else
@klass.columns
end
end

def primary_key
Expand Down
2 changes: 2 additions & 0 deletions lib/annotate_rb/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def from(options = {}, state = {})
show_indexes: true, # ModelAnnotator
show_indexes_include: false, # ModelAnnotator
show_virtual_columns: false, # ModelAnnotator
load_columns_from_schema: false, # ModelAnnotator
simple_indexes: false, # ModelAnnotator
sort: false, # ModelAnnotator
timestamp: false, # RouteAnnotator
Expand Down Expand Up @@ -119,6 +120,7 @@ def from(options = {}, state = {})
:ignore_model_sub_dir,
:ignore_unknown_models,
:include_version,
:load_columns_from_schema,
:show_check_constraints,
:show_complete_foreign_keys,
:show_foreign_keys,
Expand Down
5 changes: 5 additions & 0 deletions lib/annotate_rb/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@ def add_model_options_to_parser(option_parser)
"Place annotations directly above nested classes or modules instead of at the top of the file.") do
@options[:nested_position] = true
end

option_parser.on("--load-columns-from-schema",
"Load columns from schema to avoid ActiveRecord ignored/only column configuration") do
@options[:load_columns_from_schema] = true
end
end

def add_route_options_to_parser(option_parser)
Expand Down
1 change: 1 addition & 0 deletions spec/dummyapp/app/models/test_default.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

class TestDefault < ApplicationRecord
self.ignored_columns = ["ignored_column"]
end
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def change
t.float :float, default: 12.34
t.integer :integer, default: 99
t.string :string, default: 'hello world!'
t.string :ignored_column

t.timestamps
end
Expand Down
32 changes: 32 additions & 0 deletions spec/integration/load_columns_from_schema_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require "integration_spec_helper"

RSpec.describe "Load columns from schema with ignored columns", type: "aruba" do
let(:command_timeout_seconds) { 10 }
let(:model_file) { "app/models/test_default.rb" }

before do
copy_dummy_app_into_aruba_working_directory
reset_database
run_migrations
end

it "does not include ignored columns by default" do
run_command_and_stop("bundle exec annotaterb models #{model_file} --force", fail_on_error: true, exit_timeout: command_timeout_seconds)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does --force need to be here to trigger the changes?


annotated_content = read_file(model_file)

expect(annotated_content).to include("string")
expect(annotated_content).not_to include("# ignored_column")
end

it "includes ignored columns when --load-columns-from-schema is used" do
run_command_and_stop("bundle exec annotaterb models #{model_file} --load-columns-from-schema --force", fail_on_error: true, exit_timeout: command_timeout_seconds)

annotated_content = read_file(model_file)

expect(annotated_content).to include("string")
expect(annotated_content).to include("# ignored_column")
end
end
8 changes: 8 additions & 0 deletions spec/lib/annotate_rb/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@
end
end

context 'when default value of "load_columns_from_schema" is not set' do
let(:key) { :load_columns_from_schema }

it "returns false" do
expect(subject[key]).to eq(false)
end
end

context 'when default value of "show_complete_foreign_keys" is set' do
let(:key) { :show_complete_foreign_keys }
let(:options) { {key => true} }
Expand Down
10 changes: 10 additions & 0 deletions spec/lib/annotate_rb/parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,16 @@ module AnnotateRb # rubocop:disable Metrics/ModuleLength
end
end

%w[--load-columns-from-schema].each do |option|
describe option do
let(:args) { [option] }

it "sets load_columns_from_schema to true" do
expect(result).to include(load_columns_from_schema: true)
end
end
end

%w[-R --require].each do |option|
describe option do
let(:option) { "require" }
Expand Down
1 change: 1 addition & 0 deletions spec/templates/mysql2/test_default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
# updated_at :datetime not null
#
class TestDefault < ApplicationRecord
self.ignored_columns = ["ignored_column"]
end
1 change: 1 addition & 0 deletions spec/templates/pg/test_default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
# updated_at :datetime not null
#
class TestDefault < ApplicationRecord
self.ignored_columns = ["ignored_column"]
end
1 change: 1 addition & 0 deletions spec/templates/sqlite3/test_default.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
# updated_at :datetime not null
#
class TestDefault < ApplicationRecord
self.ignored_columns = ["ignored_column"]
end
Loading