Skip to content

Commit

Permalink
Merge pull request #582 from airblade/formatting_comments
Browse files Browse the repository at this point in the history
Format comments
  • Loading branch information
jaredbeck committed Aug 3, 2015
2 parents bdcd1f9 + 68a0ab7 commit b5c2bce
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 147 deletions.
30 changes: 17 additions & 13 deletions lib/paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def self.enabled?
end

# ActiveRecord 5 drops support for serialized attributes; for previous
# versions of ActiveRecord it is supported, we have a config option
# versions of ActiveRecord it is supported, we have a config option
# to enable it within PaperTrail.
def self.serialized_attributes?
!!PaperTrail.config.serialized_attributes && ::ActiveRecord::VERSION::MAJOR < 5
Expand All @@ -43,12 +43,14 @@ def self.enabled_for_controller?
!!paper_trail_store[:request_enabled_for_controller]
end

# Sets whether PaperTrail is enabled or disabled for this model in the current request.
# Sets whether PaperTrail is enabled or disabled for this model in the
# current request.
def self.enabled_for_model(model, value)
paper_trail_store[:"enabled_for_#{model}"] = value
end

# Returns `true` if PaperTrail is enabled for this model in the current request, `false` otherwise.
# Returns `true` if PaperTrail is enabled for this model in the current
# request, `false` otherwise.
def self.enabled_for_model?(model)
!!paper_trail_store.fetch(:"enabled_for_#{model}", true)
end
Expand All @@ -63,10 +65,9 @@ def self.timestamp_field
PaperTrail.config.timestamp_field
end

# Sets who is responsible for any changes that occur.
# You would normally use this in a migration or on the console,
# when working with models directly. In a controller it is set
# automatically to the `current_user`.
# Sets who is responsible for any changes that occur. You would normally use
# this in a migration or on the console, when working with models directly.
# In a controller it is set automatically to the `current_user`.
def self.whodunnit=(value)
paper_trail_store[:whodunnit] = value
end
Expand All @@ -76,8 +77,8 @@ def self.whodunnit
paper_trail_store[:whodunnit]
end

# Sets any information from the controller that you want PaperTrail
# to store. By default this is set automatically by a before filter.
# Sets any information from the controller that you want PaperTrail to
# store. By default this is set automatically by a before filter.
def self.controller_info=(value)
paper_trail_store[:controller_info] = value
end
Expand Down Expand Up @@ -117,8 +118,8 @@ def self.transaction_id=(id)

private

# Thread-safe hash to hold PaperTrail's data.
# Initializing with needed default values.
# Thread-safe hash to hold PaperTrail's data. Initializing with needed
# default values.
def self.paper_trail_store
RequestStore.store[:paper_trail] ||= { :request_enabled_for_controller => true }
end
Expand All @@ -135,12 +136,15 @@ class << self
end
end

# Ensure `ProtectedAttributes` gem gets required if it is available before the `Version` class gets loaded in
# Ensure `ProtectedAttributes` gem gets required if it is available before the
# `Version` class gets loaded in.
unless PaperTrail.active_record_protected_attributes?
PaperTrail.send(:remove_instance_variable, :@active_record_protected_attributes)
begin
require 'protected_attributes'
rescue LoadError; end # will rescue if `ProtectedAttributes` gem is not available
rescue LoadError
# In case `ProtectedAttributes` gem is not available.
end
end

ActiveSupport.on_load(:active_record) do
Expand Down
32 changes: 21 additions & 11 deletions lib/paper_trail/cleaner.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
module PaperTrail
module Cleaner
# Destroys all but the most recent version(s) for items on a given date (or on all dates). Useful for deleting drafts.
# Destroys all but the most recent version(s) for items on a given date
# (or on all dates). Useful for deleting drafts.
#
# Options:
# :keeping An `integer` indicating the number of versions to be kept for each item per date.
# Defaults to `1`.
# :date Should either be a `Date` object specifying which date to destroy versions for or `:all`,
# which will specify that all dates should be cleaned. Defaults to `:all`.
# :item_id The `id` for the item to be cleaned on, or `nil`, which causes all items to be cleaned.
# Defaults to `nil`.
#
# - :keeping - An `integer` indicating the number of versions to be kept for
# each item per date. Defaults to `1`.
# - :date - Should either be a `Date` object specifying which date to
# destroy versions for or `:all`, which will specify that all dates
# should be cleaned. Defaults to `:all`.
# - :item_id - The `id` for the item to be cleaned on, or `nil`, which
# causes all items to be cleaned. Defaults to `nil`.
#
def clean_versions!(options = {})
options = {:keeping => 1, :date => :all}.merge(options)
gather_versions(options[:item_id], options[:date]).each do |item_id, versions|
versions.group_by { |v| v.send(PaperTrail.timestamp_field).to_date }.each do |date, _versions|
# remove the number of versions we wish to keep from the collection of versions prior to destruction
# Remove the number of versions we wish to keep from the collection
# of versions prior to destruction.
_versions.pop(options[:keeping])
_versions.map(&:destroy)
end
Expand All @@ -22,13 +27,18 @@ def clean_versions!(options = {})

private

# Returns a hash of versions grouped by the `item_id` attribute formatted like this: {:item_id => PaperTrail::Version}.
# If `item_id` or `date` is set, versions will be narrowed to those pointing at items with those ids that were created on specified date.
# Returns a hash of versions grouped by the `item_id` attribute formatted
# like this: {:item_id => PaperTrail::Version}. If `item_id` or `date` is
# set, versions will be narrowed to those pointing at items with those ids
# that were created on specified date.
def gather_versions(item_id = nil, date = :all)
raise ArgumentError.new("`date` argument must receive a Timestamp or `:all`") unless date == :all || date.respond_to?(:to_date)
versions = item_id ? PaperTrail::Version.where(:item_id => item_id) : PaperTrail::Version
versions = versions.between(date.to_date, date.to_date + 1.day) unless date == :all
versions = PaperTrail::Version.all if versions == PaperTrail::Version # if versions has not been converted to an ActiveRecord::Relation yet, do so now

# If `versions` has not been converted to an ActiveRecord::Relation yet,
# do so now.
versions = PaperTrail::Version.all if versions == PaperTrail::Version
versions.group_by(&:item_id)
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/paper_trail/frameworks/active_record.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# This file only needs to be loaded if the gem is being used outside of Rails, since otherwise
# the model(s) will get loaded in via the `Rails::Engine`
# This file only needs to be loaded if the gem is being used outside of Rails,
# since otherwise the model(s) will get loaded in via the `Rails::Engine`.
require "paper_trail/frameworks/active_record/models/paper_trail/version_association"
require "paper_trail/frameworks/active_record/models/paper_trail/version"
20 changes: 11 additions & 9 deletions lib/paper_trail/frameworks/rails/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,26 @@ def user_for_paper_trail
#
# The columns `ip` and `user_agent` must exist in your `versions` # table.
#
# Use the `:meta` option to `PaperTrail::Model::ClassMethods.has_paper_trail`
# to store any extra model-level data you need.
# Use the `:meta` option to
# `PaperTrail::Model::ClassMethods.has_paper_trail` to store any extra
# model-level data you need.
def info_for_paper_trail
{}
end

# Returns `true` (default) or `false` depending on whether PaperTrail should
# be active for the current request.
# Returns `true` (default) or `false` depending on whether PaperTrail
# should be active for the current request.
#
# Override this method in your controller to specify when PaperTrail should
# be off.
# Override this method in your controller to specify when PaperTrail
# should be off.
def paper_trail_enabled_for_controller
::PaperTrail.enabled?
end

private

# Tells PaperTrail whether versions should be saved in the current request.
# Tells PaperTrail whether versions should be saved in the current
# request.
def set_paper_trail_enabled_for_controller
::PaperTrail.enabled_for_controller = paper_trail_enabled_for_controller
end
Expand All @@ -63,8 +65,8 @@ def set_paper_trail_whodunnit
::PaperTrail.whodunnit = user_for_paper_trail if ::PaperTrail.enabled_for_controller?
end

# Tells PaperTrail any information from the controller you want
# to store alongside any changes that occur.
# Tells PaperTrail any information from the controller you want to store
# alongside any changes that occur.
def set_paper_trail_controller_info
::PaperTrail.controller_info = info_for_paper_trail if ::PaperTrail.enabled_for_controller?
end
Expand Down
3 changes: 2 additions & 1 deletion lib/paper_trail/frameworks/sinatra.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
module PaperTrail
module Sinatra

# Register this module inside your Sinatra application to gain access to controller-level methods used by PaperTrail
# Register this module inside your Sinatra application to gain access to
# controller-level methods used by PaperTrail.
def self.registered(app)
app.use RequestStore::Middleware
app.helpers self
Expand Down
Loading

0 comments on commit b5c2bce

Please sign in to comment.