Module: Binda::FieldableAssociationHelpers::FieldableAudioHelpers

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

Instance Method Summary collapse

Instance Method Details

#get_audio_info(field_slug, info) ⇒ string, boolean

Get the object related to that field setting

Parameters:

  • field_slug (string)

    The slug of the field setting

  • 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)


40
41
42
43
44
45
46
47
48
# File 'app/models/concerns/binda/fieldable_association_helpers/fieldable_audio_helpers.rb', line 40

def get_audio_info(field_slug, info)
	obj = self.audios.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
	# Alternative query
	# obj = audio.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 audio associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
	if obj.audio.present?
		obj.audio.send(info)
	end
end

#get_audio_path(field_slug) ⇒ string

Get the audio path based on the size provided,

default is Carrierwave default (usually the real size)

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (string)

    The url of the audio



30
31
32
# File 'app/models/concerns/binda/fieldable_association_helpers/fieldable_audio_helpers.rb', line 30

def get_audio_path(field_slug)
	get_audio_info( field_slug, 'path' )
end

#get_audio_url(field_slug) ⇒ string

Get the audio url based on the size provided,

default is Carrierwave default (usually the real size)

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (string)

    The url of the audio



21
22
23
# File 'app/models/concerns/binda/fieldable_association_helpers/fieldable_audio_helpers.rb', line 21

def get_audio_url(field_slug)
	get_audio_info( field_slug, 'url' )
end

#has_audio(field_slug) ⇒ boolean

Check if the field has an attached audio

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (boolean)

Raises:

  • (ArgumentError)


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

def has_audio(field_slug)
	obj = self.audios.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 audio associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
	return obj.audio.present?
end