Sha256: 5249ac955b7cba6d6392e44ae0f13f2b9004b08af54c6d10f79d2cef9c5411c0

Contents?: true

Size: 944 Bytes

Versions: 1

Compression:

Stored size: 944 Bytes

Contents

module Troo
  module ModelHelpers
    module ClassMethods
      def first(criteria = {})
        return all.first if criteria.nil? || criteria.empty?
        find(criteria).first
      end

      def update(criteria = {})
        return false if criteria.nil? || criteria.empty?
        all.map { |record| record.update(criteria) }
        true
      end

      def default
        first(default: true)
      end

      def default?
        !!(default)
      end

      def count
        all.count
      end

      def by_external_id(id)
        first(external_id: id)
      end

      def retrieve(id = nil, options = {})
        Troo::Retrieval::Local.retrieve(self, id, options)
      end
    end

    module InstanceMethods
      def set_default!
        Behaviours::SetDefault.for(self)
      end
    end

    def self.included(receiver)
      receiver.extend(ClassMethods)
      receiver.send(:include, InstanceMethods)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
troo-0.0.9 lib/troo/helpers/model_helpers.rb