Module: Binda::FieldableAssociationHelpers

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/binda/fieldable_association_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



63
64
65
66
67
68
69
70
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 63

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

#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



23
24
25
26
27
28
29
30
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 23

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_checkbox_choices(field_slug) ⇒ array

Get the checkbox choice

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (array)

    An array of labels and values of the selected choices. [{ label: '1st label', value: '1st-value'}, { label: '2nd label', value: '2nd-value'}]

Raises:

  • (ArgumentError)


293
294
295
296
297
298
299
300
301
302
303
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 293

def get_checkbox_choices(field_slug)
  field_setting = FieldSetting.find_by(slug:field_slug)
  obj = self.checkboxes.find{ |t| t.field_setting_id == field_setting.id }
  raise ArgumentError, "There isn't any checkbox associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
  raise "There isn't any choice available for the current checkbox (#{field_slug}) on instance (#{self.class.name} ##{self.id})." unless field_setting.choices.any?
  obj_array = []
  obj.choices.order('label').each do |o|
    obj_array << { label: o.label, value: o.value }
  end
  return obj_array
end

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


224
225
226
227
228
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 224

def get_date(field_slug)
  obj = self.dates.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
  raise ArgumentError, "There isn't any date associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
  obj.date
end

#get_dependent_components(field_slug) ⇒ Object

Alias for get_related_components



331
332
333
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 331

def get_dependent_components(field_slug)
  get_related_components(field_slug)
end

#get_image_dimension(field_slug) ⇒ Object

Raises:

  • (ArgumentError)


142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 142

def get_image_dimension(field_slug)
  obj = self.images.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }

  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 medium large].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_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, 'medium' 700x700 max size, 'large' 1400x1400 max size, or blank

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


128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 128

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 medium large].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_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, 'medium' 700x700 max size, 'large' 1400x1400 max size, or blank

Returns:

  • (string)

    The url of the image



116
117
118
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 116

def get_image_path(field_slug, size = '')
  get_image_info( field_slug, size, 'path' )
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, 'medium' 700x700 max size, 'large' 1400x1400 max size, or blank

Returns:

  • (string)

    The url of the image



105
106
107
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 105

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

#get_owner_components(field_slug) ⇒ array

Get all components which owns a relation where the current instance is a dependent

Parameters:

  • field_slug (string)

    The slug of the field setting of the relation

Returns:

  • (array)

    An array of components and/or boards

Raises:

  • (ArgumentError)


339
340
341
342
343
344
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 339

def get_owner_components(field_slug)
  # obj = self.owner_relations.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
  obj = Relation.where(field_setting_id: B.get_field_settings(field_slug)).includes(dependent_relations: :dependent).where(binda_relation_links: {dependent_type: self.class.name})
  raise ArgumentError, "There isn't any relation associated to the current slug (#{field_slug}) where the current instance (#{self.class.name} ##{self.id}) is a dependent.", caller if obj.nil?
  return obj
end

#get_radio_choice(field_slug) ⇒ hash

Get the radio choice

If by mistake the Radio instance has many choices associated, only the first one will be retrieved.

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (hash)

    A hash of containing the label and value of the selected choice. { label: 'the label', value: 'the value'}

Raises:

  • (ArgumentError)


257
258
259
260
261
262
263
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 257

def get_radio_choice(field_slug)
  field_setting = FieldSetting.find_by(slug:field_slug)
  obj = self.radios.find{ |t| t.field_setting_id == field_setting.id }
  raise ArgumentError, "There isn't any radio associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
  raise "There isn't any choice available for the current radio (#{field_slug}) on instance (#{self.class.name} ##{self.id})." unless obj.choices.any?
  return { label: obj.choices.first.label, value: obj.choices.first.value }
end

Get related boards

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (array)

    An array of boards

Raises:

  • (ArgumentError)


360
361
362
363
364
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 360

def get_related_boards(field_slug)
  obj = self.relations.find{ |t| t.field_setting_idid == FieldSetting.get_id( field_slug ) }
  raise ArgumentError, "There isn't any related field associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
  return obj.dependent_relations.map{|relation| relation.dependent}
end

Get related components

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (array)

    An array of components

Raises:

  • (ArgumentError)


324
325
326
327
328
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 324

def get_related_components(field_slug)
  obj = self.relations.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
  raise ArgumentError, "There isn't any related field associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
  return obj.dependent_relations.map{|relation| relation.dependent}
end

#get_repeater(field_slug) ⇒ array

Get the all repeater instances sorted by position

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (array)

    An array of repeater items which have all sorts of fields attached

Raises:

  • (ArgumentError)


244
245
246
247
248
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 244

def get_repeater(field_slug)
  obj = self.repeaters.find_all{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
  raise ArgumentError, "There isn't any repeater associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
  obj.sort_by(&:position)
end

#get_selection_choice(field_slug) ⇒ hash

Get the select choices

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (hash)

    A hash of containing the label and value of the selected choice. { label: 'the label', 'value': 'the value'}

Raises:

  • (ArgumentError)


269
270
271
272
273
274
275
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 269

def get_selection_choice(field_slug)
  field_setting = FieldSetting.find_by(slug:field_slug)
  obj = self.selections.find{ |t| t.field_setting_id == field_setting.id }
  raise ArgumentError, "There isn't any selection associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
  raise "There isn't any choice available for the current selection (#{field_slug}) on instance (#{self.class.name} ##{self.id})." unless field_setting.choices.any?
  return { label: obj.choices.first.label, value: obj.choices.first.value }
end

#get_selection_choices(field_slug) ⇒ array

Get the select choices

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (array)

    An array of hashes of containing label and value of the selected choices. { label: 'the label', 'value': 'the value'}

Raises:

  • (ArgumentError)


281
282
283
284
285
286
287
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 281

def get_selection_choices(field_slug)
  field_setting = FieldSetting.find_by(slug:field_slug)
  obj = self.selections.find{ |t| t.field_setting_id == field_setting.id }
  raise ArgumentError, "There isn't any selection associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
  raise "There isn't any choice available for the current selection (#{field_slug}) on instance (#{self.class.name} ##{self.id})." unless field_setting.choices.any?
  return obj.choices.map{|choice| { label: choice.label, value: choice.value }}
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



52
53
54
55
56
57
58
59
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 52

def get_string(field_slug)
  obj = self.strings.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) && t.type == 'Binda::String' }
  unless obj.nil?
    obj.content
  else
    check_string_error field_slug
  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
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 12

def get_text(field_slug)
  obj = self.texts.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) && t.type != 'Binda::String' }  
  unless obj.nil?
    obj.content
  else
    check_text_error field_slug
  end
end

#get_video_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)


195
196
197
198
199
200
201
202
203
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 195

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

#get_video_path(field_slug) ⇒ string

Get the video 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)

    The size. It can be 'thumb' 200x200 cropped, 'medium' 700x700 max size, 'large' 1400x1400 max size, or blank

Returns:

  • (string)

    The url of the video



185
186
187
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 185

def get_video_path(field_slug)
  get_video_info( field_slug, 'path' )
end

#get_video_url(field_slug) ⇒ string

Get the video 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)

    The size. It can be 'thumb' 200x200 cropped, 'medium' 700x700 max size, 'large' 1400x1400 max size, or blank

Returns:

  • (string)

    The url of the video



174
175
176
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 174

def get_video_url(field_slug)
  get_video_info( field_slug, 'url' )
end

#has_date(field_slug) ⇒ datetime, boolean

Check if the field has an attached date

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (datetime)

    The date

  • (boolean)

    Reutrn false if nothing is found

Raises:

  • (ArgumentError)


210
211
212
213
214
215
216
217
218
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 210

def has_date(field_slug)
  obj = self.dates.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
  raise ArgumentError, "There isn't any date associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
  if obj.present?
    return !obj.date.nil?
  else
    return false
  end
end

#has_dependent_components(field_slug) ⇒ Object

Alias for has_related_components



316
317
318
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 316

def has_dependent_components(field_slug)
  has_related_components(field_slug)
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)


90
91
92
93
94
95
96
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 90

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

Check if has related boards

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (boolean)

Raises:

  • (ArgumentError)


350
351
352
353
354
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 350

def has_related_boards(field_slug)
  obj = self.relations.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
  raise ArgumentError, "There isn't any related field associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
  return obj.dependent_relations.any?
end

Check if has related components

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (boolean)

Raises:

  • (ArgumentError)


309
310
311
312
313
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 309

def has_related_components(field_slug)
  obj = self.relations.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
  raise ArgumentError, "There isn't any related field associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
  return obj.dependent_relations.any?
end

#has_repeater(field_slug) ⇒ boolean

Check if exists any repeater with that slug

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (boolean)

Raises:

  • (ArgumentError)


234
235
236
237
238
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 234

def has_repeater(field_slug)
  obj = self.repeaters.find_all{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) }
  raise ArgumentError, "There isn't any repeater associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
  return obj.present?
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)


76
77
78
79
80
81
82
83
84
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 76

def has_string(field_slug)
  obj = self.strings.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) && t.type == 'Binda::String' }
  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

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


36
37
38
39
40
41
42
43
44
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 36

def has_text(field_slug)
  obj = self.texts.find{ |t| t.field_setting_id == FieldSetting.get_id( field_slug ) && t.type != 'Binda::String' }
  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

#has_video(field_slug) ⇒ boolean

Check if the field has an attached video

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (boolean)

Raises:

  • (ArgumentError)


159
160
161
162
163
164
165
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 159

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