Sha256: 48fb4269938ad7ee8aacfdd2e948a020a113c2262750a9f79ad69e1e6a31fc13
Contents?: true
Size: 889 Bytes
Versions: 1
Compression:
Stored size: 889 Bytes
Contents
require "at_most/version" require "active_record/base" def downcase_and_pluralize(input) return "#{ input.to_s.downcase }s" end def validation_error(model, message) message || i18n_error(model) || "Maximum number of #{model.class}s has been reached" end def i18n_error(model) if !I18n.t("activerecord.errors.models.#{ downcase_and_pluralize(model.class) }.at_most").include?("missing") I18n.t("activerecord.errors.models.#{ downcase_and_pluralize(model.class) }.at_most") elsif !I18n.t("at_most").include?("missing") I18n.t("at_most") end end class ActiveRecord::Base def self.at_most(limiter, options = {}) validate do |model| @all = self.class.all @count = @all.is_a?(ActiveRecord::Relation) ? @all.size : @all.count if @count >= limiter model.errors.add :base, validation_error(model, options[:message]) end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
at_most-0.0.1 | lib/at_most.rb |