Commit Graph

21 Commits

Author SHA1 Message Date
Jared Beck 8f5a935c3a RSpec/ContextWording 2021-08-29 22:22:39 -04:00
Lud 1e56afdf84
Expand kwargs passed to "save_with_version" with double splat operator - Rails 6.1 compatibility (#1284)
* use double splat for ActiveRecord#save

* test it

* updates changelog

* rename args -> options
2021-01-18 13:58:06 -05:00
Jared Beck 4d877a7558 Remove touch_with_version, was deprecated in 9.0.0 2018-08-01 22:20:33 -04:00
Jared Beck 812c306892 Uninstall timecop
I'm hoping we don't actually need it because we explicitly enable
fractional seconds precision in our `dummy_app` migration by
specifying `limit(6)`.

Works on my machine, but I use mariadb. We'll see how well it works
on TravisCI, which runs mysql 5.6.33.

MySQL added support for `limit(6)` in 5.6.4, so I'm hopeful.
2018-05-14 22:31:12 -04:00
Jared Beck 97a776e255 Add save_with_version
Replaces deprecated touch_with_version
2018-04-22 23:34:28 -04:00
Weston Ganger 02b6de2dac Integrate versioning into AR touch method (#1063)
* integrate versioning into AR touch method

* add touch to list of :on events, deprecate touch_with_version

* integrate versioning into AR touch method
2018-03-16 18:21:45 -04:00
Jared Beck cfbf7a647c Lint: Style/FrozenStringLiteralComment 2017-12-10 23:05:39 -05:00
Jared Beck 515f0958a6 Lint: Fix RSpec/NamedSubject 2017-10-27 12:17:04 -04:00
Jared Beck 38fa23873c Merge rails_helper into spec_helper
The reason some projects have both is so that some spec files can be
run in isolation, without the rails stuff. In practice, I don't find
myself ever doing this. So, the complexity of two files is unnecessary.
2017-05-30 00:59:55 -04:00
Jared Beck e2505c4f30 rubocop 0.48.1 (was 0.48.0) 2017-05-21 02:40:23 -04:00
Jared Beck c4148de9d0 Configure RSpec
Enable various standard configuration.
2017-05-21 02:04:28 -04:00
Jared Beck af10cd4f00 Lint: Fix RSpec/ExampleWording 2017-04-01 01:50:13 -04:00
Jared Beck 8e6efc6052 Simplify test suite after dropping rails 3 support
Also, the only version of rails 4 that we test against is 4.2, so
we can drop the conditional around `ActiveRecord::Enum`.
2016-12-04 17:05:07 -05:00
Jared Beck 7884c143b6 Fix deserialization of enums written by PT 4
Fixes #855
2016-09-02 18:14:54 -04:00
Jared Beck bc1308ea80 Fix enum serialization in rails 4.2
Fixes #798

Our test suite has one model that tests enums, PostWithStatus. Its
spec did not cover `touch_with_version`. Somehow, probably during
our upgrade to rails 5, we broke `touch_with_version` for enums.
Unlike other methods, when an enum arrives at
`CastAttributeSerializer#serialize` during `touch_with_version`, it
is already a number. We assumed it was always a string.

This is sort of a hack, just handling both strings and numbers in
`#serialize`. I'd rather figure out how a number got to `#serialize`
in the first place. However, a hack is acceptable, as it's only
for rails 4.2, and the hack will eventually be deleted when we
drop support for 4.2.
2016-08-14 00:24:13 -04:00
Jared Beck ad3806fcbb An idea to combat model pollution
Problem
-------

`has_paper_trail` adds too many methods to the ActiveRecord model.

> If I'm counting correctly, installing the paper_trail gem adds 36 instance
> methods and 10 class methods to all of your active record models. Of those
> 46, 13 have a prefix, either "pt_" or "paper_trail_". I don't know what the
> best way to deal with this is. Ideally, we'd add far fewer methods to
> people's models. If we were able to add fewer methods to models, then I
> wouldn't mind prefixing all of them.
> https://github.com/airblade/paper_trail/issues/703

Solution
--------

Add only two methods to the AR model.

1. An instance method `#paper_trail`
2. A class method `.paper_trail`

The instance method would return a `RecordTrail` and the class method would
return a `ClassTrail`. Those names are totally up for debate.

Advantages
----------

- Plain ruby, easy to understand
- Adding new methods to e.g. the `RecordTrail` does not add any methods to
  the AR model.
- Methods privacy is more strongly enforced.
- Enables isolated testing of e.g. `RecordTrail`; it can be constructed with a
  mock AR instance.

Disadvantages
-------------

- Two new classes, though they are simple.
2016-06-06 01:18:31 -04:00
Chris Barton e1f94d4597 Maps enums to database values before storing in `object_changes`
Keep consistency between versions with regard to `changes` and
`object_changes` and how enum columns store their values.

Before, `changes` would map the changed attributes enum columns to the database
values (integer values). This allows reifying that version to maintain the
integrity of the enum. It did not do so for `object_changes` and thus, `0`
for non-json columns, and the enum value for json columns would be stored instead.
For the non-json columns, it mapped any non-integer enum value to `0` because
during serialization that column is an `integer`.  Now this is fixed,
so that `object_changes` stores the enum mapped value.

Here is an example:

```ruby
class PostWithStatus < ActiveRecord::Base
  has_paper_trail
  enum status: { draft: 0, published: 1, archived: 2 }
end

post = PostWithStatus.new(status: :draft)
post.published!
version = post.versions.last

 # Before
version.changeset #> { 'status' => ['draft', 'draft'] } (stored as [0, 0])

 # After
version.changeset #> { 'status' => ['draft', 'published'] } (stored as [0, 1])
```
2016-03-11 22:11:59 -08:00
Jared Beck 8980f08d0e Fix Style/StringLiterals: Use double quotes 2016-03-05 17:07:32 -05:00
Jared Beck 10941c8459 Fix Style/HashSyntax 2016-02-15 22:32:40 -05:00
Ben Atkins 716c3e0fb5 Use RSpec 3 for testing; Update syntax in spec suite for RSpec 3 2014-10-09 15:04:17 -04:00
Ben Atkins d9cf951dae Only run tests for ActiveRecord::Enum when it is defined 2014-06-17 11:40:31 -04:00