Sha256: f1313bc7b28b50dc5e7cbedb289de75c93d0224d703c2b696000f7d86ed93b10

Contents?: true

Size: 884 Bytes

Versions: 1

Compression:

Stored size: 884 Bytes

Contents

module Vapid
  module Models
    # Group model
    class Group < ActiveRecord::Base
      DEFAULT_NAME = "general".freeze

      serialize :fields, JSON

      has_many :records, dependent: :destroy
      accepts_nested_attributes_for :records, allow_destroy: true

      default_scope { order("groups.name = '#{DEFAULT_NAME}' DESC, groups.created_at DESC") }
      scope :with_fields, -> { where("fields <> ?", "{}") }

      def self.named(name)
        find_by(name: name.downcase) if table_exists?
      end

      def name=(name)
        super name.try(:downcase)
      end

      def fields=(fields)
        super fields.each_with_object({}) { |(k, v), h| h[k.downcase] = v }
      end

      def text_fields
        fields.select { |_k, v| v == "text" }.keys
      end

      def display_name
        (repeating? ? name.pluralize : name).capitalize
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vapid-0.1.3 lib/vapid/models/group.rb