Module: Binda::FieldableHelpers
- Extended by:
- ActiveSupport::Concern
- Included in:
- BoardsController, ComponentsController
- Defined in:
- app/controllers/concerns/binda/fieldable_helpers.rb
Instance Method Summary collapse
-
#bytes_to_megabytes(bytes) ⇒ Object
Convert bytes to megabites.
-
#fieldable_params ⇒ Object
Only allow a trusted parameter "white list" through.
-
#return_image_details(asset) ⇒ Object
Return image details.
-
#return_video_details(asset) ⇒ Object
Return video details.
-
#upload_details ⇒ hash
Uploads a details for a fieldable instance (component or board).
-
#upload_params(fieldable_type) ⇒ hash
Uploads parameters.
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_params ⇒ Object
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_details ⇒ hash
Uploads a details for a fieldable instance (component or board)
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.
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 |