binda 0.1.7 → 0.1.8

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.
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,71 @@
1
+ require_dependency "binda/application_controller"
2
+
3
+ module Binda
4
+ class SvgsController < ApplicationController
5
+ before_action :set_svg, only: [:show, :edit, :update, :destroy, :remove_svg]
6
+
7
+ # GET /svgs
8
+ def index
9
+ @svgs = Svg.all
10
+ end
11
+
12
+ # GET /svgs/1
13
+ def show
14
+ end
15
+
16
+ # GET /svgs/new
17
+ def new
18
+ @svg = Svg.new
19
+ end
20
+
21
+ # GET /svgs/1/edit
22
+ def edit
23
+ end
24
+
25
+ # POST /svgs
26
+ def create
27
+ @svg = Svg.new(svg_params)
28
+
29
+ if @svg.save
30
+ redirect_to svg_path( @svg.id ), notice: 'Svg was successfully created.'
31
+ else
32
+ render :new
33
+ end
34
+ end
35
+
36
+ # PATCH/PUT /svgs/1
37
+ def update
38
+ if @svg.update(svg_params)
39
+ redirect_to svg_path( @svg.id ), notice: 'Svg was successfully updated.'
40
+ else
41
+ render :edit
42
+ end
43
+ end
44
+
45
+ # DELETE /svgs/1
46
+ def destroy
47
+ @svg.destroy
48
+ redirect_to svgs_url, notice: 'Svg was successfully destroyed.'
49
+ end
50
+
51
+ def remove_svg
52
+ @svg.remove_svg!
53
+ if @svg.save
54
+ head :ok
55
+ else
56
+ render json: @svg.errors.full_messages, status: 400
57
+ end
58
+ end
59
+
60
+ private
61
+ # Use callbacks to share common setup or constraints between actions.
62
+ def set_svg
63
+ @svg = Svg.find(params[:id])
64
+ end
65
+
66
+ # Only allow a trusted parameter "white list" through.
67
+ def svg_params
68
+ params.require(:svg).permit( :svg, :name )
69
+ end
70
+ end
71
+ end
@@ -4,32 +4,40 @@ module Binda
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  # Only allow a trusted parameter "white list" through.
7
- def fieldable_params
8
- [ texts_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :content ],
7
+ def fieldable_params
8
+ [
9
+ texts_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :content ],
9
10
  strings_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :content ],
10
11
  images_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :image, :image_cache ],
11
12
  videos_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :video, :video_cache ],
12
- audios_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :audio, :audio_cache ],
13
+ audios_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :audio, :audio_cache ],
14
+ svgs_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :svg, :svg_cache ],
13
15
  dates_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :date ],
14
16
  galleries_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id ],
15
17
  radios_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :choice_ids ],
16
18
  selections_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :choice_ids ],
17
19
  checkboxes_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, choice_ids: [] ],
18
20
  relations_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, dependent_component_ids: [], dependent_board_ids: [] ],
19
- repeaters_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :field_group_id,
20
- texts_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :content ],
21
- strings_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :content ],
22
- images_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :image, :image_cache ],
23
- videos_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :video, :video_cache ],
24
- audios_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :audio, :audio_cache ],
25
- dates_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :date ],
26
- galleries_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id ],
27
- relations_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, dependent_component_ids: [], dependent_board_ids: [] ],
28
- repeaters_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :field_group_id ],
29
- radios_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :choice_ids ],
30
- selections_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :choice_ids ],
31
- checkboxes_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, choice_ids: [] ]
32
- ]]
21
+ repeaters_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :field_group_id, *nested_fieldable_params ]
22
+ ]
23
+ end
24
+
25
+ def nested_fieldable_params
26
+ [
27
+ texts_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :content ],
28
+ strings_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :content ],
29
+ images_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :image, :image_cache ],
30
+ videos_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :video, :video_cache ],
31
+ audios_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :audio, :audio_cache ],
32
+ svgs_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :svg, :svg_cache ],
33
+ dates_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :date ],
34
+ galleries_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id ],
35
+ relations_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, dependent_component_ids: [], dependent_board_ids: [] ],
36
+ repeaters_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :field_group_id ],
37
+ radios_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :choice_ids ],
38
+ selections_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :choice_ids ],
39
+ checkboxes_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, choice_ids: [] ]
40
+ ]
33
41
  end
34
42
 
35
43
  # Uploads parameters.
@@ -46,10 +54,12 @@ module Binda
46
54
  {images_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :image, :image_cache ]},
47
55
  {videos_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :video, :video_cache ]},
48
56
  {audios_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :audio, :audio_cache ]},
57
+ {svgs_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :svg, :svg_cache ]},
49
58
  {repeaters_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id,
50
59
  images_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :image, :image_cache ],
51
60
  videos_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :video, :video_cache ],
52
- audios_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :audio, :audio_cache ]]})
61
+ audios_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :audio, :audio_cache ],
62
+ svgs_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :svg, :svg_cache ]]})
53
63
  end
54
64
 
55
65
  # Uploads a details for a fieldable instance (component or board)
@@ -68,7 +78,6 @@ module Binda
68
78
  # }
69
79
  #
70
80
  def upload_details
71
-
72
81
  # Because this is a callback that happens right after the upload
73
82
  # chances are that it's also the latest asset that has been updated.
74
83
  # Therefore get the latest uploaded asset
@@ -80,9 +89,12 @@ module Binda
80
89
  return_video_details(asset)
81
90
  elsif asset.audio.present?
82
91
  return_audio_details(asset)
83
- else
92
+ elsif asset.svg.present?
93
+ return_svg_details(asset)
94
+ else
84
95
  raise "The record Binda::Asset with id=#{asset.id} doesn't have any image, video or audio attached. This might be due to a bug. Please report it in Binda official Github page."
85
96
  end
97
+
86
98
  end
87
99
 
88
100
 
@@ -109,6 +121,22 @@ module Binda
109
121
  }
110
122
  end
111
123
 
124
+ # Return svg details
125
+ #
126
+ # This helper is used internally by the `upload_details` method
127
+ # to retrieve and return the necessary details to be displayed on
128
+ # the editor.
129
+ def return_svg_details asset
130
+ # get image dimension
131
+ return {
132
+ type: 'svg',
133
+ name: asset.svg_identifier,
134
+ size: "#{bytes_to_megabytes( asset.svg.size )}MB",
135
+ url: asset.svg.url,
136
+ thumbnailUrl: asset.svg.url
137
+ }
138
+ end
139
+
112
140
 
113
141
  # Return video details
114
142
  #
@@ -0,0 +1,7 @@
1
+ module Binda
2
+ module Binda
3
+ def self.table_name_prefix
4
+ 'binda_binda_'
5
+ end
6
+ end
7
+ end
@@ -6,7 +6,7 @@ module Binda
6
6
  # An array of all classes which represent fields associated to Binda::FieldSetting
7
7
  # This definition must stay on the top of the file
8
8
  def self.get_field_classes
9
- %w( String Text Date Image Video Audio Repeater Radio Selection Checkbox Relation )
9
+ %w( String Text Date Image Video Audio Repeater Radio Selection Checkbox Relation Svg )
10
10
  end
11
11
 
12
12
  # ASSOCIATIONS
@@ -33,6 +33,7 @@ module Binda
33
33
  has_many :selections, as: :fieldable
34
34
  has_many :checkboxes, as: :fieldable
35
35
  has_many :relations, as: :fieldable
36
+ has_many :svgs, as: :fieldable
36
37
 
37
38
  # The following direct associations are used to securely delete associated fields
38
39
  # Infact via `fieldable` the associated fields might not be deleted
@@ -41,15 +42,16 @@ module Binda
41
42
  has_many :strings, dependent: :destroy
42
43
  has_many :dates, dependent: :destroy
43
44
  has_many :galleries, dependent: :destroy
45
+ has_many :assets, dependent: :destroy
46
+ has_many :images, dependent: :destroy
47
+ has_many :videos, dependent: :destroy
48
+ has_many :audios, dependent: :destroy
44
49
  has_many :repeaters, dependent: :destroy
45
50
  has_many :radios, dependent: :destroy
46
51
  has_many :selections, dependent: :destroy
47
52
  has_many :checkboxes, dependent: :destroy
48
53
  has_many :relations, dependent: :destroy
49
- has_many :assets, dependent: :destroy
50
- has_many :images, dependent: :destroy
51
- has_many :videos, dependent: :destroy
52
- has_many :audios, dependent: :destroy
54
+ has_many :svgs, dependent: :destroy
53
55
 
54
56
  # We don't want to run callbacks for choices!
55
57
  # If you run a callback the last choice will throw a error
@@ -62,7 +64,7 @@ module Binda
62
64
 
63
65
  accepts_nested_attributes_for :accepted_structures, :texts, :strings, :dates, :galleries,
64
66
  :assets, :images, :videos, :audios, :repeaters, :radios, :selections,
65
- :checkboxes, :relations, :choices, allow_destroy: true, reject_if: :is_rejected
67
+ :checkboxes, :relations, :svgs, :choices, allow_destroy: true, reject_if: :is_rejected
66
68
 
67
69
  # Sets the validation rules to accept and save an attribute
68
70
  def is_rejected( attributes )
@@ -7,7 +7,7 @@ module Binda
7
7
  # Register image details
8
8
  #
9
9
  # Do not delete. This method is used by a rake task
10
- def register_deatils
10
+ def register_details
11
11
  if !self.image.present?
12
12
  warn "Ops, there is no image for Binda::Image id=#{self.id}"
13
13
  elsif CarrierWave::Uploader::Base.storage == CarrierWave::Storage::File
@@ -21,7 +21,7 @@ module Binda
21
21
 
22
22
  # Register image details
23
23
  #
24
- # This method is used by register_deatils in a rake task
24
+ # This method is used by register_details in a rake task
25
25
  def register_details_of(file)
26
26
  self.file_width = file.width
27
27
  self.file_height = file.height
@@ -0,0 +1,7 @@
1
+ module Binda
2
+ class Svg < Asset
3
+
4
+ mount_uploader :svg, SvgUploader
5
+
6
+ end
7
+ end
@@ -3,460 +3,16 @@ module Binda
3
3
 
4
4
  extend ActiveSupport::Concern
5
5
 
6
- # Get the object related to that field setting
7
- # If the object doesn't exists yet it will return nil
8
- #
9
- # @param field_slug [string] The slug of the field setting
10
- # @return [string] Returns the content of the text
11
- # @return [error] Raise an error if no record is found
12
- def get_text(field_slug)
13
- obj = Text
14
- .includes(:field_setting)
15
- .where(fieldable_id: self.id, fieldable_type: self.class.name)
16
- .where(binda_field_settings: { slug: field_slug })
17
- .where.not(binda_field_settings: { field_type: "string" })
18
- .first
19
- unless obj.nil?
20
- # to_s ensures the returned object is class String
21
- obj.content.to_s
22
- else
23
- check_text_error field_slug
24
- end
25
- end
6
+ include FieldableTextHelpers
7
+ include FieldableStringHelpers
8
+ include FieldableImageHelpers
9
+ include FieldableVideoHelpers
10
+ include FieldableAudioHelpers
11
+ include FieldableSvgHelpers
12
+ include FieldableRelationHelpers
13
+ include FieldableSelectionHelpers
14
+ include FieldableRepeaterHelpers
15
+ include FieldableDateHelpers
26
16
 
27
- # Check why get_text doesn't return a value
28
- # This method isn't supposed to be used by anything other than get_text method
29
- def check_text_error(field_slug)
30
- you_mean_string = !self.strings.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) && t.type == 'Binda::String' }.nil?
31
- if you_mean_string
32
- raise ArgumentError, "This slug (#{field_slug}) is associated to a string not a text. Use get_string() instead on instance (#{self.class.name} ##{self.id}).", caller
33
- else
34
- raise ArgumentError, "There isn't any text associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller
35
- end
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
- # @return [boolean]
42
- def has_text(field_slug)
43
- obj = Text
44
- .includes(:field_setting)
45
- .where(fieldable_id: self.id, fieldable_type: self.class.name)
46
- .where(binda_field_settings: { slug: field_slug })
47
- .where.not(binda_field_settings: { field_type: "string" })
48
- .first
49
- raise ArgumentError, "There isn't any text associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
50
- if obj.present?
51
- return !obj.content.nil?
52
- else
53
- return false
54
- end
55
- end
56
-
57
- # Get the object related to that field setting
58
- # If the object doesn't exists yet it will return nil
59
- #
60
- # @param field_slug [string] The slug of the field setting
61
- # @return [string] Returns the content of the string
62
- # @return [error] Raise an error if no record is found
63
- def get_string(field_slug)
64
- obj = Text
65
- .includes(:field_setting)
66
- .where(fieldable_id: self.id, fieldable_type: self.class.name)
67
- .where(binda_field_settings: { slug: field_slug, field_type: "string" })
68
- .first
69
- unless obj.nil?
70
- # to_s ensures the returned object is class String
71
- obj.content.to_s
72
- else
73
- check_string_error field_slug
74
- end
75
- end
76
-
77
- # Check why get_string doesn't return a value
78
- # This method isn't supposed to be used by anything other than get_string method
79
- def check_string_error(field_slug)
80
- you_mean_text = !self.strings.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) && t.type == 'Binda::Text' }.nil?
81
- if you_mean_text
82
- 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
83
- else
84
- raise ArgumentError, "There isn't any string associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller
85
- end
86
- end
87
-
88
- # Get the object related to that field setting
89
- #
90
- # @param field_slug [string] The slug of the field setting
91
- # @return [boolean]
92
- def has_string(field_slug)
93
- obj = Text
94
- .includes(:field_setting)
95
- .where(fieldable_id: self.id, fieldable_type: self.class.name)
96
- .where(binda_field_settings: { slug: field_slug, field_type: "string" })
97
- .first
98
- 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?
99
- if obj.present?
100
- return !obj.content.nil?
101
- else
102
- return false
103
- end
104
- end
105
-
106
- # Check if the field has an attached image
107
- #
108
- # @param field_slug [string] The slug of the field setting
109
- # @return [boolean]
110
- def has_image(field_slug)
111
- obj = self.images.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
112
- # Alternative query
113
- # obj = Image.where(field_setting_id: FieldSetting.get_id( field_slug ), fieldable_id: self.id, fieldable_type: self.class.to_s ).first
114
- 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?
115
- return obj.image.present?
116
- end
117
-
118
- # Get the image url based on the size provided,
119
- # default is Carrierwave default (usually the real size)
120
- #
121
- # @param field_slug [string] The slug of the field setting
122
- # @param size [string] The size. It can be 'thumb' 200x200 cropped,
123
- # 'medium' 700x700 max size, 'large' 1400x1400 max size, or blank
124
- # @return [string] The url of the image
125
- def get_image_url(field_slug, size = '')
126
- get_image_info( field_slug, size, 'url' )
127
- end
128
-
129
- # Get the image path based on the size provided,
130
- # default is Carrierwave default (usually the real size)
131
- #
132
- # @param field_slug [string] The slug of the field setting
133
- # @param size [string] The size. It can be 'thumb' 200x200 cropped,
134
- # 'medium' 700x700 max size, 'large' 1400x1400 max size, or blank
135
- # @return [string] The url of the image
136
- def get_image_path(field_slug, size = '')
137
- get_image_info( field_slug, size, 'path' )
138
- end
139
-
140
- # Get the object related to that field setting
141
- #
142
- # @param field_slug [string] The slug of the field setting
143
- # @param size [string] The size. It can be 'thumb' 200x200 cropped,
144
- # 'medium' 700x700 max size, 'large' 1400x1400 max size, or blank
145
- # @param info [string] String of the info to be retrieved
146
- # @return [string] The info requested if present
147
- # @return [boolean] Returns false if no info is found or if image isn't found
148
- def get_image_info(field_slug, size, info)
149
- obj = self.images.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
150
- # Alternative query
151
- # obj = Image.where(field_setting_id: FieldSetting.get_id( field_slug ), fieldable_id: self.id, fieldable_type: self.class.to_s ).first
152
- 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?
153
- if obj.image.present? && obj.image.respond_to?(size) && %w[thumb].include?(size)
154
- obj.image.send(size).send(info)
155
- elsif obj.image.present?
156
- obj.image.send(info)
157
- else
158
- 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\""
159
- end
160
- end
161
-
162
- # Get image size
163
- #
164
- # @param field_slug [string] The slug of the field setting
165
- # @return [float] with image type
166
- def get_image_size(field_slug)
167
- obj = self.images.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
168
- return bytes_to_megabytes(obj.file_size)
169
- end
170
-
171
- # Convert bytes to megabites
172
- def bytes_to_megabytes bytes
173
- (bytes.to_f / 1.megabyte).round(2)
174
- end
175
-
176
- # Get image type
177
- #
178
- # @param field_slug [string] The slug of the field setting
179
- # @return [string] with image type
180
- def get_image_mime_type(field_slug)
181
- obj = self.images.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
182
- return obj.content_type
183
- end
184
-
185
- # Get image dimension
186
- #
187
- # @param field_slug [string] The slug of the field setting
188
- # @return [hash] with width and height as floats
189
- def get_image_dimension(field_slug)
190
- obj = self.images.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
191
- return { width: obj.file_width, height: obj.file_height }
192
- end
193
-
194
- # Check if the field has an attached video
195
- #
196
- # @param field_slug [string] The slug of the field setting
197
- # @return [boolean]
198
- def has_video(field_slug)
199
- obj = self.videos.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
200
- # Alternative query
201
- # obj = Image.where(field_setting_id: FieldSetting.get_id( field_slug ), fieldable_id: self.id, fieldable_type: self.class.to_s ).first
202
- raise ArgumentError, "There isn't any video associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
203
- return obj.video.present?
204
- end
205
-
206
- # Get the video url based on the size provided,
207
- # default is Carrierwave default (usually the real size)
208
- #
209
- # @param field_slug [string] The slug of the field setting
210
- # @param size [string] The size. It can be 'thumb' 200x200 cropped,
211
- # 'medium' 700x700 max size, 'large' 1400x1400 max size, or blank
212
- # @return [string] The url of the video
213
- def get_video_url(field_slug)
214
- get_video_info( field_slug, 'url' )
215
- end
216
-
217
- # Get the video path based on the size provided,
218
- # default is Carrierwave default (usually the real size)
219
- #
220
- # @param field_slug [string] The slug of the field setting
221
- # @param size [string] The size. It can be 'thumb' 200x200 cropped,
222
- # 'medium' 700x700 max size, 'large' 1400x1400 max size, or blank
223
- # @return [string] The url of the video
224
- def get_video_path(field_slug)
225
- get_video_info( field_slug, 'path' )
226
- end
227
-
228
- # Get the object related to that field setting
229
- #
230
- # @param field_slug [string] The slug of the field setting
231
- # @param info [string] String of the info to be retrieved
232
- # @return [string] The info requested if present
233
- # @return [boolean] Returns false if no info is found or if image isn't found
234
- def get_video_info(field_slug, info)
235
- obj = self.videos.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
236
- # Alternative query
237
- # obj = video.where(field_setting_id: FieldSetting.get_id( field_slug ), fieldable_id: self.id, fieldable_type: self.class.to_s ).first
238
- raise ArgumentError, "There isn't any video associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
239
- if obj.video.present?
240
- obj.video.send(info)
241
- end
242
- end
243
-
244
-
245
- # Check if the field has an attached audio
246
- #
247
- # @param field_slug [string] The slug of the field setting
248
- # @return [boolean]
249
- def has_audio(field_slug)
250
- obj = self.audios.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
251
- # Alternative query
252
- # obj = Image.where(field_setting_id: FieldSetting.get_id( field_slug ), fieldable_id: self.id, fieldable_type: self.class.to_s ).first
253
- 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?
254
- return obj.audio.present?
255
- end
256
-
257
- # Get the audio url based on the size provided,
258
- # default is Carrierwave default (usually the real size)
259
- #
260
- # @param field_slug [string] The slug of the field setting
261
- # @param size [string] The size. It can be 'thumb' 200x200 cropped,
262
- # 'medium' 700x700 max size, 'large' 1400x1400 max size, or blank
263
- # @return [string] The url of the audio
264
- def get_audio_url(field_slug)
265
- get_audio_info( field_slug, 'url' )
266
- end
267
-
268
- # Get the audio path based on the size provided,
269
- # default is Carrierwave default (usually the real size)
270
- #
271
- # @param field_slug [string] The slug of the field setting
272
- # @param size [string] The size. It can be 'thumb' 200x200 cropped,
273
- # 'medium' 700x700 max size, 'large' 1400x1400 max size, or blank
274
- # @return [string] The url of the audio
275
- def get_audio_path(field_slug)
276
- get_audio_info( field_slug, 'path' )
277
- end
278
-
279
- # Get the object related to that field setting
280
- #
281
- # @param field_slug [string] The slug of the field setting
282
- # @param info [string] String of the info to be retrieved
283
- # @return [string] The info requested if present
284
- # @return [boolean] Returns false if no info is found or if image isn't found
285
- def get_audio_info(field_slug, info)
286
- obj = self.audios.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
287
- # Alternative query
288
- # obj = audio.where(field_setting_id: FieldSetting.get_id( field_slug ), fieldable_id: self.id, fieldable_type: self.class.to_s ).first
289
- 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?
290
- if obj.audio.present?
291
- obj.audio.send(info)
292
- end
293
- end
294
-
295
- # Check if the field has an attached date
296
- #
297
- # @param field_slug [string] The slug of the field setting
298
- # @return [datetime] The date
299
- # @return [boolean] Reutrn false if nothing is found
300
- def has_date(field_slug)
301
- obj = self.dates.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
302
- 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?
303
- if obj.present?
304
- return !obj.date.nil?
305
- else
306
- return false
307
- end
308
- end
309
-
310
- # Get the object related to that field setting
311
- #
312
- # @param field_slug [string] The slug of the field setting
313
- # @return [boolean]
314
- def get_date(field_slug)
315
- obj = self.dates.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
316
- 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?
317
- obj.date
318
- end
319
-
320
- # Check if exists any repeater with that slug
321
- #
322
- # @param field_slug [string] The slug of the field setting
323
- # @return [boolean]
324
- def has_repeaters(field_slug)
325
- obj = self.repeaters.find_all{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
326
- 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?
327
- return obj.present?
328
- end
329
- def has_repeater(field_slug)
330
- has_repeaters(field_slug)
331
- end
332
-
333
- # Get the all repeater instances sorted by position
334
- #
335
- # @param field_slug [string] The slug of the field setting
336
- # @return [array] An array of repeater items which have all sorts of fields attached
337
- def get_repeaters(field_slug)
338
- obj = self.repeaters.find_all{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
339
- 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?
340
- obj.sort_by(&:position)
341
- end
342
- def get_repeater(field_slug)
343
- get_repeaters(field_slug)
344
- end
345
-
346
- # Get the radio choice
347
- #
348
- # If by mistake the Radio instance has many choices associated,
349
- # only the first one will be retrieved.
350
- #
351
- # @param field_slug [string] The slug of the field setting
352
- # @return [hash] A hash of containing the label and value of the selected choice. `{ label: 'the label', value: 'the value'}`
353
- def get_radio_choice(field_slug)
354
- field_setting = FieldSetting.find_by(slug:field_slug)
355
- obj = self.radios.find{ |t| t.field_setting_id == field_setting.id }
356
- 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?
357
- raise "There isn't any choice available for the current radio (#{field_slug}) on instance (#{self.class.name} ##{self.id})." unless obj.choices.any?
358
- return { label: obj.choices.first.label, value: obj.choices.first.value }
359
- end
360
-
361
- # Get the select choices
362
- #
363
- # @param field_slug [string] The slug of the field setting
364
- # @return [hash] A hash of containing the label and value of the selected choice. `{ label: 'the label', 'value': 'the value'}`
365
- def get_selection_choice(field_slug)
366
- field_setting = FieldSetting.find_by(slug:field_slug)
367
- obj = self.selections.find{ |t| t.field_setting_id == field_setting.id }
368
- 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?
369
- 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?
370
- return { label: obj.choices.first.label, value: obj.choices.first.value }
371
- end
372
-
373
- # Get the select choices
374
- #
375
- # @param field_slug [string] The slug of the field setting
376
- # @return [array] An array of hashes of containing label and value of the selected choices. `{ label: 'the label', 'value': 'the value'}`
377
- def get_selection_choices(field_slug)
378
- field_setting = FieldSetting.find_by(slug:field_slug)
379
- obj = self.selections.find{ |t| t.field_setting_id == field_setting.id }
380
- 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?
381
- 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?
382
- return obj.choices.map{|choice| { label: choice.label, value: choice.value }}
383
- end
384
-
385
- # Get the checkbox choice
386
- #
387
- # @param field_slug [string] The slug of the field setting
388
- # @return [array] An array of labels and values of the selected choices. `[{ label: '1st label', value: '1st-value'}, { label: '2nd label', value: '2nd-value'}]`
389
- def get_checkbox_choices(field_slug)
390
- field_setting = FieldSetting.find_by(slug:field_slug)
391
- obj = self.checkboxes.find{ |t| t.field_setting_id == field_setting.id }
392
- 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?
393
- 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?
394
- obj_array = []
395
- obj.choices.order('label').each do |o|
396
- obj_array << { label: o.label, value: o.value }
397
- end
398
- return obj_array
399
- end
400
-
401
- # Check if has related components
402
- #
403
- # @param field_slug [string] The slug of the field setting
404
- # @return [boolean]
405
- def has_related_components(field_slug)
406
- obj = self.relations.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
407
- 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?
408
- return obj.dependent_relations.any?
409
- end
410
-
411
- # Alias for has_related_components
412
- def has_dependent_components(field_slug)
413
- has_related_components(field_slug)
414
- end
415
-
416
- # Get related components
417
- #
418
- # @param field_slug [string] The slug of the field setting
419
- # @return [array] An array of components
420
- def get_related_components(field_slug)
421
- obj = self.relations.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
422
- 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?
423
- return obj.dependent_relations.map{|relation| relation.dependent}
424
- end
425
-
426
- # Alias for get_related_components
427
- def get_dependent_components(field_slug)
428
- get_related_components(field_slug)
429
- end
430
-
431
- # Get all components which owns a relation where the current instance is a dependent
432
- #
433
- # @param field_slug [string] The slug of the field setting of the relation
434
- # @return [array] An array of components and/or boards
435
- def get_owner_components(field_slug)
436
- # obj = self.owner_relations.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
437
- 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})
438
- 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?
439
- return obj
440
- end
441
-
442
- # Check if has related boards
443
- #
444
- # @param field_slug [string] The slug of the field setting
445
- # @return [boolean]
446
- def has_related_boards(field_slug)
447
- obj = self.relations.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
448
- 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?
449
- return obj.dependent_relations.any?
450
- end
451
-
452
- # Get related boards
453
- #
454
- # @param field_slug [string] The slug of the field setting
455
- # @return [array] An array of boards
456
- def get_related_boards(field_slug)
457
- obj = self.relations.find{ |t| t.field_setting_idid == FieldSetting.get_id( field_slug ) }
458
- 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?
459
- return obj.dependent_relations.map{|relation| relation.dependent}
460
- end
461
17
  end
462
18
  end