matterful_attributes 0.0.2 → 0.0.3
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 +4 -4
- data/lib/matterful_attributes.rb +42 -42
- data/lib/matterful_attributes/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 71b08449dae61ecb94fb522f7fa6704a1752ee7c
|
4
|
+
data.tar.gz: 35f693c553e2fda54742fb1dd1d405b02becf49e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dcae85a5ebe606d6da72088eca64e2f128ce4da5d6b17d80fe2838d83e50ea42746b16bde47eb93c31417b44a219950c6a302480b9c4bce1ea5aecac5329cc74
|
7
|
+
data.tar.gz: 352775d60b8f4629d3d0f7ff3bba2bb04c9437acc35ec6c972bf0dd45de01803a4f5734bf9820435d462c3dbb2844c9fc420593fc58b7e0348c46df5f23e1e93
|
data/lib/matterful_attributes.rb
CHANGED
@@ -1,47 +1,46 @@
|
|
1
1
|
require "matterful_attributes/version"
|
2
2
|
|
3
3
|
module MatterfulAttributes
|
4
|
-
|
5
|
-
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
# These only make sense as instance methods
|
6
|
+
def matterful_attributes(options={})
|
7
|
+
options = options.dup
|
8
|
+
default = options[:default].nil? ? true : options[:default]
|
9
|
+
foreign_key = options[:foreign_key].nil? ? true : options[:foreign_key]
|
10
|
+
sti = options[:sti].nil? ? true : options[:sti]
|
11
|
+
polymorphic = options[:polymorphic].nil? ? true : options[:polymorphic]
|
12
|
+
# extra keys supplied as array of strings to ignore in comparison
|
13
|
+
attributes_to_ignore = options[:extra].nil? ? [] : options[:extra]
|
6
14
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
attributes_to_ignore
|
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/) }
|
15
|
+
# Let's check for and add typical attributes that sti & polymorphic models have, override
|
16
|
+
# this by sti: false, polymorphic: false, :foreign_key: false
|
17
|
+
# by default when looking to compare two objects
|
18
|
+
# Customer.find(1).shipping_address.same_as? Address.new(city: 'Chicago')
|
19
|
+
# we really only want to see if any of the 'matterfule information changed', like address_line_1 or city.
|
20
|
+
# TODO: I guess I could do some smart checking on the model to see what kind of attributes it has.
|
21
|
+
# For now we'll just check for all that we want to remove
|
22
|
+
if default
|
23
|
+
attributes_to_ignore += ['id', 'created_at', 'updated_at']
|
24
|
+
end
|
25
|
+
# By default we want foreign keys like caegory_id as they provide useful information about current record.
|
26
|
+
# Let's say you import a bunch of addresses
|
27
|
+
# and they ony matterful change was from shipping to billing category.
|
28
|
+
unless foreign_key
|
29
|
+
# This will skip polymorphic style foreign keys and only deal with belongs_to style keys
|
30
|
+
attributes_to_ignore += attributes.keys.keep_if{|k| k.match(/_id\z/) && !k.match(/able_id\z/) }
|
31
|
+
end
|
32
|
+
if sti
|
33
|
+
attributes_to_ignore += ['type']
|
34
|
+
end
|
35
|
+
# If you are looking at a model that is polymorphic than most like you want to update something
|
36
|
+
# like city for an Address , not addressable_id or addresable_type
|
37
|
+
# That is more likely type of information to be updated on external import.
|
38
|
+
if polymorphic
|
39
|
+
attributes_to_ignore += attributes.keys.keep_if{|k| k.match(/able_id\z/) }
|
40
|
+
attributes_to_ignore += attributes.keys.keep_if{|k| k.match(/able_type\z/) }
|
41
|
+
end
|
42
|
+
attributes.except(*attributes_to_ignore)
|
42
43
|
end
|
43
|
-
attributes.except(*attributes_to_ignore)
|
44
|
-
end
|
45
44
|
|
46
45
|
def same_as?(source,options={})
|
47
46
|
matterful_attributes(options) == source.matterful_attributes(options)
|
@@ -68,7 +67,8 @@ module MatterfulAttributes
|
|
68
67
|
self.matterful_update(source,options={})
|
69
68
|
save
|
70
69
|
end
|
71
|
-
|
72
|
-
end
|
73
|
-
end
|
74
70
|
end
|
71
|
+
|
72
|
+
# include the extension
|
73
|
+
ActiveRecord::Base.send(:include, MatterfulAttributes)
|
74
|
+
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: matterful_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Gorbikoff
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-06-
|
11
|
+
date: 2013-06-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|