Module: Binda::FieldableAssociationHelpers
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/concerns/binda/fieldable_association_helpers.rb
Instance Method Summary collapse
-
#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.
-
#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.
-
#get_checkbox_choices(field_slug) ⇒ array
Get the checkbox choice.
-
#get_date(field_slug) ⇒ boolean
Get the object related to that field setting.
-
#get_dependent_components(field_slug) ⇒ Object
Alias for get_related_components.
- #get_image_dimension(field_slug) ⇒ Object
-
#get_image_info(field_slug, size, info) ⇒ string, boolean
Get the object related to that field setting.
-
#get_image_path(field_slug, size = '') ⇒ string
Get the image path based on the size provided, default is Carrierwave default (usually the real size).
-
#get_image_url(field_slug, size = '') ⇒ string
Get the image url based on the size provided, default is Carrierwave default (usually the real size).
-
#get_owner_components(field_slug) ⇒ array
Get all components which owns a relation where the current instance is a dependent.
-
#get_radio_choice(field_slug) ⇒ hash
Get the radio choice.
-
#get_related_boards(field_slug) ⇒ array
Get related boards.
-
#get_related_components(field_slug) ⇒ array
Get related components.
-
#get_repeater(field_slug) ⇒ array
Get the all repeater instances sorted by position.
-
#get_selection_choice(field_slug) ⇒ hash
Get the select choices.
-
#get_selection_choices(field_slug) ⇒ array
Get the select choices.
-
#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.
-
#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.
-
#get_video_info(field_slug, info) ⇒ string, boolean
Get the object related to that field setting.
-
#get_video_path(field_slug) ⇒ string
Get the video path based on the size provided, default is Carrierwave default (usually the real size).
-
#get_video_url(field_slug) ⇒ string
Get the video url based on the size provided, default is Carrierwave default (usually the real size).
-
#has_date(field_slug) ⇒ datetime, boolean
Check if the field has an attached date.
-
#has_dependent_components(field_slug) ⇒ Object
Alias for has_related_components.
-
#has_image(field_slug) ⇒ boolean
Check if the field has an attached image.
-
#has_related_boards(field_slug) ⇒ boolean
Check if has related boards.
-
#has_related_components(field_slug) ⇒ boolean
Check if has related components.
-
#has_repeater(field_slug) ⇒ boolean
Check if exists any repeater with that slug.
-
#has_string(field_slug) ⇒ boolean
Get the object related to that field setting.
-
#has_text(field_slug) ⇒ boolean
Get the object related to that field setting.
-
#has_video(field_slug) ⇒ boolean
Check if the field has an attached video.
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
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
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) (field_slug) end |
#get_image_dimension(field_slug) ⇒ Object
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
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)
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)
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
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.
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(field_slug) ⇒ array
Get related boards
360 361 362 363 364 |
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 360 def (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(field_slug) ⇒ array
Get related components
324 325 326 327 328 |
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 324 def (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
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
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
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
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
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
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)
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)
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
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) (field_slug) end |
#has_image(field_slug) ⇒ boolean
Check if the field has an attached image
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 |
#has_related_boards(field_slug) ⇒ boolean
Check if has related boards
350 351 352 353 354 |
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 350 def (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_related_components(field_slug) ⇒ boolean
Check if has related components
309 310 311 312 313 |
# File 'app/models/concerns/binda/fieldable_association_helpers.rb', line 309 def (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
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
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
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
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 |