Sha256: 3b639e3cc61ef1f456ff2fc7ebc8f845037f35b1abd4a9072d6ba31df9b94615

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

##
# ActiveRecord adapter to ExtJS::Model mixin.
#
module ExtJS
  module Model
    module ClassMethods
      
      def extjs_primary_key
        self.primary_key.to_sym
      end
      
      def extjs_column_names
        self.column_names
      end
      
      def extjs_columns_hash
        self.columns_hash
      end
      
      ##
      # determine if supplied Column object is nullable
      # @param {ActiveRecord::ConnectionAdapters::Column}
      # @return {Boolean}
      #
      def extjs_allow_blank(col)
        col.null
      end
      
      ##
      # determine datatype of supplied Column object
      # @param {ActiveRecord::ConnectionAdapters::Column}
      # @return {Symbol}
      #
      def extjs_type(col)
        type = col.type
        case type
          when :datetime, :date, :time, :timestamp
            type = :date
          when :text
            type = :string
          when :integer
            type = :int
          when :decimal
            type = :float
        end
        type
      end
      
      ##
      # return a simple, normalized list of AR associations having the :name, :type and association class
      # @return {Array}
      #
      def extjs_associations
        if @extjs_associations.nil?
          @extjs_associations = {}
          self.reflections.keys.each do |key|
            assn = self.reflections[key]
            type = (assn.macro === :has_many || assn.macro === :has_and_belongs_to_many) ? :many : assn.macro
            @extjs_associations[key.to_sym] = {
              :name => key, 
              :type => type, 
              :class => assn.class_name.constantize,
              :foreign_key => assn.association_foreign_key
            }
          end
        end        
        @extjs_associations
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
extjs-mvc-0.2.8 lib/model/active_record.rb