Module: Locomotive::Concerns::ContentType::GroupBy
- Included in:
- Locomotive::ContentType
- Defined in:
- app/models/locomotive/concerns/content_type/group_by.rb
Instance Method Summary collapse
- #group_by=(name) ⇒ Object
- #group_by_field ⇒ Object
- #groupable? ⇒ Boolean
-
#list_of_groups ⇒ Array
List the name and _id of the groups if the entries are grouped by a field.
Instance Method Details
#group_by=(name) ⇒ Object
46 47 48 |
# File 'app/models/locomotive/concerns/content_type/group_by.rb', line 46 def group_by=(name) @group_by = name end |
#group_by_field ⇒ Object
42 43 44 |
# File 'app/models/locomotive/concerns/content_type/group_by.rb', line 42 def group_by_field self.find_entries_custom_field(self.group_by_field_id) end |
#groupable? ⇒ Boolean
38 39 40 |
# File 'app/models/locomotive/concerns/content_type/group_by.rb', line 38 def groupable? !!self.group_by_field && %w(select belongs_to boolean).include?(group_by_field.type) end |
#list_of_groups ⇒ Array
List the name and _id of the groups if the entries are grouped by a field. The type of this field can be either a select or a belongs_to. It returns nil if groupable? returns false.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/models/locomotive/concerns/content_type/group_by.rb', line 13 def list_of_groups return nil unless self.groupable? field = self.group_by_field case field.type.to_sym when :boolean [ { name: "#{field.label} (#{I18n.t('yes', scope: 'locomotive.shared')})", _id: true }, { name: "#{field.label} (#{I18n.t('no', scope: 'locomotive.shared')})", _id: false } ] when :select self.entries_class.(field.name).map(&:with_indifferent_access) when :belongs_to target = self.class_name_to_content_type(field.class_name) label = target.label_field_name.to_sym # FIXME: applying "only" with _id and label sounds like a good option for performance # but it fails because of Mongoid and its way of dealing with localized attributes. target.ordered_entries.map do |entry| { _id: entry._id, name: entry.send(label) } end end end |