Module: Binda::FieldableAssociationHelpers::FieldableStringHelpers

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

Instance Method Summary collapse

Instance Method Details

#check_string_error(field_slug) ⇒ Object

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



27
28
29
30
31
32
33
34
# File 'app/models/concerns/binda/fieldable_association_helpers/fieldable_string_helpers.rb', line 27

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

#get_string(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 string

  • (error)

    Raise an error if no record is found



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

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

#has_string(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)


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/concerns/binda/fieldable_association_helpers/fieldable_string_helpers.rb', line 40

def has_string(field_slug)
	obj = Text
		.includes(:field_setting)
		.where(fieldable_id: self.id, fieldable_type: self.class.name)
		.where(binda_field_settings: { slug: field_slug, field_type: "string" })
		.first
	raise ArgumentError, "There isn't any string 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