matterful_attributes 0.0.2 → 0.0.3

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: c917cbd924d618b2e066d3025690bad096794c24
4
- data.tar.gz: 120c19f87f9b7446b71b1643db84dfd29a6d5727
3
+ metadata.gz: 71b08449dae61ecb94fb522f7fa6704a1752ee7c
4
+ data.tar.gz: 35f693c553e2fda54742fb1dd1d405b02becf49e
5
5
  SHA512:
6
- metadata.gz: 9f7287fd99ed3b119589f898690c9d900fa0d554ec435639c3b0f79038d1ff4e7dbdc8ac378b6456a5d077c950d008ae10b82928e254234b4f5f85aba22dcd0a
7
- data.tar.gz: 205d9ca712d84a24862965543197a7f919bd3170bbbd027883dc96b01cbe4a32e810f5f559bf4b6dbe3de3d8fcac1aee74db1c9273c9ab6c3de5f0671361516f
6
+ metadata.gz: dcae85a5ebe606d6da72088eca64e2f128ce4da5d6b17d80fe2838d83e50ea42746b16bde47eb93c31417b44a219950c6a302480b9c4bce1ea5aecac5329cc74
7
+ data.tar.gz: 352775d60b8f4629d3d0f7ff3bba2bb04c9437acc35ec6c972bf0dd45de01803a4f5734bf9820435d462c3dbb2844c9fc420593fc58b7e0348c46df5f23e1e93
@@ -1,47 +1,46 @@
1
1
  require "matterful_attributes/version"
2
2
 
3
3
  module MatterfulAttributes
4
- module ActiveRecord
5
- class Base
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
- 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']
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/) }
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
+
@@ -1,3 +1,3 @@
1
1
  module MatterfulAttributes
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
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.2
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-09 00:00:00.000000000 Z
11
+ date: 2013-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler