has-meta 0.9.0 → 0.9.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6449bf1d52f37f801937de1aa1dc88860cf438a0d8af2327ca515bd4e7b74beb
4
- data.tar.gz: 786bd5e56684b80b714800ae2fcf38f32477c12dc8e8ad0492f3c70a7fd1da4e
3
+ metadata.gz: a94d69c3cbc7fe392f26fb743ad04986eae8111f2f7a22cb33724cbf5af2ca34
4
+ data.tar.gz: 00f60029849421089cefd0b0502d1a656d0a037b7d9f7c41739106ee5e21634b
5
5
  SHA512:
6
- metadata.gz: c68307d445a2da8111c8a9318e4b0e0433233d7b94074b21c8c274c300e234706a49e31ff1cb698129c43109ce69d9a03de5e001c59d9ee4d865423456ea77bb
7
- data.tar.gz: 616fb1958da452a0fadb480b09898a8f4d886f162e40e293dd84436f24554b07a142d01237e96798f13908ca3026016655e4df421ee7e148a674f7461d48a3b7
6
+ metadata.gz: 6e7388397b46800e75e0eb3a4b4006697e3e8afb6eb329440c5e067ea29448f088f8b88d15769afbfdcbd376a863079ec36e3da6ba73bb9c6fe0068b81c85976
7
+ data.tar.gz: 1bfa120ca372cb3634640345341ba8e5f2e47d3bc5236a58cf74a5a6fd455601a1f4bab7de5e4d3fbc1ee83ddc2c1d442a46f19c8dcee782827edea95693caa7
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- has-meta (0.9.0)
4
+ has-meta (0.9.1)
5
5
  activerecord (>= 4.2.8)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -87,8 +87,6 @@ new_part.attributes = {catalog_number: 12345}
87
87
  new_part.catalog_number # => 12345
88
88
  ```
89
89
 
90
- **NB**: _Declaring a meta attribute on a model creates a polymorphic relationship between the model and the MetaData model. Therefore, the parent model must be saved before assigning meta attributes._
91
-
92
90
  Meta attributes may also represent an Active Record model. Perhaps some of our parts may conform to a uniform standard represented by class `Standard`. Just declare the meta attribute `:standard` and `has-meta` will treat the meta attribute as a one-to-one relation if the attribute corresponds to an Active Record model in your app.
93
91
 
94
92
  ```ruby
@@ -3,3 +3,6 @@
3
3
  * Return duck-typed values (strings to dates and times)
4
4
  * Allow manually setting data type via `:as` option: `#meta_set :key, value, as: :type`
5
5
 
6
+ ###0.9.1
7
+ * Don't try to set meta attributes until parent object is persisted
8
+ * Update meta_set! method to accept options hash
@@ -38,6 +38,9 @@ module HasMeta
38
38
 
39
39
  class_eval do
40
40
  has_many :meta_data, as: :meta_model, dependent: :destroy, class_name: '::HasMeta::MetaData'
41
+ attr_accessor :meta_attributes_pending_save
42
+ after_save :save_pending_meta_attributes,
43
+ if: ->(x) { x.persisted? and x.meta_attributes_pending_save.present? }
41
44
  include HasMeta::InstanceMethods
42
45
  include HasMeta::DynamicMethods
43
46
  include HasMeta::QueryMethods
@@ -2,20 +2,34 @@ module HasMeta
2
2
  module InstanceMethods
3
3
 
4
4
  def meta_get key
5
+ return self.meta_attributes_pending_save[key][:value] if self.meta_attributes_pending_save.try(:[], key).present?
6
+
5
7
  self.meta_data.where(key: key.to_s).try(:first).try(:value)
6
8
  end
7
9
 
8
10
  def meta_set key, value, options={}
9
11
  return meta_destroy key if value.nil? or value == ''
10
-
11
- meta = self.meta_data.where(key: key.to_s).first_or_create
12
- # meta.value = value
13
- meta.send :value=, value, options
14
- meta
12
+ if self.persisted?
13
+ meta = self.meta_data.where(key: key.to_s).first_or_create
14
+ meta.send :value=, value, options
15
+ meta
16
+ else
17
+ add_to_pending_save key => {value: value, options: options}
18
+ end
15
19
  end
16
20
 
17
- def meta_set! key, value
18
- result = meta_set(key, value)
21
+ def add_to_pending_save args
22
+ self.meta_attributes_pending_save ||= {}
23
+ self.meta_attributes_pending_save.merge!(args)
24
+ end
25
+
26
+ def save_pending_meta_attributes
27
+ self.meta_attributes_pending_save.each { |key, v| meta_set! key, v[:value], v[:options] }
28
+ self.meta_attributes_pending_save.clear
29
+ end
30
+
31
+ def meta_set! key, value, options={}
32
+ result = meta_set(key, value, options)
19
33
  result.respond_to?(:save) ? result.save : result
20
34
  end
21
35
 
@@ -1,3 +1,3 @@
1
1
  module HasMeta
2
- VERSION = "0.9.0"
2
+ VERSION = "0.9.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has-meta
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Drust
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-02-09 00:00:00.000000000 Z
11
+ date: 2018-02-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord