Class: ArBigTable

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/ar_big_table.rb

Overview

Schema information

Table name: ar_big_table : Big Table

id                 Integer           id
created_at           Time                 created_at
updated_at           Time                 updated_at
key                  String               Key (ident) used to retrieve key/values
description          String               description
site_id              Integer       Data will be used only for defined site. If empty, then it is default for all sites in database.
active               Boolean     This key is active
created_by           Integer       created_by
updated_by           Integer       updated_by
ar_big_table_values  Embedded: ArBigTableValue Values defined by this key

Big table is meant to be a common space for defining default choices for select fields on forms. Documents are organized as key-value pair with the difference that values for the key can be defined for each site and can also be localized.

Usage (as used in forms):

In the example administrator may help user by providing values that can be used on ArAd document position field by defining them in ads-position key of big table. Example is from ar_ads.yml form.

10:
  name: position
  type: text_with_select
  eval: ar_big_table 'ads-positions'
  size: 20

Class Method Summary collapse

Class Method Details

.choices_for(key, site = nil, locale = nil) ⇒ Object

Will return possible choices for specified key prepared for usage in select input field.



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'app/models/ar_big_table.rb', line 67

def self.choices_for(key, site = nil, locale = nil)
  result = []
  data = find_by(key: key, site_id: site)
  data ||= find_by(key: key, site_id: nil) if site
  if data
    data.ar_big_table_values.order(description: 'asc').each do |one_value|
      next unless one_value.active

      description = one_value.get_localized_description(locale)
      result << [description, one_value.value]
    end
  end
  result.empty? ? [nil] : result
end