wiser_trails 2.0.0 → 2.1.0

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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- Y2E1MmRmZmNmYjVmZWU1MWY4MjNlNWI3MzlkOTk0MjcxYTFhNWZiZA==
4
+ MzZmZDkyMjc4NDdhYTJiMzMxY2IzOTliMDZjZTk1NzhjMTExODlkNA==
5
5
  data.tar.gz: !binary |-
6
- MGVjZTU3ZWYxNTk3NjE2ODFiMzFkNDgxYjBmYjA2NmM4NGNkYTZmNw==
6
+ YjU2NTAwZGIwOGIzOGIxYWY0MzIyMDJlNzE2YWNjOWQ4Nzc1NmQ1OQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- ZThiNWU0MGI3OGQ1NTM3M2JiYTk0NWY5OTJlNWY4NzgzMDQyOGIwMjNlZmQx
10
- MmM4OTMxZGMwMzJkZTE5YWM0OTE4MWY0Njc2YjA1NjBkMzk2MDg1ZGYzZDZk
11
- NWIyZTJhMmVhYWVkMGQ0YzZkMzMzZGM2Njk4YjJjZjFlNGVlZGM=
9
+ YWMyNzIxYzUyODRlYzY2NGRlYWM0ZDk5MjdmNTc0NTY5NWFhMDAxMThhOGJi
10
+ Y2QwM2E4NmFiMDEyNjhkYmM3OGE3M2RmNTc5MTA0YjZkNGVjZjY3NTFjODQ2
11
+ NDgxZmUyNTY1YjIwYzQzNjdkNjRiYTEyNWY2ODA2MWM4OTI4ZDM=
12
12
  data.tar.gz: !binary |-
13
- ZGFjMTE4OTVhZTQ5ZTY3YTI4OWE1ZDQ5ZWJmOWJkN2I2ZDg3ZTU3ZDNjMjQ2
14
- ZjIxMmQwN2FhNDBkMDhhNzFiYmU3MDI3NTg5MWIwYWIxNTViZDhkNGQ5ZTUy
15
- ZGE5ODhkMmIxYTdiNWVmMjBlZTI0YjNmYTcyNmMwNWU0NjI1MjA=
13
+ MDYxMzgyNzg2ZDk1NDUzNThiZjE4NWUwOTBmNGFmNzdhYzkzNTVlNGY1N2I3
14
+ YjM0YmJmYjRiNTUwM2FiYzE3NjY5MGMxY2E5ZmIzODYyMmJiYzk5M2RhYmEw
15
+ ODQxODkxMDc0ZDRlYjY3NzlhMmVmZTNiZGU1ZGU4ZTFkMWY5Yjg=
data/README.md CHANGED
@@ -17,7 +17,7 @@ You can do normal gem installation for `wiser_trails`:
17
17
  or in your Gemfile:
18
18
 
19
19
  ```ruby
20
- gem 'wiser_trails', ~> '2.0.0'
20
+ gem 'wiser_trails', '~> 2.0.0'
21
21
  ```
22
22
 
23
23
  Then restart your application.
@@ -65,8 +65,8 @@ Or even the `account` if you're having a multi-account structure:
65
65
  class Notes < ActiveRecord::Base
66
66
  include WiserTrails::Model
67
67
  trail_it
68
- owner: ->(controller, model) { controller && controller.current\_user },
69
- account: ->(controller, model) { controller && controller.current\_account }
68
+ owner: ->(controller, model) { controller && controller.current_user },
69
+ account: ->(controller, model) { controller && controller.current_account }
70
70
  end
71
71
  ```
72
72
 
@@ -10,13 +10,22 @@ module WiserTrails
10
10
  private
11
11
  # Creates activity upon modification of the tracked model
12
12
  def initialize_activity
13
- create_activity(:update) if self.changed_attributes.except("updated_at").count > 0
13
+ changed_attrs = strip_changed_attributes
14
+ create_activity(:update) if changed_attrs.count > 0
14
15
  end
15
16
  def activity_on_update
16
- if self.changed_attributes.except("updated_at").count > 0
17
+ changed_attrs = strip_changed_attributes
18
+ if changed_attrs.count > 0
17
19
  activity = Activity.where(trackable_id: self.id, trackable_type: self.class, key: "#{self.class.to_s.downcase}.update").last
18
20
  activity.update_attribute(:new_value, activity.trackable.attributes.stringify_keys) if activity
19
21
  end
20
22
  end
23
+ def strip_changed_attributes
24
+ changed_attributes = self.changed_attributes
25
+ self.activity_skip_fields_global.each do |attr|
26
+ changed_attributes = changed_attributes.except(attr)
27
+ end
28
+ return changed_attributes
29
+ end
21
30
  end
22
31
  end
@@ -23,8 +23,9 @@ module WiserTrails
23
23
 
24
24
  included do
25
25
  include Trackable
26
- class_attribute :activity_owner_global, :activity_account_global,
27
- :activity_new_value_global, :activity_hooks, :activity_custom_fields_global
26
+ class_attribute :activity_owner_global, :activity_account_global, :activity_skip_fields_global,
27
+ :activity_new_value_global, :activity_hooks, :activity_custom_fields_global,
28
+ :activity_force_fields_global
28
29
  set_wiser_trails_class_defaults
29
30
  end
30
31
 
@@ -71,49 +72,18 @@ module WiserTrails
71
72
  # @return [Hash<Symbol, Object>]
72
73
  attr_accessor :activity_new_value
73
74
  @activity_new_value = {}
74
- # Set or get owner object responsible for the {Activity}.
75
- #
76
- # == Usage:
77
- #
78
- # # where current_user is an object of logged in user
79
- # @article.activity_owner = current_user
80
- # # OR: take @article.author association
81
- # @article.activity_owner = :author
82
- # # OR: provide a Proc with custom code
83
- # @article.activity_owner = proc {|controller, model| model.author }
84
- # @article.save
85
- # @article.activities.last.owner #=> Returns owner object
86
- # @return [Model] Polymorphic model
87
- # @see #activity_owner_global
88
75
  attr_accessor :activity_owner
89
76
  @activity_owner = nil
90
-
91
- # Set or get recipient for activity.
92
- #
93
- # Association is polymorphic, thus allowing assignment of
94
- # all types of models. This can be used for example in the case of sending
95
- # private notifications for only a single user.
96
- # @return (see #activity_owner)
97
77
  attr_accessor :activity_account
98
78
  @activity_account = nil
99
- # Set or get custom i18n key passed to {Activity}, later used in {Renderable#text}
100
- #
101
- # == Usage:
102
- #
103
- # @article = Article.new
104
- # @article.activity_key = "my.custom.article.key"
105
- # @article.save
106
- # @article.activities.last.key #=> "my.custom.article.key"
107
- #
108
- # @return [String]
109
79
  attr_accessor :activity_key
110
80
  @activity_key = nil
111
-
112
- # Set or get custom fields for later processing
113
- #
114
- # @return [Hash]
115
81
  attr_accessor :activity_custom_fields
116
82
  @activity_custom_fields = {}
83
+ attr_accessor :activity_skip_fields_global
84
+ @activity_skip_fields_global = {}
85
+ attr_accessor :activity_force_fields_global
86
+ @activity_force_fields_global = {}
117
87
 
118
88
  # @!visibility private
119
89
  @@activity_hooks = {}
@@ -131,18 +101,10 @@ module WiserTrails
131
101
  self.activity_new_value_global = {}
132
102
  self.activity_hooks = {}
133
103
  self.activity_custom_fields_global = {}
104
+ self.activity_skip_fields_global = {}
105
+ self.activity_force_fields_global = {}
134
106
  end
135
107
 
136
- # Extracts a hook from the _:on_ option provided in
137
- # {Tracked::ClassMethods#tracked}. Returns nil when no hook exists for
138
- # given action
139
- # {Common#get_hook}
140
- #
141
- # @see Tracked#get_hook
142
- # @param key [String, Symbol] action to retrieve a hook for
143
- # @return [Proc, nil] callable hook or nil
144
- # @since 0.4.0
145
- # @api private
146
108
  def get_hook(key)
147
109
  key = key.to_sym
148
110
  if self.activity_hooks.has_key?(key) and self.activity_hooks[key].is_a? Proc
@@ -335,6 +297,8 @@ module WiserTrails
335
297
  @activity_owner = nil
336
298
  @activity_account = nil
337
299
  @activity_custom_fields = {}
300
+ @activity_skip_fields_global = {}
301
+ @activity_force_fields_global = {}
338
302
  end
339
303
  end
340
304
  end
@@ -7,10 +7,10 @@ module WiserTrails
7
7
  def text(params = {})
8
8
  # TODO: some helper for key transformation for two supported formats
9
9
  k = key.split('.')
10
- k.unshift('activity') if k.first != 'activity'
10
+ k.unshift('wiser_trails') if k.first != 'wiser_trails'
11
11
  k = k.join('.')
12
12
 
13
- I18n.t(k, parameters.merge(params) || {})
13
+ I18n.t(k, params || {})
14
14
  end
15
15
 
16
16
  # Renders activity from views.
@@ -91,15 +91,14 @@ module WiserTrails
91
91
 
92
92
  locals = params.delete(:locals) || Hash.new
93
93
 
94
- params_indifferent = self.parameters.with_indifferent_access
94
+ params_indifferent = self.new_value.with_indifferent_access
95
95
  params_indifferent.merge!(params)
96
96
 
97
97
  context.render params.merge(:partial => (partial_path || self.template_path(self.key)),
98
98
  :layout => layout,
99
99
  :locals => locals.merge(:a => self, :activity => self,
100
100
  :controller => controller,
101
- :current_user => controller.respond_to?(:current_user) ?
102
- controller.current_user : nil ,
101
+ :current_user => controller.respond_to?(:current_user) ? controller.current_user : nil,
103
102
  :p => params_indifferent, :params => params_indifferent))
104
103
  end
105
104
 
@@ -2,31 +2,7 @@ module WiserTrails
2
2
  # Main module extending classes we want to keep track of.
3
3
  module TrailIt
4
4
  extend ActiveSupport::Concern
5
- # A shortcut method for setting custom key, owner and parameters of {Activity}
6
- # in one line. Accepts a hash with 3 keys:
7
- # :key, :owner, :params. You can specify all of them or just the ones you want to overwrite.
8
- #
9
- # == Options
10
- #
11
- # [:key]
12
- # See {Common#activity_key}
13
- # [:owner]
14
- # See {Common#activity_owner}
15
- # [:params]
16
- # See {Common#activity_new_value}
17
- # [:recipient]
18
- # Set the recipient for this activity. Useful for private notifications, which should only be visible to a certain user. See {Common#activity_account}.
19
- # @example
20
- #
21
- # @article = Article.new
22
- # @article.title = "New article"
23
- # @article.activity :key => "my.custom.article.key", :owner => @article.author, :params => {:title => @article.title}
24
- # @article.save
25
- # @article.activities.last.key #=> "my.custom.article.key"
26
- # @article.activities.last.parameters #=> {:title => "New article"}
27
- #
28
- # @param options [Hash] instance options to set on the tracked model
29
- # @return [nil]
5
+
30
6
  def activity(options = {})
31
7
  rest = options.clone
32
8
  self.activity_key = rest.delete(:key) if rest[:key]
@@ -39,102 +15,21 @@ module WiserTrails
39
15
 
40
16
  # Module with basic +tracked+ method that enables tracking models.
41
17
  module ClassMethods
42
- # Adds required callbacks for creating and updating
43
- # tracked models and adds +activities+ relation for listing
44
- # associated activities.
45
- #
46
- # == Parameters:
47
- # [:owner]
48
- # Specify the owner of the {Activity} (person responsible for the action).
49
- # It can be a Proc, Symbol or an ActiveRecord object:
50
- # == Examples:
51
- #
52
- # tracked :owner => :author
53
- # tracked :owner => proc {|o| o.author}
54
- #
55
- # Keep in mind that owner relation is polymorphic, so you can't just
56
- # provide id number of the owner object.
57
- # [:recipient]
58
- # Specify the recipient of the {Activity}
59
- # It can be a Proc, Symbol, or an ActiveRecord object
60
- # == Examples:
61
- #
62
- # tracked :recipient => :author
63
- # tracked :recipient => proc {|o| o.author}
64
- #
65
- # Keep in mind that recipient relation is polymorphic, so you can't just
66
- # provide id number of the owner object.
67
- # [:params]
68
- # Accepts a Hash with custom parameters you want to pass to i18n.translate
69
- # method. It is later used in {Renderable#text} method.
70
- # == Example:
71
- # class Article < ActiveRecord::Base
72
- # include WiserTrails::Model
73
- # tracked :params => {
74
- # :title => :title,
75
- # :author_name => "Michael",
76
- # :category_name => proc {|controller, model_instance| model_instance.category.name},
77
- # :summary => proc {|controller, model_instance| truncate(model.text, :length => 30)}
78
- # }
79
- # end
80
- #
81
- # Values in the :params hash can either be an *exact* *value*, a *Proc/Lambda* executed before saving the activity or a *Symbol*
82
- # which is a an attribute or a method name executed on the tracked model's instance.
83
- #
84
- # Everything specified here has a lower priority than parameters
85
- # specified directly in {#activity} method.
86
- # So treat it as a place where you provide 'default' values or where you
87
- # specify what data should be gathered for every activity.
88
- # For more dynamic settings refer to {Activity} model documentation.
89
- # [:skip_defaults]
90
- # Disables recording of activities on create/update/destroy leaving that to programmer's choice. Check {WiserTrails::Common#create_activity}
91
- # for a guide on how to manually record activities.
92
- # [:only]
93
- # Accepts a symbol or an array of symbols, of which any combination of the three is accepted:
94
- # * _:create_
95
- # * _:update_
96
- # * _:destroy_
97
- # Selecting one or more of these will make WiserTrails create activities
98
- # automatically for the tracked model on selected actions.
99
- #
100
- # Resulting activities will have have keys assigned to, respectively:
101
- # * _article.create_
102
- # * _article.update_
103
- # * _article.destroy_
104
- # Since only three options are valid,
105
- # see _:except_ option for a shorter version
106
- # [:except]
107
- # Accepts a symbol or an array of symbols with values like in _:only_, above.
108
- # Values provided will be subtracted from all default actions:
109
- # (create, update, destroy).
110
- #
111
- # So, passing _create_ would track and automatically create
112
- # activities on _update_ and _destroy_ actions,
113
- # but not on the _create_ action.
114
- # [:on]
115
- # Accepts a Hash with key being the *action* on which to execute *value* (proc)
116
- # Currently supported only for CRUD actions which are enabled in _:only_
117
- # or _:except_ options on this method.
118
- #
119
- # Key-value pairs in this option define callbacks that can decide
120
- # whether to create an activity or not. Procs have two attributes for
121
- # use: _model_ and _controller_. If the proc returns true, the activity
122
- # will be created, if not, then activity will not be saved.
123
- #
124
- # == Example:
125
- # # app/models/article.rb
126
- # tracked :on => {:update => proc {|model, controller| model.published? }}
127
- #
128
- # In the example above, given a model Article with boolean column _published_.
129
- # The activities with key _article.update_ will only be created
130
- # if the published status is set to true on that article.
131
- # @param opts [Hash] options
132
- # @return [nil] options
133
18
  def trail_it(opts = {})
134
19
  options = opts.clone
135
-
136
20
  all_options = [:create, :update, :destroy]
137
21
 
22
+ self.activity_skip_fields_global = ["updated_at", "created_at"]
23
+ if options[:skip_fields].present?
24
+ self.activity_skip_fields_global += options[:skip_fields]
25
+ end
26
+ options.delete(:skip_fields)
27
+
28
+ if options[:force_fields].present?
29
+ self.activity_force_fields_global = options[:skip_fields]
30
+ end
31
+ options.delete(:skip_fields)
32
+
138
33
  if !options.has_key?(:skip_defaults) && !options[:only] && !options[:except]
139
34
  include Creation
140
35
  include Destruction
@@ -1,3 +1,3 @@
1
1
  module WiserTrails
2
- VERSION = '2.0.0'
2
+ VERSION = '2.1.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wiser_trails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenneth John Balgos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-04 00:00:00.000000000 Z
11
+ date: 2013-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler