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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +0 -2
- data/changelog.md +3 -0
- data/lib/has-meta.rb +3 -0
- data/lib/has_meta/instance_methods.rb +21 -7
- data/lib/has_meta/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a94d69c3cbc7fe392f26fb743ad04986eae8111f2f7a22cb33724cbf5af2ca34
|
|
4
|
+
data.tar.gz: 00f60029849421089cefd0b0502d1a656d0a037b7d9f7c41739106ee5e21634b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6e7388397b46800e75e0eb3a4b4006697e3e8afb6eb329440c5e067ea29448f088f8b88d15769afbfdcbd376a863079ec36e3da6ba73bb9c6fe0068b81c85976
|
|
7
|
+
data.tar.gz: 1bfa120ca372cb3634640345341ba8e5f2e47d3bc5236a58cf74a5a6fd455601a1f4bab7de5e4d3fbc1ee83ddc2c1d442a46f19c8dcee782827edea95693caa7
|
data/Gemfile.lock
CHANGED
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
|
data/changelog.md
CHANGED
|
@@ -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
|
data/lib/has-meta.rb
CHANGED
|
@@ -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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
|
18
|
-
|
|
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
|
|
data/lib/has_meta/version.rb
CHANGED
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.
|
|
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-
|
|
11
|
+
date: 2018-02-13 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activerecord
|