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



125
126
127
# File 'app/controllers/concerns/binda/fieldable_helpers.rb', line 125

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
24
25
26
27
28
29
30
31
# 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 ], 
    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: [], parent_related_board_ids: []  ],
    repeaters_attributes:  [ :id, :field_setting_id, :fieldable_type, :fieldable_id, :field_group_id,
      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 ], 
      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: [],  parent_related_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_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.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/controllers/concerns/binda/fieldable_helpers.rb', line 90

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_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.



114
115
116
117
118
119
120
121
122
# File 'app/controllers/concerns/binda/fieldable_helpers.rb', line 114

def return_video_details asset
  return {
    type: 'video',
    name: asset.video_identifier,
    size: "#{bytes_to_megabytes( asset.video.size )}MB",
    url: asset.video.url,
    ext: asset.video.file.extension.downcase
  }
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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/concerns/binda/fieldable_helpers.rb', line 66

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? && asset.video.present?
    raise "The record Binda::Asset with id=#{asset.id} has both image and video attached. This might have been caused by updating the record outside Binda's interface. Please make sure this record has either an image or a video."
  elsif asset.image.present?
    return_image_details(asset)
  elsif asset.video.present?
    return_video_details(asset)
  else
    raise "The record Binda::Asset with id=#{asset.id} doesn't have any image or video 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



39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/concerns/binda/fieldable_helpers.rb', line 39

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 ]}, 
    {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 ]]})
end