Module: ExtJS::Model::ClassMethods

Defined in:
lib/active_record/model.rb

Overview

ClassMethods

Constant Summary collapse

@@fields =
[]

Instance Method Summary collapse

Instance Method Details

#extjs_fields(*params) ⇒ Object

Defines the subset of AR columns used to create Ext.data.Record def’n.

Parameters:

  • list-of-fields (Array/Hash)

    to include, :only, or :exclude



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_record/model.rb', line 29

def extjs_fields(*params)
  options = params.extract_options!
  if !options.keys.empty?
    if options[:only]
      @@fields = options[:only]
    elsif options[:exclude]
      @@fields = self.columns.reject {|c| options[:exclude].find {|ex| c.name.to_sym === ex}}.collect {|c| c.name.to_sym}
    end
  elsif !params.empty?
    @@fields = params
  else
    @@fields
  end
end

#extjs_recordObject

render AR columns to Ext.data.Record.create format eg: type: ‘string’



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/active_record/model.rb', line 48

def extjs_record
  @@fields = self.columns.collect {|c| c.name.to_sym } if @@fields.empty?
  {
    "fields" => @@fields.collect {|f|
      col = self.columns.find {|c| c.name.to_sym === f}
      type = col.type
      case col.type
        when :datetime || :date || :time || :timestamp
          type = :date
        when :text
          type = :string
        when :integer
          type = :int
      end
      field = {:name => col.name, :allowBlank => (col.primary) ? true : col.null, :type => type}
      field[:dateFormat] = "c" if col.type === :datetime || col.type === :date  # <-- ugly hack for date
      field
    },
    "idProperty" => self.primary_key
  }
end