Netzke::Basepack::GridPanel::Services

Public Instance Methods

add_form__form_panel0__netzke_submit(params) click to toggle source

Edit in form specific API

     # File lib/netzke/basepack/grid_panel/services.rb, line 106
106:         def add_form__form_panel0__netzke_submit(params)
107:           res = component_instance(:add_form__form_panel0).netzke_submit(params)
108: 
109:           if res[:set_form_values]
110:             # successful creation
111:             on_data_changed
112:             res[:set_form_values] = nil
113:           end
114:           res.to_nifty_json
115:         end
deliver_component_endpoint(params) click to toggle source

When providing the edit_form component, fill in the form with the requested record

     # File lib/netzke/basepack/grid_panel/services.rb, line 149
149:         def deliver_component_endpoint(params)
150:           components[:edit_form][:items].first.merge!(:record_id => params[:record_id].to_i) if params[:name] == 'edit_form'
151:           super
152:         end
edit_form__form_panel0__netzke_submit(params) click to toggle source
     # File lib/netzke/basepack/grid_panel/services.rb, line 117
117:         def edit_form__form_panel0__netzke_submit(params)
118:           res = component_instance(:edit_form__form_panel0).netzke_submit(params)
119: 
120:           if res[:set_form_values]
121:             on_data_changed
122:             res[:set_form_values] = nil
123:           end
124: 
125:           res.to_nifty_json
126:         end
get_data(*args) click to toggle source

Implementation for the “get_data“ endpoint

     # File lib/netzke/basepack/grid_panel/services.rb, line 155
155:         def get_data(*args)
156:           params = args.first || {} # params are optional!
157:           if !config[:prohibit_read]
158:             records = get_records(params)
159:             {:data => records.map{|r| r.to_array(columns)}, :total => config[:enable_pagination] && records.total_entries}
160:           else
161:             flash :error => "You don't have permissions to read data"
162:             {:feedback => @flash}
163:           end
164:         end
multi_edit_form__multi_edit_form0__netzke_submit(params) click to toggle source
     # File lib/netzke/basepack/grid_panel/services.rb, line 128
128:         def multi_edit_form__multi_edit_form0__netzke_submit(params)
129:           ids = ActiveSupport::JSON.decode(params.delete(:ids))
130:           data = ids.collect{ |id| ActiveSupport::JSON.decode(params[:data]).merge("id" => id) }
131: 
132:           data.map!{|el| el.delete_if{ |k,v| v.blank? }} # only interested in set values
133: 
134:           mod_records_count = process_data(data, :update).count
135: 
136:           # remove duplicated flash messages
137:           @flash = @flash.inject([]){ |r,hsh| r.include?(hsh) ? r : r.push(hsh) }
138: 
139:           if mod_records_count > 0
140:             on_data_changed
141:             flash :notice => "Updated #{mod_records_count} records."
142:             {:set_result => "ok", :feedback => @flash}.to_nifty_json
143:           else
144:             {:feedback => @flash}.to_nifty_json
145:           end
146:         end

Protected Instance Methods

convert_filters(column_filter) click to toggle source

Converts Ext.ux.grid.GridFilters filters to searchlogic conditions, e.g.

    {"0" => {
      "data" => {
        "type" => "numeric",
        "comparison" => "gt",
        "value" => 10 },
      "field" => "id"
    },
    "1" => {
      "data" => {
        "type" => "string",
        "value" => "pizza"
      },
      "field" => "food_name"
    }}
     =>
 metawhere:   :id.gt => 100, :food_name.matches => '%pizza%'
     # File lib/netzke/basepack/grid_panel/services.rb, line 318
318:           def convert_filters(column_filter)
319:             res = {}
320:             column_filter.each_pair do |k,v|
321:               assoc, method = v["field"].split('__')
322:               if method
323:                 assoc = data_class.reflect_on_association(assoc.to_sym)
324:                 field = [assoc.klass.table_name, method].join('.').to_sym
325:               else
326:                 field = assoc.to_sym
327:               end
328: 
329:               value = v["data"]["value"]
330:               case v["data"]["type"]
331:               when "string"
332:                 field = field.send :matches
333:                 value = "%#{value}%"
334:               when "numeric", "date"
335:                 field = field.send :"#{v['data']['comparison']}"
336:               end
337:               res.merge!({field => value})
338:             end
339:             res
340:           end
get_records(params) click to toggle source
     # File lib/netzke/basepack/grid_panel/services.rb, line 168
168:           def get_records(params)
169: 
170:             # Restore params from component_session if requested
171:             if params[:with_last_params]
172:               params = component_session[:last_params]
173:             else
174:               # remember the last params
175:               component_session[:last_params] = params
176:             end
177: 
178:             # build initial relation based on passed params
179:             relation = get_relation(params)
180: 
181:             # addressing the n+1 query problem
182:             columns.each do |c|
183:               assoc, method = c[:name].split('__')
184:               relation = relation.includes(assoc.to_sym) if method
185:             end
186: 
187:             # apply sorting if needed
188:             if params[:sort]
189:               assoc, method = params[:sort].split('__')
190:               dir = params[:dir].downcase
191: 
192:               # if a sorting scope is set, call the scope with the given direction
193:               column = columns.detect { |c| c[:name] == params[:sort] }
194:               if column.has_key?(:sorting_scope)
195:                 relation = relation.send(column[:sorting_scope].to_sym, dir.to_sym)
196:               else
197:                 relation = if method.nil?
198:                   relation.order(assoc.to_sym.send(dir))
199:                 else
200:                   assoc = data_class.reflect_on_association(assoc.to_sym)
201:                   relation.order(assoc.klass.table_name.to_sym => method.to_sym.send(dir)).joins(assoc.name)
202:                 end
203:               end
204:             end
205: 
206:             # apply pagination if needed
207:             if config[:enable_pagination]
208:               per_page = config[:rows_per_page]
209:               page = params[:limit] ? params[:start].to_i/params[:limit].to_i + 1 : 1
210:               relation.paginate(:per_page => per_page, :page => page)
211:             else
212:               relation.all
213:             end
214:           end
get_relation(params) click to toggle source

An ActiveRecord::Relation instance encapsulating all the necessary conditions

     # File lib/netzke/basepack/grid_panel/services.rb, line 217
217:           def get_relation(params)
218:             # make params coming from Ext grid filters understandable by meta_where
219:             conditions = params[:filter] && convert_filters(params[:filter]) || {}
220: 
221:             relation = data_class.where(conditions)
222: 
223:             if params[:extra_conditions]
224:               extra_conditions = normalize_extra_conditions(ActiveSupport::JSON.decode(params[:extra_conditions]))
225:               relation = relation.extend_with_netzke_conditions(extra_conditions) if params[:extra_conditions]
226:             end
227: 
228:             relation = relation.extend_with(config[:scope]) if config[:scope]
229: 
230:             relation
231:           end
normalize_extra_conditions(conditions) click to toggle source
     # File lib/netzke/basepack/grid_panel/services.rb, line 342
342:           def normalize_extra_conditions(conditions)
343:             conditions.each_pair do |k,v|
344:               conditions[k] = "%#{v}%" if ["like", "matches"].include?(k.to_s.split("__").last)
345:             end
346:           end
normalize_index(index) click to toggle source

Given an index of a column among enabled (non-excluded) columns, provides the index (position) in the table

     # File lib/netzke/basepack/grid_panel/services.rb, line 237
237:           def normalize_index(index)
238:             norm_index = 0
239:             index.times do
240:               while true do
241:                 norm_index += 1
242:                 break unless columns[norm_index][:included] == false
243:               end
244:             end
245:             norm_index
246:           end
on_data_changed() click to toggle source

Override this method to react on each operation that caused changing of data

     # File lib/netzke/basepack/grid_panel/services.rb, line 234
234:           def on_data_changed; end
process_data(data, operation) click to toggle source

Params: :operation: :update or :create

     # File lib/netzke/basepack/grid_panel/services.rb, line 250
250:           def process_data(data, operation)
251:             success = true
252:             mod_records = {}
253:             if !config[:"prohibit_#{operation}"]
254:               modified_records = 0
255:               data.each do |record_hash|
256:                 id = record_hash.delete('id')
257:                 record = operation == :create ? data_class.new : data_class.find(id)
258:                 success = true
259: 
260:                 # merge with strong default attirbutes
261:                 record_hash.merge!(config[:strong_default_attrs]) if config[:strong_default_attrs]
262: 
263:                 record_hash.each_pair do |k,v|
264:                   record.set_value_for_attribute(columns_hash[k.to_sym].nil? ? {:name => k} : columns_hash[k.to_sym], v)
265:                 end
266: 
267:                 # process all attirubutes for this record
268:                 #record_hash.each_pair do |k,v|
269:                   #begin
270:                     #record.send("#{k}=",v)
271:                   #rescue ArgumentError => exc
272:                     #flash :error => exc.message
273:                     #success = false
274:                     #break
275:                   #end
276:                 #end
277: 
278:                 # try to save
279:                 # modified_records += 1 if success && record.save
280:                 mod_records[id] = record.to_array(columns) if success && record.save
281:                 # mod_record_ids << id if success && record.save
282: 
283:                 # flash eventual errors
284:                 if !record.errors.empty?
285:                   success = false
286:                   record.errors.to_a.each do |msg|
287:                     flash :error => msg
288:                   end
289:                 end
290:               end
291:               # flash :notice => "#{operation.to_s.capitalize}d #{modified_records} record(s)"
292:             else
293:               success = false
294:               flash :error => "You don't have permissions to #{operation} data"
295:             end
296:             mod_records
297:           end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.