matterful_attributes 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ab5c548e45b69263103845a1625c849bd03ce30a
4
- data.tar.gz: 20b3d880f2911cd653f321d5ea0747e0b0bccc64
3
+ metadata.gz: c917cbd924d618b2e066d3025690bad096794c24
4
+ data.tar.gz: 120c19f87f9b7446b71b1643db84dfd29a6d5727
5
5
  SHA512:
6
- metadata.gz: 637c48c60453952b52c801563f1ac45926aac456d0d88b5c1bcb2dd1b30f5d4e31a596e93e09b5397ee593583f646462e065cbb84c419bee4a3897e4ab9188e8
7
- data.tar.gz: e2095a1958f80956e6d3d8f608ece71c36dbb70e0b859def9de1bfdf5f05fc161c3c033573e3928e7cda57249dae86d9cf43db47be437f0b5892a8358cd65818
6
+ metadata.gz: 9f7287fd99ed3b119589f898690c9d900fa0d554ec435639c3b0f79038d1ff4e7dbdc8ac378b6456a5d077c950d008ae10b82928e254234b4f5f85aba22dcd0a
7
+ data.tar.gz: 205d9ca712d84a24862965543197a7f919bd3170bbbd027883dc96b01cbe4a32e810f5f559bf4b6dbe3de3d8fcac1aee74db1c9273c9ab6c3de5f0671361516f
@@ -1,3 +1,3 @@
1
1
  module MatterfulAttributes
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -4,34 +4,44 @@ module MatterfulAttributes
4
4
  module ActiveRecord
5
5
  class Base
6
6
 
7
- def matterful_attributes(options={})
8
- options = options.dup
9
- default = options[:default].nil? ? true : options[:default]
10
- foreign_key = options[:foreign_key].nil? ? true : options[:foreign_key]
11
- sti = options[:sti].nil? ? true : options[:sti]
12
- polymorphic = options[:polymorphic].nil? ? true : options[:polymorphic]
13
- # extra keys supplied as array of strings to ignore in comparison
14
- attributes_to_ignore = options[:extra].nil? ? [] : options[:extra]
15
-
16
- # Let's check for and add typical attributes that sti & polymorphic models have, override this by sti: false, polymorphic: false, :foreign_key: false
17
- # by default when looking to compare two objects Customer.find(1).shipping_address.same_as? Address.new(city: 'Chicago')
18
- # we really only want to see if any of the 'matterfule information changed', like address_line_1 or city.
19
- # TODO: I guess I could do some smart checking on the model to see what kind of attributes it has. For now we'll just check for all that we want to remove
20
- if default
21
- attributes_to_ignore += ['id', 'created_at', 'updated_at']
22
- end
23
- if foreign_key
24
- attributes_to_ignore += attributes.keys.keep_if{|k| k.match(/_id\z/) && !k.match(/able_id\z/) } # This will skipp polymorphic style foreign keys
25
- end
26
- if sti
27
- attributes_to_ignore += ['type']
28
- end
29
- if polymorphic
30
- attributes_to_ignore += attributes.keys.keep_if{|k| k.match(/able_id\z/) }
31
- attributes_to_ignore += attributes.keys.keep_if{|k| k.match(/able_type\z/) }
32
- end
33
- attributes.except(*attributes_to_ignore)
7
+ def matterful_attributes(options={})
8
+ options = options.dup
9
+ default = options[:default].nil? ? true : options[:default]
10
+ foreign_key = options[:foreign_key].nil? ? true : options[:foreign_key]
11
+ sti = options[:sti].nil? ? true : options[:sti]
12
+ polymorphic = options[:polymorphic].nil? ? true : options[:polymorphic]
13
+ # extra keys supplied as array of strings to ignore in comparison
14
+ attributes_to_ignore = options[:extra].nil? ? [] : options[:extra]
15
+
16
+ # Let's check for and add typical attributes that sti & polymorphic models have, override
17
+ # this by sti: false, polymorphic: false, :foreign_key: false
18
+ # by default when looking to compare two objects
19
+ # Customer.find(1).shipping_address.same_as? Address.new(city: 'Chicago')
20
+ # we really only want to see if any of the 'matterfule information changed', like address_line_1 or city.
21
+ # TODO: I guess I could do some smart checking on the model to see what kind of attributes it has.
22
+ # For now we'll just check for all that we want to remove
23
+ if default
24
+ attributes_to_ignore += ['id', 'created_at', 'updated_at']
34
25
  end
26
+ # By default we want foreign keys like caegory_id as they provide useful information about current record.
27
+ # Let's say you import a bunch of addresses
28
+ # and they ony matterful change was from shipping to billing category.
29
+ unless foreign_key
30
+ # This will skip polymorphic style foreign keys and only deal with belongs_to style keys
31
+ attributes_to_ignore += attributes.keys.keep_if{|k| k.match(/_id\z/) && !k.match(/able_id\z/) }
32
+ end
33
+ if sti
34
+ attributes_to_ignore += ['type']
35
+ end
36
+ # If you are looking at a model that is polymorphic than most like you want to update something
37
+ # like city for an Address , not addressable_id or addresable_type
38
+ # That is more likely type of information to be updated on external import.
39
+ if polymorphic
40
+ attributes_to_ignore += attributes.keys.keep_if{|k| k.match(/able_id\z/) }
41
+ attributes_to_ignore += attributes.keys.keep_if{|k| k.match(/able_type\z/) }
42
+ end
43
+ attributes.except(*attributes_to_ignore)
44
+ end
35
45
 
36
46
  def same_as?(source,options={})
37
47
  matterful_attributes(options) == source.matterful_attributes(options)
@@ -41,9 +51,11 @@ module MatterfulAttributes
41
51
  matterful_attributes(options).diff(source.matterful_attributes(options))
42
52
  end
43
53
 
54
+ # Update self if source object has new data but DO NOT save. Let's say you want to do
55
+ # futer processing on the new data. Validation, upcasing, concatination
44
56
  def matterful_update(source,options={})
45
- # Update self if source object has new data but DO NOT save. Let's say you want to do futer processing on the new data. Validation, upcasing, concatination
46
- if !(target_attributes = source.matterful_diff(self, options={})).empty? # Pay attention to this! Reversing comparison to get expected results
57
+ # Pay attention to this! Reversing comparison to get expected results
58
+ if !(target_attributes = source.matterful_diff(self, options={})).empty?
47
59
  target_attributes.each_pair do |k,v|
48
60
  self[k] = v
49
61
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: matterful_attributes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Gorbikoff