track_changes 1.0.0.pre1 → 1.0.0.pre2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION.yml +1 -1
 - data/app/helpers/audits_helper.rb +36 -0
 - data/app/models/audit.rb +54 -0
 - data/app/views/audits/_audit.html.erb +15 -0
 - data/lib/track_changes/action_controller.rb +2 -0
 - data/lib/track_changes/active_record.rb +2 -0
 - data/lib/track_changes/engine.rb +2 -0
 - data/lib/track_changes.rb +8 -10
 - metadata +6 -3
 
    
        data/VERSION.yml
    CHANGED
    
    
| 
         @@ -0,0 +1,36 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            module AuditsHelper
         
     | 
| 
      
 2 
     | 
    
         
            +
              # Given a <tt>value</tt>, returns the string NONE if the value is <tt>blank?</tt>,
         
     | 
| 
      
 3 
     | 
    
         
            +
              # otherwise returns the given <tt>value</tt>
         
     | 
| 
      
 4 
     | 
    
         
            +
              def blank_is_none(value)
         
     | 
| 
      
 5 
     | 
    
         
            +
                value.blank? ? "NONE" : value
         
     | 
| 
      
 6 
     | 
    
         
            +
              end
         
     | 
| 
      
 7 
     | 
    
         
            +
             
     | 
| 
      
 8 
     | 
    
         
            +
              # Allows you to iterate over a given Audit#change_set. Given a block, each time
         
     | 
| 
      
 9 
     | 
    
         
            +
              # yield is called, it will return the attribute, the old version and the new
         
     | 
| 
      
 10 
     | 
    
         
            +
              # version as arguments. If the Audit is a message audit, it will set the new
         
     | 
| 
      
 11 
     | 
    
         
            +
              # attribute to the audit message.
         
     | 
| 
      
 12 
     | 
    
         
            +
              #
         
     | 
| 
      
 13 
     | 
    
         
            +
              # Example:
         
     | 
| 
      
 14 
     | 
    
         
            +
              #  <% change_set_iter(audit.change_set) do |attr, old, new| %>
         
     | 
| 
      
 15 
     | 
    
         
            +
              #    <p class="audit"><strong><%=h attr %></strong>
         
     | 
| 
      
 16 
     | 
    
         
            +
              #      <% if audit.message %>
         
     | 
| 
      
 17 
     | 
    
         
            +
              #        <em><%= new %></em>
         
     | 
| 
      
 18 
     | 
    
         
            +
              #      <% else %>
         
     | 
| 
      
 19 
     | 
    
         
            +
              #        changed from <em><%=h old %></em> to <em><%=h new %></em>.
         
     | 
| 
      
 20 
     | 
    
         
            +
              #      <% end %>
         
     | 
| 
      
 21 
     | 
    
         
            +
              #    </p>
         
     | 
| 
      
 22 
     | 
    
         
            +
              #  <% end %>
         
     | 
| 
      
 23 
     | 
    
         
            +
              def change_set_iter(change_set, &block)
         
     | 
| 
      
 24 
     | 
    
         
            +
                change_set.each_pair do |attr, change|
         
     | 
| 
      
 25 
     | 
    
         
            +
                  if change.is_a?(Array)
         
     | 
| 
      
 26 
     | 
    
         
            +
                    old_val = blank_is_none(change.first)
         
     | 
| 
      
 27 
     | 
    
         
            +
                    new_val = blank_is_none(change.last)
         
     | 
| 
      
 28 
     | 
    
         
            +
                  else
         
     | 
| 
      
 29 
     | 
    
         
            +
                    old_val = blank_is_none("")
         
     | 
| 
      
 30 
     | 
    
         
            +
                    new_val = blank_is_none(change)
         
     | 
| 
      
 31 
     | 
    
         
            +
                  end
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
                  yield attr.titleize, old_val, new_val
         
     | 
| 
      
 34 
     | 
    
         
            +
                end
         
     | 
| 
      
 35 
     | 
    
         
            +
              end
         
     | 
| 
      
 36 
     | 
    
         
            +
            end
         
     | 
    
        data/app/models/audit.rb
    ADDED
    
    | 
         @@ -0,0 +1,54 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # == Audit
         
     | 
| 
      
 2 
     | 
    
         
            +
            # A model for handling audits. Expects a User model to be available.
         
     | 
| 
      
 3 
     | 
    
         
            +
            #
         
     | 
| 
      
 4 
     | 
    
         
            +
            # Provides the following scopes:
         
     | 
| 
      
 5 
     | 
    
         
            +
            #
         
     | 
| 
      
 6 
     | 
    
         
            +
            # * <tt>desc</tt> - Orders by <tt>created_at</tt> DESC and then <tt>id</tt> DESC
         
     | 
| 
      
 7 
     | 
    
         
            +
            # * <tt>recent</tt> - Returns the last 25 audits
         
     | 
| 
      
 8 
     | 
    
         
            +
            # * <tt>changes_on_attributes</tt> - Returns the audits with changes to the given attribute.
         
     | 
| 
      
 9 
     | 
    
         
            +
            #
         
     | 
| 
      
 10 
     | 
    
         
            +
            # If you want to simply provide some notification of an event or a message.
         
     | 
| 
      
 11 
     | 
    
         
            +
            # Assign to the <tt>messsage</tt> attribute instead of the <tt>change_set</tt>
         
     | 
| 
      
 12 
     | 
    
         
            +
            # attribute.
         
     | 
| 
      
 13 
     | 
    
         
            +
            #
         
     | 
| 
      
 14 
     | 
    
         
            +
            # == Schema Information
         
     | 
| 
      
 15 
     | 
    
         
            +
            #
         
     | 
| 
      
 16 
     | 
    
         
            +
            # Table name: audits
         
     | 
| 
      
 17 
     | 
    
         
            +
            #
         
     | 
| 
      
 18 
     | 
    
         
            +
            #  id           :integer         not null, primary key
         
     | 
| 
      
 19 
     | 
    
         
            +
            #  audited_type :string(255)     not null
         
     | 
| 
      
 20 
     | 
    
         
            +
            #  audited_id   :integer         not null
         
     | 
| 
      
 21 
     | 
    
         
            +
            #  user_id      :integer         not null
         
     | 
| 
      
 22 
     | 
    
         
            +
            #  change_set   :text            not null
         
     | 
| 
      
 23 
     | 
    
         
            +
            #  created_at   :datetime        not null
         
     | 
| 
      
 24 
     | 
    
         
            +
            #
         
     | 
| 
      
 25 
     | 
    
         
            +
            class Audit < ActiveRecord::Base
         
     | 
| 
      
 26 
     | 
    
         
            +
              MESSAGE = "Message"
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
              belongs_to :audited, :polymorphic => true
         
     | 
| 
      
 29 
     | 
    
         
            +
              belongs_to :user
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
              serialize :change_set, Hash
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
              scope :desc, :order => "audits.created_at DESC, audits.id DESC"
         
     | 
| 
      
 34 
     | 
    
         
            +
              scope :recent, :order => "aduits.created_at DESC", :limit => 25
         
     | 
| 
      
 35 
     | 
    
         
            +
              scope :changees_on_attribute, lambda { |attr|
         
     | 
| 
      
 36 
     | 
    
         
            +
                { :conditions => ["audits.change_set LIKE ?", "%#{attr}:%"]}
         
     | 
| 
      
 37 
     | 
    
         
            +
              }
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
              # Create an Audit given the <tt>model</tt>, <tt>change_set</tt> and <tt>user</tt>.
         
     | 
| 
      
 40 
     | 
    
         
            +
              def self.create_changes(model, change_set, user=nil)
         
     | 
| 
      
 41 
     | 
    
         
            +
                user_id = user && user.id
         
     | 
| 
      
 42 
     | 
    
         
            +
                self.create!(:audited => model, :user_id => user_id, :change_set => change_set)
         
     | 
| 
      
 43 
     | 
    
         
            +
              end
         
     | 
| 
      
 44 
     | 
    
         
            +
             
     | 
| 
      
 45 
     | 
    
         
            +
              # Returns a message if this audit was a message.
         
     | 
| 
      
 46 
     | 
    
         
            +
              def message
         
     | 
| 
      
 47 
     | 
    
         
            +
                self.change_set.has_key?(MESSAGE) && self.change_set[MESSAGE]
         
     | 
| 
      
 48 
     | 
    
         
            +
              end
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
      
 50 
     | 
    
         
            +
              # Sets this audit to be a message with the given message.
         
     | 
| 
      
 51 
     | 
    
         
            +
              def message=(message)
         
     | 
| 
      
 52 
     | 
    
         
            +
                self.change_set = {MESSAGE => message}
         
     | 
| 
      
 53 
     | 
    
         
            +
              end
         
     | 
| 
      
 54 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,15 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            <li class="audit">
         
     | 
| 
      
 2 
     | 
    
         
            +
              <h4 class="audit"><%= audit.audited.class.to_s.humanize %> Modification
         
     | 
| 
      
 3 
     | 
    
         
            +
                <span class="audit_meta">by <%= audit.try(:user).try(:name) %> on <%= audit.created_at.to_formatted_s(:long) %></span>
         
     | 
| 
      
 4 
     | 
    
         
            +
              </h4>
         
     | 
| 
      
 5 
     | 
    
         
            +
              <% change_set_iter(audit.change_set) do |attr, old, new| %>
         
     | 
| 
      
 6 
     | 
    
         
            +
                <p class="audit"><strong><%=h attr %></strong>
         
     | 
| 
      
 7 
     | 
    
         
            +
                  <% if nil %>
         
     | 
| 
      
 8 
     | 
    
         
            +
                  <%# if audit.message %>
         
     | 
| 
      
 9 
     | 
    
         
            +
                    <em><%= new %></em>
         
     | 
| 
      
 10 
     | 
    
         
            +
                  <% else %>
         
     | 
| 
      
 11 
     | 
    
         
            +
                    changed from <em><%=h old %></em> to <em><%=h new %></em>.
         
     | 
| 
      
 12 
     | 
    
         
            +
                  <% end %>
         
     | 
| 
      
 13 
     | 
    
         
            +
                </p>
         
     | 
| 
      
 14 
     | 
    
         
            +
              <% end %>
         
     | 
| 
      
 15 
     | 
    
         
            +
            </li>
         
     | 
    
        data/lib/track_changes/engine.rb
    CHANGED
    
    
    
        data/lib/track_changes.rb
    CHANGED
    
    | 
         @@ -1,13 +1,11 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            module TrackChanges
         
     | 
| 
       2 
     | 
    
         
            -
              require 'track_changes/engine' if defined?(Rails) 
     | 
| 
       3 
     | 
    
         
            -
              require 'track_changes/action_controller'
         
     | 
| 
       4 
     | 
    
         
            -
              require 'track_changes/active_record'
         
     | 
| 
       5 
     | 
    
         
            -
              require 'track_changes/around_save'
         
     | 
| 
       6 
     | 
    
         
            -
              require 'track_changes/changes'
         
     | 
| 
       7 
     | 
    
         
            -
              require 'track_changes/configuration'
         
     | 
| 
       8 
     | 
    
         
            -
              require 'track_changes/current_user'
         
     | 
| 
       9 
     | 
    
         
            -
              require 'track_changes/current_user_filter'
         
     | 
| 
      
 2 
     | 
    
         
            +
              require 'track_changes/engine' if defined?(Rails)
         
     | 
| 
       10 
3 
     | 
    
         
             
            end
         
     | 
| 
       11 
4 
     | 
    
         | 
| 
       12 
     | 
    
         
            -
             
     | 
| 
       13 
     | 
    
         
            -
             
     | 
| 
      
 5 
     | 
    
         
            +
            require 'track_changes/action_controller'
         
     | 
| 
      
 6 
     | 
    
         
            +
            require 'track_changes/active_record'
         
     | 
| 
      
 7 
     | 
    
         
            +
            require 'track_changes/around_save'
         
     | 
| 
      
 8 
     | 
    
         
            +
            require 'track_changes/changes'
         
     | 
| 
      
 9 
     | 
    
         
            +
            require 'track_changes/configuration'
         
     | 
| 
      
 10 
     | 
    
         
            +
            require 'track_changes/current_user'
         
     | 
| 
      
 11 
     | 
    
         
            +
            require 'track_changes/current_user_filter'
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -6,8 +6,8 @@ version: !ruby/object:Gem::Version 
     | 
|
| 
       6 
6 
     | 
    
         
             
                - 1
         
     | 
| 
       7 
7 
     | 
    
         
             
                - 0
         
     | 
| 
       8 
8 
     | 
    
         
             
                - 0
         
     | 
| 
       9 
     | 
    
         
            -
                -  
     | 
| 
       10 
     | 
    
         
            -
              version: 1.0.0. 
     | 
| 
      
 9 
     | 
    
         
            +
                - pre2
         
     | 
| 
      
 10 
     | 
    
         
            +
              version: 1.0.0.pre2
         
     | 
| 
       11 
11 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       12 
12 
     | 
    
         
             
            authors: 
         
     | 
| 
       13 
13 
     | 
    
         
             
              - Matt Haley
         
     | 
| 
         @@ -15,7 +15,7 @@ autorequire: 
     | 
|
| 
       15 
15 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       16 
16 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       17 
17 
     | 
    
         | 
| 
       18 
     | 
    
         
            -
            date: 2010-08- 
     | 
| 
      
 18 
     | 
    
         
            +
            date: 2010-08-20 00:00:00 -07:00
         
     | 
| 
       19 
19 
     | 
    
         
             
            default_executable: 
         
     | 
| 
       20 
20 
     | 
    
         
             
            dependencies: 
         
     | 
| 
       21 
21 
     | 
    
         
             
              - !ruby/object:Gem::Dependency 
         
     | 
| 
         @@ -74,6 +74,9 @@ files: 
     | 
|
| 
       74 
74 
     | 
    
         
             
              - LICENSE
         
     | 
| 
       75 
75 
     | 
    
         
             
              - TODO
         
     | 
| 
       76 
76 
     | 
    
         
             
              - VERSION.yml
         
     | 
| 
      
 77 
     | 
    
         
            +
              - app/helpers/audits_helper.rb
         
     | 
| 
      
 78 
     | 
    
         
            +
              - app/models/audit.rb
         
     | 
| 
      
 79 
     | 
    
         
            +
              - app/views/audits/_audit.html.erb
         
     | 
| 
       77 
80 
     | 
    
         
             
              - lib/generators/track_changes/templates/initializer.rb
         
     | 
| 
       78 
81 
     | 
    
         
             
              - lib/generators/track_changes/templates/migration.rb
         
     | 
| 
       79 
82 
     | 
    
         
             
              - lib/generators/track_changes/templates/stylesheet.css
         
     |