Sha256: b609a8be37866ccbdc3e2b643f5ef194d6c0b395fabad72b30ce9135596e67a8

Contents?: true

Size: 1.43 KB

Versions: 5

Compression:

Stored size: 1.43 KB

Contents

# -*- encoding : utf-8 -*-
module RademadeAdmin
  module Model
    class Reflection

      ORM_TYPE_ACTIVERECORD = 'ActiveRecord'
      ORM_TYPE_MONGOID = 'Mongoid'

      attr_reader :model, :controller, :module_name

      def initialize(model, controller, module_name)
        @model, @controller, @module_name = model, controller, module_name
      end

      # Load data adapter for current model
      #
      # @return [RademadeAdmin::Model::Adapter::Data]
      def data_adapter
        @data_adapter ||= "RademadeAdmin::Model::Adapter::Data::#{orm_type}".constantize.new(@model)
      end

      # Load query adapter for current model
      #
      # @return [RademadeAdmin::Model::Adapter::Query]
      def query_adapter
        @query_adapter ||= "RademadeAdmin::Model::Adapter::Query::#{orm_type}".constantize.new(@model)
      end

      def hideable?
        _model_ancestors.include? RademadeAdmin::Hideable.name
      end

      protected

      def _model_ancestors
        @model_ancestors = @model.ancestors.map(&:to_s)
      end

      def orm_type
        return @orm_type unless @orm_type.nil?
        orm_list.each do |orm_class, orm_type|
          @orm_type = orm_type if _model_ancestors.include? orm_class
        end
        @orm_type
      end

      def orm_list
        @orm_list ||= {
          'ActiveRecord::Base' => ORM_TYPE_ACTIVERECORD,
          'Mongoid::Document' => ORM_TYPE_MONGOID
        }
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rademade_admin-0.2.2 lib/rademade_admin/model/reflection.rb
rademade_admin-0.2.1 lib/rademade_admin/model/reflection.rb
rademade_admin-0.2.0 lib/rademade_admin/model/reflection.rb
rademade_admin-0.1.6 lib/rademade_admin/model/reflection.rb
rademade_admin-0.1.5 lib/rademade_admin/model/reflection.rb