Module: FilepickerioRails::ActionView::FormTagHelper

Defined in:
lib/filepickerio_rails/action_view/form_tag_helper.rb

Overview

This module creates filepicker.io fields

Examples:

filepickerio_upload_tag :user, 'Pick file', 'http://example.com/existing-upload.jpg', data: { "fp-mimetypes": "image/jpg" }

filepickerio_save_button_tag 'Save file to cloud', 'http://example.com/existing-upload.jpg', 'image/jpeg'

Instance Method Summary collapse

Instance Method Details

#fp_file_field(object_name, method, text_or_options, options) ⇒ Object



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
68
69
70
# File 'lib/filepickerio_rails/action_view/form_tag_helper.rb', line 43

def fp_file_field(object_name, method, text_or_options, options)
  if text_or_options.is_a? Hash
    text = nil
    options = text_or_options
  else
    text = text_or_options
  end

  dragdrop = options[:dragdrop] && options[:dragdrop] == true

  input_type = if dragdrop
    options.delete(:dragdrop)
    'filepicker-dragdrop'
  else
    'filepicker'
  end

  options = { 
    type: input_type,
    size: nil,
    data: {
      "fp-apikey" => fp_api_key,
      "fp-button-text" => text || 'Pick File'
    }
  }.deep_merge(options)

  ::ActionView::Helpers::InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag(input_type, options)
end

#fp_file_field_tag(object_name, text_or_options = nil, value_or_options = nil, options = {}) ⇒ Object



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
# File 'lib/filepickerio_rails/action_view/form_tag_helper.rb', line 14

def fp_file_field_tag(object_name, text_or_options=nil, value_or_options=nil, options={})
  # Allow users to pass in variable length arguments
  if text_or_options.is_a? Hash
    # Nothing passed but options
    text = nil
    value = nil
    options = text_or_options
  elsif value_or_options.is_a? Hash
    # only text and options
    text = text_or_options
    value = nil
    options = value_or_options
  else
    # full params
    text = text_or_options
    value = value_or_options
    # options = options
  end

  options.merge!(
    # This avoids the ActiveModel naming of `object_name[method]`
    id: object_name,
    name: object_name,
    value: value
  )

  fp_file_field(object_name, nil, text, options)
end

#fp_save_button(object, method, content = nil, mime = nil, options = {}, &block) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/filepickerio_rails/action_view/form_tag_helper.rb', line 84

def fp_save_button(object, method, content=nil, mime=nil, options={}, &block)
  raise "Mime type of file to be saved must be set" if mime.nil?

  value = options[:value]
  if object && method
    if !value
      value = options.fetch(:value){ ::ActionView::Helpers::InstanceTag::value_before_type_cast(object, method.to_s) }
      value &&= ERB::Util.html_escape(value)
    end
  end

  options = { 
    name: nil,
    type: 'button',
    data: {
      "fp-apikey" => fp_api_key,
      "fp-mimetype" => mime,
      "fp-url" => value
    } 
  }.deep_merge(options)

  # Convert services array into string
  if options[:data]['fp-option-services'].is_a? Array
    options[:data]['fp-option-services'] = options[:data]['fp-option-services'].join(',')
  end

  button_tag(content || 'Save file', options, &block)
end

#fp_save_button_tag(content = nil, url = nil, mime = nil, options = nil, &block) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/filepickerio_rails/action_view/form_tag_helper.rb', line 72

def fp_save_button_tag(content=nil, url=nil, mime=nil, options=nil, &block)
  raise "URL of file to be saved must be set" if url.nil?

  options.deep_merge!({ 
      data: {
        "fp-url" => url
      } 
    })

  fp_save_button(nil, nil, content, mime, options, &block)
end