Class: ActionAdmin::AttachmentInput

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

Instance Method Summary collapse

Instance Method Details

#attachment(image_url = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'app/inputs/action_admin/attachment_input.rb', line 49

def attachment(image_url=nil)
  image   =  :img, nil, src: image_url, class: 'width-100 margin-bottom-1', data: { src: 'file.preview.url', url: "#{template.root_url.chomp('/')}[src]" }
  remove  =  :a, 'Remove', class: 'button alert small hollow margin-0', data: { remove: '' }
  change  =  :a, 'Change', class: 'button success small hollow margin-0', data: { open: input_html_id }
  remove  =  :div, remove, class: 'cell auto text-left'
  change  =  :div, change, class: 'cell shrink'
  buttons =  :div, remove + change, class: 'panel-section expanded border last grid-x'

   :div, hidden_input + image + buttons, class: 'attachment text-center', data: { list_item: '' }
end

#attachment_urlObject



45
46
47
# File 'app/inputs/action_admin/attachment_input.rb', line 45

def attachment_url
  object.try(attribute_name).try(:file_url, :preview)
end

#empty_inputObject



28
29
30
# File 'app/inputs/action_admin/attachment_input.rb', line 28

def empty_input
  @builder.hidden_field(final_attribute_name, value: '', id: nil)
end

#final_attribute_nameObject



20
21
22
23
24
25
26
# File 'app/inputs/action_admin/attachment_input.rb', line 20

def final_attribute_name
  if object.is_a? ::ActiveRecord::Base
    :"#{attribute_name}_id"
  else
    :"#{attribute_name}"
  end
end

#hidden_inputObject



32
33
34
35
36
37
38
39
# File 'app/inputs/action_admin/attachment_input.rb', line 32

def hidden_input
  input_options = input_html_options

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

  @builder.hidden_field(final_attribute_name, input_options)
end

#input(wrapper_options) ⇒ Object



3
4
5
6
7
8
# File 'app/inputs/action_admin/attachment_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 }

  html + input_template
end

#input_html_idObject



41
42
43
# File 'app/inputs/action_admin/attachment_input.rb', line 41

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

#input_placeholderObject



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

def input_placeholder
  span    =  :span, 'No thumbnail', class: 'margin-bottom-1'
  icon    =  :i, nil, class: 'mdi mdi-camera-off'
  button  =  :a, 'Add Thumbnail', data: { open: input_html_id }, class: 'button success small hollow'
  content =  :div, empty_input + icon + span + button, class: 'no-content panel-section expanded border first last hide', data: { empty_state: '' }
  image   = attachment(attachment_url) if attachment_url.present?

  content + (:div, image, data: { list_remove: '' }, class: 'attachments')
end

#input_templateObject



60
61
62
# File 'app/inputs/action_admin/attachment_input.rb', line 60

def input_template
   :script, attachment, id: "#{input_html_id}-item-template", type: 'text/template'
end

#label(wrapper_options) ⇒ Object



64
65
66
# File 'app/inputs/action_admin/attachment_input.rb', line 64

def label(wrapper_options)
  ''
end