push_type_core 0.5.3 → 0.6.0.beta.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/fields/push_type/asset_field.rb +5 -7
- data/app/fields/push_type/date_field.rb +4 -3
- data/app/fields/push_type/markdown_field.rb +20 -16
- data/app/fields/push_type/matrix_field.rb +26 -16
- data/app/fields/push_type/node_field.rb +7 -9
- data/app/fields/push_type/number_field.rb +2 -5
- data/app/fields/push_type/relation_field.rb +78 -0
- data/app/fields/push_type/repeater_field.rb +45 -6
- data/app/fields/push_type/select_field.rb +9 -21
- data/app/fields/push_type/structure_field.rb +40 -0
- data/app/fields/push_type/tag_list_field.rb +23 -12
- data/app/fields/push_type/taxonomy_field.rb +6 -8
- data/app/fields/push_type/time_field.rb +4 -3
- data/app/models/concerns/push_type/customizable.rb +41 -21
- data/app/models/concerns/push_type/presentable.rb +3 -3
- data/app/models/concerns/push_type/templatable.rb +1 -1
- data/app/models/push_type/structure.rb +27 -0
- data/lib/push_type/field_type.rb +93 -8
- data/lib/push_type/primitives/array_type.rb +14 -0
- data/lib/push_type/primitives/base.rb +21 -0
- data/lib/push_type/primitives/number_type.rb +14 -0
- data/lib/push_type/primitives/object_type.rb +7 -0
- data/lib/push_type/primitives/string_type.rb +12 -0
- data/lib/push_type/testing/factories.rb +1 -1
- data/lib/push_type/testing/setup.rb +2 -0
- data/lib/push_type/version.rb +1 -1
- data/test/dummy/config/initializers/push_type.rb +1 -1
- data/test/dummy/config/secrets.yml +2 -2
- data/test/dummy/db/schema.rb +1 -1
- data/test/dummy/log/test.log +15836 -6164
- data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/0ANSDcZJu9gPyqtYPIW729qm1kriQ29Vn9LU8T2SGQU.cache +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/3T6ioyhwSsEzte_a9viHBvfmuKgORQ019srIEoUYkVs.cache +1 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/5Lly_CA8DZvPhQV2jDQx-Y6P_y3Ygra9t5jfSlGhHDA.cache +2 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/IYzwMwq7DWWSuojH6oXsrP__pcD7O8TQieMFSzQiwnc.cache +1 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/OI6uxGcnsKavdWTtwDAasU3wPx8QXhzBgV0X2n1KjMQ.cache +3 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/_d65qW7wxRtKVSYpmfSenECz5QUWVI9pppUgV7TA1Rk.cache +0 -0
- data/test/dummy/tmp/generators/config/initializers/push_type.rb +1 -1
- data/test/fields/push_type/asset_field_test.rb +12 -25
- data/test/fields/push_type/date_field_test.rb +11 -3
- data/test/fields/push_type/markdown_field_test.rb +12 -24
- data/test/fields/push_type/matrix_field_test.rb +16 -24
- data/test/fields/push_type/node_field_test.rb +36 -59
- data/test/fields/push_type/number_field_test.rb +18 -4
- data/test/fields/push_type/relation_field_test.rb +48 -0
- data/test/fields/push_type/repeater_field_test.rb +20 -11
- data/test/fields/push_type/select_field_test.rb +23 -28
- data/test/fields/push_type/string_field_test.rb +21 -0
- data/test/fields/push_type/structure_field_test.rb +27 -0
- data/test/fields/push_type/tag_list_field_test.rb +15 -14
- data/test/fields/push_type/taxonomy_field_test.rb +36 -45
- data/test/fields/push_type/text_field_test.rb +11 -2
- data/test/fields/push_type/time_field_test.rb +11 -3
- data/test/lib/push_type/core_test.rb +3 -3
- data/test/lib/push_type/field_type_test.rb +15 -13
- data/test/lib/push_type/primitives_test.rb +43 -0
- data/test/models/concerns/push_type/customizable_test.rb +9 -27
- data/test/models/concerns/push_type/nestable_test.rb +3 -0
- data/test/models/concerns/push_type/templatable_test.rb +17 -16
- metadata +65 -40
- data/lib/push_type/fields/arrays.rb +0 -23
- data/lib/push_type/fields/base.rb +0 -65
- data/lib/push_type/fields/class_methods.rb +0 -21
- data/lib/push_type/fields/relations.rb +0 -39
- data/lib/push_type/fields/ui.rb +0 -16
- data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/5hBD45unr3-A8x6pxXjxq708mnY9gJisy1yA5AojZWQ.cache +0 -3
- data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/M0EVptJKeB1GMjwoMb3w3mgp8VB55MSbItnFhItY4hA.cache +0 -1
- data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/MSjNaQjz3SH1LnDyk7nWta_i3hcCEV9m1eoNFbOXjwI.cache +0 -2
- data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/g97nJc5YmnbscVR5pZZoxgfj652ml7dLjUIu0FHDwLA.cache +0 -0
- data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/utXmZy0kE8zUnHIqwtbjUUJlYlFcGDJreK2YPDuR3n8.cache +0 -1
- data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/vlJujjzOikOTUvCvT3C4o3vK0ySfxbQQ3RBE37vXH0U.cache +0 -0
- /data/test/dummy/db/migrate/{20150928114659_create_push_type_users.push_type.rb → 20151015181653_create_push_type_users.push_type.rb} +0 -0
- /data/test/dummy/db/migrate/{20150928114660_create_push_type_nodes.push_type.rb → 20151015181654_create_push_type_nodes.push_type.rb} +0 -0
- /data/test/dummy/db/migrate/{20150928114661_create_push_type_node_hierarchies.push_type.rb → 20151015181655_create_push_type_node_hierarchies.push_type.rb} +0 -0
- /data/test/dummy/db/migrate/{20150928114662_create_push_type_assets.push_type.rb → 20151015181656_create_push_type_assets.push_type.rb} +0 -0
- /data/test/dummy/db/migrate/{20150928114663_create_push_type_taxonomies.push_type.rb → 20151015181657_create_push_type_taxonomies.push_type.rb} +0 -0
- /data/test/dummy/db/migrate/{20150928114664_create_push_type_taxonomy_hierarchies.push_type.rb → 20151015181658_create_push_type_taxonomy_hierarchies.push_type.rb} +0 -0
- /data/test/dummy/db/migrate/{20150928114665_add_field_store_default_values.push_type.rb → 20151015181659_add_field_store_default_values.push_type.rb} +0 -0
- /data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/{0VLfu-ipRCm5lg6MOptoyejoCkDcrvsfiei7dJqJR7o.cache → MSZIb53M-WSslpcsxtT_LlGognclDpLjB9YyJ1qLm3c.cache} +0 -0
- /data/test/dummy/tmp/cache/assets/test/sprockets/v3.0/{8Mt8KKhDfWMlVzIUM3vc5LZsEDGQkPmtR1lWxp_W0KU.cache → jE1v4kSAksoYAFuvE0Nbl5ZIcuyFYlWX8cb9YkFd5BY.cache} +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6d2e4ab5d33d0814637934a8bdcf85e998a5a59d
|
4
|
+
data.tar.gz: 69ef1e5c8346a126fb9de81c9287444666904a09
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aca95e9efd081d8c74567fc9f1a2350dd1b8ffb66efda6fcdfbd23e9d0ad559d00683f5335051862f60017080571f0963653fb056c21c4a5b64f03284d5e3caf
|
7
|
+
data.tar.gz: 8c8aa84c7ea8912e2fbb86904ddc922177a17fabcd6961a50b5cda08ea0376b72813705173af742f97be218aacbf11d920304ba72c0000aeba660ef8b73e2bee
|
@@ -1,7 +1,5 @@
|
|
1
1
|
module PushType
|
2
|
-
class AssetField <
|
3
|
-
|
4
|
-
include PushType::Fields::Relations
|
2
|
+
class AssetField < RelationField
|
5
3
|
|
6
4
|
options template: 'asset'
|
7
5
|
|
@@ -13,11 +11,11 @@ module PushType
|
|
13
11
|
PushType::Asset
|
14
12
|
end
|
15
13
|
|
16
|
-
|
14
|
+
on_instance do |object, field|
|
17
15
|
object.class_eval do
|
18
|
-
define_method
|
19
|
-
field.relation_class.not_trash.find
|
20
|
-
end
|
16
|
+
define_method(field.relation_name.to_sym) do
|
17
|
+
field.relation_class.not_trash.find field.json_value unless field.json_value.blank?
|
18
|
+
end unless method_defined?(field.relation_name.to_sym)
|
21
19
|
end
|
22
20
|
end
|
23
21
|
|
@@ -1,10 +1,11 @@
|
|
1
1
|
module PushType
|
2
2
|
class DateField < PushType::FieldType
|
3
3
|
|
4
|
-
options template:
|
4
|
+
options template: 'date',
|
5
|
+
form_helper: :date_field
|
5
6
|
|
6
|
-
def
|
7
|
-
|
7
|
+
def value
|
8
|
+
json_value.to_date unless json_value.blank?
|
8
9
|
end
|
9
10
|
|
10
11
|
end
|
@@ -3,15 +3,24 @@ require 'redcarpet'
|
|
3
3
|
module PushType
|
4
4
|
class MarkdownField < PushType::FieldType
|
5
5
|
|
6
|
-
options form_helper:
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
6
|
+
options form_helper: :text_area,
|
7
|
+
renderer: Redcarpet::Render::HTML,
|
8
|
+
render_options: {},
|
9
|
+
extensions: {
|
10
|
+
autolink: true,
|
11
|
+
fenced_code_blocks: true,
|
12
|
+
no_intra_emphasis: true,
|
13
|
+
space_after_headers: true,
|
14
|
+
strikethrough: true,
|
15
|
+
tables: true,
|
16
|
+
underline: true
|
17
|
+
}
|
18
|
+
|
19
|
+
def compiled_value
|
20
|
+
markdown.render value unless value.nil?
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
15
24
|
|
16
25
|
def markdown
|
17
26
|
@markdown ||= Redcarpet::Markdown.new(renderer, @opts[:extensions])
|
@@ -21,14 +30,9 @@ module PushType
|
|
21
30
|
@opts[:renderer].new @opts[:render_options]
|
22
31
|
end
|
23
32
|
|
24
|
-
|
33
|
+
on_instance do |object, field|
|
25
34
|
object.presenter_class.class_eval do
|
26
|
-
|
27
|
-
define_method field.name.to_sym do
|
28
|
-
raw = super()
|
29
|
-
fields[field.json_key].markdown.render raw unless raw.nil?
|
30
|
-
end
|
31
|
-
|
35
|
+
define_method(field.name) { field.compiled_value } unless method_defined?(field.name)
|
32
36
|
end
|
33
37
|
end
|
34
38
|
|
@@ -1,32 +1,42 @@
|
|
1
1
|
module PushType
|
2
2
|
class MatrixField < PushType::FieldType
|
3
3
|
|
4
|
-
|
4
|
+
options json_primitive: :array,
|
5
|
+
template: 'matrix'
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
def initialize(*args, &block)
|
8
|
+
super
|
9
|
+
structure_class.class_eval(&block) if block
|
10
|
+
validate_field_types!
|
10
11
|
end
|
11
12
|
|
12
|
-
def
|
13
|
-
|
14
|
-
|
13
|
+
def value
|
14
|
+
return if json_value.blank?
|
15
|
+
json_value.map do |h|
|
16
|
+
structure_class.new(field_store: h)
|
15
17
|
end
|
16
18
|
end
|
17
19
|
|
18
|
-
def
|
19
|
-
@
|
20
|
+
def fields
|
21
|
+
@fields ||= structure_class.new.fields
|
20
22
|
end
|
21
23
|
|
22
|
-
|
23
|
-
|
24
|
-
|
24
|
+
private
|
25
|
+
|
26
|
+
def structure_class
|
27
|
+
@structure_class ||= PushType::Structure.clone
|
28
|
+
end
|
29
|
+
|
30
|
+
def validate_field_types!
|
31
|
+
fields.values.each do |f|
|
32
|
+
unless field_type_whitelist.include?(f.kind)
|
33
|
+
raise ArgumentError, "Invalid field type. `#{ kind }` cannot be used in #{ self.class.name }."
|
34
|
+
end
|
35
|
+
end
|
25
36
|
end
|
26
37
|
|
27
|
-
def
|
28
|
-
|
29
|
-
super.reject { |v| v.blank? or v.values.all?(&:blank?) }.map { |h| struct.new(*h.values) }
|
38
|
+
def field_type_whitelist
|
39
|
+
[:number, :string, :text]
|
30
40
|
end
|
31
41
|
|
32
42
|
end
|
@@ -1,9 +1,7 @@
|
|
1
1
|
module PushType
|
2
|
-
class NodeField <
|
2
|
+
class NodeField < RelationField
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
options template: 'relation', field_options: {}, root: '/'
|
4
|
+
options template: 'relation', root: '/'
|
7
5
|
|
8
6
|
def relation_class
|
9
7
|
super
|
@@ -15,13 +13,13 @@ module PushType
|
|
15
13
|
root = relation_class.not_trash
|
16
14
|
root = root.find_by_path(@opts[:root].split('/')) unless @opts[:root] == '/'
|
17
15
|
root or raise "Cannot find root node at path '#{ @opts[:root] }'"
|
18
|
-
end
|
16
|
+
end
|
19
17
|
|
20
|
-
|
18
|
+
on_instance do |object, field|
|
21
19
|
object.class_eval do
|
22
|
-
define_method
|
23
|
-
field.relation_class.not_trash.find
|
24
|
-
end
|
20
|
+
define_method(field.relation_name.to_sym) do
|
21
|
+
field.relation_class.not_trash.find field.json_value unless field.json_value.blank?
|
22
|
+
end unless method_defined?(field.relation_name.to_sym)
|
25
23
|
end
|
26
24
|
end
|
27
25
|
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module PushType
|
2
|
+
class RelationField < PushType::FieldType
|
3
|
+
|
4
|
+
options template: 'relation'
|
5
|
+
|
6
|
+
def initialize(*args)
|
7
|
+
super
|
8
|
+
raise ArgumentError, "Relation field names must end with suffix `_id` or `ids`." unless relation_name
|
9
|
+
end
|
10
|
+
|
11
|
+
def json_primitive
|
12
|
+
multiple? ? :array : super
|
13
|
+
end
|
14
|
+
|
15
|
+
def label
|
16
|
+
super || relation_name.humanize
|
17
|
+
end
|
18
|
+
|
19
|
+
def html_options
|
20
|
+
super.merge(multiple: multiple?)
|
21
|
+
end
|
22
|
+
|
23
|
+
def choices
|
24
|
+
if relation_root.respond_to?(:hash_tree)
|
25
|
+
flatten_tree(relation_root.hash_tree)
|
26
|
+
else
|
27
|
+
relation_root.all.map { |item| item_hash(item) }
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def relation_name
|
32
|
+
@relation_name ||= (rel = name.to_s.gsub!(/_ids?$/, '')) && (rel && multiple? ? rel.pluralize : rel)
|
33
|
+
end
|
34
|
+
|
35
|
+
def relation_class
|
36
|
+
(@opts[:to] || relation_name.singularize).classify.constantize
|
37
|
+
end
|
38
|
+
|
39
|
+
def relation_root
|
40
|
+
relation_class
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def defaults
|
46
|
+
super.merge({
|
47
|
+
label: nil,
|
48
|
+
mapping: { value: :id, text: :title }
|
49
|
+
})
|
50
|
+
end
|
51
|
+
|
52
|
+
def item_hash(item, d = 0)
|
53
|
+
{
|
54
|
+
value: item.send(@opts[:mapping][:value]),
|
55
|
+
text: item.send(@opts[:mapping][:text]),
|
56
|
+
depth: d
|
57
|
+
}
|
58
|
+
end
|
59
|
+
|
60
|
+
def flatten_tree(hash_tree, d = 0)
|
61
|
+
hash_tree.flat_map do |parent, children|
|
62
|
+
[
|
63
|
+
item_hash(parent, d),
|
64
|
+
flatten_tree(children, d+1)
|
65
|
+
]
|
66
|
+
end.flatten
|
67
|
+
end
|
68
|
+
|
69
|
+
on_instance do |object, field|
|
70
|
+
object.class_eval do
|
71
|
+
define_method(field.relation_name.to_sym) do
|
72
|
+
field.relation_class.find field.json_value unless field.json_value.blank?
|
73
|
+
end unless method_defined?(field.relation_name.to_sym)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
@@ -1,16 +1,55 @@
|
|
1
1
|
module PushType
|
2
2
|
class RepeaterField < FieldType
|
3
3
|
|
4
|
-
|
4
|
+
options json_primitive: :array,
|
5
|
+
repeats: :string
|
5
6
|
|
6
|
-
|
7
|
+
def initialize(*args)
|
8
|
+
super
|
9
|
+
validate_field_type!
|
10
|
+
end
|
11
|
+
|
12
|
+
def value
|
13
|
+
return if json_value.blank?
|
14
|
+
json_value.map do |j|
|
15
|
+
generate_field('_f' => j).value
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def template
|
20
|
+
'repeater'
|
21
|
+
end
|
22
|
+
|
23
|
+
def field
|
24
|
+
@field ||= generate_field
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def defaults
|
30
|
+
super.except(:template, :form_helper)
|
31
|
+
end
|
32
|
+
|
33
|
+
def generate_field(json = {})
|
34
|
+
field_type.new :_f, PushType::Structure.new(field_store: json), @opts.except(:css_class).merge(multiple: false)
|
35
|
+
end
|
36
|
+
|
37
|
+
def field_type
|
38
|
+
begin
|
39
|
+
"push_type/#{ @opts[:repeats] }_field".camelize.constantize
|
40
|
+
rescue NameError
|
41
|
+
"#{ @opts[:repeats] }_field".camelize.constantize
|
42
|
+
end
|
43
|
+
end
|
7
44
|
|
8
|
-
def
|
9
|
-
|
45
|
+
def validate_field_type!
|
46
|
+
unless field_type_whitelist.include?(@opts[:repeats])
|
47
|
+
raise ArgumentError, "Invalid field type. `#{ @opts[:repeats] }` cannot be used in #{ self.class.name }."
|
48
|
+
end
|
10
49
|
end
|
11
50
|
|
12
|
-
def
|
13
|
-
|
51
|
+
def field_type_whitelist
|
52
|
+
[:number, :string, :text]
|
14
53
|
end
|
15
54
|
|
16
55
|
end
|
@@ -1,15 +1,9 @@
|
|
1
1
|
module PushType
|
2
2
|
class SelectField < PushType::FieldType
|
3
3
|
|
4
|
-
options template:
|
5
|
-
|
6
|
-
|
7
|
-
@opts[:multiple]
|
8
|
-
end
|
9
|
-
|
10
|
-
def param
|
11
|
-
multiple? ? { json_key => [] } : super
|
12
|
-
end
|
4
|
+
options template: 'select',
|
5
|
+
choices: [],
|
6
|
+
field_options: { include_blank: 'Please select...' }
|
13
7
|
|
14
8
|
def choices
|
15
9
|
if @opts[:choices].respond_to? :call
|
@@ -19,22 +13,16 @@ module PushType
|
|
19
13
|
end
|
20
14
|
end
|
21
15
|
|
22
|
-
def
|
23
|
-
@opts[:
|
24
|
-
end
|
25
|
-
|
26
|
-
def html_options
|
27
|
-
super.merge(multiple: multiple?)
|
16
|
+
def multiple?
|
17
|
+
@opts[:multiple]
|
28
18
|
end
|
29
19
|
|
30
|
-
def
|
31
|
-
|
32
|
-
multiple? ? Array(val).reject(&:blank?) : super
|
20
|
+
def json_primitive
|
21
|
+
multiple? ? :array : super
|
33
22
|
end
|
34
23
|
|
35
|
-
def
|
36
|
-
|
37
|
-
multiple? ? Array(val).reject(&:blank?) : super
|
24
|
+
def html_options
|
25
|
+
@opts[:html_options].merge(multiple: multiple?)
|
38
26
|
end
|
39
27
|
|
40
28
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module PushType
|
2
|
+
class StructureField < PushType::FieldType
|
3
|
+
|
4
|
+
options json_primitive: :object,
|
5
|
+
template: 'structure'
|
6
|
+
|
7
|
+
def initialize(*args, &block)
|
8
|
+
super
|
9
|
+
structure_class.class_eval(&block) if block
|
10
|
+
end
|
11
|
+
|
12
|
+
def value
|
13
|
+
structure.field_store = json_value unless json_value.blank?
|
14
|
+
structure
|
15
|
+
end
|
16
|
+
|
17
|
+
def fields
|
18
|
+
structure.fields
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def structure_class
|
24
|
+
@structure_class ||= PushType::Structure.clone
|
25
|
+
end
|
26
|
+
|
27
|
+
def structure
|
28
|
+
@structure ||= structure_class.new
|
29
|
+
end
|
30
|
+
|
31
|
+
on_class do |klass, field_name, field_class|
|
32
|
+
klass.validate do |n|
|
33
|
+
unless n.send(field_name).valid?
|
34
|
+
n.errors.add field_name, :invalid
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
@@ -1,22 +1,33 @@
|
|
1
1
|
module PushType
|
2
2
|
class TagListField < PushType::FieldType
|
3
3
|
|
4
|
-
|
4
|
+
options json_primitive: :array,
|
5
|
+
template: 'select',
|
6
|
+
html_options: { placeholder: 'Tags...' }
|
5
7
|
|
6
|
-
|
8
|
+
def choices
|
9
|
+
json_value || []
|
10
|
+
end
|
11
|
+
|
12
|
+
def multiple?
|
13
|
+
true
|
14
|
+
end
|
7
15
|
|
8
|
-
def
|
9
|
-
|
16
|
+
def field_options
|
17
|
+
{}
|
10
18
|
end
|
11
19
|
|
12
|
-
|
20
|
+
def html_options
|
21
|
+
super.merge(multiple: multiple?)
|
22
|
+
end
|
13
23
|
|
14
|
-
|
24
|
+
on_class do |klass, field_name, field_class|
|
25
|
+
klass.class_eval do
|
15
26
|
|
16
27
|
# Dynamically define standard scope .with_all_`field_name`
|
17
28
|
# Returns ActiveRecord::Relation
|
18
29
|
#
|
19
|
-
scope "with_all_#{
|
30
|
+
scope "with_all_#{ field_name }".to_sym, ->(*args) {
|
20
31
|
raise ArgumentError, 'wrong number of arguments' unless args.present?
|
21
32
|
tags = args[0].is_a?(Array) ? args[0] : args
|
22
33
|
|
@@ -27,13 +38,13 @@ module PushType
|
|
27
38
|
# Dynamically define class method .with_any_`field_name`
|
28
39
|
# Returns Array
|
29
40
|
#
|
30
|
-
define_singleton_method "with_any_#{
|
41
|
+
define_singleton_method "with_any_#{ field_name }".to_sym do |*args, &block|
|
31
42
|
raise ArgumentError, 'wrong number of arguments' unless args.present?
|
32
43
|
tags = args[0].is_a?(Array) ? args[0] : args
|
33
44
|
|
34
45
|
composed_scope = (
|
35
46
|
block.respond_to?(:call) ? block.call : all
|
36
|
-
).where(["field_store->'#{
|
47
|
+
).where(["field_store->'#{ field_name }' ?| ARRAY[:tags]", { tags: tags }])
|
37
48
|
|
38
49
|
t = Arel::Table.new('t', ActiveRecord::Base)
|
39
50
|
ct = Arel::Table.new('ct', ActiveRecord::Base)
|
@@ -43,7 +54,7 @@ module PushType
|
|
43
54
|
|
44
55
|
lateral = ct
|
45
56
|
.project(Arel.sql('e').count(true).as('ct'))
|
46
|
-
.from(Arel.sql "jsonb_array_elements_text(t.field_store->'#{
|
57
|
+
.from(Arel.sql "jsonb_array_elements_text(t.field_store->'#{ field_name }') e")
|
47
58
|
.where(Arel::Nodes::Equality.new Arel.sql('e'), any_tags_func)
|
48
59
|
|
49
60
|
query = t
|
@@ -59,12 +70,12 @@ module PushType
|
|
59
70
|
# Dynamically define class method .all_`field_name`
|
60
71
|
# Returns Array
|
61
72
|
#
|
62
|
-
define_singleton_method "all_#{
|
73
|
+
define_singleton_method "all_#{ field_name }".to_sym do |*args, &block|
|
63
74
|
composed_scope = block.respond_to?(:call) ? block.call : all
|
64
75
|
composed_scope.projections = []
|
65
76
|
|
66
77
|
query = composed_scope
|
67
|
-
.project(Arel.sql "jsonb_array_elements_text(field_store->'#{
|
78
|
+
.project(Arel.sql "jsonb_array_elements_text(field_store->'#{ field_name }') t")
|
68
79
|
.distinct
|
69
80
|
.order(Arel.sql 't')
|
70
81
|
connection.select_all(query.to_sql).rows.flatten
|
@@ -1,9 +1,7 @@
|
|
1
1
|
module PushType
|
2
|
-
class TaxonomyField <
|
2
|
+
class TaxonomyField < RelationField
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
options template: 'relation', field_options: {}
|
4
|
+
options template: 'relation'
|
7
5
|
|
8
6
|
def relation_class
|
9
7
|
super
|
@@ -11,11 +9,11 @@ module PushType
|
|
11
9
|
PushType::Taxonomy
|
12
10
|
end
|
13
11
|
|
14
|
-
|
12
|
+
on_instance do |object, field|
|
15
13
|
object.class_eval do
|
16
|
-
define_method
|
17
|
-
field.relation_class.find
|
18
|
-
end
|
14
|
+
define_method(field.relation_name.to_sym) do
|
15
|
+
field.relation_class.find field.json_value unless field.json_value.blank?
|
16
|
+
end unless method_defined?(field.relation_name.to_sym)
|
19
17
|
end
|
20
18
|
end
|
21
19
|
|
@@ -1,10 +1,11 @@
|
|
1
1
|
module PushType
|
2
2
|
class TimeField < PushType::FieldType
|
3
3
|
|
4
|
-
options template:
|
4
|
+
options template: 'date',
|
5
|
+
form_helper: :time_field
|
5
6
|
|
6
|
-
def
|
7
|
-
|
7
|
+
def value
|
8
|
+
json_value.to_time unless json_value.blank?
|
8
9
|
end
|
9
10
|
|
10
11
|
end
|
@@ -2,12 +2,30 @@ module PushType
|
|
2
2
|
module Customizable
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
+
included do
|
6
|
+
after_initialize :initialize_fields
|
7
|
+
end
|
8
|
+
|
5
9
|
def fields
|
6
|
-
|
10
|
+
@fields ||= {}
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def initialize_fields
|
16
|
+
self.class.fields.keys.each do |key|
|
17
|
+
f = initialized_field(key)
|
18
|
+
if block = f.class.init_block
|
19
|
+
block.call(self, f)
|
20
|
+
end
|
21
|
+
end
|
7
22
|
end
|
8
23
|
|
9
|
-
def
|
10
|
-
fields
|
24
|
+
def initialized_field(key)
|
25
|
+
fields[key] ||= begin
|
26
|
+
klass, opts, blk = self.class.fields[key]
|
27
|
+
klass.new(key, self, opts, &blk)
|
28
|
+
end
|
11
29
|
end
|
12
30
|
|
13
31
|
module ClassMethods
|
@@ -15,10 +33,10 @@ module PushType
|
|
15
33
|
attr_reader :fields
|
16
34
|
|
17
35
|
def fields
|
18
|
-
@fields ||=
|
36
|
+
@fields ||= {}
|
19
37
|
end
|
20
38
|
|
21
|
-
def field(name, *args)
|
39
|
+
def field(name, *args, &blk)
|
22
40
|
raise ArgumentError if args.size > 2
|
23
41
|
kind, opts = case args.size
|
24
42
|
when 2 then args
|
@@ -26,21 +44,32 @@ module PushType
|
|
26
44
|
else [ :string, {} ]
|
27
45
|
end
|
28
46
|
|
29
|
-
|
30
|
-
|
47
|
+
# Store field type and set accessor
|
48
|
+
fields[name] = [ field_type(kind), opts, blk ].compact
|
49
|
+
store_accessor :field_store, name
|
31
50
|
|
32
|
-
|
51
|
+
# Set inline validates
|
52
|
+
validates name, opts[:validates] if opts[:validates]
|
33
53
|
|
34
|
-
|
54
|
+
# Override setter accessor
|
55
|
+
define_method "#{ name }=".to_sym do |val|
|
56
|
+
f = initialized_field(name)
|
57
|
+
super f.primitive.to_json(val)
|
58
|
+
end
|
59
|
+
|
60
|
+
# Override getter accessor
|
61
|
+
define_method name do
|
62
|
+
self.fields[name].value
|
63
|
+
end
|
35
64
|
|
36
|
-
if block =
|
37
|
-
block.call(self,
|
65
|
+
if block = fields[name][0].def_block
|
66
|
+
block.call(self, name, fields[name][0])
|
38
67
|
end
|
39
68
|
end
|
40
69
|
|
41
70
|
protected
|
42
71
|
|
43
|
-
def
|
72
|
+
def field_type(kind)
|
44
73
|
begin
|
45
74
|
"push_type/#{ kind }_field".camelize.constantize
|
46
75
|
rescue NameError
|
@@ -48,15 +77,6 @@ module PushType
|
|
48
77
|
end
|
49
78
|
end
|
50
79
|
|
51
|
-
def override_accessor(f)
|
52
|
-
define_method "#{ f.json_key }=".to_sym do |val|
|
53
|
-
super f.to_json(val)
|
54
|
-
end
|
55
|
-
define_method f.json_key do
|
56
|
-
f.from_json super()
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
80
|
end
|
61
81
|
end
|
62
82
|
end
|