mirror of
https://github.com/paper-trail-gem/paper_trail.git
synced 2022-11-09 11:33:19 -05:00
Alphabetize methods, other code style
This commit is contained in:
parent
8bc5370c7a
commit
3b17989d02
4 changed files with 62 additions and 59 deletions
14
CHANGELOG.md
14
CHANGELOG.md
|
@ -3,7 +3,7 @@
|
|||
This project follows [semver 2.0.0](http://semver.org/spec/v2.0.0.html) and the
|
||||
recommendations of [keepachangelog.com](http://keepachangelog.com/).
|
||||
|
||||
## Unreleased
|
||||
## 10.1.0 (Unreleased)
|
||||
|
||||
### Breaking Changes
|
||||
|
||||
|
@ -11,14 +11,16 @@ recommendations of [keepachangelog.com](http://keepachangelog.com/).
|
|||
|
||||
### Deprecated
|
||||
|
||||
- [#1158](https://github.com/paper-trail-gem/paper_trail/pull/1158) — Passing association name as
|
||||
`versions:` option or Version class name as `class_name:` options directly to `has_paper_trail`.
|
||||
Use `has_paper_trail versions: {name: :drafts, class_name: "MyVersionModel"}` instead.
|
||||
- [#1158](https://github.com/paper-trail-gem/paper_trail/pull/1158) - Passing
|
||||
association name as `versions:` option or Version class name as `class_name:`
|
||||
options directly to `has_paper_trail`. Use `has_paper_trail versions: {name:
|
||||
:my_name, class_name: "MyVersionModel"}` instead.
|
||||
|
||||
### Added
|
||||
|
||||
- [#1158](https://github.com/paper-trail-gem/paper_trail/pull/1158) — Add the ability to pass
|
||||
options, such as `scope` or `extend:` to the `has_many :versions` association macro.
|
||||
- [#1158](https://github.com/paper-trail-gem/paper_trail/pull/1158) — Add the
|
||||
ability to pass options, such as `scope` or `extend:` to the `has_many
|
||||
:versions` association macro.
|
||||
|
||||
### Fixed
|
||||
|
||||
|
|
|
@ -24,8 +24,8 @@ module PaperTrail
|
|||
#
|
||||
# - :on - The events to track (optional; defaults to all of them). Set
|
||||
# to an array of `:create`, `:update`, `:destroy` as desired.
|
||||
# - :class_name - The name of a custom Version class. This class should
|
||||
# inherit from `PaperTrail::Version`.
|
||||
# - :class_name (deprecated) - The name of a custom Version class that
|
||||
# includes `PaperTrail::VersionConcern`.
|
||||
# - :ignore - An array of attributes for which a new `Version` will not be
|
||||
# created if only they change. It can also aceept a Hash as an
|
||||
# argument where the key is the attribute to ignore (a `String` or
|
||||
|
@ -47,13 +47,15 @@ module PaperTrail
|
|||
# are called with `self`, i.e. the model with the paper trail). See
|
||||
# `PaperTrail::Controller.info_for_paper_trail` for how to store data
|
||||
# from the controller.
|
||||
# - :versions - The name to use for the versions association. Default
|
||||
# is `:versions`.
|
||||
# - :versions - Either,
|
||||
# - A String (deprecated) - The name to use for the versions
|
||||
# association. Default is `:versions`.
|
||||
# - A Hash - options passed to `has_many`, plus `name:` and `scope:`.
|
||||
# - :version - The name to use for the method which returns the version
|
||||
# the instance was reified from. Default is `:version`.
|
||||
#
|
||||
# If you are using the experimental `paper_trail-association_tracking` gem
|
||||
# it may accept additional options.
|
||||
# Plugins like the experimental `paper_trail-association_tracking` gem
|
||||
# may accept additional options.
|
||||
#
|
||||
# @api public
|
||||
def has_paper_trail(options = {})
|
||||
|
|
|
@ -21,11 +21,12 @@ module PaperTrail
|
|||
DPR_PASSING_ASSOC_NAME_DIRECTLY_TO_VERSIONS_OPTION = <<~STR.squish
|
||||
Passing versions association name as `has_paper_trail versions: %{versions_name}`
|
||||
is deprecated. Use `has_paper_trail versions: {name: %{versions_name}}` instead.
|
||||
The hash you pass to `versions:` is now passed directly to `has_many`.
|
||||
STR
|
||||
DPR_CLASS_NAME_OPTION = <<~STR.squish
|
||||
Passing Version class name as `has_paper_trail class_name: %{class_name}`
|
||||
is deprecated. Use `has_paper_trail versions: {class_name: %{class_name}}`
|
||||
instead.
|
||||
instead. The hash you pass to `versions:` is now passed directly to `has_many`.
|
||||
STR
|
||||
|
||||
def initialize(model_class)
|
||||
|
@ -138,19 +139,27 @@ module PaperTrail
|
|||
::ActiveRecord::Base.belongs_to_required_by_default
|
||||
end
|
||||
|
||||
def setup_associations(options)
|
||||
# @api private - version_association_name
|
||||
@model_class.class_attribute :version_association_name
|
||||
@model_class.version_association_name = options[:version] || :version
|
||||
def check_version_class_name(options)
|
||||
# @api private - `version_class_name`
|
||||
@model_class.class_attribute :version_class_name
|
||||
if options[:class_name]
|
||||
::ActiveSupport::Deprecation.warn(
|
||||
format(
|
||||
DPR_CLASS_NAME_OPTION,
|
||||
class_name: options[:class_name].inspect
|
||||
),
|
||||
caller(1)
|
||||
)
|
||||
options[:versions][:class_name] = options.delete(:class_name)
|
||||
end
|
||||
@model_class.version_class_name = options[:versions][:class_name] || "PaperTrail::Version"
|
||||
assert_concrete_activerecord_class(@model_class.version_class_name)
|
||||
end
|
||||
|
||||
# The version this instance was reified from.
|
||||
# @api public
|
||||
@model_class.send :attr_accessor, @model_class.version_association_name
|
||||
|
||||
# @api public - paper_trail_event
|
||||
@model_class.send :attr_accessor, :paper_trail_event
|
||||
|
||||
define_has_many_versions(options)
|
||||
def check_versions_association_name(options)
|
||||
# @api private - versions_association_name
|
||||
@model_class.class_attribute :versions_association_name
|
||||
@model_class.versions_association_name = options[:versions].delete(:name) || :versions
|
||||
end
|
||||
|
||||
def define_has_many_versions(options)
|
||||
|
@ -187,33 +196,25 @@ module PaperTrail
|
|||
options
|
||||
end
|
||||
|
||||
def check_version_class_name(options)
|
||||
# @api private - `version_class_name`
|
||||
@model_class.class_attribute :version_class_name
|
||||
if options[:class_name]
|
||||
::ActiveSupport::Deprecation.warn(
|
||||
format(
|
||||
DPR_CLASS_NAME_OPTION,
|
||||
class_name: options[:class_name].inspect
|
||||
),
|
||||
caller(1)
|
||||
)
|
||||
options[:versions][:class_name] = options.delete(:class_name)
|
||||
end
|
||||
@model_class.version_class_name = options[:versions][:class_name] || "PaperTrail::Version"
|
||||
assert_concrete_activerecord_class(@model_class.version_class_name)
|
||||
end
|
||||
|
||||
def check_versions_association_name(options)
|
||||
# @api private - versions_association_name
|
||||
@model_class.class_attribute :versions_association_name
|
||||
@model_class.versions_association_name = options[:versions].delete(:name) || :versions
|
||||
end
|
||||
|
||||
def get_versions_scope(options)
|
||||
options[:versions].delete(:scope) || -> { order(model.timestamp_sort_order) }
|
||||
end
|
||||
|
||||
def setup_associations(options)
|
||||
# @api private - version_association_name
|
||||
@model_class.class_attribute :version_association_name
|
||||
@model_class.version_association_name = options[:version] || :version
|
||||
|
||||
# The version this instance was reified from.
|
||||
# @api public
|
||||
@model_class.send :attr_accessor, @model_class.version_association_name
|
||||
|
||||
# @api public - paper_trail_event
|
||||
@model_class.send :attr_accessor, :paper_trail_event
|
||||
|
||||
define_has_many_versions(options)
|
||||
end
|
||||
|
||||
def setup_callbacks_from_options(options_on = [])
|
||||
options_on.each do |event|
|
||||
public_send("on_#{event}")
|
||||
|
|
|
@ -15,13 +15,11 @@ module PaperTrail
|
|||
ActiveRecord::Reflection::HasManyReflection
|
||||
)
|
||||
expect(::ActiveSupport::Deprecation).to have_received(:warn).once.with(
|
||||
%(Passing versions association name as `has_paper_trail versions: :drafts` is ) +
|
||||
%(deprecated. Use `has_paper_trail versions: {name: :drafts}` instead.),
|
||||
array_including(
|
||||
/#{__FILE__}:/
|
||||
)
|
||||
a_string_starting_with("Passing versions association name"),
|
||||
array_including(/#{__FILE__}:/)
|
||||
)
|
||||
end
|
||||
|
||||
it "name can be passed in the options hash" do
|
||||
klass = Class.new(ActiveRecord::Base) do
|
||||
has_paper_trail versions: { name: :drafts }
|
||||
|
@ -30,6 +28,7 @@ module PaperTrail
|
|||
ActiveRecord::Reflection::HasManyReflection
|
||||
)
|
||||
end
|
||||
|
||||
it "class_name can be passed in the options hash" do
|
||||
klass = Class.new(ActiveRecord::Base) do
|
||||
has_paper_trail versions: { class_name: "NoObjectVersion" }
|
||||
|
@ -38,6 +37,7 @@ module PaperTrail
|
|||
"NoObjectVersion"
|
||||
)
|
||||
end
|
||||
|
||||
it "allows any option that has_many supports" do
|
||||
klass = Class.new(ActiveRecord::Base) do
|
||||
has_paper_trail versions: { autosave: true, validate: true }
|
||||
|
@ -45,12 +45,14 @@ module PaperTrail
|
|||
expect(klass.reflect_on_association(:versions).options[:autosave]).to eq true
|
||||
expect(klass.reflect_on_association(:versions).options[:validate]).to eq true
|
||||
end
|
||||
|
||||
it "can even override options that PaperTrail adds to has_many" do
|
||||
klass = Class.new(ActiveRecord::Base) do
|
||||
has_paper_trail versions: { as: :foo }
|
||||
end
|
||||
expect(klass.reflect_on_association(:versions).options[:as]).to eq :foo
|
||||
end
|
||||
|
||||
it "raises an error on unknown has_many options" do
|
||||
expect {
|
||||
Class.new(ActiveRecord::Base) do
|
||||
|
@ -84,12 +86,8 @@ module PaperTrail
|
|||
"NoObjectVersion"
|
||||
)
|
||||
expect(::ActiveSupport::Deprecation).to have_received(:warn).once.with(
|
||||
%(Passing Version class name as `has_paper_trail class_name: "NoObjectVersion"` ) +
|
||||
%(is deprecated. Use `has_paper_trail versions: {class_name: "NoObjectVersion"}` ) +
|
||||
%(instead.),
|
||||
array_including(
|
||||
/#{__FILE__}:/
|
||||
)
|
||||
a_string_starting_with("Passing Version class name"),
|
||||
array_including(/#{__FILE__}:/)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue