Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An idea to combat model pollution #719

Merged
merged 1 commit into from
Jun 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 0 additions & 4 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ Metrics/AbcSize:
Exclude:
- 'test/dummy/db/migrate/*'

Metrics/ClassLength:
Exclude:
- test/**/*

# The Ruby Style Guide recommends to "Limit lines to 80 characters."
# (https://github.com/bbatsov/ruby-style-guide#80-character-limits)
# but 100 is also reasonable.
Expand Down
7 changes: 6 additions & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
Metrics/AbcSize:
Max: 30 # Goal: 15

Metrics/ClassLength:
Max: 400
Exclude:
- test/**/*

Metrics/CyclomaticComplexity:
Max: 13 # Goal: 6

Metrics/ModuleLength:
Max: 313
Max: 317

Metrics/PerceivedComplexity:
Max: 16 # Goal: 7
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
## 5.?.? (Unreleased)
## 5.2.0 (Unreleased)

### Breaking Changes

- None

### Deprecated

- [#719](https://github.com/airblade/paper_trail/pull/719) -
The majority of model methods. Use paper_trail.x instead of x. Why? Your
models are a crowded namespace, and we want to get out of your way!

### Added

- None
Expand Down
74 changes: 38 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,35 +170,35 @@ widget.version

# Returns true if this widget is the current, live one; or false if it is from
# a previous version.
widget.live?
widget.paper_trail.live?

# Returns who put the widget into its current state.
widget.paper_trail_originator
widget.paper_trail.originator

# Returns the widget (not a version) as it looked at the given timestamp.
widget.version_at(timestamp)
widget.paper_trail.version_at(timestamp)

# Returns the widget (not a version) as it was most recently.
widget.previous_version
widget.paper_trail.previous_version

# Returns the widget (not a version) as it became next.
widget.next_version
widget.paper_trail.next_version

# Generates a version for a `touch` event (`widget.touch` does NOT generate a
# version)
widget.touch_with_version
widget.paper_trail.touch_with_version

# Turn PaperTrail off for all widgets.
Widget.paper_trail_off!
Widget.paper_trail.disable

# Turn PaperTrail on for all widgets.
Widget.paper_trail_on!
Widget.paper_trail.enable

# Is PaperTrail enabled for Widget, the class?
Widget.paper_trail_enabled_for_model?
Widget.paper_trail.enabled?

# Is PaperTrail enabled for widget, the instance?
widget.paper_trail_enabled_for_model?
widget.paper_trail.enabled_for_model?
```

And a `PaperTrail::Version` instance (which is just an ordinary ActiveRecord
Expand Down Expand Up @@ -301,13 +301,13 @@ class Article < ActiveRecord::Base
has_paper_trail :on => []

# Add callbacks in the order you need.
paper_trail_on_destroy # add destroy callback
paper_trail_on_update # etc.
paper_trail_on_create
paper_trail.on_destroy # add destroy callback
paper_trail.on_update # etc.
paper_trail.on_create
end
```

The `paper_trail_on_destroy` method can be further configured to happen
The `paper_trail.on_destroy` method can be further configured to happen
`:before` or `:after` the destroy event. In PaperTrail 4, the default is
`:after`. In PaperTrail 5, the default will be `:before`, to support
ActiveRecord 5. (see https://github.com/airblade/paper_trail/pull/683)
Expand Down Expand Up @@ -353,7 +353,7 @@ a.update_attributes :title => 'My Title', :rating => 3
a.versions.length # 1
a.update_attributes :title => 'Greeting', :content => 'Hello'
a.versions.length # 2
a.previous_version.title # 'My Title'
a.paper_trail.previous_version.title # 'My Title'
```

Or, you can specify a list of all attributes you care about:
Expand All @@ -373,7 +373,7 @@ a.update_attributes :title => 'My Title'
a.versions.length # 2
a.update_attributes :content => 'Hello'
a.versions.length # 2
a.previous_version.content # nil
a.paper_trail.previous_version.content # nil
```

The `:ignore` and `:only` options can also accept `Hash` arguments, where the :
Expand All @@ -396,10 +396,10 @@ a.update_attributes :title => 'My Title'
a.versions.length # 3
a.update_attributes :content => 'Hai'
a.versions.length # 3
a.previous_version.content # "Hello"
a.paper_trail.previous_version.content # "Hello"
a.update_attributes :title => 'Dif Title'
a.versions.length # 4
a.previous_version.content # "Hai"
a.paper_trail.previous_version.content # "Hai"
```

Passing both `:ignore` and `:only` options will result in the article being
Expand Down Expand Up @@ -454,8 +454,8 @@ end
#### Per Class

```ruby
Widget.paper_trail_off!
Widget.paper_trail_on!
Widget.paper_trail.disable
Widget.paper_trail.enable
```

#### Per Method
Expand All @@ -464,13 +464,13 @@ You can call a method without creating a new version using `without_versioning`.
It takes either a method name as a symbol:

```ruby
@widget.without_versioning :destroy
@widget.paper_trail.without_versioning :destroy
```

Or a block:

```ruby
@widget.without_versioning do
@widget.paper_trail.without_versioning do
@widget.update_attributes :name => 'Ford'
end
```
Expand All @@ -497,15 +497,15 @@ PaperTrail makes reverting to a previous version easy:
widget = Widget.find 42
widget.update_attributes :name => 'Blah blah'
# Time passes....
widget = widget.previous_version # the widget as it was before the update
widget.save # reverted
widget = widget.paper_trail.previous_version # the widget as it was before the update
widget.save # reverted
```

Alternatively you can find the version at a given time:

```ruby
widget = widget.version_at(1.day.ago) # the widget as it was one day ago
widget.save # reverted
widget = widget.paper_trail.version_at(1.day.ago) # the widget as it was one day ago
widget.save # reverted
```

Note `version_at` gives you the object, not a version, so you don't need to call
Expand All @@ -518,7 +518,7 @@ widget = Widget.find 42
widget.destroy
# Time passes....
widget = PaperTrail::Version.find(153).reify # the widget as it was before destruction
widget.save # the widget lives!
widget.save # the widget lives!
```

You could even use PaperTrail to implement an undo system, [Ryan Bates has!][3]
Expand All @@ -533,11 +533,11 @@ was/became. Note that these methods reify the item for you.

```ruby
live_widget = Widget.find 42
live_widget.versions.length # 4 for example
widget = live_widget.previous_version # => widget == live_widget.versions.last.reify
widget = widget.previous_version # => widget == live_widget.versions[-2].reify
widget = widget.next_version # => widget == live_widget.versions.last.reify
widget.next_version # live_widget
live_widget.versions.length # 4, for example
widget = live_widget.paper_trail.previous_version # => widget == live_widget.versions.last.reify
widget = widget.paper_trail.previous_version # => widget == live_widget.versions[-2].reify
widget = widget.paper_trail.next_version # => widget == live_widget.versions.last.reify
widget.paper_trail.next_version # live_widget
```

If instead you have a particular `version` of an item you can navigate to the
Expand Down Expand Up @@ -571,7 +571,7 @@ it came instead from a previous version -- with `live?`:
```ruby
widget = Widget.find 42
widget.live? # true
widget = widget.previous_version
widget = widget.paper_trail.previous_version
widget.live? # false
```

Expand Down Expand Up @@ -728,10 +728,10 @@ like it does, call `paper_trail_originator` on the object.
widget = Widget.find 153 # assume widget has 0 versions
PaperTrail.whodunnit = 'Alice'
widget.update_attributes :name => 'Yankee'
widget.paper_trail_originator # 'Alice'
widget.paper_trail.originator # 'Alice'
PaperTrail.whodunnit = 'Bob'
widget.update_attributes :name => 'Zulu'
widget.paper_trail_originator # 'Bob'
widget.paper_trail.originator # 'Bob'
first_version, last_version = widget.versions.first, widget.versions.last
first_version.whodunnit # 'Alice'
first_version.paper_trail_originator # nil
Expand Down Expand Up @@ -1068,7 +1068,9 @@ class Post < ActiveRecord::Base
end
```

Unlike ActiveRecord's `class_name`, you'll have to supply the complete module path to the class (e.g. `Foo::BarVersion` if your class is inside the module `Foo`).
Unlike ActiveRecord's `class_name`, you'll have to supply the complete module
path to the class (e.g. `Foo::BarVersion` if your class is inside the module
`Foo`).

#### Advantages

Expand Down
5 changes: 5 additions & 0 deletions lib/paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ module PaperTrail
extend PaperTrail::Cleaner

class << self
# @api private
def clear_transaction_id
self.transaction_id = nil
end

# Switches PaperTrail on or off.
# @api public
def enabled=(value)
Expand Down
2 changes: 1 addition & 1 deletion lib/paper_trail/attribute_serializers/object_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def alter(attributes, serialization_method)
end

def object_col_is_json?
@model_class.paper_trail_version_class.object_col_is_json?
@model_class.paper_trail.version_class.object_col_is_json?
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def alter(changes, serialization_method)
end

def object_changes_col_is_json?
@item_class.paper_trail_version_class.object_changes_col_is_json?
@item_class.paper_trail.version_class.object_changes_col_is_json?
end
end
end
Expand Down