Class: Locomotive::ImageInput

Inherits:
SimpleForm::Inputs::StringInput
  • Object
show all
Extended by:
Forwardable
Defined in:
app/inputs/locomotive/image_input.rb

Direct Known Subclasses

SimpleImageInput

Instance Method Summary collapse

Instance Method Details

#actionsObject



42
43
44
45
46
47
48
49
50
# File 'app/inputs/locomotive/image_input.rb', line 42

def actions
  <<-EOF
  <i class="fa fa-trash" data-action="delete" style="#{'display: none;' unless file?}"></i>
  <i class="fa fa-undo" data-action="undo" style="display: none;"></i>
  <span class="spinner" style="display: none;">
    <i class="fa fa-circle-o-notch fa-spin" ></i>
  </span>
  EOF
end

#browse_button_htmlObject



35
36
37
38
39
40
# File 'app/inputs/locomotive/image_input.rb', line 35

def browse_button_html
  label, url = text(:browse), template.content_assets_path(template.current_site)
  <<-EOF
  <a class="file-browse" href="#{url}">#{label}</a>
  EOF
end

#default_urlObject



78
79
80
# File 'app/inputs/locomotive/image_input.rb', line 78

def default_url
  options[:default_url] || template.asset_path('locomotive/blank.png')
end

#file?Boolean

Returns:



64
65
66
# File 'app/inputs/locomotive/image_input.rb', line 64

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

#file_inputObject



26
27
28
# File 'app/inputs/locomotive/image_input.rb', line 26

def file_input
  @builder.text_field(attribute_name)
end

#file_urlObject



68
69
70
# File 'app/inputs/locomotive/image_input.rb', line 68

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

#file_wrapper_optionsObject



82
83
84
85
86
87
88
89
90
# File 'app/inputs/locomotive/image_input.rb', line 82

def file_wrapper_options
  {
    resize:         resize_format,
    persisted:      file?,
    url:            resized_file_url,
    default_url:    default_url,
    no_file_label:  text(:none)
  }.map { |k, value| "data-#{k.to_s.dasherize}=\"#{value}\"" }.join(' ')
end

#filename_htmlObject



56
57
58
59
60
61
62
# File 'app/inputs/locomotive/image_input.rb', line 56

def filename_html
  if file?
    template.link_to(File.basename(file_url), file_url, target: 'blank')
  else
    text(:none)
  end
end

#image_htmlObject



30
31
32
33
# File 'app/inputs/locomotive/image_input.rb', line 30

def image_html
  url = resized_file_url || default_url
  template.image_tag(url)
end

#input(wrapper_options = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/inputs/locomotive/image_input.rb', line 8

def input(wrapper_options = nil)
  <<-EOF
<div class="file-wrapper" #{file_wrapper_options}>
  <div class="file-image">
#{image_html}
#{actions}
  </div>
  <label class="file">
#{file_input}
<span class="file-custom">
  <span class="file-name">#{filename_html}</span>
  #{browse_button_html}
</span>
  </label>
</div>
  EOF
end

#resize_formatObject



52
53
54
# File 'app/inputs/locomotive/image_input.rb', line 52

def resize_format
  options[:resize] || '96x96#'
end

#resized_file_urlObject



72
73
74
75
76
# File 'app/inputs/locomotive/image_input.rb', line 72

def resized_file_url
  if file?
    Locomotive::Dragonfly.resize_url file_url, resize_format
  end
end

#text(name) ⇒ Object



92
93
94
# File 'app/inputs/locomotive/image_input.rb', line 92

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