historyable 0.2.0 → 0.3.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
- NzU1M2JlMTY5YjkyMjUyMmI1NTVmZDgwNTA1NDY5MmViYTQ2YjJiOA==
4
+ ZjkzMWQ0Mjk3OGMwODJhMGQzNjViZDNiMjg1N2VkYTg4OGU2M2UxNw==
5
5
  data.tar.gz: !binary |-
6
- MGEyNzQyY2JlMWMyMzU3NTI0NGJkNTVhMTNiNmU2MzAxYzA2M2IxMw==
6
+ MjQyMDFmZDlhZTRmMGVmYjlmNDk2MTRjNmY1ZGE2NDIxNzFjOGNiYg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZjU4NmFmMGVjODIyYWI1YTNjYTRiZDA4OWI0NDBhZTkzNjFjZTNlMWVmOTM5
10
- ZjY5MTY0Zjc3M2U3MDdjMjg3OGRhYmMzZjNmY2VjMzdiODllY2IzMzcwYzI2
11
- N2E3NWE3ZTM2NjQ3OGQ4OTJiMWEwZDljZDRlMDViMWM1ZTkwYTE=
9
+ YWQ4YWE3ODhkN2U1MGJiYTVkYjBmMzZjYzg2NjcyOWNhY2RlZjU5Yzc4NmMx
10
+ OTBmMjg0ZDZkZTdkNWI2YjFkNWViZTBjOTIyYzFmYWE3YWU0NjM5ZTZjOGI4
11
+ ODk3MmUxYzZhMjVkZDRkOTU4ODJlN2ZiNjY2YTc2MGM3NWY4MGM=
12
12
  data.tar.gz: !binary |-
13
- MzUyZjJmNmYwMGI4NmVhZmVmMGEzODVmYTNlZTM5N2JkYjE5ZDM4Y2JjMTUx
14
- YTU5MGIxYjgxZmQ5ZmM0OTc3ZTZkNTY3YjQzZDkzOGJhOWFlNWE0OGUwYWEy
15
- NGQyZGUyMmMxYTkwNmNlNGQzODI4NDg2YmQ2M2Q1YzRiMjNlMDA=
13
+ Mzk3YmYzNDFhYTk2MDExZjlhNDZkNDI3YzhjMWRjNmRiMWZlMjI4MDE3MDQy
14
+ YmI4OTQ0MThlODQ3N2Q2OTkxMjZjZmZlMjhhOGFlYWNkMjdmNmU4ODViYjVi
15
+ YWMwYWRkYzU0ZmY4MDI4MDg5MjVkYWY5NGNmMTFlZTYzZWE5NmE=
checksums.yaml.gz.sig CHANGED
Binary file
@@ -1,4 +1,6 @@
1
- class Change < ActiveRecord::Base
2
- belongs_to :historyable, polymorphic: true
3
- serialize :object_attribute_value
1
+ module Historyable
2
+ class Change < ActiveRecord::Base
3
+ belongs_to :historyable, polymorphic: true
4
+ serialize :object_attribute_value
5
+ end
4
6
  end
@@ -1,3 +1,3 @@
1
1
  module Historyable
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/historyable.rb CHANGED
@@ -9,7 +9,8 @@ module Historyable
9
9
  Item = Struct.new(:attribute_name, :association_name)
10
10
 
11
11
  included do
12
- class_attribute :historyable_items, :historyable_cache, instance_writer: false
12
+ class_attribute :historyable_items, instance_writer: false
13
+ attr_accessor :historyable_cache
13
14
  end
14
15
 
15
16
 
@@ -20,24 +21,18 @@ module Historyable
20
21
  #
21
22
  # @param attributes [Array, Symbol]
22
23
  def has_history(*attributes)
23
- self.historyable_cache = ActiveSupport::Cache::MemoryStore.new
24
24
  self.historyable_items = attributes.map do |attribute|
25
25
  attribute_name = attribute.to_sym
26
- association_name = attribute.to_s.insert(-1, '_changes').to_sym
26
+ association_name = "#{attribute}_changes".to_sym
27
27
 
28
28
  Item.new(attribute_name, association_name)
29
29
  end
30
30
 
31
- # Associations
32
31
  historyable_items.each do |historyable|
33
- has_many historyable.association_name,
34
- as: :item,
35
- class_name: '::Change',
36
- dependent: :destroy
37
- end
32
+ # Associations
33
+ define_historyable_association(historyable)
38
34
 
39
- # Instance methods
40
- historyable_items.each do |historyable|
35
+ # Instance methods
41
36
  define_historyable_attribute_history_raw(historyable)
42
37
  define_historyable_attribute_history(historyable)
43
38
  define_historyable_attribute_history?(historyable)
@@ -50,6 +45,13 @@ module Historyable
50
45
 
51
46
  private
52
47
 
48
+ def define_historyable_association(historyable)
49
+ has_many historyable.association_name,
50
+ as: :item,
51
+ class_name: Change,
52
+ dependent: :destroy
53
+ end
54
+
53
55
  # attribute_history_raw
54
56
  #
55
57
  # @example
@@ -75,19 +77,15 @@ module Historyable
75
77
  # @return [Array]
76
78
  def define_historyable_attribute_history(historyable)
77
79
  define_method("#{historyable.attribute_name.to_s}_history") do
78
- historyable_cache.fetch(historyable.attribute_name) do
79
- collection = []
80
-
81
- records = send("#{historyable.attribute_name}_history_raw")
82
- records.each do |record|
83
- item = HashWithIndifferentAccess.new
84
- item[:attribute_value] = record.object_attribute_value
85
- item[:changed_at] = record.created_at
86
-
87
- collection << item
88
- end
89
-
90
- collection
80
+ self.historyable_cache ||= Hash.new
81
+ historyable_cache[historyable.attribute_name] ||= send("#{historyable.attribute_name}_history_raw").inject([]) do |memo, record|
82
+ # @TODO Create a class instead of a hash to store the attributes value
83
+ # instead of doing collection.map {|hash| hash[:changed_at] } will do collection.map(&:changed_at)
84
+ item = HashWithIndifferentAccess.new
85
+ item[:attribute_value] = record.object_attribute_value
86
+ item[:changed_at] = record.created_at
87
+
88
+ memo << item
91
89
  end
92
90
  end
93
91
  end
@@ -121,7 +119,7 @@ module Historyable
121
119
  send(historyable.association_name).create(object_attribute: historyable.attribute_name,
122
120
  object_attribute_value: send(historyable.attribute_name))
123
121
  # Expires attribute_history cache
124
- historyable_cache.delete(historyable.attribute_name)
122
+ historyable_cache && historyable_cache.delete(historyable.attribute_name)
125
123
  end
126
124
 
127
125
  true
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: historyable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philippe Dionne
@@ -38,7 +38,7 @@ cert_chain:
38
38
  aW9OckllMC8xNWRIcERvSUVESUxhNnpVNDZCClhEdHAxWXhkZVZHSUJ1Tm9Q
39
39
  MXZqRFN2TktZajBwTWpSQUVQcnFLMzlqS0U9Ci0tLS0tRU5EIENFUlRJRklD
40
40
  QVRFLS0tLS0K
41
- date: 2013-09-24 00:00:00.000000000 Z
41
+ date: 2013-09-25 00:00:00.000000000 Z
42
42
  dependencies:
43
43
  - !ruby/object:Gem::Dependency
44
44
  requirement: !ruby/object:Gem::Requirement
@@ -54,20 +54,6 @@ dependencies:
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  name: activerecord
57
- - !ruby/object:Gem::Dependency
58
- requirement: !ruby/object:Gem::Requirement
59
- requirements:
60
- - - ! '>='
61
- - !ruby/object:Gem::Version
62
- version: 3.2.14
63
- version_requirements: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - ! '>='
66
- - !ruby/object:Gem::Version
67
- version: 3.2.14
68
- type: :runtime
69
- prerelease: false
70
- name: activesupport
71
57
  description: A simple and solid concern to track ActiveRecord models attributes changes.
72
58
  email:
73
59
  - philippe.dionne@hooktstudios.com
metadata.gz.sig CHANGED
Binary file