Class: Bureaucrat::Widgets::ClearableFileInput

Inherits:
FileInput show all
Defined in:
lib/bureaucrat/widgets.rb

Constant Summary collapse

FILE_INPUT_CONTRADICTION =
Object.new

Constants included from Utils

Utils::ESCAPES

Instance Attribute Summary

Attributes inherited from Widget

#attrs, #is_required

Instance Method Summary collapse

Methods inherited from FileInput

#has_changed?, #input_type, #needs_multipart?

Methods inherited from Input

#input_type

Methods inherited from Widget

#build_attrs, #has_changed?, #hidden?, id_for_label, #initialize, #initialize_copy, #needs_multipart?

Methods included from Utils

#blank_value?, #conditional_escape, #escape, #flatatt, #format_string, #make_bool, #make_float, #mark_safe, #pretty_name

Constructor Details

This class inherits a constructor from Bureaucrat::Widgets::Widget

Instance Method Details

#clear_checkbox_id(checkbox_name) ⇒ Object



186
187
188
# File 'lib/bureaucrat/widgets.rb', line 186

def clear_checkbox_id(checkbox_name)
  "#{checkbox_name}_id"
end

#clear_checkbox_labelObject



170
171
172
# File 'lib/bureaucrat/widgets.rb', line 170

def clear_checkbox_label
  'Clear'
end

#clear_checkbox_name(name) ⇒ Object



182
183
184
# File 'lib/bureaucrat/widgets.rb', line 182

def clear_checkbox_name(name)
  "#{name}-clear"
end

#initial_textObject



162
163
164
# File 'lib/bureaucrat/widgets.rb', line 162

def initial_text
  'Currently'
end

#input_textObject



166
167
168
# File 'lib/bureaucrat/widgets.rb', line 166

def input_text
  'Change'
end

#render(name, value, attrs = nil) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/bureaucrat/widgets.rb', line 190

def render(name, value, attrs = nil)
  substitutions = {
    initial_text: initial_text,
    input_text: input_text,
    clear_template: '',
    clear_checkbox_label: clear_checkbox_label
  }
  template = '%(input)s'
  substitutions[:input] = super(name, value, attrs)

  if value && value.respond_to?(:url) && value.url
    template = template_with_initial
    substitutions[:initial] = '<a href="%s">%s</a>' % [escape(value.url),
                                                       escape(value.to_s)]
    unless is_required
      checkbox_name = clear_checkbox_name(name)
      checkbox_id = clear_checkbox_id(checkbox_name)
      substitutions[:clear_checkbox_name] = conditional_escape(checkbox_name)
      substitutions[:clear_checkbox_id] = conditional_escape(checkbox_id)
      substitutions[:clear] = CheckboxInput.new.
        render(checkbox_name, false, {id: checkbox_id})
      substitutions[:clear_template] =
        Utils.format_string(template_with_clear, substitutions)
    end
  end

  mark_safe(Utils.format_string(template, substitutions))
end

#template_with_clearObject



178
179
180
# File 'lib/bureaucrat/widgets.rb', line 178

def template_with_clear
  '%(clear)s <label for="%(clear_checkbox_id)s">%(clear_checkbox_label)s</label>'
end

#template_with_initialObject



174
175
176
# File 'lib/bureaucrat/widgets.rb', line 174

def template_with_initial
  '%(initial_text)s: %(initial)s %(clear_template)s<br />%(input_text)s: %(input)s'
end

#value_from_formdata(data, name) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/bureaucrat/widgets.rb', line 219

def value_from_formdata(data, name)
  upload = super(data, name)
  checked = CheckboxInput.new.
    value_from_formdata(data, clear_checkbox_name(name))

  if !is_required && checked
    if upload
      # If the user contradicts themselves (uploads a new file AND
      # checks the "clear" checkbox), we return a unique marker
      # object that FileField will turn into a ValidationError.
      FILE_INPUT_CONTRADICTION
    else
      # False signals to clear any existing value, as opposed to just None
      false
    end
  else
    upload
  end
end