Sha256: f58ad6f5dab2cf210ae41f90ad971449d7024ff63bee2a4d8933583f35ce16a5

Contents?: true

Size: 870 Bytes

Versions: 3

Compression:

Stored size: 870 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("name = '#{DEFAULT_NAME}' DESC, 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

3 entries across 3 versions & 1 rubygems

Version Path
vapid-0.1.2 lib/vapid/models/group.rb
vapid-0.1.1 lib/vapid/models/group.rb
vapid-0.1.0 lib/vapid/models/group.rb