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
24
# 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
             hash.symbolize_keys!
             permitted_params = ActionController::Parameters.new(hash.slice(:public_id, :version, :width, :height, :format, :resource_type)).permit!
             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



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

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'
    upload_info = Cloudinary::Uploader.upload(input, upload_options)
    process_hash upload_info, 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



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

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