Module: HasFilepickerImage::FormBuilderHelper

Defined in:
lib/has_filepicker_image/helpers/form_builder_helper.rb

Instance Method Summary collapse

Instance Method Details

#filepicker_field(attribute_name, *args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/has_filepicker_image/helpers/form_builder_helper.rb', line 7

def filepicker_field(attribute_name, *args)
  config = Rails.application.config.has_filepicker_image
  config_name = nil
  opts = {}

  case args.size
  when 0
  when 1
    if args[0].is_a?(Hash)
      opts = args[0]
    else
      config_name = args[0]
    end
  when 2
    config_name, opts = args
  else
    raise ArgumentError.new('Wrong number of arguments for filepicker_field')
  end

  value = object.send(attribute_name)
  options = config.get_config(config_name).deep_merge(opts)
  html_options = options[:html_options] || {}

  preview = @template.(:div, :class => 'filepicker-image', :style => (value.present? ? '' : 'display:none;')) do
    if value.present?
      # Render preview + Delete link
      thumb_url = value + "/convert?w=260&h=180"
      thumb_alt = "#{attribute_name} thumbnail"
      @template.image_tag(thumb_url, alt: thumb_alt )
    end
  end

  pick_button = @template.(:a,
    options[:pick_button_html],
    html_options.merge(
      :href  => '#',
      :style => value.present? ? 'display:none;' : '',
      :'data-action' => 'pickImage'
    )
  )

  remove_button = @template.link_to(
    options[:delete_button_html],
    '#',
    :style => value.present? ? '' : 'display:none;',
    :'data-action' => 'removeImage'
  )

  buttons = @template.(
    :div,
    pick_button + remove_button,
    :class => 'filepicker-button'
  )

  buttons + preview + ActionView::Helpers::InstanceTag.new(
    @object_name,
    attribute_name,
    @template,
    object
  ).to_input_field_tag('hidden')
end

#filepicker_image_field(attribute_name, *args) ⇒ Object



3
4
5
# File 'lib/has_filepicker_image/helpers/form_builder_helper.rb', line 3

def filepicker_image_field(attribute_name, *args)
  filepicker_field(attribute_name, *args)
end