Module: ActsAsHocAvatarable

Extended by:
ActiveSupport::Concern
Defined in:
lib/acts_as_hoc_avatarable.rb,
lib/acts_as_hoc_avatarable/version.rb,
lib/acts_as_hoc_avatarable/configuration.rb,
lib/acts_as_hoc_avatarable/acts_as_hoc_avatarable.rb,
lib/generators/acts_as_hoc_avatarable/install_generator.rb

Defined Under Namespace

Modules: ClassMethods, Generators Classes: Configuration

Constant Summary collapse

LOCK =
Mutex.new
VERSION =
"0.1.13"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configurationObject



17
18
19
20
# File 'lib/acts_as_hoc_avatarable.rb', line 17

def configuration
  @configuration = nil unless defined?(@configuration)
  @configuration || LOCK.synchronize { @configuration ||= ActsAsHocAvatarable::Configuration.new }
end

.configure(config_hash = nil) {|configuration| ... } ⇒ Object

Yields:



7
8
9
10
11
12
13
14
15
# File 'lib/acts_as_hoc_avatarable.rb', line 7

def configure(config_hash=nil)
  if config_hash
    config_hash.each do |k,v|
      configuration.send("#{k}=", v) rescue nil if configuration.respond_to?("#{k}=")
    end
  end

  yield(configuration) if block_given?
end

Instance Method Details

#avatar_urlObject



18
19
20
21
22
23
24
25
# File 'lib/acts_as_hoc_avatarable/acts_as_hoc_avatarable.rb', line 18

def avatar_url
  unless persisted?
    return nil
  end
  return Rails.application.routes.url_helpers.rails_blob_url(avatar, host: ActsAsHocAvatarable.configuration.default_host) if avatar.attached?
  return "https://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(email ||= '')}" if ActsAsHocAvatarable.configuration.fallback_to_gravatar
  return nil
end

#clear_avatarObject



27
28
29
30
31
32
# File 'lib/acts_as_hoc_avatarable/acts_as_hoc_avatarable.rb', line 27

def clear_avatar
  if remove_avatar == '1'
    @avatar_contents = nil
    avatar.purge_later
  end
end

#parse_avatarObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/acts_as_hoc_avatarable/acts_as_hoc_avatarable.rb', line 34

def parse_avatar
  # If directly uploaded
  unless avatar_contents.nil? || avatar_contents[/(image\/[a-z]{3,4})/] == ''
    content_type = avatar_contents[/(image\/[a-z]{3,4})/]
    content_type = content_type[/\b(?!.*\/).*/]
    contents = avatar_contents.sub /data:((image)\/.{3,}),/, ''
    decoded_data = Base64.decode64(contents)
    mini_magick = MiniMagick::Image.read(decoded_data)
    if ActsAsHocAvatarable.configuration.resize_on_create
      mini_magick.resize (ActsAsHocAvatarable.configuration.resize_size)
    end
    decoded_data = mini_magick.to_blob
    filename = avatar_name || "avatar_#{Time.zone.now.to_i}.#{content_type}"
    File.open("#{Rails.root}/tmp/storage/#{filename}", 'wb') do |f|
      f.write(decoded_data)
    end
    avatar.attach(io: File.open("#{Rails.root}/tmp/storage/#{filename}"), filename: filename)
    FileUtils.rm("#{Rails.root}/tmp/images/#{filename}")
  end
end