historyable 0.1.0 → 0.1.1
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 +8 -8
 - checksums.yaml.gz.sig +0 -0
 - data.tar.gz.sig +0 -0
 - data/lib/historyable.rb +33 -14
 - data/lib/historyable/version.rb +1 -1
 - metadata +2 -2
 - metadata.gz.sig +0 -0
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,15 +1,15 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            !binary "U0hBMQ==":
         
     | 
| 
       3 
3 
     | 
    
         
             
              metadata.gz: !binary |-
         
     | 
| 
       4 
     | 
    
         
            -
                 
     | 
| 
      
 4 
     | 
    
         
            +
                YTM5ODJlNzZkMzQ0YzUyZGRhNDUzMzM4MjhhZTIwODRiMGUzMmM0Yg==
         
     | 
| 
       5 
5 
     | 
    
         
             
              data.tar.gz: !binary |-
         
     | 
| 
       6 
     | 
    
         
            -
                 
     | 
| 
      
 6 
     | 
    
         
            +
                YTE5MWRmNWJhNGUyNDhmYzBkYjNjMTNmZjRkZDA0ZjlmYmQzNjBiOQ==
         
     | 
| 
       7 
7 
     | 
    
         
             
            !binary "U0hBNTEy":
         
     | 
| 
       8 
8 
     | 
    
         
             
              metadata.gz: !binary |-
         
     | 
| 
       9 
     | 
    
         
            -
                 
     | 
| 
       10 
     | 
    
         
            -
                 
     | 
| 
       11 
     | 
    
         
            -
                 
     | 
| 
      
 9 
     | 
    
         
            +
                ZWYzNDQ2NTMyYjc1OWVhODJhZmUxMjg5NGQ1ZWQzMDllY2JiN2M0ZTdiYmJk
         
     | 
| 
      
 10 
     | 
    
         
            +
                YThhODc1NjFhNmU4YjJmMzQwOWJkNzQ1ODUyNjI1ODVmMjdiN2MxOGMzOTBi
         
     | 
| 
      
 11 
     | 
    
         
            +
                NGM5NmVlMzRmNGFlZTFjN2UyZGYxYWM4YTRkYjQyNjE5OTY2MDU=
         
     | 
| 
       12 
12 
     | 
    
         
             
              data.tar.gz: !binary |-
         
     | 
| 
       13 
     | 
    
         
            -
                 
     | 
| 
       14 
     | 
    
         
            -
                 
     | 
| 
       15 
     | 
    
         
            -
                 
     | 
| 
      
 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(: 
     | 
| 
      
 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 
     | 
    
         
            -
                     
     | 
| 
      
 21 
     | 
    
         
            +
                    attribute_name   = attribute.to_sym
         
     | 
| 
       22 
22 
     | 
    
         
             
                    association_name = attribute.to_s.insert(-1, '_changes').to_sym
         
     | 
| 
       23 
23 
     | 
    
         | 
| 
       24 
     | 
    
         
            -
                    Historyable::Item.new( 
     | 
| 
      
 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. 
     | 
| 
      
 46 
     | 
    
         
            +
                    define_method("#{historyable.attribute_name.to_s}_history_raw") do
         
     | 
| 
       43 
47 
     | 
    
         
             
                      send(historyable.association_name)
         
     | 
| 
       44 
     | 
    
         
            -
                        .where(object_attribute: historyable. 
     | 
| 
      
 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. 
     | 
| 
       54 
     | 
    
         
            -
                      unless instance_variable_get("@#{historyable. 
     | 
| 
      
 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. 
     | 
| 
      
 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. 
     | 
| 
      
 76 
     | 
    
         
            +
                        instance_variable_set("@#{historyable.attribute_name.to_s}_history".to_sym, array)
         
     | 
| 
       69 
77 
     | 
    
         
             
                        array
         
     | 
| 
       70 
78 
     | 
    
         
             
                      else
         
     | 
| 
       71 
     | 
    
         
            -
                        instance_variable_get("@#{historyable. 
     | 
| 
      
 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. 
     | 
| 
      
 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. 
     | 
| 
       95 
     | 
    
         
            -
                                                            object_attribute_value: send(historyable. 
     | 
| 
      
 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. 
     | 
| 
      
 116 
     | 
    
         
            +
                  instance_variable_set("@#{historyable.attribute_name.to_s}_history".to_sym, nil)
         
     | 
| 
       98 
117 
     | 
    
         
             
                end
         
     | 
| 
       99 
118 
     | 
    
         | 
| 
       100 
119 
     | 
    
         
             
                true
         
     | 
    
        data/lib/historyable/version.rb
    CHANGED
    
    
    
        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. 
     | 
| 
      
 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- 
     | 
| 
      
 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 
     |