Module: Attachinary::Utils

Defined in:
lib/attachinary/utils.rb

Class Method Summary collapse

Class Method Details

.process_hash(hash, scope = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/attachinary/utils.rb', line 10

def self.process_hash(hash, scope=nil)
  if hash['id']
    Attachinary::File.find hash['id']
  else
    file = if Rails::VERSION::MAJOR == 3
      Attachinary::File.new hash.slice(*Attachinary::File.attr_accessible[:default].to_a)
    else
      permitted_params = ActionController::Parameters.new(hash).permit(:public_id, :version, :width, :height, :format, :resource_type)
      Attachinary::File.new(permitted_params)
    end
    file.scope = scope.to_s if scope && file.respond_to?(:scope=)
    file
  end
end

.process_input(input, upload_options, scope = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/attachinary/utils.rb', line 26

def self.process_input(input, upload_options, scope=nil)
  case input
  when :blank?.to_proc
    nil
  when lambda { |e| e.respond_to?(:read) }
    upload_options.merge! resource_type: 'auto'
    process_hash Cloudinary::Uploader.upload(input, upload_options), scope
  when String
    process_json(input, scope)
  when Hash
    process_hash(input, scope)
  when Array
    input = input.map{ |el| process_input(el, upload_options, scope) }.flatten.compact
    input = nil if input.empty?
    input
  else
    input
  end
end

.process_json(json, scope = nil) ⇒ Object



4
5
6
7
8
# File 'lib/attachinary/utils.rb', line 4

def self.process_json(json, scope=nil)
  [JSON.parse(json)].flatten.compact.map do |data|
    process_hash(data, scope)
  end
end

.process_options(options) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/attachinary/utils.rb', line 46

def self.process_options(options)
  options = options.reverse_merge({
    accessible: true
  })
  options[:maximum] = 1 if options[:single]

  if options[:single]
    options[:singular] = options[:scope].to_s
    options[:plural] = options[:scope].to_s.pluralize
  else
    options[:plural] = options[:scope].to_s
    options[:singular] = options[:scope].to_s.singularize
  end

  options
end