Sha256: fd045420c34030f07d23060bb74af33073d68922acccec9a2b47a89d72d8e26e

Contents?: true

Size: 1.25 KB

Versions: 4

Compression:

Stored size: 1.25 KB

Contents

module AbAdmin
  module Models
    class TypeModel
      include ::EnumField::DefineEnum

      attr_reader :code

      class_attribute :codes, :i18n_scope, instance_writer: false
      self.codes = []
      self.i18n_scope = [:admin, :type_model]

      def initialize(code)
        @code = code.to_sym
      end

      class << self
        def define_enum_by_codes
          define_enum do |builder|
            codes.each do |kind|
              builder.member kind, object: new(kind.to_s)
            end
          end
          define_question_methods
        end

        def define_question_methods
          codes.each do |code_check|
            define_method "#{code_check}?" do
              self.code == code_check
            end
          end
        end

        def legal?(value)
          ActiveSupport::Deprecation.warn('legal? is deprecated, use valid? instead')
          valid?(value)
        end

        def valid?(c_id)
          all.map(&:id).include?(c_id.to_i)
        end

        def valid_code?(code)
          return unless code
          codes.include?(code.to_sym)
        end
      end

      def title
        I18n.t!(@code, scope: i18n_scope)
      rescue I18n::MissingTranslationData
        @code.to_s.humanize
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ab_admin-0.6.1 lib/ab_admin/models/type_model.rb
ab_admin-0.6.0 lib/ab_admin/models/type_model.rb
ab_admin-0.5.0 lib/ab_admin/models/type_model.rb
ab_admin-0.4.0 lib/ab_admin/models/type_model.rb