binda 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +7 -0
  3. data/app/assets/javascripts/binda/components/fileupload.js +19 -0
  4. data/app/assets/javascripts/binda/dist/binda.bundle.js +161 -139
  5. data/app/controllers/binda/svgs_controller.rb +71 -0
  6. data/app/controllers/concerns/binda/fieldable_helpers.rb +48 -20
  7. data/app/models/binda/binda.rb +7 -0
  8. data/app/models/binda/field_setting.rb +8 -6
  9. data/app/models/binda/image.rb +2 -2
  10. data/app/models/binda/svg.rb +7 -0
  11. data/app/models/concerns/binda/fieldable_association_helpers.rb +10 -454
  12. data/app/models/concerns/binda/fieldable_association_helpers/fieldable_audio_helpers.rb +55 -0
  13. data/app/models/concerns/binda/fieldable_association_helpers/fieldable_date_helpers.rb +30 -0
  14. data/app/models/concerns/binda/fieldable_association_helpers/fieldable_image_helpers.rb +95 -0
  15. data/app/models/concerns/binda/fieldable_association_helpers/fieldable_relation_helpers.rb +68 -0
  16. data/app/models/concerns/binda/fieldable_association_helpers/fieldable_repeater_helpers.rb +32 -0
  17. data/app/models/concerns/binda/fieldable_association_helpers/fieldable_selection_helpers.rb +60 -0
  18. data/app/models/concerns/binda/fieldable_association_helpers/fieldable_string_helpers.rb +56 -0
  19. data/app/models/concerns/binda/fieldable_association_helpers/fieldable_svg_helpers.rb +74 -0
  20. data/app/models/concerns/binda/fieldable_association_helpers/fieldable_text_helpers.rb +59 -0
  21. data/app/models/concerns/binda/fieldable_association_helpers/fieldable_video_helpers.rb +56 -0
  22. data/app/models/concerns/binda/fieldable_associations.rb +4 -2
  23. data/app/uploaders/binda/svg_uploader.rb +62 -0
  24. data/app/views/binda/fieldable/_form_item_svg.html.erb +33 -0
  25. data/app/views/binda/fieldable/_form_item_video.html.erb +9 -8
  26. data/app/views/binda/fieldable/_form_section.html.erb +1 -1
  27. data/app/views/binda/structures/form_section/_form_section_header.html.erb +1 -1
  28. data/config/initializers/simple_form__fileupload.rb +14 -9
  29. data/config/initializers/simple_form_custom.rb +1 -1
  30. data/config/locales/en.yml +1 -0
  31. data/config/routes.rb +5 -0
  32. data/db/migrate/1_create_binda_tables.rb +1 -0
  33. data/lib/binda/version.rb +1 -1
  34. data/lib/generators/binda/setup/setup_generator.rb +5 -1
  35. data/lib/tasks/update_image_details_task.rake +1 -1
  36. metadata +17 -2
@@ -0,0 +1,55 @@
1
+ module Binda
2
+ module FieldableAssociationHelpers
3
+ module FieldableAudioHelpers
4
+ # Check if the field has an attached audio
5
+ #
6
+ # @param field_slug [string] The slug of the field setting
7
+ # @return [boolean]
8
+ def has_audio(field_slug)
9
+ obj = self.audios.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
10
+ # Alternative query
11
+ # obj = Image.where(field_setting_id: FieldSetting.get_id( field_slug ), fieldable_id: self.id, fieldable_type: self.class.to_s ).first
12
+ raise ArgumentError, "There isn't any audio associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
13
+ return obj.audio.present?
14
+ end
15
+
16
+ # Get the audio url based on the size provided,
17
+ # default is Carrierwave default (usually the real size)
18
+ #
19
+ # @param field_slug [string] The slug of the field setting
20
+ # @param size [string] The size. It can be 'thumb' 200x200 cropped,
21
+ # 'medium' 700x700 max size, 'large' 1400x1400 max size, or blank
22
+ # @return [string] The url of the audio
23
+ def get_audio_url(field_slug)
24
+ get_audio_info( field_slug, 'url' )
25
+ end
26
+
27
+ # Get the audio path based on the size provided,
28
+ # default is Carrierwave default (usually the real size)
29
+ #
30
+ # @param field_slug [string] The slug of the field setting
31
+ # @param size [string] The size. It can be 'thumb' 200x200 cropped,
32
+ # 'medium' 700x700 max size, 'large' 1400x1400 max size, or blank
33
+ # @return [string] The url of the audio
34
+ def get_audio_path(field_slug)
35
+ get_audio_info( field_slug, 'path' )
36
+ end
37
+
38
+ # Get the object related to that field setting
39
+ #
40
+ # @param field_slug [string] The slug of the field setting
41
+ # @param info [string] String of the info to be retrieved
42
+ # @return [string] The info requested if present
43
+ # @return [boolean] Returns false if no info is found or if image isn't found
44
+ def get_audio_info(field_slug, info)
45
+ obj = self.audios.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
46
+ # Alternative query
47
+ # obj = audio.where(field_setting_id: FieldSetting.get_id( field_slug ), fieldable_id: self.id, fieldable_type: self.class.to_s ).first
48
+ raise ArgumentError, "There isn't any audio associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
49
+ if obj.audio.present?
50
+ obj.audio.send(info)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,30 @@
1
+ module Binda
2
+ module FieldableAssociationHelpers
3
+ module FieldableDateHelpers
4
+ # Check if the field has an attached date
5
+ #
6
+ # @param field_slug [string] The slug of the field setting
7
+ # @return [datetime] The date
8
+ # @return [boolean] Reutrn false if nothing is found
9
+ def has_date(field_slug)
10
+ obj = self.dates.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
11
+ raise ArgumentError, "There isn't any date associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
12
+ if obj.present?
13
+ return !obj.date.nil?
14
+ else
15
+ return false
16
+ end
17
+ end
18
+
19
+ # Get the object related to that field setting
20
+ #
21
+ # @param field_slug [string] The slug of the field setting
22
+ # @return [boolean]
23
+ def get_date(field_slug)
24
+ obj = self.dates.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
25
+ raise ArgumentError, "There isn't any date associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
26
+ obj.date
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,95 @@
1
+ module Binda
2
+ module FieldableAssociationHelpers
3
+ module FieldableImageHelpers
4
+
5
+ # Check if the field has an attached image
6
+ #
7
+ # @param field_slug [string] The slug of the field setting
8
+ # @return [boolean]
9
+ def has_image(field_slug)
10
+ obj = self.images.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
11
+ # Alternative query
12
+ # obj = Image.where(field_setting_id: FieldSetting.get_id( field_slug ), fieldable_id: self.id, fieldable_type: self.class.to_s ).first
13
+ raise ArgumentError, "There isn't any image associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
14
+ return obj.image.present?
15
+ end
16
+
17
+ # Get the image url based on the size provided,
18
+ # default is Carrierwave default (usually the real size)
19
+ #
20
+ # @param field_slug [string] The slug of the field setting
21
+ # @param size [string] The size. It can be 'thumb' 200x200 cropped,
22
+ # 'medium' 700x700 max size, 'large' 1400x1400 max size, or blank
23
+ # @return [string] The url of the image
24
+ def get_image_url(field_slug, size = '')
25
+ get_image_info( field_slug, size, 'url' )
26
+ end
27
+
28
+ # Get the image path based on the size provided,
29
+ # default is Carrierwave default (usually the real size)
30
+ #
31
+ # @param field_slug [string] The slug of the field setting
32
+ # @param size [string] The size. It can be 'thumb' 200x200 cropped,
33
+ # 'medium' 700x700 max size, 'large' 1400x1400 max size, or blank
34
+ # @return [string] The url of the image
35
+ def get_image_path(field_slug, size = '')
36
+ get_image_info( field_slug, size, 'path' )
37
+ end
38
+
39
+ # Get the object related to that field setting
40
+ #
41
+ # @param field_slug [string] The slug of the field setting
42
+ # @param size [string] The size. It can be 'thumb' 200x200 cropped,
43
+ # 'medium' 700x700 max size, 'large' 1400x1400 max size, or blank
44
+ # @param info [string] String of the info to be retrieved
45
+ # @return [string] The info requested if present
46
+ # @return [boolean] Returns false if no info is found or if image isn't found
47
+ def get_image_info(field_slug, size, info)
48
+ obj = self.images.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
49
+ # Alternative query
50
+ # obj = Image.where(field_setting_id: FieldSetting.get_id( field_slug ), fieldable_id: self.id, fieldable_type: self.class.to_s ).first
51
+ raise ArgumentError, "There isn't any image associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
52
+ if obj.image.present? && obj.image.respond_to?(size) && %w[thumb].include?(size)
53
+ obj.image.send(size).send(info)
54
+ elsif obj.image.present?
55
+ obj.image.send(info)
56
+ else
57
+ raise "Looks like the image you are looking for isn't present. See field setting with slug=\"#{field_slug}\" on component with id=\"self.id\""
58
+ end
59
+ end
60
+
61
+ # Get image size
62
+ #
63
+ # @param field_slug [string] The slug of the field setting
64
+ # @return [float] with image type
65
+ def get_image_size(field_slug)
66
+ obj = self.images.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
67
+ return bytes_to_megabytes(obj.file_size)
68
+ end
69
+
70
+ # Get image type
71
+ #
72
+ # @param field_slug [string] The slug of the field setting
73
+ # @return [string] with image type
74
+ def get_image_mime_type(field_slug)
75
+ obj = self.images.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
76
+ return obj.content_type
77
+ end
78
+
79
+ # Get image dimension
80
+ #
81
+ # @param field_slug [string] The slug of the field setting
82
+ # @return [hash] with width and height as floats
83
+ def get_image_dimension(field_slug)
84
+ obj = self.images.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
85
+ return { width: obj.file_width, height: obj.file_height }
86
+ end
87
+
88
+ # Convert bytes to megabites
89
+ def bytes_to_megabytes bytes
90
+ (bytes.to_f / 1.megabyte).round(2)
91
+ end
92
+
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,68 @@
1
+ module Binda
2
+ module FieldableAssociationHelpers
3
+ module FieldableRelationHelpers
4
+
5
+ # Check if has related components
6
+ #
7
+ # @param field_slug [string] The slug of the field setting
8
+ # @return [boolean]
9
+ def has_related_components(field_slug)
10
+ obj = self.relations.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
11
+ raise ArgumentError, "There isn't any related field associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
12
+ return obj.dependent_relations.any?
13
+ end
14
+
15
+ # Alias for has_related_components
16
+ def has_dependent_components(field_slug)
17
+ has_related_components(field_slug)
18
+ end
19
+
20
+ # Get related components
21
+ #
22
+ # @param field_slug [string] The slug of the field setting
23
+ # @return [array] An array of components
24
+ def get_related_components(field_slug)
25
+ obj = self.relations.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
26
+ raise ArgumentError, "There isn't any related field associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
27
+ return obj.dependent_relations.map{|relation| relation.dependent}
28
+ end
29
+
30
+ # Alias for get_related_components
31
+ def get_dependent_components(field_slug)
32
+ get_related_components(field_slug)
33
+ end
34
+
35
+ # Get all components which owns a relation where the current instance is a dependent
36
+ #
37
+ # @param field_slug [string] The slug of the field setting of the relation
38
+ # @return [array] An array of components and/or boards
39
+ def get_owner_components(field_slug)
40
+ # obj = self.owner_relations.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
41
+ obj = Relation.where(field_setting_id: B.get_field_settings(field_slug)).includes(dependent_relations: :dependent).where(binda_relation_links: {dependent_type: self.class.name})
42
+ raise ArgumentError, "There isn't any relation associated to the current slug (#{field_slug}) where the current instance (#{self.class.name} ##{self.id}) is a dependent.", caller if obj.nil?
43
+ return obj
44
+ end
45
+
46
+ # Check if has related boards
47
+ #
48
+ # @param field_slug [string] The slug of the field setting
49
+ # @return [boolean]
50
+ def has_related_boards(field_slug)
51
+ obj = self.relations.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
52
+ raise ArgumentError, "There isn't any related field associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
53
+ return obj.dependent_relations.any?
54
+ end
55
+
56
+ # Get related boards
57
+ #
58
+ # @param field_slug [string] The slug of the field setting
59
+ # @return [array] An array of boards
60
+ def get_related_boards(field_slug)
61
+ obj = self.relations.find{ |t| t.field_setting_idid == FieldSetting.get_id( field_slug ) }
62
+ raise ArgumentError, "There isn't any related field associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
63
+ return obj.dependent_relations.map{|relation| relation.dependent}
64
+ end
65
+
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,32 @@
1
+ module Binda
2
+ module FieldableAssociationHelpers
3
+ module FieldableRepeaterHelpers
4
+ # Check if exists any repeater with that slug
5
+ #
6
+ # @param field_slug [string] The slug of the field setting
7
+ # @return [boolean]
8
+ def has_repeaters(field_slug)
9
+ obj = self.repeaters.find_all{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
10
+ raise ArgumentError, "There isn't any repeater associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
11
+ return obj.present?
12
+ end
13
+ def has_repeater(field_slug)
14
+ has_repeaters(field_slug)
15
+ end
16
+
17
+ # Get the all repeater instances sorted by position
18
+ #
19
+ # @param field_slug [string] The slug of the field setting
20
+ # @return [array] An array of repeater items which have all sorts of fields attached
21
+ def get_repeaters(field_slug)
22
+ obj = self.repeaters.find_all{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
23
+ raise ArgumentError, "There isn't any repeater associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
24
+ obj.sort_by(&:position)
25
+ end
26
+ def get_repeater(field_slug)
27
+ get_repeaters(field_slug)
28
+ end
29
+
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,60 @@
1
+ module Binda
2
+ module FieldableAssociationHelpers
3
+ module FieldableSelectionHelpers
4
+ # Get the radio choice
5
+ #
6
+ # If by mistake the Radio instance has many choices associated,
7
+ # only the first one will be retrieved.
8
+ #
9
+ # @param field_slug [string] The slug of the field setting
10
+ # @return [hash] A hash of containing the label and value of the selected choice. `{ label: 'the label', value: 'the value'}`
11
+ def get_radio_choice(field_slug)
12
+ field_setting = FieldSetting.find_by(slug:field_slug)
13
+ obj = self.radios.find{ |t| t.field_setting_id == field_setting.id }
14
+ raise ArgumentError, "There isn't any radio associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
15
+ raise "There isn't any choice available for the current radio (#{field_slug}) on instance (#{self.class.name} ##{self.id})." unless obj.choices.any?
16
+ return { label: obj.choices.first.label, value: obj.choices.first.value }
17
+ end
18
+
19
+ # Get the select choices
20
+ #
21
+ # @param field_slug [string] The slug of the field setting
22
+ # @return [hash] A hash of containing the label and value of the selected choice. `{ label: 'the label', 'value': 'the value'}`
23
+ def get_selection_choice(field_slug)
24
+ field_setting = FieldSetting.find_by(slug:field_slug)
25
+ obj = self.selections.find{ |t| t.field_setting_id == field_setting.id }
26
+ raise ArgumentError, "There isn't any selection associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
27
+ raise "There isn't any choice available for the current selection (#{field_slug}) on instance (#{self.class.name} ##{self.id})." unless field_setting.choices.any?
28
+ return { label: obj.choices.first.label, value: obj.choices.first.value }
29
+ end
30
+
31
+ # Get the select choices
32
+ #
33
+ # @param field_slug [string] The slug of the field setting
34
+ # @return [array] An array of hashes of containing label and value of the selected choices. `{ label: 'the label', 'value': 'the value'}`
35
+ def get_selection_choices(field_slug)
36
+ field_setting = FieldSetting.find_by(slug:field_slug)
37
+ obj = self.selections.find{ |t| t.field_setting_id == field_setting.id }
38
+ raise ArgumentError, "There isn't any selection associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
39
+ raise "There isn't any choice available for the current selection (#{field_slug}) on instance (#{self.class.name} ##{self.id})." unless field_setting.choices.any?
40
+ return obj.choices.map{|choice| { label: choice.label, value: choice.value }}
41
+ end
42
+
43
+ # Get the checkbox choice
44
+ #
45
+ # @param field_slug [string] The slug of the field setting
46
+ # @return [array] An array of labels and values of the selected choices. `[{ label: '1st label', value: '1st-value'}, { label: '2nd label', value: '2nd-value'}]`
47
+ def get_checkbox_choices(field_slug)
48
+ field_setting = FieldSetting.find_by(slug:field_slug)
49
+ obj = self.checkboxes.find{ |t| t.field_setting_id == field_setting.id }
50
+ raise ArgumentError, "There isn't any checkbox associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
51
+ raise "There isn't any choice available for the current checkbox (#{field_slug}) on instance (#{self.class.name} ##{self.id})." unless field_setting.choices.any?
52
+ obj_array = []
53
+ obj.choices.order('label').each do |o|
54
+ obj_array << { label: o.label, value: o.value }
55
+ end
56
+ return obj_array
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,56 @@
1
+ module Binda
2
+ module FieldableAssociationHelpers
3
+ module FieldableStringHelpers
4
+
5
+ # Get the object related to that field setting
6
+ # If the object doesn't exists yet it will return nil
7
+ #
8
+ # @param field_slug [string] The slug of the field setting
9
+ # @return [string] Returns the content of the string
10
+ # @return [error] Raise an error if no record is found
11
+ def get_string(field_slug)
12
+ obj = Text
13
+ .includes(:field_setting)
14
+ .where(fieldable_id: self.id, fieldable_type: self.class.name)
15
+ .where(binda_field_settings: { slug: field_slug, field_type: "string" })
16
+ .first
17
+ unless obj.nil?
18
+ # to_s ensures the returned object is class String
19
+ obj.content.to_s
20
+ else
21
+ check_string_error field_slug
22
+ end
23
+ end
24
+
25
+ # Check why get_string doesn't return a value
26
+ # This method isn't supposed to be used by anything other than get_string method
27
+ def check_string_error(field_slug)
28
+ you_mean_text = !self.strings.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) && t.type == 'Binda::Text' }.nil?
29
+ if you_mean_text
30
+ raise ArgumentError, "This slug (#{field_slug}) is associated to a text not a string. Use get_text() instead on instance (#{self.class.name} ##{self.id}).", caller
31
+ else
32
+ raise ArgumentError, "There isn't any string associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller
33
+ end
34
+ end
35
+
36
+ # Get the object related to that field setting
37
+ #
38
+ # @param field_slug [string] The slug of the field setting
39
+ # @return [boolean]
40
+ def has_string(field_slug)
41
+ obj = Text
42
+ .includes(:field_setting)
43
+ .where(fieldable_id: self.id, fieldable_type: self.class.name)
44
+ .where(binda_field_settings: { slug: field_slug, field_type: "string" })
45
+ .first
46
+ raise ArgumentError, "There isn't any string associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
47
+ if obj.present?
48
+ return !obj.content.nil?
49
+ else
50
+ return false
51
+ end
52
+ end
53
+
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,74 @@
1
+ module Binda
2
+ module FieldableAssociationHelpers
3
+ module FieldableSvgHelpers
4
+ # Check if the field has an attached audio
5
+ #
6
+ # @param field_slug [string] The slug of the field setting
7
+ # @return [boolean]
8
+ def has_svg(field_slug)
9
+ obj = self.svgs.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
10
+ # Alternative query
11
+ # obj = Image.where(field_setting_id: FieldSetting.get_id( field_slug ), fieldable_id: self.id, fieldable_type: self.class.to_s ).first
12
+ raise ArgumentError, "There isn't any svg associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
13
+ return obj.svg.present?
14
+ end
15
+
16
+ # Get the svg url based on the size provided,
17
+ # default is Carrierwave default (usually the real size)
18
+ #
19
+ # @param field_slug [string] The slug of the field setting
20
+ # @param size [string] The size. It can be 'thumb' 200x200 cropped,
21
+ # 'medium' 700x700 max size, 'large' 1400x1400 max size, or blank
22
+ # @return [string] The url of the svg
23
+ def get_svg_url(field_slug)
24
+ get_svg_info( field_slug, 'url' )
25
+ end
26
+
27
+ # Get the svg path based on the size provided,
28
+ # default is Carrierwave default (usually the real size)
29
+ #
30
+ # @param field_slug [string] The slug of the field setting
31
+ # @param size [string] The size. It can be 'thumb' 200x200 cropped,
32
+ # 'medium' 700x700 max size, 'large' 1400x1400 max size, or blank
33
+ # @return [string] The url of the svg
34
+ def get_svg_path(field_slug)
35
+ get_svg_info( field_slug, 'path' )
36
+ end
37
+
38
+ # Get the object related to that field setting
39
+ #
40
+ # @param field_slug [string] The slug of the field setting
41
+ # @param info [string] String of the info to be retrieved
42
+ # @return [string] The info requested if present
43
+ # @return [boolean] Returns false if no info is found or if image isn't found
44
+ def get_svg_info(field_slug, info)
45
+ obj = self.svgs.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
46
+ # Alternative query
47
+ # obj = svg.where(field_setting_id: FieldSetting.get_id( field_slug ), fieldable_id: self.id, fieldable_type: self.class.to_s ).first
48
+ raise ArgumentError, "There isn't any svg associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
49
+ if obj.svg.present?
50
+ obj.svg.send(info)
51
+ end
52
+ end
53
+
54
+ def get_svg_size(field_slug)
55
+ obj = self.svgs.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
56
+ return bytes_to_megabytes(obj.file_size)
57
+ end
58
+
59
+ # Get svg type
60
+ #
61
+ # @param field_slug [string] The slug of the field setting
62
+ # @return [string] with svg type
63
+ def get_svg_mime_type(field_slug)
64
+ obj = self.svgs.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
65
+ return obj.content_type
66
+ end
67
+
68
+ # Convert bytes to megabites
69
+ def bytes_to_megabytes bytes
70
+ (bytes.to_f / 1.megabyte).round(2)
71
+ end
72
+ end
73
+ end
74
+ end