Module: Extjs::ActsAsExtjs::SingletonMethods

Defined in:
lib/acts_as_extjs.rb

Instance Method Summary collapse

Instance Method Details

#extjs_result(*args) ⇒ Object

search on a model. the result is a hash that can use as json for extjs store

args

all options that you will use on active record finder with following extras


Options:

:fields

List with hashes like extjs store fields.

:name

Name off field. This will be call on row if not use an custom handler

:custom

can use with Proc.new for own field content

:mapping

Client side mapping by extjs store

:sort_mapping

A Hash to map columns

:fieldname => “sqlfiels” :user_name => “users.name”

:start

start value from extjs paginate toolbar

:limit

limit value from extjs paginate toolbar

:page

page - will overritten if start/limit set

:per_page

per_page - will overritten if start/limit set

:sort_by

sort field. ignore fields that are not in sort_mapping and use this mapping

:group_by

group field. ignore fields that are not in sort_mapping and use this mapping

:sort_dir

direction: asc | desc

:group_dir

direction: asc | desc


Example:

render :json => User.extjs_result :fields = [=> :superid, :type => :id, :custom => Proc.new { |row| row.id }]



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/acts_as_extjs.rb', line 55

def extjs_result(*args)
  extjs_scope = self.extjs


  options = args.extract_options!
  fields = options.delete(:fields)
  start = options.delete(:start).to_i
  limit = options.delete(:limit).to_i

  sort_mapping = options.delete(:sort_mapping)
  sort_by = options.delete(:sort_by)
  sort_dir = options.delete(:sort_dir)
  group_by = options.delete(:group_by)
  group_dir = options.delete(:group_dir)

  unless sort_mapping.blank?
    if not sort_by.blank? and sort_mapping.has_key?(sort_by.to_sym)
      if sort_dir.to_s.downcase == "desc"
        sort_dir = " DESC"
      else
        sort_dir = " ASC"
      end
      order = sort_mapping[sort_by.to_sym].to_s + sort_dir
    else
      order = 'created_at DESC'
    end

    if not group_by.blank? and sort_mapping.has_key?(group_by.to_sym)
      if group_dir.to_s.downcase == "desc"
        group_dir = " DESC"
      else
        group_dir = " ASC"
      end
      order = sort_mapping[group_by.to_sym].to_s + group_dir + ', ' + order
    end

    extjs_scope = extjs_scope.order(order) if order
  end


  if limit.to_i > 0 or options[:per_page].to_i > 0
    options[:per_page] = limit if limit > 0
    options[:page] = (start > 0) ? (start/limit)+1 : 1
    result = extjs_scope.paginate options
    total = result.total
  else
    result = extjs_scope.all options
    total = result.size
  end

  rows = []
  result.each do |result_row|
    row = {}
    fields.collect do |field|
      if field[:mapping].blank?
        if field[:custom].is_a? Proc
          row[field[:name]] = field[:custom].call(result_row)
        else
          row[field[:name]] = result_row.send(field[:name])
        end
        if row[field].is_a? ActiveSupport::TimeWithZone or row[field[:name]].is_a? DateTime or row[field[:name]].is_a? Time
          row[field[:name]] = row[field[:name]].strftime("%Y-%m-%d %H:%M:%S")
        end

        row[field[:name]] = row[field[:name]].to_f if field[:type] == :float
        row[field[:name]] = row[field[:name]].to_i if field[:type] == :int
      end
    end
    rows << row
  end

  {
    :total => total,
    :data => rows,
    :metaData => {
      :root => :data,
      :messageProperty => 'message',
      :successProperty => 'success',
      :fields => fields,
      :idProperty => :id,
      :totalProperty => :total,
    }
  }
end