Skip to content

Commit 2c0a980

Browse files
committed
Implement custom audit model generator within install script
1 parent 374e896 commit 2c0a980

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

lib/generators/audited/install_generator.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ class InstallGenerator < Rails::Generators::Base
2323
def copy_migration
2424
migration_template "install.rb", "db/migrate/install_audited.rb"
2525
end
26+
27+
def create_custom_audit
28+
return if options[:audited_table_name] == "audits"
29+
30+
template "custom_audit.rb", "app/models/#{options[:audited_table_name].singularize.underscore}.rb"
31+
end
2632
end
2733
end
2834
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# frozen_string_literal: true
2+
3+
<% table_name = options[:audited_table_name].underscore.pluralize %>
4+
class <%= table_name.singularize.classify %> < Audited::Audit
5+
self.table_name = "<%= table_name %>"
6+
end

lib/generators/audited/templates/install.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# frozen_string_literal: true
22

3-
<% table_name = options[:audited_table_name] %>
3+
<% table_name = options[:audited_table_name].underscore.pluralize %>
44
class <%= migration_class_name %> < <%= migration_parent %>
55
def self.up
66
create_table :<%= table_name %>, :force => true do |t|

spec/audited/auditor_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,7 @@ class CallbacksSpecified < ::ActiveRecord::Base
495495
end
496496

497497
it "should not create default audit record" do
498+
binding.irb
498499
expect { company }.not_to change(Audited::Audit, :count)
499500
expect(Audited::Audit.where(auditable_type: CustomCompany.name).count).to eq(0)
500501
end

test/install_generator_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,12 @@ class InstallGeneratorTest < Rails::Generators::TestCase
5959
assert_includes(content, "class InstallAudited < ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]\n")
6060
end
6161
end
62+
63+
test "generate custom audit" do
64+
run_generator %w[--audited-table-name custom_audits]
65+
66+
assert_file "app/models/custom_audit.rb" do |content|
67+
assert_includes(content, "class CustomAudit < Audited::Audit")
68+
end
69+
end
6270
end

0 commit comments

Comments
 (0)