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 +4 -4
- data/lib/matterful_attributes/version.rb +1 -1
- data/lib/matterful_attributes.rb +41 -29
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c917cbd924d618b2e066d3025690bad096794c24
|
4
|
+
data.tar.gz: 120c19f87f9b7446b71b1643db84dfd29a6d5727
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9f7287fd99ed3b119589f898690c9d900fa0d554ec435639c3b0f79038d1ff4e7dbdc8ac378b6456a5d077c950d008ae10b82928e254234b4f5f85aba22dcd0a
|
7
|
+
data.tar.gz: 205d9ca712d84a24862965543197a7f919bd3170bbbd027883dc96b01cbe4a32e810f5f559bf4b6dbe3de3d8fcac1aee74db1c9273c9ab6c3de5f0671361516f
|
data/lib/matterful_attributes.rb
CHANGED
@@ -4,34 +4,44 @@ module MatterfulAttributes
|
|
4
4
|
module ActiveRecord
|
5
5
|
class Base
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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
|
-
#
|
46
|
-
if !(target_attributes = source.matterful_diff(self, options={})).empty?
|
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
|