Class: Locomotive::FileInput

Inherits:
SimpleForm::Inputs::FileInput
  • Object
show all
Extended by:
Forwardable
Includes:
SimpleForm::BootstrapHelpers
Defined in:
app/inputs/locomotive/file_input.rb

Instance Method Summary collapse

Methods included from SimpleForm::BootstrapHelpers

#col_wrapping, #row_wrapping

Instance Method Details

#button_html(name, dropdown = true) ⇒ Object


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/inputs/locomotive/file_input.rb', line 37

def button_html(name, dropdown = true)
  if dropdown
    (:div,
      (:button,
        (text(name) + ' ' + (:span, '', class: 'caret')).html_safe,
        class: 'btn btn-primary btn-sm dropdown-toggle', data: { toggle: 'dropdown', aria_expanded: false }) +
      (:ul,
        (:li, (:a, text(:select_local_file), href: '#', class: "local-file #{name}")) +
        (:li, (:a, text(:select_content_asset), href: template.content_assets_path(template.current_site), class: 'content-assets')),
        class: 'dropdown-menu dropdown-menu-right', role: 'menu'),
      class: "btn-group #{name} #{hidden_css(name)}")
  else
    template.link_to(text(name), '#', class: "#{name} btn btn-primary btn-sm #{hidden_css(name)}")
  end
end

#buttons_htmlObject


28
29
30
31
32
33
34
35
# File 'app/inputs/locomotive/file_input.rb', line 28

def buttons_html
  col_wrapping :buttons, 4 do
    button_html(:choose, options[:select_content_asset]) +
    button_html(:change, options[:select_content_asset]) +
    button_html(:cancel, false) +
    template.link_to(trash_icon, '#', class: "delete #{hidden_css(:delete)}")
  end
end

#file_htmlObject


16
17
18
19
20
21
22
23
24
25
26
# File 'app/inputs/locomotive/file_input.rb', line 16

def file_html
  col_wrapping :file, 8 do
    no_file_html +
    new_file_html +
    filename_or_image +
    @builder.file_field(attribute_name, input_html_options) +
    @builder.hidden_field(:"remove_#{attribute_name}", class: 'remove', value: '0') +
    @builder.hidden_field(:"remote_#{attribute_name}_url", class: 'remote-url', value: '') +
    hidden_fields
  end
end

#filename_htmlObject


89
90
91
# File 'app/inputs/locomotive/file_input.rb', line 89

def filename_html
  template.link_to(File.basename(persisted_file.to_s), persisted_file.url)
end

#filename_or_imageObject


57
58
59
60
61
62
63
64
# File 'app/inputs/locomotive/file_input.rb', line 57

def filename_or_image
  if object.persisted? && persisted_file?
    css = "current-file #{persisted_file.image? ? 'image' : ''}"
    template. :span, (image_html + filename_html).html_safe, class: css
  else
    ''
  end
end

#hidden_css(name) ⇒ Object


101
102
103
104
105
106
107
108
109
# File 'app/inputs/locomotive/file_input.rb', line 101

def hidden_css(name)
  displayed = case name
  when :choose, :no_file then !object.persisted? || !persisted_file?
  when :change, :delete then object.persisted? && persisted_file?
  else false
  end

  displayed ? '' : 'hide'
end

#hidden_fieldsObject


83
84
85
86
87
# File 'app/inputs/locomotive/file_input.rb', line 83

def hidden_fields
  (options[:hidden_fields] || []).map do |name|
    @builder.hidden_field(name)
  end.join.html_safe
end

#image_htmlObject


74
75
76
77
78
79
80
81
# File 'app/inputs/locomotive/file_input.rb', line 74

def image_html
  if object.persisted? && persisted_file.image?
    url = Locomotive::Dragonfly.resize_url persisted_file.url, '60x60#'
    template.image_tag(url)
  else
    ''
  end
end

#input(wrapper_options = nil) ⇒ Object


10
11
12
13
14
# File 'app/inputs/locomotive/file_input.rb', line 10

def input(wrapper_options = nil)
  row_wrapping(data: { persisted: persisted_file?, persisted_file: persisted_file?, resize_format: options[:resize_format] }) do
    file_html + buttons_html
  end
end

#new_file_htmlObject


70
71
72
# File 'app/inputs/locomotive/file_input.rb', line 70

def new_file_html
  template. :span, 'New file here', class: "new-file #{hidden_css(:new_file)}"
end

#no_file_htmlObject


66
67
68
# File 'app/inputs/locomotive/file_input.rb', line 66

def no_file_html
  template. :span, text(:none), class: "no-file #{hidden_css(:no_file)}"
end

#persisted_fileObject


97
98
99
# File 'app/inputs/locomotive/file_input.rb', line 97

def persisted_file
  self.object.send(attribute_name.to_sym)
end

#persisted_file?Boolean

Returns:


93
94
95
# File 'app/inputs/locomotive/file_input.rb', line 93

def persisted_file?
  self.object.send(:"#{attribute_name}?")
end

#text(name) ⇒ Object


111
112
113
# File 'app/inputs/locomotive/file_input.rb', line 111

def text(name)
  I18n.t(name, scope: 'locomotive.inputs.file')
end

#trash_iconObject


53
54
55
# File 'app/inputs/locomotive/file_input.rb', line 53

def trash_icon
  template.(:i, '', class: 'far fa-trash-alt')
end