Skip to content

Commit

Permalink
An idea to combat model pollution
Browse files Browse the repository at this point in the history
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.
> #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.
  • Loading branch information
jaredbeck committed May 26, 2016
1 parent 17c8f34 commit f7e23e4
Show file tree
Hide file tree
Showing 20 changed files with 604 additions and 434 deletions.
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: 151
Exclude:
- test/**/*

Metrics/CyclomaticComplexity:
Max: 13 # Goal: 6

Metrics/ModuleLength:
Max: 313
Max: 317

Metrics/PerceivedComplexity:
Max: 16 # Goal: 7
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

0 comments on commit f7e23e4

Please sign in to comment.