Class: ActionAdmin::AttachmentsInput
- Inherits:
-
SimpleForm::Inputs::Base
- Object
- SimpleForm::Inputs::Base
- ActionAdmin::AttachmentsInput
- Defined in:
- app/inputs/action_admin/attachments_input.rb
Instance Method Summary collapse
- #attachment(image_id = nil, image_url = nil, image_name = nil) ⇒ Object
- #attachment_urls ⇒ Object
- #attachments(urls = []) ⇒ Object
- #empty_input ⇒ Object
- #final_attribute_name ⇒ Object
- #hidden_input(image_id = nil) ⇒ Object
- #input(wrapper_options) ⇒ Object
- #input_html_id ⇒ Object
- #input_placeholder ⇒ Object
- #input_template ⇒ Object
- #label(wrapper_options) ⇒ Object
Instance Method Details
#attachment(image_id = nil, image_url = nil, image_name = nil) ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'app/inputs/action_admin/attachments_input.rb', line 67 def (image_id=nil, image_url=nil, image_name=nil) image = content_tag :img, nil, src: image_url, class: 'width-100 margin-bottom-1', data: { src: 'file.small.url', url: "#{template.root_url.chomp('/')}[src]" } filename = content_tag :span, image_name, class: 'filename', data: { text: 'name' } remove = content_tag :span, nil, class: 'remove-button mdi mdi-close', data: { remove: '' } thumb = content_tag :div, image + filename + remove, class: 'thumbnail' content_tag :div, hidden_input(image_id) + thumb, class: 'attachment', data: { list_item: '' } end |
#attachment_urls ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/inputs/action_admin/attachments_input.rb', line 46 def media = nil if object.is_a? ::ActiveRecord::Base media = object.try(attribute_name) elsif [:model_name] media_model = "#{[:model_name]}".safe_constantize if media_model.present? attval = object.send(attribute_name) rescue nil media = media_model.where(id: attval) end end Array(media).map { |a| [a.try(:id), a.try(:file_url, :small), a.try(:name)] } end |
#attachments(urls = []) ⇒ Object
63 64 65 |
# File 'app/inputs/action_admin/attachments_input.rb', line 63 def (urls=[]) urls.map { |u| (*u) }.join.html_safe end |
#empty_input ⇒ Object
29 30 31 |
# File 'app/inputs/action_admin/attachments_input.rb', line 29 def empty_input @builder.hidden_field(final_attribute_name, value: '', id: nil, multiple: true) end |
#final_attribute_name ⇒ Object
21 22 23 24 25 26 27 |
# File 'app/inputs/action_admin/attachments_input.rb', line 21 def final_attribute_name if object.is_a? ::ActiveRecord::Base :"#{attribute_name}_ids" else :"#{attribute_name}" end end |
#hidden_input(image_id = nil) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'app/inputs/action_admin/attachments_input.rb', line 33 def hidden_input(image_id=nil) = [:data] ||= {} [:data][:value] = :id @builder.hidden_field(final_attribute_name, .merge(multiple: true, value: image_id)) end |
#input(wrapper_options) ⇒ Object
3 4 5 6 7 8 |
# File 'app/inputs/action_admin/attachments_input.rb', line 3 def input() modal = .fetch :modal, 'media-modal' html = content_tag :div, input_placeholder, id: input_html_id, data: { media_attach: modal, media_multiple: true } html + input_template end |
#input_html_id ⇒ Object
42 43 44 |
# File 'app/inputs/action_admin/attachments_input.rb', line 42 def input_html_id hidden_input[/id=\"(.*)\"/, 1].dasherize end |
#input_placeholder ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'app/inputs/action_admin/attachments_input.rb', line 10 def input_placeholder span = content_tag :span, 'No media attached', class: 'margin-bottom-1' icon = content_tag :i, nil, class: 'mdi mdi-camera-off' = content_tag :a, 'Add media', data: { open: input_html_id }, class: 'button success small hollow margin-0' content = content_tag :div, empty_input + icon + span, class: 'no-content hide', data: { empty_state: '' } images = () if .present? grid = content_tag(:div, images, data: { list_remove: '' }, class: 'attachments-grid removable') content + grid + content_tag(:div, , class: 'panel-section expanded border last') end |
#input_template ⇒ Object
76 77 78 |
# File 'app/inputs/action_admin/attachments_input.rb', line 76 def input_template content_tag :script, , id: "#{input_html_id}-item-template", type: 'text/template' end |
#label(wrapper_options) ⇒ Object
80 81 82 |
# File 'app/inputs/action_admin/attachments_input.rb', line 80 def label() '' end |