Module: Binda::FieldableAssociationHelpers::FieldableImageHelpers

Included in:
Binda::FieldableAssociationHelpers
Defined in:
app/models/concerns/binda/fieldable_association_helpers/fieldable_image_helpers.rb

Instance Method Summary collapse

Instance Method Details

#bytes_to_megabytes(bytes) ⇒ Object

Convert bytes to megabites



86
87
88
# File 'app/models/concerns/binda/fieldable_association_helpers/fieldable_image_helpers.rb', line 86

def bytes_to_megabytes bytes
  (bytes.to_f / 1.megabyte).round(2)
end

#get_image_dimension(field_slug) ⇒ hash

Get image dimension

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (hash)

    with width and height as floats



80
81
82
83
# File 'app/models/concerns/binda/fieldable_association_helpers/fieldable_image_helpers.rb', line 80

def get_image_dimension(field_slug)
	obj = self.images.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
   	return { width: obj.file_width, height: obj.file_height }
end

#get_image_info(field_slug, size, info) ⇒ string, boolean

Get the object related to that field setting

Parameters:

  • field_slug (string)

    The slug of the field setting

  • size (string)

    The size. It can be ‘thumb’ 200x200 cropped.

  • info (string)

    String of the info to be retrieved

Returns:

  • (string)

    The info requested if present

  • (boolean)

    Returns false if no info is found or if image isn’t found

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/concerns/binda/fieldable_association_helpers/fieldable_image_helpers.rb', line 44

def get_image_info(field_slug, size, info)
	obj = self.images.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
	# Alternative query
	# obj = Image.where(field_setting_id: FieldSetting.get_id( field_slug ), fieldable_id: self.id, fieldable_type: self.class.to_s ).first
	raise ArgumentError, "There isn't any image associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
	if obj.image.present? && obj.image.respond_to?(size) && %w[thumb].include?(size)
			obj.image.send(size).send(info)
	elsif obj.image.present?
		obj.image.send(info)
	else
		raise "Looks like the image you are looking for isn't present. See field setting with slug=\"#{field_slug}\" on component with id=\"self.id\""
	end
end

#get_image_mime_type(field_slug) ⇒ string

Get image type

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (string)

    with image type



71
72
73
74
# File 'app/models/concerns/binda/fieldable_association_helpers/fieldable_image_helpers.rb', line 71

def get_image_mime_type(field_slug)
	obj = self.images.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
   	return obj.content_type
end

#get_image_path(field_slug, size = '') ⇒ string

Get the image path based on the size provided,

default is Carrierwave default (usually the real size)

Parameters:

  • field_slug (string)

    The slug of the field setting

  • size (string) (defaults to: '')

    The size. It can be ‘thumb’ 200x200 cropped.

Returns:

  • (string)

    The url of the image



33
34
35
# File 'app/models/concerns/binda/fieldable_association_helpers/fieldable_image_helpers.rb', line 33

def get_image_path(field_slug, size = '')
	get_image_info( field_slug, size, 'path' )
end

#get_image_size(field_slug) ⇒ float

Get image size

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (float)

    with image type



62
63
64
65
# File 'app/models/concerns/binda/fieldable_association_helpers/fieldable_image_helpers.rb', line 62

def get_image_size(field_slug)
	obj = self.images.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
   	return bytes_to_megabytes(obj.file_size)
end

#get_image_url(field_slug, size = '') ⇒ string

Get the image url based on the size provided,

default is Carrierwave default (usually the real size)

Parameters:

  • field_slug (string)

    The slug of the field setting

  • size (string) (defaults to: '')

    The size. It can be ‘thumb’ 200x200 cropped.

Returns:

  • (string)

    The url of the image



23
24
25
# File 'app/models/concerns/binda/fieldable_association_helpers/fieldable_image_helpers.rb', line 23

def get_image_url(field_slug, size = '')
	get_image_info( field_slug, size, 'url' )
end

#has_image(field_slug) ⇒ boolean

Check if the field has an attached image

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (boolean)

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
# File 'app/models/concerns/binda/fieldable_association_helpers/fieldable_image_helpers.rb', line 9

def has_image(field_slug)
	obj = self.images.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
	# Alternative query
	# obj = Image.where(field_setting_id: FieldSetting.get_id( field_slug ), fieldable_id: self.id, fieldable_type: self.class.to_s ).first
	raise ArgumentError, "There isn't any image associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
	return obj.image.present?
end