Sha256: a63c1931c857552d98b2e1f67bd839a585a0da3ef34115dc7de9d0be7a84ed60

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 KB

Contents

module SimpleAdmin
  class Entity < Base
    include Helpers::BooleanHelper

    boolean_fields :status

    has_many :entity_fields

    scope :enabled, -> { where(status: true) }

    validates :model_klass_name, presence: true

    before_save :label_default_value!
    after_create :create_default_fields!

    after_save :reload_routes! unless Rails.env.test?

    class << self
      def form_attributes(model_klass)
        find_by(model_klass_name: model_klass.to_s).entity_fields.form.pluck(:name)
      end

      def resource_attributes
        enabled.map do |entity|
          [entity.model_klass.model_name.collection, entity.model_klass_name]
        end
      end
    end

    def model_klass
      model_klass_name.constantize
    end

    private

      def reload_routes!
        Rails.application.routes_reloader.reload!
      end

      def label_default_value!
        return if label.present?
        self.label = model_klass.model_name.human.pluralize
      end

      def create_default_fields!
        SimpleAdmin::EntityFieldType.reload_helper_methods!
        SimpleAdmin::EntityField.create_number_field(name: :id, label: '#', entity: self, presentation: :collection)
      end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
simple-admin-0.2.0.pre.alpha app/models/simple_admin/entity.rb
simple-admin-0.1.2.pre.alpha app/models/simple_admin/entity.rb
simple-admin-0.1.1.pre.alpha app/models/simple_admin/entity.rb
simple-admin-0.1.0.pre.alpha app/models/simple_admin/entity.rb