ar_doc_store 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 +4 -4
- data/.gitignore +1 -0
- data/README.md +15 -3
- data/lib/ar_doc_store/attribute_types/array.rb +10 -5
- data/lib/ar_doc_store/attribute_types/base.rb +9 -0
- data/lib/ar_doc_store/attribute_types/boolean.rb +5 -1
- data/lib/ar_doc_store/attribute_types/float.rb +5 -0
- data/lib/ar_doc_store/attribute_types/integer.rb +5 -0
- data/lib/ar_doc_store/embeddable_model.rb +19 -13
- data/lib/ar_doc_store/storage.rb +13 -10
- data/lib/ar_doc_store/version.rb +1 -1
- data/test/test_helper.rb +4 -0
- 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: 9bc6988ec4fcb270a64c6e8ab1bca2ba8fd6590a
|
4
|
+
data.tar.gz: 7d11854e6bca4785b91a87e5f3bec76847e621c5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b219537a7081e7325f968e674efe0e31c16d48c5f145e1d470dc95316628f75196232ec7aaa44197dd27152b5eba6ead6579ca6d2ca6f2d0178ee1ef95c27742
|
7
|
+
data.tar.gz: 22efd90e20b683daef4bb7b2497f0353de52da1b09a28ab2e76279c312d05b3bb9b67073fd8ca6dbaf3852679859af7a37efa7a2619a7b045b81539b5981b8e6
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -30,7 +30,7 @@ The first thing you need to do is create a migration that adds a column called "
|
|
30
30
|
change_table :buildings do |t|
|
31
31
|
t.json :data
|
32
32
|
end
|
33
|
-
|
33
|
+
```
|
34
34
|
|
35
35
|
Then in the model file:
|
36
36
|
|
@@ -49,7 +49,7 @@ class Building < ActiveRecord::Base
|
|
49
49
|
|
50
50
|
attribute :name, :string
|
51
51
|
attribute :width, :float
|
52
|
-
attribute :height, as: :float
|
52
|
+
attribute :height, as: :float # the :as is optional but if you like the consistency with SimpleForm
|
53
53
|
attribute :storeys, :integer
|
54
54
|
attribute :finished, :boolean
|
55
55
|
attribute :construction_type, :enumeration, values: %w{wood plaster mud brick}, multiple: true, strict: true
|
@@ -100,6 +100,7 @@ Now let's put a Door on a Building:
|
|
100
100
|
```ruby
|
101
101
|
class Building ...
|
102
102
|
embeds_one :door
|
103
|
+
embeds_one :secret_door, class_name: 'Door'
|
103
104
|
end
|
104
105
|
```
|
105
106
|
|
@@ -110,6 +111,8 @@ building.build_door
|
|
110
111
|
building.door.clear_distance = 30
|
111
112
|
building.door.opening_force = 20
|
112
113
|
building.door.open_handle = %w{pull knob}
|
114
|
+
building.build_secret_door
|
115
|
+
building.secret_door.clear_distance = 3
|
113
116
|
building.save
|
114
117
|
```
|
115
118
|
|
@@ -124,6 +127,7 @@ def resource_params
|
|
124
127
|
params.require(:building).permit :name, :height, door_attributes: [:door_type]
|
125
128
|
end
|
126
129
|
|
130
|
+
# in the view, with a bonus plug for Slim templates:
|
127
131
|
= simple_form_for @building do |form|
|
128
132
|
= form.input :name, as: :string
|
129
133
|
= form.input :height, as: :float
|
@@ -147,6 +151,7 @@ end
|
|
147
151
|
|
148
152
|
class Building ...
|
149
153
|
embeds_many :rooms
|
154
|
+
embeds_one :foyer, class_name: 'Room'
|
150
155
|
end
|
151
156
|
|
152
157
|
building = Building.new
|
@@ -221,9 +226,16 @@ class BooleanInput < SimpleForm::Inputs::CollectionRadioButtonsInput
|
|
221
226
|
end
|
222
227
|
```
|
223
228
|
|
229
|
+
## Roadmap
|
230
|
+
1. Default values for attributes. (I haven't needed yet...)
|
231
|
+
2. Ransackers for embedded model attributes. (I haven't needed yet...)
|
232
|
+
3. Refactor the EmbedsOne and EmbedsMany modules to use a Builder class instead of procedural metaprogramming. (Hello Code Climate...)
|
233
|
+
4. Currently when you mass-assign values to an embedded model, you need to assign all the values. It basically replaces what is there with what you send in, removing what has :_destroy set. I would be nice if it could do a smarter find or create behavior.
|
234
|
+
5. It would be nice if you could use the AR fluent query API on stored attributes, where is knows to replace, say, "name" with "data->>name" but I don't see how to do that, and Ransack provides a nice enough wrapper around ARel to get the job done another way.
|
235
|
+
|
224
236
|
## Contributing
|
225
237
|
|
226
|
-
1. Fork it ( https://github.com/
|
238
|
+
1. Fork it ( https://github.com/dfurber/ar_doc_store/fork )
|
227
239
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
228
240
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
229
241
|
4. Push to the branch (`git push origin my-new-feature`)
|
@@ -2,12 +2,17 @@ module ArDocStore
|
|
2
2
|
module AttributeTypes
|
3
3
|
|
4
4
|
class ArrayAttribute < Base
|
5
|
-
def conversion
|
6
|
-
:to_a
|
7
|
-
end
|
8
5
|
|
9
|
-
def
|
10
|
-
|
6
|
+
def build
|
7
|
+
key = attribute.to_sym
|
8
|
+
model.class_eval do
|
9
|
+
store_accessor :data, key
|
10
|
+
define_method "#{key}=".to_sym, -> (value) {
|
11
|
+
value = nil if value == ['']
|
12
|
+
write_store_attribute(:data, key, value)
|
13
|
+
}
|
14
|
+
add_ransacker(key, 'text')
|
15
|
+
end
|
11
16
|
end
|
12
17
|
|
13
18
|
end
|
@@ -9,11 +9,20 @@ module ArDocStore
|
|
9
9
|
|
10
10
|
def initialize(model, attribute, options)
|
11
11
|
@model, @attribute, @options = model, attribute, options
|
12
|
+
add_to_columns_hash
|
12
13
|
end
|
13
14
|
|
14
15
|
def build
|
15
16
|
model.store_attributes conversion, predicate, attribute
|
16
17
|
end
|
18
|
+
|
19
|
+
def add_to_columns_hash
|
20
|
+
model.columns_hash[attribute.to_s] = ActiveRecord::ConnectionAdapters::PostgreSQLColumn.new(attribute, nil, column_type)
|
21
|
+
end
|
22
|
+
|
23
|
+
def column_type
|
24
|
+
ActiveRecord::Type::String.new
|
25
|
+
end
|
17
26
|
end
|
18
27
|
|
19
28
|
end
|
@@ -6,7 +6,6 @@ module ArDocStore
|
|
6
6
|
key = attribute.to_sym
|
7
7
|
model.class_eval do
|
8
8
|
store_accessor :data, key
|
9
|
-
# define_method key, -> { item = super(); item && item == true }
|
10
9
|
define_method "#{key}?".to_sym, -> { !!key }
|
11
10
|
define_method "#{key}=".to_sym, -> (value) {
|
12
11
|
res = nil
|
@@ -17,6 +16,11 @@ module ArDocStore
|
|
17
16
|
add_ransacker(key, 'bool')
|
18
17
|
end
|
19
18
|
end
|
19
|
+
|
20
|
+
def column_type
|
21
|
+
ActiveRecord::Type::Boolean.new
|
22
|
+
end
|
23
|
+
|
20
24
|
end
|
21
25
|
|
22
26
|
end
|
@@ -4,17 +4,6 @@ module ArDocStore
|
|
4
4
|
|
5
5
|
def self.included(mod)
|
6
6
|
|
7
|
-
mod.send :include, ArDocStore::Storage
|
8
|
-
mod.send :include, ArDocStore::Embedding
|
9
|
-
mod.send :include, InstanceMethods
|
10
|
-
mod.send :extend, ClassMethods
|
11
|
-
mod.send :include, ActiveModel::AttributeMethods
|
12
|
-
mod.send :include, ActiveModel::Validations
|
13
|
-
mod.send :include, ActiveModel::Conversion
|
14
|
-
mod.send :extend, ActiveModel::Naming
|
15
|
-
mod.send :include, ActiveModel::Dirty
|
16
|
-
mod.send :include, ActiveModel::Serialization
|
17
|
-
|
18
7
|
mod.class_eval do
|
19
8
|
attr_accessor :_destroy
|
20
9
|
attr_accessor :attributes
|
@@ -23,11 +12,28 @@ module ArDocStore
|
|
23
12
|
|
24
13
|
def self.virtual_attributes; @virtual_attributes; end
|
25
14
|
def self.virtual_attributes=(value); @virtual_attributes=value; end
|
26
|
-
def virtual_attributes; self.class.virtual_attributes
|
15
|
+
def virtual_attributes; self.class.virtual_attributes; end
|
16
|
+
|
17
|
+
@columns_hash = HashWithIndifferentAccess.new
|
18
|
+
def self.columns_hash; @columns_hash; end
|
19
|
+
def self.columns_hash=(value); @columns_hash=value; end
|
20
|
+
def columns_hash; self.class.columns_hash; end
|
27
21
|
|
28
22
|
delegate :as_json, to: :attributes
|
29
23
|
|
30
24
|
end
|
25
|
+
|
26
|
+
mod.send :include, ArDocStore::Storage
|
27
|
+
mod.send :include, ArDocStore::Embedding
|
28
|
+
mod.send :include, InstanceMethods
|
29
|
+
mod.send :extend, ClassMethods
|
30
|
+
mod.send :include, ActiveModel::AttributeMethods
|
31
|
+
mod.send :include, ActiveModel::Validations
|
32
|
+
mod.send :include, ActiveModel::Conversion
|
33
|
+
mod.send :extend, ActiveModel::Naming
|
34
|
+
mod.send :include, ActiveModel::Dirty
|
35
|
+
mod.send :include, ActiveModel::Serialization
|
36
|
+
|
31
37
|
end
|
32
38
|
|
33
39
|
module InstanceMethods
|
@@ -76,7 +82,7 @@ module ArDocStore
|
|
76
82
|
define_method key, -> { read_store_attribute(:data, key) }
|
77
83
|
define_method "#{key}=".to_sym, -> (value) { write_store_attribute :data, key, value }
|
78
84
|
end
|
79
|
-
|
85
|
+
|
80
86
|
end
|
81
87
|
|
82
88
|
end
|
data/lib/ar_doc_store/storage.rb
CHANGED
@@ -48,10 +48,8 @@ module ArDocStore
|
|
48
48
|
type = args.shift if args.first.is_a?(Symbol)
|
49
49
|
options = args.extract_options!
|
50
50
|
type ||= options.delete(:as) || :string
|
51
|
-
class_name = ArDocStore.mappings[type]
|
52
|
-
unless const_defined?(class_name)
|
53
|
-
raise "Invalid attribute type: #{name}"
|
54
|
-
end
|
51
|
+
class_name = ArDocStore.mappings[type] || "ArDocStore::AttributeTypes::#{type.to_s.classify}Attribute"
|
52
|
+
raise "Invalid attribute type: #{name}" unless const_defined?(class_name)
|
55
53
|
class_name = class_name.constantize
|
56
54
|
class_name.build self, name, options
|
57
55
|
end
|
@@ -87,29 +85,34 @@ module ArDocStore
|
|
87
85
|
}
|
88
86
|
define_method "#{key}=".to_sym, -> (value) {
|
89
87
|
# data_will_change! if @initalized
|
90
|
-
|
88
|
+
if value == '' || value.nil?
|
89
|
+
write_store_attribute :data, key, nil
|
90
|
+
else
|
91
|
+
write_store_attribute(:data, key, value.public_send(typecast_method))
|
92
|
+
end
|
91
93
|
}
|
92
94
|
end
|
93
95
|
|
94
96
|
def store_attribute_from_class(class_name, key)
|
95
97
|
define_method key.to_sym, -> {
|
96
98
|
ivar = "@#{key}"
|
97
|
-
|
98
|
-
existing || begin
|
99
|
+
instance_variable_get(ivar) || begin
|
99
100
|
item = read_store_attribute(:data, key)
|
100
101
|
class_name = class_name.constantize if class_name.respond_to?(:constantize)
|
101
102
|
item = class_name.new(item) unless item.is_a?(class_name)
|
102
103
|
instance_variable_set ivar, item
|
103
|
-
item
|
104
104
|
end
|
105
105
|
}
|
106
106
|
define_method "#{key}=".to_sym, -> (value) {
|
107
107
|
ivar = "@#{key}"
|
108
108
|
class_name = class_name.constantize if class_name.respond_to?(:constantize)
|
109
|
-
|
109
|
+
if value == ''
|
110
|
+
value = nil
|
111
|
+
else
|
112
|
+
value = class_name.new(value) unless value.is_a?(class_name)
|
113
|
+
end
|
110
114
|
instance_variable_set ivar, value
|
111
115
|
write_store_attribute :data, key, value
|
112
|
-
# data_will_change! if @initialized
|
113
116
|
}
|
114
117
|
end
|
115
118
|
|
data/lib/ar_doc_store/version.rb
CHANGED
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ar_doc_store
|
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
|
- David Furber
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|