Skip to content

Commit

Permalink
Merge pull request #28 from kwstannard/simplify_change_title_for
Browse files Browse the repository at this point in the history
Simplify change_title_for
  • Loading branch information
reidab committed Sep 18, 2015
2 parents 7f75d39 + 8d3f1f5 commit 34cce04
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 15 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Restart the server and go to the `/changes` URI to browse, subscribe, view and r
### Configuration

Several aspects of PaperTrailManager may be optionally configured
by creating an initializer in your application
by creating an initializer in your application
(e.g. `config/initializers/paper_trail_manager.rb`).

To specify when reverts are allowed:
Expand All @@ -49,6 +49,10 @@ To specify how to look up users/memebers/etc specified in Paper Trail's 'whodunn
PaperTrailManager.whodunnit_class = User
PaperTrailManager.whodunnit_name_method = :nicename # defaults to :name

To specify a method to call to identify an item on an index page:

PaperTrailManager.item_name_method = :nicename

And for linking (or not) to the user with a custom path helper:

PaperTrailManager.user_path_method = :admin_path # defaults to :user_path
Expand Down
16 changes: 5 additions & 11 deletions app/helpers/paper_trail_manager/changes_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,11 @@ def changes_for(version)

# Returns string title for the versioned record.
def change_title_for(version)
current = version.next.try(:reify)
previous = version.reify rescue nil
record = version.item_type.constantize.find(version.item_id) rescue nil

name = nil
[:name, :title, :to_s].each do |name_method|
[previous, current, record].each do |obj|
name = obj.send(name_method) if obj.respond_to?(name_method)
break if name
end
break if name
if PaperTrailManager.item_name_method
record = version_reify(version) || version.next.try(:reify) || version.item
name = record.send(PaperTrailManager.item_name_method)
else
name = "#{version.item_type} #{version.item_id}"
end

return h(name)
Expand Down
2 changes: 0 additions & 2 deletions app/views/paper_trail_manager/changes/_version.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
<td class='change_details'>
<p class='change_details_description'>
<strong class='event'><%= version.event %></strong>
<span class='item_type'><%= version.item_type %></span>
<span class='item_id'><%= version.item_id %></span>
<%= change_item_link(version) %>
<% if PaperTrailManager.whodunnit_class && version.whodunnit %>
<% if user = PaperTrailManager.whodunnit_class.find(version.whodunnit) rescue nil %>
Expand Down
2 changes: 1 addition & 1 deletion lib/paper_trail_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class PaperTrailManager < Rails::Engine

@@whodunnit_name_method = :name
cattr_accessor :whodunnit_class, :whodunnit_name_method, :route_helpers,
:layout, :base_controller, :user_path_method
:layout, :base_controller, :user_path_method, :item_name_method

self.base_controller = "ApplicationController"
self.user_path_method = :user_path
Expand Down

0 comments on commit 34cce04

Please sign in to comment.