Module: Binda::FieldableAssociationHelpers::FieldableTextHelpers

Extended by:
ActiveSupport::Concern
Included in:
Binda::FieldableAssociationHelpers
Defined in:
app/models/concerns/binda/fieldable_association_helpers/fieldable_text_helpers.rb

Instance Method Summary collapse

Instance Method Details

#check_text_error(field_slug) ⇒ Object

Check why get_text doesn’t return a value This method isn’t supposed to be used by anything other than get_text method



29
30
31
32
33
34
35
36
# File 'app/models/concerns/binda/fieldable_association_helpers/fieldable_text_helpers.rb', line 29

def check_text_error(field_slug)
	you_mean_string = !self.strings.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) && t.type == 'Binda::String' }.nil?
	if you_mean_string
		raise ArgumentError, "This slug (#{field_slug}) is associated to a string not a text. Use get_string() instead on instance (#{self.class.name} ##{self.id}).", caller
	else
		raise ArgumentError, "There isn't any text associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller
	end
end

#get_text(field_slug) ⇒ string, error

Get the object related to that field setting If the object doesn’t exists yet it will return nil

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (string)

    Returns the content of the text

  • (error)

    Raise an error if no record is found



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/concerns/binda/fieldable_association_helpers/fieldable_text_helpers.rb', line 12

def get_text(field_slug)
	obj = Text
		.includes(:field_setting)
		.where(fieldable_id: self.id, fieldable_type: self.class.name)
		.where(binda_field_settings: { slug: field_slug })
		.where.not(binda_field_settings: { field_type: "string" })
		.first
	unless obj.nil?
		# to_s ensures the returned object is class String
		obj.content.to_s
	else
		check_text_error field_slug
	end
end

#has_text(field_slug) ⇒ boolean

Get the object related to that field setting

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (boolean)

Raises:

  • (ArgumentError)


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

def has_text(field_slug)
	obj = Text
		.includes(:field_setting)
		.where(fieldable_id: self.id, fieldable_type: self.class.name)
		.where(binda_field_settings: { slug: field_slug })
		.where.not(binda_field_settings: { field_type: "string" })
		.first
	raise ArgumentError, "There isn't any text associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
	if obj.present?
		return !obj.content.nil?
	else
		return false
	end
end