Skip to content

Commit

Permalink
Merge branch 'main' into not-null
Browse files Browse the repository at this point in the history
  • Loading branch information
jdufresne authored Jan 23, 2023
2 parents f1a44ac + 857c9bc commit 593185d
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- ruby: 2.3
db: MYSQL

# PostgresSQL is segfaulting on 2.3
# PostgreSQL is segfaulting on 2.3
# Doesn't seem worth solving.
- ruby: 2.3
db: POSTGRES
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ Audited.current_user_method = :authenticated_user
Outside of a request, Audited can still record the user with the `as_user` method:

```ruby
Audited.audit_model.as_user(User.find(1)) do
Audited.audit_class.as_user(User.find(1)) do
post.update!(title: "Hello, world!")
end
post.audits.last.user # => #<User id: 1>
Expand All @@ -246,7 +246,7 @@ end
`as_user` also accepts a string, which can be useful for auditing updates made in a CLI environment:

```rb
Audited.audit_model.as_user("console-user-#{ENV['SSH_USER']}") do
Audited.audit_class.as_user("console-user-#{ENV['SSH_USER']}") do
post.update_attributes!(title: "Hello, world!")
end
post.audits.last.user # => 'console-user-username'
Expand Down Expand Up @@ -314,7 +314,7 @@ If you want to audit only under specific conditions, you can provide conditional
```ruby
class User < ActiveRecord::Base
audited if: :active?

def active?
last_login > 6.months.ago
end
Expand Down Expand Up @@ -428,7 +428,7 @@ Audited.store_synthesized_enums = true

## Support

You can find documentation at: http://rdoc.info/github/collectiveidea/audited
You can find documentation at: https://www.rubydoc.info/gems/audited

Or join the [mailing list](http://groups.google.com/group/audited) to get help or offer suggestions.

Expand Down
11 changes: 7 additions & 4 deletions lib/audited.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ class << self
attr_writer :audit_class

def audit_class
@audit_class ||= "Audited::Audit"
# The audit_class is set as String in the initializer. It can not be constantized during initialization and must
# be constantized at runtime. See https://github.com/collectiveidea/audited/issues/608
@audit_class = @audit_class.constantize if @audit_class.is_a?(String)
@audit_class ||= Audited::Audit
end

def audit_model
audit_class.constantize
end
# remove audit_model in next major version it was only shortly present in 5.1.0
alias_method :audit_model, :audit_class
deprecate audit_model: "use Audited.audit_class instead of Audited.audit_model. This method will be removed."

def store
current_store_value = Thread.current.thread_variable_get(:audited_store)
Expand Down
2 changes: 1 addition & 1 deletion lib/audited/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def dump(obj)
end

def text_column?
Audited.audit_model.columns_hash["audited_changes"].type.to_s == "text"
Audited.audit_class.columns_hash["audited_changes"].type.to_s == "text"
end
end
end
Expand Down
18 changes: 9 additions & 9 deletions lib/audited/auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ def audited(options = {})
before_destroy :require_comment if audited_options[:on].include?(:destroy)
end

has_many :audits, -> { order(version: :asc) }, as: :auditable, class_name: Audited.audit_class, inverse_of: :auditable
Audited.audit_model.audited_class_names << to_s
has_many :audits, -> { order(version: :asc) }, as: :auditable, class_name: Audited.audit_class.name, inverse_of: :auditable
Audited.audit_class.audited_class_names << to_s

after_create :audit_create if audited_options[:on].include?(:create)
before_update :audit_update if audited_options[:on].include?(:update)
Expand All @@ -97,7 +97,7 @@ def audited(options = {})
end

def has_associated_audits
has_many :associated_audits, as: :associated, class_name: Audited.audit_model.name
has_many :associated_audits, as: :associated, class_name: Audited.audit_class.name
end
end

Expand Down Expand Up @@ -159,14 +159,14 @@ def revisions(from_version = 1)
# Returns nil for versions greater than revisions count
def revision(version)
if version == :previous || audits.last.version >= version
revision_with Audited.audit_model.reconstruct_attributes(audits_to(version))
revision_with Audited.audit_class.reconstruct_attributes(audits_to(version))
end
end

# Find the oldest revision recorded prior to the date/time provided.
def revision_at(date_or_time)
audits = self.audits.up_until(date_or_time)
revision_with Audited.audit_model.reconstruct_attributes(audits) unless audits.empty?
revision_with Audited.audit_class.reconstruct_attributes(audits) unless audits.empty?
end

# List of attributes that are audited.
Expand All @@ -179,8 +179,8 @@ def audited_attributes

# Returns a list combined of record audits and associated audits.
def own_and_associated_audits
Audited.audit_model.unscoped.where(auditable: self)
.or(Audited.audit_model.unscoped.where(associated: self))
Audited.audit_class.unscoped.where(auditable: self)
.or(Audited.audit_class.unscoped.where(associated: self))
.order(created_at: :desc)
end

Expand Down Expand Up @@ -212,7 +212,7 @@ def revision_with(attributes)
revision.send :instance_variable_set, "@destroyed", false
revision.send :instance_variable_set, "@_destroyed", false
revision.send :instance_variable_set, "@marked_for_destruction", false
Audited.audit_model.assign_revision_attributes(revision, attributes)
Audited.audit_class.assign_revision_attributes(revision, attributes)

# Remove any association proxies so that they will be recreated
# and reference the correct object for this revision. The only way
Expand Down Expand Up @@ -455,7 +455,7 @@ def enable_auditing
# convenience wrapper around
# @see Audit#as_user.
def audit_as(user, &block)
Audited.audit_model.as_user(user, &block)
Audited.audit_class.as_user(user, &block)
end

def auditing_enabled
Expand Down
2 changes: 1 addition & 1 deletion lib/audited/rspec_matchers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def reflection
def association_exists?
!reflection.nil? &&
reflection.macro == :has_many &&
reflection.options[:class_name] == Audited.audit_model.name
reflection.options[:class_name] == Audited.audit_class.name
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/audited/audit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Models::ActiveRecord::CustomUserSubclass < Models::ActiveRecord::CustomUse
end

describe "#audited_changes" do
let(:audit) { Audited.audit_model.new }
let(:audit) { Audited.audit_class.new }

it "can unserialize yaml from text columns" do
audit.audited_changes = {foo: "bar"}
Expand Down

0 comments on commit 593185d

Please sign in to comment.