Class: AbAdmin::Views::Inputs::UploaderInput

Inherits:
SimpleForm::Inputs::Base
  • Object
show all
Defined in:
lib/ab_admin/views/inputs/uploader_input.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ UploaderInput


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ab_admin/views/inputs/uploader_input.rb', line 7

def initialize(*args, &block)
  super
  @assoc = object.class.reflect_on_association(attribute_name)
  defaults = {
      file_type: 'image',
      container_id: "#{attribute_name}_#{object.fileupload_guid}",
      multiple: @assoc.collection?,
      max_size: @assoc.klass.try(:max_size),
      error: @builder.error(attribute_name)
  }
  @options.reverse_merge!(defaults)
  @options[:extensions] = @assoc.klass.ext_list if @assoc.klass.ext_list
  @options[:sortable] = @options[:multiple] unless @options.has_key?(:sortable)
  @options[:asset_template] ||= @options[:file_type]
  @options[:container_class] = container_class
end

Instance Method Details

#container_classObject


45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ab_admin/views/inputs/uploader_input.rb', line 45

def container_class
  classes = Array(@options[:container_class])
  classes << (@options[:multiple] ? 'fileupload-multiple' : 'fileupload-single')
  classes << "fileupload-klass-#{@assoc.klass.name}"
  classes << "fileupload-record-#{object.fileupload_guid}"
  classes << "fileupload-theme-#{@options[:theme]}" if @options[:theme]
  classes << "fileupload-file_type-#{@options[:file_type]}"
  classes << "fileupload-asset_template-#{@options[:asset_template]}"
  classes << 'fileupload-sortable' if @options[:sortable]
  classes << 'error' if @options[:error]
  classes
end

#input(wrapper_options = nil) ⇒ Object


24
25
26
27
28
29
30
31
32
33
# File 'lib/ab_admin/views/inputs/uploader_input.rb', line 24

def input(wrapper_options=nil)
  title = options[:title] || object.class.han(attribute_name)
  template.capture do
    if @options[:unwrapped]
      render_input
    else
      template.input_set(title) { render_input }
    end
  end
end

#js_optionsObject


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ab_admin/views/inputs/uploader_input.rb', line 71

def js_options
  {
    container_id: @options[:container_id],
    file_type: @options[:file_type],
    asset_template: @options[:asset_template],
    multiple: @options[:multiple],
    sort_url: template.sort_admin_assets_path(klass: @assoc.klass.name),
    extensions: @options[:extensions],
    klass: @assoc.klass.name,
    sortable: @options[:sortable],
    edit_meta: @options[:edit_meta],
    crop: @options[:crop],
    disabled: @options[:disabled],
    fileupload: {
      url: AbAdmin.fileupload_url,
      maxNumberOfFiles: @options[:max_files],
      maxFileSize: @options[:max_size].try(:megabytes),
      minFileSize: @options[:min_size].try(:megabytes),
      formData: {
        assetable_type: object.class.name,
        assetable_id: object.id,
        method: attribute_name,
        guid: object.fileupload_guid
      }
    }
  }
end

#render_inputObject


35
36
37
38
39
40
41
42
43
# File 'lib/ab_admin/views/inputs/uploader_input.rb', line 35

def render_input
  locals = {
      options: @options,
      js_options: js_options,
      assets: uploader_values,
      asset_template: "#{theme_path}/#{@options[:asset_template]}"
  }
  template.render(partial: "#{theme_path}/container", locals: locals)
end

#theme_pathObject


58
59
60
# File 'lib/ab_admin/views/inputs/uploader_input.rb', line 58

def theme_path
  [@options[:views_path] || 'admin/fileupload', @options[:theme]].compact.join('/')
end

#uploader_valuesObject


62
63
64
65
66
67
68
69
# File 'lib/ab_admin/views/inputs/uploader_input.rb', line 62

def uploader_values
  if options.key?(:value)
    values = options.delete(:value)
  else
    values = object.fileupload_asset(attribute_name)
  end
  Array(values).delete_if { |v| v.nil? || v.new_record? }
end