Class: ActionAdmin::AttachmentsInput

Inherits:
SimpleForm::Inputs::Base
  • Object
show all
Defined in:
app/inputs/action_admin/attachments_input.rb

Instance Method Summary collapse

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 attachment(image_id=nil, image_url=nil, image_name=nil)
  image    =  :img, nil, src: image_url, class: 'width-100 margin-bottom-1', data: { src: 'file.small.url', url: "#{template.root_url.chomp('/')}[src]" }
  filename =  :span, image_name, class: 'filename', data: { text: 'name' }
  remove   =  :span, nil, class: 'remove-button mdi mdi-close', data: { remove: '' }
  thumb    =  :div, image + filename + remove, class: 'thumbnail'

   :div, hidden_input(image_id) + thumb, class: 'attachment', data: { list_item: '' }
end

#attachment_urlsObject



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 attachment_urls
  media = nil

  if object.is_a? ::ActiveRecord::Base
    media = object.try(attribute_name)
  elsif options[:model_name]
    media_model = "#{options[: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 attachments(urls=[])
  urls.map { |u| attachment(*u) }.join.html_safe
end

#empty_inputObject



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_nameObject



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)
  input_options = input_html_options

  input_options[:data]        ||= {}
  input_options[:data][:value]  = :id

  @builder.hidden_field(final_attribute_name, input_options.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(wrapper_options)
  modal = options.fetch :modal, 'media-modal'
  html  =  :div, input_placeholder, id: input_html_id, data: { media_attach: modal, media_multiple: true }

  html + input_template
end

#input_html_idObject



42
43
44
# File 'app/inputs/action_admin/attachments_input.rb', line 42

def input_html_id
  hidden_input[/id=\"(.*)\"/, 1].dasherize
end

#input_placeholderObject



10
11
12
13
14
15
16
17
18
19
# File 'app/inputs/action_admin/attachments_input.rb', line 10

def input_placeholder
  span    =  :span, 'No media attached', class: 'margin-bottom-1'
  icon    =  :i, nil, class: 'mdi mdi-camera-off'
  button  =  :a, 'Add media', data: { open: input_html_id }, class: 'button success small hollow margin-0'
  content =  :div, empty_input + icon + span, class: 'no-content hide', data: { empty_state: '' }
  images  = attachments(attachment_urls) if attachment_urls.present?
  grid    = (:div, images, data: { list_remove: '' }, class: 'attachments-grid removable')

  content + grid + (:div, button, class: 'panel-section expanded border last')
end

#input_templateObject



76
77
78
# File 'app/inputs/action_admin/attachments_input.rb', line 76

def input_template
   :script, attachment, 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(wrapper_options)
  ''
end