infopark_fiona7 1.2.0.0.1 → 1.2.0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/scrivito_patches/models/obj.js +2 -6
  3. data/infopark_fiona7.gemspec +1 -1
  4. data/lib/fiona7/attribute_readers/attribute_reader.rb +17 -0
  5. data/lib/fiona7/attribute_readers/binary_as_binary.rb +12 -0
  6. data/lib/fiona7/attribute_readers/binary_as_linklist.rb +26 -0
  7. data/lib/fiona7/attribute_readers/date_as_date.rb +18 -0
  8. data/lib/fiona7/attribute_readers/factory.rb +91 -0
  9. data/lib/fiona7/attribute_readers/helpers/html_deserializer.rb +21 -0
  10. data/lib/fiona7/attribute_readers/helpers/json_deserializer.rb +11 -0
  11. data/lib/fiona7/attribute_readers/helpers/link_deserializer.rb +32 -0
  12. data/lib/fiona7/attribute_readers/html_as_html.rb +14 -0
  13. data/lib/fiona7/attribute_readers/link_as_linklist.rb +13 -0
  14. data/lib/fiona7/attribute_readers/linklist_as_linklist.rb +15 -0
  15. data/lib/fiona7/attribute_readers/multienum_as_multienum.rb +12 -0
  16. data/lib/fiona7/attribute_readers/multienum_as_text.rb +14 -0
  17. data/lib/fiona7/attribute_readers/number_as_string.rb +11 -0
  18. data/lib/fiona7/attribute_readers/reference_as_linklist.rb +14 -0
  19. data/lib/fiona7/attribute_readers/reference_as_string.rb +11 -0
  20. data/lib/fiona7/attribute_readers/referencelist_as_linklist.rb +16 -0
  21. data/lib/fiona7/attribute_readers/referencelist_as_text.rb +14 -0
  22. data/lib/fiona7/attribute_readers/simple.rb +18 -0
  23. data/lib/fiona7/attribute_readers/stringlist_as_text.rb +18 -0
  24. data/lib/fiona7/attribute_readers/widgetlist_as_linklist.rb +33 -0
  25. data/lib/fiona7/attribute_type_mapper.rb +76 -0
  26. data/lib/fiona7/attribute_writers/attribute_writer.rb +16 -0
  27. data/lib/fiona7/attribute_writers/binary_as_binary.rb +74 -0
  28. data/lib/fiona7/attribute_writers/binary_as_linklist.rb +86 -0
  29. data/lib/fiona7/attribute_writers/date_as_date.rb +11 -0
  30. data/lib/fiona7/attribute_writers/factory.rb +90 -0
  31. data/lib/fiona7/attribute_writers/helpers/html_serializer.rb +21 -0
  32. data/lib/fiona7/attribute_writers/helpers/json_serializer.rb +11 -0
  33. data/lib/fiona7/attribute_writers/helpers/link_serializer.rb +37 -0
  34. data/lib/fiona7/attribute_writers/html_as_html.rb +12 -0
  35. data/lib/fiona7/attribute_writers/link_as_linklist.rb +18 -0
  36. data/lib/fiona7/attribute_writers/linklist_as_linklist.rb +16 -0
  37. data/lib/fiona7/attribute_writers/multienum_as_multienum.rb +11 -0
  38. data/lib/fiona7/attribute_writers/multienum_as_text.rb +12 -0
  39. data/lib/fiona7/attribute_writers/number_as_string.rb +11 -0
  40. data/lib/fiona7/attribute_writers/reference_as_linklist.rb +17 -0
  41. data/lib/fiona7/attribute_writers/reference_as_string.rb +11 -0
  42. data/lib/fiona7/attribute_writers/referencelist_as_linklist.rb +19 -0
  43. data/lib/fiona7/attribute_writers/referencelist_as_text.rb +13 -0
  44. data/lib/fiona7/attribute_writers/simple.rb +11 -0
  45. data/lib/fiona7/attribute_writers/stringlist_as_text.rb +16 -0
  46. data/lib/fiona7/attribute_writers/widgetlist_as_linklist.rb +33 -0
  47. data/lib/fiona7/builder/obj_builder.rb +12 -201
  48. data/lib/fiona7/controllers/rest_api/obj_controller.rb +23 -5
  49. data/lib/fiona7/engine.rb +39 -0
  50. data/lib/fiona7/json/obj_decorator.rb +29 -121
  51. data/lib/fiona7/prefetch/obj_prefetch.rb +42 -0
  52. data/lib/fiona7/prefetch/widget_resolver_prefetch.rb +36 -0
  53. data/lib/fiona7/scrivito_patches/date_attribute.rb +16 -0
  54. data/lib/fiona7/tools/attribute_remover.rb +70 -0
  55. data/lib/fiona7/type_register.rb +9 -1
  56. data/lib/fiona7/version.rb +1 -1
  57. data/lib/fiona7/widget_resolver.rb +6 -6
  58. metadata +51 -4
@@ -0,0 +1,18 @@
1
+ require 'fiona7/attribute_readers/attribute_reader'
2
+
3
+ module Fiona7
4
+ module AttributeReaders
5
+ class Simple < AttributeReader
6
+ def call
7
+ value = self.obj[self.attr_name]
8
+
9
+ if self.attr_name.to_s == "suppress_export"
10
+ value.to_s
11
+ else
12
+ value
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+
@@ -0,0 +1,18 @@
1
+ require 'fiona7/attribute_readers/attribute_reader'
2
+ require 'fiona7/attribute_readers/helpers/json_deserializer'
3
+
4
+ module Fiona7
5
+ module AttributeReaders
6
+ class StringlistAsText < AttributeReader
7
+ def call
8
+ if self.attr_name.to_s == "channels"
9
+ self.obj["channels"] || []
10
+ else
11
+ value = self.obj[self.attr_name]
12
+ Helpers::JsonDeserializer.new.call(value).presence || []
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+
@@ -0,0 +1,33 @@
1
+ require 'fiona7/attribute_readers/attribute_reader'
2
+
3
+ module Fiona7
4
+ module AttributeReaders
5
+ class WidgetlistAsLinklist < AttributeReader
6
+ def call
7
+ linklist = self.obj.attr_values[self.attr_name] || []
8
+ if !linklist.empty?
9
+ widget_ids = []
10
+ widgets = linklist.map do |link|
11
+ widget_id = link["title"]
12
+ # sometimes a released page references an unreleased widget
13
+ # it is then impossible to load it into widget_pool
14
+ # but the reference in the widgetlist field is still there
15
+ # therefore we need to check if the widget has been loaded
16
+ # into the pool - only then it can be included
17
+ if valid_widget_id?(widget_id)
18
+ widget_ids << widget_id
19
+ end
20
+ end
21
+
22
+ widget_ids
23
+ end
24
+ end
25
+
26
+ protected
27
+ def valid_widget_id?(widget_id)
28
+ self.widget_resolver && self.widget_resolver.path_map[widget_id]
29
+ end
30
+ end
31
+ end
32
+ end
33
+
@@ -0,0 +1,76 @@
1
+ module Fiona7
2
+ # This class is used to map from virtual attribute types
3
+ # to real attribute types.
4
+ # For example number to string or referencelist to linklist
5
+ class AttributeTypeMapper
6
+ def initialize(obj_class, source=Fiona7.custom_attribute_types)
7
+ self.custom = CustomAttributeTypeMapper.new(obj_class, source)
8
+ self.built_in = BuiltInTypeMapper.new(obj_class)
9
+ end
10
+
11
+ def call(attribute, virtual_type)
12
+ self.validate!(attribute,
13
+ self.custom.call(attribute, virtual_type) ||
14
+ self.built_in.call(attribute, virtual_type)
15
+ )
16
+ end
17
+
18
+ protected
19
+ attr_accessor :custom, :built_in
20
+
21
+ def validate!(attribute, mapped_type)
22
+ [:string, :text, :enum, :multienum,
23
+ :linklist, :html, :markdown, :date].include?(mapped_type) ||
24
+ raise(Fiona7::TypeSystemError.new("Unhandled mapping for attribute #{attribute} in #{obj_class}: #{mapped_type}"))
25
+
26
+ return mapped_type
27
+ end
28
+
29
+ class CustomAttributeTypeMapper
30
+ def initialize(obj_class, source)
31
+ self.obj_class = obj_class
32
+ self.source = source
33
+ end
34
+
35
+ def call(attribute, virtual_type)
36
+ return nil if source.kind_of?(Hash) && source.empty?
37
+
38
+ if source.kind_of? Hash
39
+ (source[self.obj_class] || {})[attribute] || source[attribute]
40
+ elsif source.respond_to? :call
41
+ source.call(self.obj_class, attribute)
42
+ else
43
+ nil
44
+ end
45
+ end
46
+
47
+ protected
48
+ attr_accessor :obj_class, :source
49
+ end
50
+
51
+ class BuiltInTypeMapper
52
+ BUILT_IN_TYPE_MAP = {
53
+ linklist: :linklist,
54
+ string: :string,
55
+ date: :date,
56
+ html: :html,
57
+ enum: :enum,
58
+ multienum: :multienum,
59
+ markdown: :markdown,
60
+ signature: :signature
61
+ }.freeze
62
+
63
+ def initialize(obj_class)
64
+ self.obj_class = obj_class
65
+ end
66
+
67
+ def call(attribute, virtual_type)
68
+ Fiona7::TypeRegister::TYPE_MAP[virtual_type] ||
69
+ BUILT_IN_TYPE_MAP[virtual_type]
70
+ end
71
+
72
+ protected
73
+ attr_accessor :obj_class
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,16 @@
1
+ module Fiona7
2
+ module AttributeWriters
3
+ class AttributeWriter
4
+ def initialize(obj, attr_name, obj_class, klass, widget_map)
5
+ self.obj = obj
6
+ self.attr_name = attr_name
7
+ self.obj_class = obj_class
8
+ self.klass = klass
9
+ self.widget_map = widget_map
10
+ end
11
+
12
+ protected
13
+ attr_accessor :obj, :attr_name, :obj_class, :klass, :widget_map
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,74 @@
1
+ require 'fiona7/attribute_writers/attribute_writer'
2
+
3
+
4
+ module Fiona7
5
+ module AttributeWriters
6
+ class BinaryAsBinary < AttributeWriter
7
+ def call(value, claimed_type=nil)
8
+ if value.kind_of?(File)
9
+ self.special_upload_handling(self.attr_name, value)
10
+ elsif value.kind_of?(ActionDispatch::Http::UploadedFile)
11
+ self.special_upload_uploaded_handling(self.attr_name, value)
12
+ elsif value.nil?
13
+ self.obj.set(self.attr_name, '')
14
+ elsif value.kind_of?(Hash)
15
+ source_blob_id = value["id"] || value["id_to_copy"]
16
+ filename = value["filename"]
17
+ content_type = value["content_type"]
18
+
19
+ # There is a little bit of magic behind this.
20
+ # This will pass some data directly to the crul_obj
21
+ # bypassing self.obj.set(), but calling self.obj.save
22
+ # afterwards still persists the data correctly
23
+ #
24
+ # This works the same way as Reactor::Tools::Uploader
25
+ Fiona7::Builder::LazyBlobCopier.new({
26
+ destination_obj: self.obj,
27
+ attr_name: self.attr_name,
28
+ source_blob_id: source_blob_id,
29
+ filename: filename,
30
+ content_type: content_type
31
+ }).call
32
+
33
+ # NOTE: no self.obj.set() required here
34
+ else
35
+ raise Scrivito::ClientError.new("Invalid input for binary field", 422)
36
+ end
37
+ end
38
+
39
+ protected
40
+
41
+ def special_upload_handling(attribute_name, file)
42
+ if !self.obj.binary?
43
+ # standard handling!
44
+ self.obj.set(attribute_name.to_s, self.upload_file(file))
45
+ else
46
+ ext = ::File.extname(file.path).to_s[1..-1]
47
+ self.obj.upload(file, ext)
48
+ end
49
+ end
50
+
51
+ def special_upload_uploaded_handling(attribute_name, file)
52
+ if !self.obj.binary?
53
+ # standard handling!
54
+ self.obj.set(attribute_name.to_s, self.upload_uploaded_file(file))
55
+ else
56
+ ext = ::File.extname(file.original_filename).to_s[1..-1]
57
+ self.obj.upload(file.open, ext)
58
+ end
59
+ end
60
+
61
+ def upload_file(file)
62
+ upload = Fiona7::Builder::IndirectBlobBuilder.new(self.obj, ::File.basename(file.path), file).call
63
+ encoded_id = Fiona7::BlobIdGenerator.new(upload.id, upload.last_changed).call
64
+ {title: encoded_id, destination_object: upload}
65
+ end
66
+
67
+ def upload_uploaded_file(file)
68
+ upload = Fiona7::Builder::IndirectBlobBuilder.new(self.obj, file.original_filename, file.open).call
69
+ encoded_id = Fiona7::BlobIdGenerator.new(upload.id, upload.last_changed).call
70
+ {title: encoded_id, destination_object: upload}
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,86 @@
1
+ require 'fiona7/attribute_writers/attribute_writer'
2
+
3
+
4
+ module Fiona7
5
+ module AttributeWriters
6
+ class BinaryAsLinklist < AttributeWriter
7
+ def call(value, claimed_type=nil)
8
+ if value.kind_of?(String)
9
+ # NOTE: this code path has not been tested yet.
10
+ target = {title: value, destination_object: InternalReleasedObj.find(value.to_i)}
11
+ self.obj.set(self.attr_name, target)
12
+ elsif value.kind_of?(File)
13
+ if !Fiona7.mode == :legacy || self.attr_name != "blob"
14
+ self.obj.set(self.attr_name, self.upload_file(value))
15
+ else
16
+ self.special_upload_handling(self.attr_name, value)
17
+ end
18
+ elsif value.kind_of?(ActionDispatch::Http::UploadedFile)
19
+ if !Fiona7.mode == :legacy || self.attr_name != "blob"
20
+ self.obj.set(self.attr_name, self.upload_uploaded_file(value))
21
+ else
22
+ self.special_upload_uploaded_handling(self.attr_name, value)
23
+ end
24
+ elsif value.nil?
25
+ self.obj.set(self.attr_name, [])
26
+ elsif value.kind_of?(Hash)
27
+ source_blob_id = value["id"] || value["id_to_copy"]
28
+ filename = value["filename"]
29
+ content_type = value["content_type"]
30
+
31
+ # There is a little bit of magic behind this.
32
+ # This will pass some data directly to the crul_obj
33
+ # bypassing self.obj.set(), but calling self.obj.save
34
+ # afterwards still persists the data correctly
35
+ #
36
+ # This works the same way as Reactor::Tools::Uploader
37
+ Fiona7::Builder::LazyBlobCopier.new({
38
+ destination_obj: self.obj,
39
+ attr_name: self.attr_name,
40
+ source_blob_id: source_blob_id,
41
+ filename: filename,
42
+ content_type: content_type
43
+ }).call
44
+
45
+ # NOTE: no self.obj.set() required here
46
+ else
47
+ raise Scrivito::ClientError.new("Invalid input for binary field", 422)
48
+ end
49
+ end
50
+
51
+ protected
52
+
53
+ def special_upload_handling(attribute_name, file)
54
+ if !self.obj.binary?
55
+ # standard handling!
56
+ self.obj.set(attribute_name.to_s, self.upload_file(file))
57
+ else
58
+ ext = ::File.extname(file.path).to_s[1..-1]
59
+ self.obj.upload(file, ext)
60
+ end
61
+ end
62
+
63
+ def special_upload_uploaded_handling(attribute_name, file)
64
+ if !self.obj.binary?
65
+ # standard handling!
66
+ self.obj.set(attribute_name.to_s, self.upload_uploaded_file(file))
67
+ else
68
+ ext = ::File.extname(file.original_filename).to_s[1..-1]
69
+ self.obj.upload(file.open, ext)
70
+ end
71
+ end
72
+
73
+ def upload_file(file)
74
+ upload = Fiona7::Builder::IndirectBlobBuilder.new(self.obj, ::File.basename(file.path), file).call
75
+ encoded_id = Fiona7::BlobIdGenerator.new(upload.id, upload.last_changed).call
76
+ {title: encoded_id, destination_object: upload}
77
+ end
78
+
79
+ def upload_uploaded_file(file)
80
+ upload = Fiona7::Builder::IndirectBlobBuilder.new(self.obj, file.original_filename, file.open).call
81
+ encoded_id = Fiona7::BlobIdGenerator.new(upload.id, upload.last_changed).call
82
+ {title: encoded_id, destination_object: upload}
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,11 @@
1
+ require 'fiona7/attribute_writers/attribute_writer'
2
+
3
+ module Fiona7
4
+ module AttributeWriters
5
+ class DateAsDate < AttributeWriter
6
+ def call(value, claimed_type=nil)
7
+ self.obj.set(self.attr_name, value)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,90 @@
1
+ require 'fiona7/attribute_writers/binary_as_binary'
2
+ require 'fiona7/attribute_writers/binary_as_linklist'
3
+ require 'fiona7/attribute_writers/html_as_html'
4
+ require 'fiona7/attribute_writers/link_as_linklist'
5
+ require 'fiona7/attribute_writers/linklist_as_linklist'
6
+ require 'fiona7/attribute_writers/multienum_as_multienum'
7
+ require 'fiona7/attribute_writers/multienum_as_text'
8
+ require 'fiona7/attribute_writers/number_as_string'
9
+ require 'fiona7/attribute_writers/reference_as_linklist'
10
+ require 'fiona7/attribute_writers/reference_as_string'
11
+ require 'fiona7/attribute_writers/referencelist_as_linklist'
12
+ require 'fiona7/attribute_writers/referencelist_as_text'
13
+ require 'fiona7/attribute_writers/simple'
14
+ require 'fiona7/attribute_writers/date_as_date'
15
+ require 'fiona7/attribute_writers/stringlist_as_text'
16
+ require 'fiona7/attribute_writers/widgetlist_as_linklist'
17
+
18
+
19
+ module Fiona7
20
+ module AttributeWriters
21
+ class Factory
22
+ def initialize(obj, obj_class, klass, widget_map)
23
+ self.obj = obj
24
+ self.obj_class = obj_class
25
+ self.klass = klass
26
+ self.widget_map = widget_map
27
+
28
+ self.type_def = Fiona7::TypeRegister.instance.write(self.obj_class) || raise(Fiona7::TypeSystemError.new("Obj class definition not found for #{self.obj_class}"))
29
+ end
30
+
31
+ def call(attribute)
32
+ attr_def = self.type_def.find_attribute(attribute) ||
33
+ raise(Fiona7::TypeSystemError.new("Attribute #{attribute} not found in #{self.obj_class}"))
34
+
35
+ virtual = attr_def.type.to_sym
36
+ real = attr_def.real_type.to_sym
37
+
38
+ worker = case [virtual, real]
39
+ when [:linklist, :linklist]
40
+ Fiona7::AttributeWriters::LinklistAsLinklist
41
+ when [:link, :linklist]
42
+ Fiona7::AttributeWriters::LinkAsLinklist
43
+ when [:reference, :linklist]
44
+ Fiona7::AttributeWriters::ReferenceAsLinklist
45
+ when [:reference, :string], [:reference, :text]
46
+ Fiona7::AttributeWriters::ReferenceAsString
47
+ when [:referencelist, :linklist]
48
+ Fiona7::AttributeWriters::ReferencelistAsLinklist
49
+ when [:referencelist, :text], [:referencelist, :string]
50
+ Fiona7::AttributeWriters::ReferencelistAsText
51
+ when [:widgetlist, :linklist]
52
+ Fiona7::AttributeWriters::WidgetlistAsLinklist
53
+ when [:multienum, :text], [:multienum, :string]
54
+ Fiona7::AttributeWriters::MultienumAsText
55
+ when [:multienum, :multienum]
56
+ Fiona7::AttributeWriters::MultienumAsMultienum
57
+ when [:number, :string], [:number, :text]
58
+ Fiona7::AttributeWriters::NumberAsString
59
+ when [:html, :html]
60
+ Fiona7::AttributeWriters::HtmlAsHtml
61
+ when [:binary, :linklist]
62
+ Fiona7::AttributeWriters::BinaryAsLinklist
63
+ when [:binary, :binary]
64
+ Fiona7::AttributeWriters::BinaryAsBinary
65
+ when [:stringlist, :text], [:stringlist, :string]
66
+ Fiona7::AttributeWriters::StringlistAsText
67
+ when [:stringlist, :multienum]
68
+ Fiona7::AttributeWriters::Simple
69
+ when [:stringlist, :stringlist]
70
+ # NOTE: this is practically only used for "channels"
71
+ Fiona7::AttributeWriters::Simple
72
+ else
73
+ case virtual
74
+ when :text, :string, :enum, :markdown
75
+ Fiona7::AttributeWriters::Simple
76
+ when :date
77
+ Fiona7::AttributeWriters::DateAsDate
78
+ else
79
+ raise(Fiona7::TypeSystemError.new("Unable to write #{attribute} in #{self.obj_class} with typing #{virtual}:#{real}"))
80
+ end
81
+ end
82
+
83
+ worker.new(self.obj, attr_def.real_name.to_s, self.obj_class, self.klass, self.widget_map)
84
+ end
85
+
86
+ protected
87
+ attr_accessor :obj, :obj_class, :klass, :widget_map, :type_def
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,21 @@
1
+ require 'fiona7/link_converter/scrivito_to_fiona'
2
+
3
+ module Fiona7
4
+ module AttributeWriters
5
+ module Helpers
6
+ class HtmlSerializer
7
+ def initialize(obj, klass)
8
+ self.obj = obj
9
+ self.klass = klass
10
+ end
11
+
12
+ def call(value)
13
+ converted_links = LinkConverter::ScrivitoToFiona.new(self.klass, value.to_s).convert
14
+ end
15
+
16
+ protected
17
+ attr_accessor :obj, :klass
18
+ end
19
+ end
20
+ end
21
+ end