module SimpleFormAttachments


Constants

VERSION

Public Class Methods

dom_class(*args) click to toggle source
# File lib/simple_form_attachments.rb, line 19
def self.dom_class(*args)
  prefix, alts = args.partition { |i| !i.is_a?(Array) }
  prefix = ['simple_form_attachments'] + prefix
  return prefix.compact.join('__') if alts.empty?
  alts.flatten.map do |item|
    prefix += [item]
    prefix.compact.join('__')
  end
end
jquery_ui_asset(asset) click to toggle source
# File lib/simple_form_attachments.rb, line 29
def self.jquery_ui_asset(asset)
  jquery_ui_version = Gem::Version.new(Jquery::Ui::Rails::JQUERY_UI_VERSION)
  case
  when Gem::Dependency.new('', '< 1.12').match?('', jquery_ui_version) then "jquery-ui/#{asset}"
  when Gem::Dependency.new('', '>= 1.12').match?('', jquery_ui_version) then "jquery-ui/widgets/#{asset}"
  else raise "This version of jQuery UI is not supported"
  end
end

Public Instance Methods

accepted_file_types() click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 128
def accepted_file_types
  return unless validators
  [
    validated_extensions,
    validated_formats,
    validated_mime_types
  ].reject(&:blank?).flatten.join(',')
end
attachment_blank_field() click to toggle source

# File lib/simple_form_attachments/attachment_input.rb, line 177
def attachment_blank_field
  if multiple?
    template.hidden_field_tag("#{parent_name}[#{relation_key}][]", nil)
  else
    template.hidden_field_tag("#{parent_name}[#{relation_key}]", nil)
  end
end
attachment_file_field() click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 185
def attachment_file_field
  input_html_options = {
    multiple: multiple?,
    accept: accepted_file_types,
    class: 'file'
  }
  template.label_tag('attachment[file]') do
    template.file_field_tag('attachment[file]', input_html_options) +
    template.content_tag(:span, I18n.t(:choose_file, scope: 'simple_form_attachments.links', count: (multiple? ? 2 : 1)), class: SimpleFormAttachments.dom_class(:label, [:choose_file]))
  end
end
attachment_list() click to toggle source

# File lib/simple_form_attachments/attachment_input.rb, line 231
def attachment_list
  container_classes = [SimpleFormAttachments.dom_class(:attachment_list)]
  container_classes << SimpleFormAttachments.dom_class(:attachment_list, :is_sortable) if sortable?
  template.content_tag :div, class: container_classes do
    @builder.simple_fields_for attribute_name do |attachment_fields|
      template.render(
        partial: attachment_fields.object.to_simple_form_partial_path, format: :html, layout: File.join('layouts', 'simple_form_attachments', 'attachment_layout'),
        locals: {
          attachment: attachment_fields.object,
          fields: attachment_fields,
          multiple: multiple?,
          form: @builder,
          relation_key: relation_key
        }
      )
    end
  end
end
attachment_multiple_field() click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 201
def attachment_multiple_field
  template.hidden_field_tag('attachment[multiple]', multiple?.to_s)
end
attachment_parent_class_field() click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 205
def attachment_parent_class_field
  template.hidden_field_tag('attachment_parent[class]', parent_class)
end
attachment_parent_name_field() click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 209
def attachment_parent_name_field
  template.hidden_field_tag('attachment_parent[name]', parent_name)
end
attachment_relation_class_field() click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 217
def attachment_relation_class_field
  template.hidden_field_tag('attachment_relation[type]', relation_class.to_s)
end
attachment_relation_key_field() click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 225
def attachment_relation_key_field
  template.hidden_field_tag('attachment_relation[key]', relation_key.to_s)
end
attachment_relation_name_field() click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 213
def attachment_relation_name_field
  template.hidden_field_tag('attachment_relation[name]', @attribute_name)
end
attachment_relation_referenced_field() click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 221
def attachment_relation_referenced_field
  template.hidden_field_tag('attachment_relation[referenced]', referenced?)
end
attachment_type_field() click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 197
def attachment_type_field
  template.hidden_field_tag('attachment_type', relation_class_name)
end
attachments() click to toggle source

# File lib/simple_form_attachments/attachment_input.rb, line 123
def attachments
  return @builder.object.send(attribute_name).to_a unless multiple?
  @builder.object.send(attribute_name).sort_by { |a| @builder.object.send(relation_key).index(a.id) }
end
embedded?() click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 112
def embedded?
  return false unless relation
  !!relation_class.to_s.end_with?('Embedded')
end
file_validation_for(property) click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 166
def file_validation_for(property)
  return unless validators
  validators.select { |v| v.options[:property_name].to_s == property.to_s }
end
file_validation_values_for(property) click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 160
def file_validation_values_for(property)
  return unless file_validation_for(property)
  validation = file_validation_for(property)
  validation.map { |v| v.options.values_at(:as, :in) }.flatten.reject(&:blank?).uniq
end
handlebars_js_template() click to toggle source

# File lib/simple_form_attachments/attachment_input.rb, line 252
def handlebars_js_template
  template.content_tag :script, id: 'simple_form_attachments__template', type: 'text/html' do
    template.render(File.join('simple_form_attachments', 'attachments', 'attachment.hbs.slim'))
  end
end
input_html_classes() click to toggle source
Calls superclass method
# File lib/simple_form_attachments/attachment_input.rb, line 50
def input_html_classes
  super.push(SimpleFormAttachments.dom_class)
end
multiple?() click to toggle source

# File lib/simple_form_attachments/attachment_input.rb, line 102
def multiple?
  return false unless relation
  !!relation.macro.to_s.end_with?('many')
end
param_key() click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 82
def param_key
  @builder.object_name
end
parent_class() click to toggle source

FIXME: this should be inferred also from the :as options!!

# File lib/simple_form_attachments/attachment_input.rb, line 87
def parent_class
  return @builder.object.model.class.name if @builder.object.class.name.to_s =~ /decorator\z/i
  @builder.object.class.name
end
parent_name() click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 92
def parent_name
  @builder.object_name
end
referenced?() click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 107
def referenced?
  return false unless relation
  !!relation_class.to_s.end_with?('Referenced')
end
relation() click to toggle source

# File lib/simple_form_attachments/attachment_input.rb, line 63
def relation
  return unless @builder
  return unless @builder.object
  return unless @builder.object.relations
  relation = @builder.object.relations[attribute_name.to_s]
  raise "Relation #{attribute_name} not found" unless relation
  relation
end
relation_class() click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 96
def relation_class
  relation.relation.to_s
end
relation_class_name() click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 77
def relation_class_name
  return unless relation
  relation.class_name
end
relation_key() click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 72
def relation_key
  return unless relation
  relation.key
end
route_from_configuration() click to toggle source

# File lib/simple_form_attachments/attachment_input.rb, line 56
def route_from_configuration
  res = AttachmentInput.configuration.route
  res.respond_to?(:call) ? res.call : res
end
sortable?() click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 117
def sortable?
  options.fetch(:sortable, multiple?)
end
validated_extensions() click to toggle source

# File lib/simple_form_attachments/attachment_input.rb, line 139
def validated_extensions
  return unless validators
  return unless file_validation_values_for(:ext)
  extensions = file_validation_values_for(:ext)
  extensions.map { |e| ".#{e}" }
end
validated_formats() click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 146
def validated_formats
  return unless validators
  return unless file_validation_values_for(:format)
  formats = file_validation_values_for(:format)
  formats.map { |f| ".#{f}" }
end
validated_mime_types() click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 153
def validated_mime_types
  return unless validators
  return unless file_validation_values_for(:mime_type)
  mime_types = file_validation_values_for(:mime_type)
  mime_types
end
validators(field = :file) click to toggle source
# File lib/simple_form_attachments/attachment_input.rb, line 171
def validators(field = :file)
  relation_class_name.constantize.validators_on(field)
end