historyable 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YTk1ODJjMGVlYmQ3YzJmYWFjNzU2MjQ2YzM5MDRiMGJmOGZlZThkYQ==
4
+ NzU1M2JlMTY5YjkyMjUyMmI1NTVmZDgwNTA1NDY5MmViYTQ2YjJiOA==
5
5
  data.tar.gz: !binary |-
6
- OGY2N2QzYzhkMWZkOTZjNTlhNGMyOTY3YTFkODc3ZGFlMjk3ZjdiYQ==
6
+ MGEyNzQyY2JlMWMyMzU3NTI0NGJkNTVhMTNiNmU2MzAxYzA2M2IxMw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YmFhNjhlMTY1YjFlNGI1ZjhiMTA1NjRjMmExZjYzYTlkNmM5Mjc5OTI2MTM5
10
- YjY1OGExNDcyMTUxMWM1YjdiYmYyNzg4Y2RmM2IzNDM3MmQxZTllYzFiOTk4
11
- ZDZiMGU4OGNjMGVhOGZkNThmZDYwZjE0NTg1YjhkNTQwZjc0YTQ=
9
+ ZjU4NmFmMGVjODIyYWI1YTNjYTRiZDA4OWI0NDBhZTkzNjFjZTNlMWVmOTM5
10
+ ZjY5MTY0Zjc3M2U3MDdjMjg3OGRhYmMzZjNmY2VjMzdiODllY2IzMzcwYzI2
11
+ N2E3NWE3ZTM2NjQ3OGQ4OTJiMWEwZDljZDRlMDViMWM1ZTkwYTE=
12
12
  data.tar.gz: !binary |-
13
- NGE5OWQ4NzI3Y2RiODg3YmZkNDM0NTkzY2Y4NTUwNDJjNGZmYTU5MzRjZTk0
14
- OTc0OTUyMGQyYmZjZmRkMGQ5NzI3MzZkMGEwOGM3M2Q3M2Y2MzAzODAwOWFh
15
- YmJlYzYxMWUyMGU1NDU3ZDE4MzhmYWYzOWQxMjM2N2JhMzI1NWM=
13
+ MzUyZjJmNmYwMGI4NmVhZmVmMGEzODVmYTNlZTM5N2JkYjE5ZDM4Y2JjMTUx
14
+ YTU5MGIxYjgxZmQ5ZmM0OTc3ZTZkNTY3YjQzZDkzOGJhOWFlNWE0OGUwYWEy
15
+ NGQyZGUyMmMxYTkwNmNlNGQzODI4NDg2YmQ2M2Q1YzRiMjNlMDA=
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
@@ -1,3 +1,2 @@
1
- E:�]�a�]sb�4����-H�� ���D0���&Å-��
2
- ���h�C�v�I��[M^Nr��;�ڵ%S�~��NGGaQ�K�
3
- ƙ
1
+ a�~�rk`��CW�;�}{��N�w����3*f�Dz��*��7�� ZHm_7{wn,�t�#ut��zӬ��X�dq��yJ������g��h��U��� �����µ4"wL�3��t�ƿ���jq�߸+��6��A�������ѻ����ZΜ����J
2
+ ;�
data/lib/historyable.rb CHANGED
@@ -4,24 +4,28 @@ require 'active_record'
4
4
  require 'historyable/change'
5
5
 
6
6
  module Historyable
7
- Item = Struct.new(:attribute_name, :association_name)
8
-
9
7
  extend ActiveSupport::Concern
10
8
 
9
+ Item = Struct.new(:attribute_name, :association_name)
10
+
11
11
  included do
12
- class_attribute :historyable_items, instance_writer: false
12
+ class_attribute :historyable_items, :historyable_cache, instance_writer: false
13
13
  end
14
14
 
15
15
 
16
16
  module ClassMethods
17
17
 
18
+ # Define
19
+ #
20
+ #
18
21
  # @param attributes [Array, Symbol]
19
22
  def has_history(*attributes)
23
+ self.historyable_cache = ActiveSupport::Cache::MemoryStore.new
20
24
  self.historyable_items = attributes.map do |attribute|
21
25
  attribute_name = attribute.to_sym
22
26
  association_name = attribute.to_s.insert(-1, '_changes').to_sym
23
27
 
24
- Historyable::Item.new(attribute_name, association_name)
28
+ Item.new(attribute_name, association_name)
25
29
  end
26
30
 
27
31
  # Associations
@@ -58,7 +62,7 @@ module Historyable
58
62
  send(historyable.association_name)
59
63
  .where(object_attribute: historyable.attribute_name)
60
64
  .order('created_at DESC')
61
- .select(:object_attribute_value, :created_at)
65
+ .select([:object_attribute_value, :created_at])
62
66
  end
63
67
  end
64
68
 
@@ -71,24 +75,19 @@ module Historyable
71
75
  # @return [Array]
72
76
  def define_historyable_attribute_history(historyable)
73
77
  define_method("#{historyable.attribute_name.to_s}_history") do
74
- unless instance_variable_get("@#{historyable.attribute_name.to_s}_history".to_sym)
78
+ historyable_cache.fetch(historyable.attribute_name) do
75
79
  collection = []
76
80
 
77
81
  records = send("#{historyable.attribute_name}_history_raw")
78
- .pluck(:object_attribute_value, :created_at)
79
- records.map do |attribute_value, created_at|
82
+ records.each do |record|
80
83
  item = HashWithIndifferentAccess.new
81
- item[:attribute_value] = attribute_value
82
- item[:changed_at] = created_at
84
+ item[:attribute_value] = record.object_attribute_value
85
+ item[:changed_at] = record.created_at
83
86
 
84
87
  collection << item
85
88
  end
86
89
 
87
- # Sets attribute_history cache
88
- instance_variable_set("@#{historyable.attribute_name.to_s}_history".to_sym, collection)
89
90
  collection
90
- else
91
- instance_variable_get("@#{historyable.attribute_name.to_s}_history".to_sym)
92
91
  end
93
92
  end
94
93
  end
@@ -122,7 +121,7 @@ module Historyable
122
121
  send(historyable.association_name).create(object_attribute: historyable.attribute_name,
123
122
  object_attribute_value: send(historyable.attribute_name))
124
123
  # Expires attribute_history cache
125
- instance_variable_set("@#{historyable.attribute_name.to_s}_history".to_sym, nil)
124
+ historyable_cache.delete(historyable.attribute_name)
126
125
  end
127
126
 
128
127
  true
@@ -1,3 +1,3 @@
1
1
  module Historyable
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Philippe Dionne
@@ -45,85 +45,29 @@ dependencies:
45
45
  requirements:
46
46
  - - ! '>='
47
47
  - !ruby/object:Gem::Version
48
- version: 3.0.0
48
+ version: 3.2.14
49
49
  version_requirements: !ruby/object:Gem::Requirement
50
50
  requirements:
51
51
  - - ! '>='
52
52
  - !ruby/object:Gem::Version
53
- version: 3.0.0
53
+ version: 3.2.14
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: '1.3'
63
- version_requirements: !ruby/object:Gem::Requirement
64
- requirements:
65
- - - ~>
66
- - !ruby/object:Gem::Version
67
- version: '1.3'
68
- type: :development
69
- prerelease: false
70
- name: bundler
71
- - !ruby/object:Gem::Dependency
72
- requirement: !ruby/object:Gem::Requirement
73
- requirements:
74
- - - ! '>='
75
- - !ruby/object:Gem::Version
76
- version: '0'
77
- version_requirements: !ruby/object:Gem::Requirement
78
- requirements:
79
- - - ! '>='
80
- - !ruby/object:Gem::Version
81
- version: '0'
82
- type: :development
83
- prerelease: false
84
- name: rake
85
57
  - !ruby/object:Gem::Dependency
86
58
  requirement: !ruby/object:Gem::Requirement
87
59
  requirements:
88
60
  - - ! '>='
89
61
  - !ruby/object:Gem::Version
90
- version: '0'
62
+ version: 3.2.14
91
63
  version_requirements: !ruby/object:Gem::Requirement
92
64
  requirements:
93
65
  - - ! '>='
94
66
  - !ruby/object:Gem::Version
95
- version: '0'
96
- type: :development
97
- prerelease: false
98
- name: rspec
99
- - !ruby/object:Gem::Dependency
100
- requirement: !ruby/object:Gem::Requirement
101
- requirements:
102
- - - ! '>='
103
- - !ruby/object:Gem::Version
104
- version: '0'
105
- version_requirements: !ruby/object:Gem::Requirement
106
- requirements:
107
- - - ! '>='
108
- - !ruby/object:Gem::Version
109
- version: '0'
110
- type: :development
111
- prerelease: false
112
- name: sqlite3
113
- - !ruby/object:Gem::Dependency
114
- requirement: !ruby/object:Gem::Requirement
115
- requirements:
116
- - - ! '>='
117
- - !ruby/object:Gem::Version
118
- version: '0'
119
- version_requirements: !ruby/object:Gem::Requirement
120
- requirements:
121
- - - ! '>='
122
- - !ruby/object:Gem::Version
123
- version: '0'
124
- type: :development
67
+ version: 3.2.14
68
+ type: :runtime
125
69
  prerelease: false
126
- name: database_cleaner
70
+ name: activesupport
127
71
  description: A simple and solid concern to track ActiveRecord models attributes changes.
128
72
  email:
129
73
  - philippe.dionne@hooktstudios.com
metadata.gz.sig CHANGED
@@ -1,4 +1 @@
1
- ��wV!!k��D)�N�ɣ@�p3�\U��W���JW�����`�e5�F���|"���#w�
2
- ��_�1���sNO���VdK�*�b(#�('J�ٙ��M�<�qa��:�(�Cd;n��P���W�)Ǚܔ"a���=I�I�:vRt`a��3
3
- 7u�O����u���=�I�KA�BKc)(�>.�c~xz�䒛؅8�u��j���,pLQU2�&��g��E{� E�y����k?�o�
4
- "^�lYCa�.�'M��ݰ�
1
+ ��|I;Z�