historyable 0.1.0 → 0.1.1

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
- ODVjM2I0MTUxOTZjM2FiNTUwNGNkNDU2ZjUyNDg5M2Y3MmM3YTMzZQ==
4
+ YTM5ODJlNzZkMzQ0YzUyZGRhNDUzMzM4MjhhZTIwODRiMGUzMmM0Yg==
5
5
  data.tar.gz: !binary |-
6
- ZTRlMDEwMDBhNmYyZGQ5MmE2OTI3MTVkOTdhYmNkY2YyMWZkODdkYg==
6
+ YTE5MWRmNWJhNGUyNDhmYzBkYjNjMTNmZjRkZDA0ZjlmYmQzNjBiOQ==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- YWM1NjE2OGQwMjlkNDkyNmRjZTljOTQ5N2IyOTdlNTFjMjM3YjA2MThkNjg1
10
- NjUxYmNiNTYzY2M3NmViMGYxNjk2NDZhNjJhMTBkZTBiZmE4ZWU5NGVmNjIx
11
- NGJhZjFjYjNhZWI4MjBhYjg4ZWE2M2Y5Nzk0NjdlMTk2NzMxNTQ=
9
+ ZWYzNDQ2NTMyYjc1OWVhODJhZmUxMjg5NGQ1ZWQzMDllY2JiN2M0ZTdiYmJk
10
+ YThhODc1NjFhNmU4YjJmMzQwOWJkNzQ1ODUyNjI1ODVmMjdiN2MxOGMzOTBi
11
+ NGM5NmVlMzRmNGFlZTFjN2UyZGYxYWM4YTRkYjQyNjE5OTY2MDU=
12
12
  data.tar.gz: !binary |-
13
- NWQzMjJmZDkzZTJmZWQxMWY0MmY5NDBjNmRjMTI0ZTBlMTBiNzUzODRjMWM0
14
- YmRmNTk1ZTY1NWNhNWI0ZWY4MDVhODE5YzBiMjJlYTYxZjNhZDU4NTc2ZmRm
15
- MTdhM2FiNDFjOGFmN2ExOTc4NTk0ZDc3NDg4MGVlMmIzYTgyYjY=
13
+ NmMyYjg2NWI2ZGM4ZjU1MDk2ODhhN2VkZmNjNGJiMzQzOTRlNzdkYzMzYzI1
14
+ Y2ZhMzY1NmE3ODljZDE5OWVkYjFiMmNiOTJkMDkxMTBiOGIzOTU5ZWNmZGM1
15
+ M2YyMWQxNzNhYTE3ZWM1MWJhZWY0ODNkMzZkODZmZDE3ODE5MjQ=
checksums.yaml.gz.sig CHANGED
Binary file
data.tar.gz.sig CHANGED
Binary file
data/lib/historyable.rb CHANGED
@@ -4,7 +4,7 @@ require "active_record"
4
4
  require "historyable/change"
5
5
 
6
6
  module Historyable
7
- class Item < Struct.new(:attribute, :association_name); end
7
+ class Item < Struct.new(:attribute_name, :association_name); end
8
8
 
9
9
  extend ActiveSupport::Concern
10
10
 
@@ -18,10 +18,10 @@ module Historyable
18
18
  # @param attributes [Array, Symbol]
19
19
  def has_history(*attributes)
20
20
  self.historyable_items = attributes.map do |attribute|
21
- attribute = attribute.to_sym
21
+ attribute_name = attribute.to_sym
22
22
  association_name = attribute.to_s.insert(-1, '_changes').to_sym
23
23
 
24
- Historyable::Item.new(attribute, association_name)
24
+ Historyable::Item.new(attribute_name, association_name)
25
25
  end
26
26
 
27
27
  # Associations
@@ -38,10 +38,14 @@ module Historyable
38
38
 
39
39
  # attribute_history_raw
40
40
  #
41
+ # @example
42
+ #
43
+ # @user.name_history_raw
44
+ #
41
45
  # @return [ActiveRecord::Relation]
42
- define_method("#{historyable.attribute.to_s}_history_raw") do
46
+ define_method("#{historyable.attribute_name.to_s}_history_raw") do
43
47
  send(historyable.association_name)
44
- .where(object_attribute: historyable.attribute)
48
+ .where(object_attribute: historyable.attribute_name)
45
49
  .order('created_at DESC')
46
50
  .select(:object_attribute_value, :created_at)
47
51
  end
@@ -49,12 +53,16 @@ module Historyable
49
53
 
50
54
  # attribute_history
51
55
  #
56
+ # @example
57
+ #
58
+ # @user.name_history
59
+ #
52
60
  # @return [Array]
53
- define_method("#{historyable.attribute.to_s}_history") do
54
- unless instance_variable_get("@#{historyable.attribute.to_s}_history".to_sym)
61
+ define_method("#{historyable.attribute_name.to_s}_history") do
62
+ unless instance_variable_get("@#{historyable.attribute_name.to_s}_history".to_sym)
55
63
  array = []
56
64
 
57
- records = send("#{historyable.attribute}_history_raw")
65
+ records = send("#{historyable.attribute_name}_history_raw")
58
66
  .pluck(:object_attribute_value, :created_at)
59
67
  records.map do |attribute_value, created_at|
60
68
  hash = HashWithIndifferentAccess.new
@@ -65,12 +73,23 @@ module Historyable
65
73
  end
66
74
 
67
75
  # Sets attribute_history cache
68
- instance_variable_set("@#{historyable.attribute.to_s}_history".to_sym, array)
76
+ instance_variable_set("@#{historyable.attribute_name.to_s}_history".to_sym, array)
69
77
  array
70
78
  else
71
- instance_variable_get("@#{historyable.attribute.to_s}_history".to_sym)
79
+ instance_variable_get("@#{historyable.attribute_name.to_s}_history".to_sym)
72
80
  end
73
81
  end
82
+
83
+ # attribute_history?
84
+ #
85
+ # @example
86
+ #
87
+ # @user.name_history?
88
+ #
89
+ # @return [Boolean]
90
+ define_method("#{historyable.attribute_name.to_s}_history?") do
91
+ send("#{historyable.attribute_name}_history_raw").any?
92
+ end
74
93
  end
75
94
 
76
95
 
@@ -85,16 +104,16 @@ module Historyable
85
104
  # Creates a Change record when an attribute marked as 'historyable' changes
86
105
  def save_changes
87
106
  changed_historyable_items = historyable_items.select do |historyable|
88
- changed.include?(historyable.attribute.to_s)
107
+ changed.include?(historyable.attribute_name.to_s)
89
108
  end
90
109
 
91
110
  yield # saves the records
92
111
 
93
112
  changed_historyable_items.each do |historyable|
94
- send(historyable.association_name).create(object_attribute: historyable.attribute,
95
- object_attribute_value: send(historyable.attribute))
113
+ send(historyable.association_name).create(object_attribute: historyable.attribute_name,
114
+ object_attribute_value: send(historyable.attribute_name))
96
115
  # Expires attribute_history cache
97
- instance_variable_set("@#{historyable.attribute.to_s}_history".to_sym, nil)
116
+ instance_variable_set("@#{historyable.attribute_name.to_s}_history".to_sym, nil)
98
117
  end
99
118
 
100
119
  true
@@ -1,3 +1,3 @@
1
1
  module Historyable
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
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.0
4
+ version: 0.1.1
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-08-21 00:00:00.000000000 Z
41
+ date: 2013-08-23 00:00:00.000000000 Z
42
42
  dependencies:
43
43
  - !ruby/object:Gem::Dependency
44
44
  prerelease: false
metadata.gz.sig CHANGED
Binary file