Sha256: d3087fd0d7b60a3b2f433a6c14d0b32dd47c7df9665aefcde43a43d07948b683

Contents?: true

Size: 1.25 KB

Versions: 8

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

8 entries across 8 versions & 1 rubygems

Version Path
ab_admin-0.11.0 lib/ab_admin/models/type_model.rb
ab_admin-0.10.0 lib/ab_admin/models/type_model.rb
ab_admin-0.9.0 lib/ab_admin/models/type_model.rb
ab_admin-0.8.3 lib/ab_admin/models/type_model.rb
ab_admin-0.8.2 lib/ab_admin/models/type_model.rb
ab_admin-0.8.1 lib/ab_admin/models/type_model.rb
ab_admin-0.8.0 lib/ab_admin/models/type_model.rb
ab_admin-0.7.0 lib/ab_admin/models/type_model.rb