Module: Binda::FieldableHelpers

Extended by:
ActiveSupport::Concern
Included in:
BoardsController, ComponentsController
Defined in:
app/controllers/concerns/binda/fieldable_helpers.rb

Instance Method Summary collapse

Instance Method Details

#bytes_to_megabytes(bytes) ⇒ Object

Convert bytes to megabites



173
174
175
# File 'app/controllers/concerns/binda/fieldable_helpers.rb', line 173

def bytes_to_megabytes bytes
  (bytes.to_f / 1.megabyte).round(2)
end

#fieldable_paramsObject

Only allow a trusted parameter “white list” through.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/concerns/binda/fieldable_helpers.rb', line 7

def fieldable_params
  [
    texts_attributes:      [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :content ], 
    strings_attributes:    [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :content ], 
    images_attributes:     [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :image, :image_cache ], 
    videos_attributes:     [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :video, :video_cache ], 
    audios_attributes:     [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :audio, :audio_cache ],
    svgs_attributes:       [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :svg, :svg_cache ],  
    dates_attributes:      [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :date ], 
    galleries_attributes:  [ :id, :field_setting_id, :fieldable_type, :fieldable_id ],
    radios_attributes:     [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :choice_ids ],
    selections_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :choice_ids ],
    checkboxes_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, choice_ids: [] ],
    relations_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, dependent_component_ids: [], dependent_board_ids: []  ],
    repeaters_attributes:  [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :field_group_id, *nested_fieldable_params ]
  ]
end

#nested_fieldable_paramsObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/controllers/concerns/binda/fieldable_helpers.rb', line 25

def nested_fieldable_params
  [ 
    texts_attributes:      [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :content ], 
    strings_attributes:    [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :content ], 
    images_attributes:     [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :image, :image_cache ], 
    videos_attributes:     [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :video, :video_cache ], 
    audios_attributes:     [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :audio, :audio_cache ],
    svgs_attributes:       [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :svg, :svg_cache ],   
    dates_attributes:      [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :date ], 
    galleries_attributes:  [ :id, :field_setting_id, :fieldable_type, :fieldable_id ],
    relations_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, dependent_component_ids: [],  dependent_board_ids: []  ],
    repeaters_attributes:  [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :field_group_id ],
    radios_attributes:     [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :choice_ids ],
    selections_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :choice_ids ],
    checkboxes_attributes: [ :id, :field_setting_id, :fieldable_type, :fieldable_id, choice_ids: [] ] 
  ]
end

#return_audio_details(asset) ⇒ Object

Return audio details

This helper is used internally by the ‘upload_details` method

to retrieve and return the necessary details to be displayed on
the editor.


162
163
164
165
166
167
168
169
170
# File 'app/controllers/concerns/binda/fieldable_helpers.rb', line 162

def return_audio_details asset
  return {
    type: 'audio',
    name: asset.audio_identifier,
    size: "#{bytes_to_megabytes( asset.audio.size )}MB",
    url: asset.audio.url,
    contentType: asset.content_type
  }
end

#return_image_details(asset) ⇒ Object

Return image details

This helper is used internally by the ‘upload_details` method

to retrieve and return the necessary details to be displayed on
the editor.


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/concerns/binda/fieldable_helpers.rb', line 106

def return_image_details asset
  # get image dimension
  if CarrierWave::Uploader::Base.storage == CarrierWave::Storage::File
    file = MiniMagick::Image.open(::Rails.root.join(asset.image.path))
  else
    file = MiniMagick::Image.open(asset.image.url)
  end
  return { 
    type: 'image',
    name: asset.image_identifier,
    size: "#{bytes_to_megabytes( asset.image.size )}MB",
    width: file.width,
    height: file.height,
    url: asset.image.url,
    thumbnailUrl: asset.image.thumb.url 
  }
end

#return_svg_details(asset) ⇒ Object

Return svg details

This helper is used internally by the ‘upload_details` method

to retrieve and return the necessary details to be displayed on
the editor.


129
130
131
132
133
134
135
136
137
138
# File 'app/controllers/concerns/binda/fieldable_helpers.rb', line 129

def return_svg_details asset
  # get image dimension
  return { 
    type: 'svg',
    name: asset.svg_identifier,
    size: "#{bytes_to_megabytes( asset.svg.size )}MB",
    url: asset.svg.url,
    thumbnailUrl: asset.svg.url 
  }
end

#return_video_details(asset) ⇒ Object

Return video details

This helper is used internally by the ‘upload_details` method

to retrieve and return the necessary details to be displayed on
the editor.


146
147
148
149
150
151
152
153
154
# File 'app/controllers/concerns/binda/fieldable_helpers.rb', line 146

def return_video_details asset
  return {
    type: 'video',
    name: asset.video_identifier,
    size: "#{bytes_to_megabytes( asset.video.size )}MB",
    url: asset.video.url,
    contentType: asset.content_type
  }
end

#upload_detailshash

Uploads a details for a fieldable instance (component or board)

Examples:

The return value will be something like:

# Return hash
{ 
  name: 'my_image.png',
  size: 543876,
  width: 300,
  height: 300,
  url: 'url/to/my_image.png',
  thumbnailUrl: 'url/to/my_image_thumb.png'
}

Returns:

  • (hash)

    containig the array of images



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/concerns/binda/fieldable_helpers.rb', line 80

def upload_details
  # Because this is a callback that happens right after the upload
  # chances are that it's also the latest asset that has been updated.
  # Therefore get the latest uploaded asset
  asset = Asset.order('updated_at').last

  if asset.image.present?
    return_image_details(asset)
  elsif asset.video.present?
    return_video_details(asset)
  elsif asset.audio.present?
    return_audio_details(asset)
  elsif asset.svg.present?
    return_svg_details(asset)
  else 
    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."
  end

end

#upload_params(fieldable_type) ⇒ hash

Uploads parameters.

Parameters:

  • fieldable_type (symbol)

    It can be ‘:component` or `:board`.

Returns:

  • (hash)

    It returns a hash with the permitted upload parameters



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/concerns/binda/fieldable_helpers.rb', line 49

def upload_params fieldable_type
  params.require(fieldable_type).permit( 
    :id, :name, :slug, :position, :publish_state, :structure_id, :category_ids,
    {structure_attributes:  [ :id ]}, 
    {categories_attributes: [ :id, :category_id ]}, 
    {images_attributes:     [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :image, :image_cache ]}, 
    {videos_attributes:     [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :video, :video_cache ]}, 
    {audios_attributes:     [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :audio, :audio_cache ]},
    {svgs_attributes:       [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :svg, :svg_cache ]}, 
    {repeaters_attributes:  [ :id, :field_setting_id, :fieldable_type, :fieldable_id,
      images_attributes:     [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :image, :image_cache ],
      videos_attributes:     [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :video, :video_cache ],
      audios_attributes:     [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :audio, :audio_cache ],
      svgs_attributes:       [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :svg, :svg_cache ]]})
end