trance 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/trance.rb ADDED
@@ -0,0 +1 @@
1
+ require 'trance/base.rb'
@@ -0,0 +1,503 @@
1
+ require 'trance/helper.rb'
2
+ require 'trance/form.rb'
3
+
4
+ module Trance
5
+
6
+ class Base < ActionController::Base
7
+ before_filter :_init, :init
8
+ layout 'application'
9
+
10
+ def index
11
+ respond_to do |format|
12
+ format.html { render :inline => _index_view, :layout => _grid_layout }
13
+ format.json { render :json => _grid_fetch }
14
+ end
15
+ end
16
+
17
+ def show
18
+ @row = @model.find(params[:id])
19
+ respond_to do |format|
20
+ format.html { render :inline => show_view, :layout => _grid_layout }
21
+ format.json { render json: @row }
22
+ end
23
+ end
24
+
25
+ def new
26
+ @row = @model.new
27
+ respond_to do |format|
28
+ format.html { render :inline => new_view, :layout => _grid_layout }
29
+ format.json { render json: @row }
30
+ end
31
+ end
32
+
33
+ def edit
34
+ @row = @model.find(params[:id])
35
+ respond_to do |format|
36
+ format.html { render :inline => edit_view, :layout => _grid_layout }
37
+ end
38
+ end
39
+
40
+ def create
41
+ @row = @model.new(params[@singular])
42
+ respond_to do |format|
43
+ if @row.save
44
+ format.html { redirect_to @row, notice: t('custom.successfully_created', :model => @model_h1) }
45
+ format.json { render json: @row, status: :created, location: @row }
46
+ else
47
+ format.html { render :inline => new_view, :layout => true }
48
+ format.json { render json: @row.errors, status: :unprocessable_entity }
49
+ end
50
+ end
51
+ end
52
+
53
+ def update
54
+ @row = @model.find(params[:id])
55
+ respond_to do |format|
56
+ if @row.update_attributes(params[@singular])
57
+ format.html { redirect_to @row, notice: t('custom.successfully_updated', :model => @model_h1) }
58
+ format.json { head :no_content }
59
+ else
60
+ format.html { render :inline => edit_view, :layout => true }
61
+ format.json { render json: @row.errors, status: :unprocessable_entity }
62
+ end
63
+ end
64
+ end
65
+
66
+ def destroy
67
+ @row = @model.find(params[:id])
68
+ @row.destroy
69
+ respond_to do |format|
70
+ format.html { redirect_to :action => 'index' }
71
+ format.json { head :no_content }
72
+ end
73
+ end
74
+
75
+ def upload_create
76
+ NoticiasController::layout nil
77
+
78
+ if params[:filedata]
79
+ img = params[:filedata][:image]
80
+ tmp = RAILS_ROOT + '/tmp/uploads/' + Digest::SHA1.hexdigest(Time.now.to_i.to_s) + '.dat'
81
+ File::copy_stream img.tempfile.path, tmp
82
+
83
+ session[:upload] = {
84
+ :original_filename => img.original_filename,
85
+ :content_type => img.content_type,
86
+ :tempfile => File::basename(tmp),
87
+ :size => File::size(tmp)
88
+ }
89
+
90
+ @upload = session[:upload]
91
+
92
+ render :inline => <<-EOF
93
+ <% if @upload %>
94
+
95
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
96
+ "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
97
+ <html dir=ltr= "pt-br" xmlns="http://www.w3.org/1999/xhtml">
98
+ <head>
99
+ <title><%= @title.nil? ? @head_title : @title + @title_separator + @head_title %></title>
100
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
101
+ <%= javascript_include_tag 'jquery' %>
102
+ </head>
103
+ <body>
104
+ <div>
105
+
106
+
107
+ <script type="text/javascript">
108
+ var upload = {};
109
+
110
+ upload.cancel = function() {
111
+ $.ajax({
112
+ type: 'post',
113
+ url: 'upload-destroy',
114
+ success: function(responseText) {
115
+ parent.$('#upload-item-0').remove();
116
+ }
117
+ });
118
+ };
119
+
120
+ upload.add = function (data) {
121
+ var item = $('<div>').attr({
122
+ 'class': 'upload-item',
123
+ 'id': 'upload-item-0'
124
+ });
125
+ var text = data.original_filename + ' (' + (data.size / 1024).toFixed(2) + 'KB)';
126
+ var name = $('<span>').attr('class', 'upload-name').html(text);
127
+ var cancel = $('<a>').attr({
128
+ 'class': 'upload-cancel ui-state-default ui-corner-all',
129
+ 'href': 'javascript:void(0);'
130
+ }).click(function(evt){
131
+ upload.cancel();
132
+ });
133
+ var icon = $('<span>').attr('class', 'ui-icon ui-icon-closethick');
134
+ var input = $('<input>').attr({
135
+ 'id': 'upload-input-0',
136
+ 'name': '#{params[:filedata][:name]}',
137
+ 'type': 'hidden',
138
+ 'value': data.tempfile
139
+ });
140
+
141
+ item.append(name).append(cancel).append(input);
142
+ cancel.append(icon);
143
+
144
+ parent.$('#upload-queue-0').append(item);
145
+ parent.$('#upload-form-0').trigger('reset');
146
+ }
147
+
148
+ parent.upload = upload;
149
+
150
+ upload.add(<%= raw @upload.to_json %>);
151
+ </script>
152
+
153
+
154
+ </div>
155
+ </body>
156
+ </html>
157
+
158
+ <% end %>
159
+ EOF
160
+ end
161
+ end
162
+
163
+ def upload_destroy
164
+ filename = RAILS_ROOT + '/tmp/uploads/' + session[:upload][:tempfile]
165
+ File::unlink filename
166
+ session[:upload] = nil
167
+
168
+ head :no_content
169
+ end
170
+
171
+ def init
172
+
173
+ end
174
+
175
+ private
176
+
177
+ def _init
178
+ @model = params[:controller].classify.constantize
179
+ @model_h1 = @model.model_name.human
180
+ @model_h2 = @model.model_name.human :count => 2
181
+ @singular = @model.model_name.singular
182
+ @plural = @model.model_name.plural
183
+ @model_path = @plural + '_path'
184
+ @new_model_path = 'new_' + @singular + '_path'
185
+ @edit_model_path = 'edit_' + @singular + '_path'
186
+ @limit = 15
187
+ @offset = 0
188
+ end
189
+
190
+ def _grid_layout
191
+ params[:layout] ? false : true
192
+ end
193
+
194
+ def tabs
195
+ if (params[:layout])
196
+ ''
197
+ else
198
+ <<-EOL
199
+ <div id="tabs">
200
+ <ul>
201
+ <li><%= link_to(t('custom.listing_data', :model => @model_h2), send(@model_path, nil, {:layout => 0}), :class => tab_class('index')) %></li>
202
+ </ul>
203
+ </div>
204
+ EOL
205
+ end
206
+
207
+ end
208
+
209
+ def form
210
+ <<-EOL
211
+ <%= form_for(@row, :builder => Trance::Form) do |f| %>
212
+ <% if @row.errors.any? %>
213
+ <div id="error_explanation">
214
+ <h2>
215
+ <%= pluralize(@row.errors.count, "error") %>
216
+ <%= t 'custom.error_prompt', :model => @model_h1 %>
217
+ </h2>
218
+ <ul>
219
+ <% @row.errors.full_messages.each do |msg| %>
220
+ <li><%= msg %></li>
221
+ <% end %>
222
+ </ul>
223
+ </div>
224
+ <% end %>
225
+ <%= render :partial => 'form', :locals => {:f => f} %>
226
+ <div class="actions">
227
+ <%= f.submit %>
228
+ </div>
229
+ <% end %>
230
+ <%= yield :form_closure %>
231
+ EOL
232
+ end
233
+
234
+ def index_view
235
+ <<-EOL
236
+ <% if (params[:layout] ? true : false) %>
237
+ <div class="tabs-content">
238
+ <h1><%= t 'custom.listing_data', :model => @model_h2 %></h1>
239
+ <p id="notice"><%= notice %></p>
240
+ <%= render('list') %>
241
+ </div>
242
+ <% else %>
243
+ #{tabs}
244
+ <% end %>
245
+ EOL
246
+ end
247
+
248
+ def new_view
249
+ <<-EOL
250
+ <div class="tabs-content">
251
+ <h1><%= t 'custom.new_model', :model => @model_h1 %></h1>
252
+ #{form}
253
+ </div>
254
+ EOL
255
+ end
256
+
257
+ def show_view
258
+ <<-EOL
259
+ <% if (params[:layout] ? true : false) %>
260
+ <div class="tabs-content">
261
+ <p id="notice"><%= notice %></p>
262
+ <%= render 'page' %>
263
+ </div>
264
+ <% else %>
265
+ #{tabs}
266
+ <% end %>
267
+ EOL
268
+ end
269
+
270
+ def edit_view
271
+ <<-EOL
272
+ <% if (params[:layout] ? true : false) %>
273
+ <div class="tabs-content">
274
+ <h1><%= t 'custom.editing_model', :model => @model_h1 %></h1>
275
+ <p id="notice"><%= notice %></p>
276
+ #{form}
277
+ </div>
278
+ <% else %>
279
+ #{tabs}
280
+ <% end %>
281
+ EOL
282
+
283
+ end
284
+
285
+
286
+
287
+
288
+
289
+
290
+
291
+
292
+
293
+
294
+
295
+
296
+
297
+
298
+
299
+
300
+
301
+
302
+
303
+ def grid_options
304
+ {
305
+ :sortname => 'id',
306
+ :sortorder => 'asc',
307
+ :width => 740
308
+ }
309
+ end
310
+
311
+ def _index_view
312
+ @grid = _grid_options(grid_options)
313
+ _grid_columns grid_columns
314
+ <<-EOL
315
+ <% if (params[:layout] ? true : false) %>
316
+
317
+ <table id="data-grid"><tbody><tr><td>&nbsp;</td></tr></tbody></table>
318
+ <div id="data-pager"></div>
319
+ <script type="text/javascript">
320
+ $(function(){
321
+ $('#data-grid').jqGrid($.extend(<%= raw @grid.to_json %>, {
322
+ beforeEdit: function(id){
323
+ grupus.fn.edit(id);
324
+ }
325
+ }));
326
+ $("#data-grid").jqGrid('navGrid','#data-pager',{edit:false,add:false,del:false,refresh:true});
327
+ $("#t_data-grid").append($('<span/>').button({icons:{primary:'ui-icon-plus'}, label: 'Novo', text: true}).click(grupus.fn.create));
328
+ $("#t_data-grid").append($('<span/>').button({icons:{primary:'ui-icon-document'}, label: 'Ver', text: true}).click(grupus.fn.show));
329
+ $("#t_data-grid").append($('<span/>').button({icons:{primary:'ui-icon-pencil'}, label: 'Editar', text: true}).click(grupus.fn.edit));
330
+ $("#t_data-grid").append($('<span/>').button({icons:{primary:'ui-icon-trash'}, label: 'Excluir', text: true}).click(grupus.fn.gridDelete));
331
+ //jQuery("#resize").jqGrid('gridResize',{minWidth:350,maxWidth:800,minHeight:80, maxHeight:350});
332
+ });
333
+ </script>
334
+ <% else %>
335
+ #{tabs}
336
+ <% end %>
337
+ EOL
338
+
339
+ end
340
+
341
+ def grid_columns
342
+ []
343
+ end
344
+
345
+ def _grid_columns(columns = [])
346
+
347
+ keys = columns.size > 0 ? columns :
348
+ @model.new.attributes.keys - ['id', 'created_at', 'updated_at']
349
+
350
+ keys.each do |attribute|
351
+ _grid_add attribute
352
+ end
353
+ end
354
+
355
+ def _grid_add(name, options = {})
356
+ @grid[:colNames] << @model.human_attribute_name(name)
357
+ @grid[:colModel] << options.merge({:name => name, :index => name})
358
+ end
359
+
360
+ def _grid_options(options = {})
361
+ ({
362
+ :altRows => true,
363
+ :url => root_path + params[:controller] + '.json',
364
+ #:editurl => root_path + 'costs/update.json',
365
+ :datatype => 'json',
366
+ :mtype => 'GET',
367
+ :colNames => [],
368
+ :colModel => [],
369
+ :pager => '#data-pager',
370
+ :rowNum => 15,
371
+ :rowList => [15, 30, 60, 3],
372
+ #:sortname => nil,
373
+ #:sortorder => nil,
374
+ #:multiselect => true,
375
+ #:viewrecords => true,
376
+ #:gridview => true,
377
+ :jsonReader => {
378
+ :repeatitems => false
379
+ },
380
+ #:caption => t('custom.listing_data', :model => @model_h2),
381
+ :width => '100%',
382
+ :height => '100%',
383
+ :toolbar => [true, 'bottom']
384
+ }).merge(options)
385
+ end
386
+
387
+ def grid_cell(attrs, row)
388
+ attrs
389
+ end
390
+
391
+ def grid_format(result)
392
+ rows = []
393
+
394
+ result.each do |row|
395
+ rows << grid_cell(row.attributes, row)
396
+ end
397
+
398
+ rows
399
+ end
400
+
401
+ def _grid_fetch
402
+ page = params[:page].to_i # get the requested page
403
+ limit = params[:rows].to_i # get how many rows we want to have into the grid
404
+ sidx = params[:sidx] # get index row - i.e. user click to sort
405
+ sord = params[:sord] == 'desc' ? 'desc' : 'asc' # get the direction
406
+
407
+ if sidx == 0
408
+ sidx = 1
409
+ end
410
+
411
+ count = @model.count
412
+
413
+ if count > 0
414
+ total_frac = count / limit
415
+ total_pages = total_frac.ceil
416
+
417
+ if total_pages == 0
418
+ total_pages = 1
419
+ end
420
+ else
421
+ total_pages = 1
422
+ end
423
+
424
+ if page > total_pages
425
+ page = total_pages
426
+ end
427
+
428
+ where = ''
429
+
430
+ if params[:_search] == "true"
431
+ field = params[:searchField]
432
+ term = params[:searchString]
433
+
434
+ case params[:searchOper]
435
+ when 'eq'
436
+ oper = '='
437
+ when 'ne'
438
+ oper = '!='
439
+ when 'bw'
440
+ oper = 'LIKE'
441
+ term = term + '%'
442
+ when 'bn'
443
+ oper = 'NOT LIKE'
444
+ term = term + '%'
445
+ when 'ew'
446
+ oper = 'LIKE'
447
+ term = '%' + term
448
+ when 'en'
449
+ oper = 'NOT LIKE'
450
+ term = '%' + term
451
+ when 'cn'
452
+ oper = 'LIKE'
453
+ term = '%' + term + '%'
454
+ when 'nc'
455
+ oper = 'NOT LIKE'
456
+ term = '%' + term + '%'
457
+ when 'nu'
458
+ oper = 'IS'
459
+ term = 'NULL'
460
+ when 'nn'
461
+ oper = 'IS NOT'
462
+ term = 'NULL'
463
+ when 'in'
464
+ oper = 'IN'
465
+ term = ''
466
+ when 'ni'
467
+ oper = 'NOT IN'
468
+ term = ''
469
+ end
470
+
471
+ where = field + ' ' + oper + ' "' + term + '"'
472
+ end
473
+
474
+ start = limit * page - limit
475
+ result = @model.where(where).limit(limit).offset(start)
476
+
477
+ if sidx.size > 0
478
+ result = result.order(sidx + ' ' + sord)
479
+ end
480
+
481
+ {
482
+ :page => params[:page],
483
+ :total => total_pages,
484
+ :records => count,
485
+ :rows => grid_format(result)
486
+ }
487
+ end
488
+
489
+
490
+
491
+
492
+
493
+
494
+
495
+
496
+
497
+
498
+
499
+
500
+
501
+ end
502
+
503
+ end
@@ -0,0 +1,69 @@
1
+ module Trance
2
+
3
+ class Form < ActionView::Helpers::FormBuilder
4
+
5
+ def datetime_picker(method, options = {}, html_options = {})
6
+ dtp_id = 'dtp-' + _get_id(method)
7
+ value = _get_value(method)
8
+ value_string = value ? value.strftime('%d/%m/%Y %H:%M') : ''
9
+ hidden = hidden_field(method)
10
+ @template.raw(<<-EOL
11
+ #{hidden}
12
+ <input id="#{dtp_id}" size="16" type="text" value="#{value_string}"/>
13
+ <script type="text/javascript">
14
+ $(document).ready(function() {
15
+ $('##{dtp_id}').datetimepicker({
16
+ timeFormat: 'hh:mm',
17
+ hourGrid: 4,
18
+ minuteGrid: 10,
19
+ onSelect: grupus.fn.dtpSelect
20
+ });
21
+ });
22
+ </script>
23
+ EOL
24
+ )
25
+ end
26
+
27
+ def date_picker(method, options = {}, html_options = {})
28
+ dp_id = 'dp-' + _get_id(method)
29
+ value = _get_value(method)
30
+ value_string = value ? value.strftime('%d/%m/%Y') : ''
31
+ hidden = hidden_field(method)
32
+ @template.raw(<<-EOL
33
+ #{hidden}
34
+ <input id="#{dp_id}" size="10" type="text" value="#{value_string}"/>
35
+ <script type="text/javascript">
36
+ $(document).ready(function() {
37
+ $('##{dp_id}').datepicker({
38
+ altField: '#{dp_id}',
39
+ altFormat: 'yy-mm-dd'
40
+ });
41
+ });
42
+ </script>
43
+ EOL
44
+ )
45
+ end
46
+
47
+ # Field for file upload with JavaScript
48
+ def upload_field(method, options = {}, html_options = {})
49
+ @template.raw <<-EOF
50
+ <label>Imagens</label><br/>
51
+ <input id="upload-field-0" type="button" value="Adicionar arquivo" data-name="#{@object_name}[#{method}]" />
52
+ <div class="clearfix" id="upload-queue-0"></div>
53
+ EOF
54
+ end
55
+
56
+ private
57
+
58
+ def _get_id(method)
59
+ @object_name + '_' + method.to_s
60
+ end
61
+
62
+ def _get_value(method)
63
+ value = @object.send(method)
64
+ value ? value : nil
65
+ end
66
+
67
+ end
68
+
69
+ end
@@ -0,0 +1,111 @@
1
+ ActionView::Helpers.module_eval do
2
+
3
+ def table_for(header = {}, options = {}, &block)
4
+ if options[:order]
5
+ @rowset = @rowset.order(options[:order])
6
+ end
7
+
8
+ endl = "\n"
9
+ html = '<table cellpadding="0" cellspacing="0" class="grid">' << endl
10
+
11
+ if header
12
+ html << ' <colgroup>' << endl
13
+
14
+ header.each do |name, size|
15
+ col_data = size.to_i < 1 ? '<col/>' : '<col width="' + size.to_s + '"/>'
16
+ html << ' ' << col_data << endl
17
+ end
18
+
19
+ html << ' <col width="150"/>' << endl << ' </colgroup>' << endl
20
+ end
21
+
22
+ if header
23
+ html << ' <thead>' << endl
24
+ html << ' <tr>' << endl
25
+
26
+ header.each do |name, size|
27
+ html << ' <th>' << col(name) << '</th>' << endl
28
+ end
29
+
30
+ html << ' <th>' << t('custom.operation', :count => 2) << '</th>' << endl
31
+ html << ' </tr>' << endl
32
+ html << ' </thead>' << endl
33
+ end
34
+
35
+ html << ' <tbody>' << endl
36
+
37
+ style = 'even'
38
+ @rowset.each do |row|
39
+ style = style == 'even' ? 'odd' : 'even'
40
+ data = capture(row, &block).strip
41
+ show_link = link_to(t('custom.show'), row, :class => 'ui-icon ui-icon-document')
42
+ edit_link = link_to(t('custom.edit'), send(@edit_model_path, row), :class => 'ui-icon ui-icon-pencil')
43
+ destroy_link = link_to(t('custom.destroy'), row, confirm: t('custom.are_you_sure'), method: :delete, :class => 'ui-icon ui-icon-trash')
44
+
45
+ html << <<-EOL
46
+ <tr class="#{style}">
47
+ #{data}
48
+ <td>
49
+ #{show_link}
50
+ #{edit_link}
51
+ #{destroy_link}
52
+ </td>
53
+ </tr>
54
+ EOL
55
+ end
56
+
57
+ html << ' </tbody>' << endl << '</table>'
58
+
59
+ raw html
60
+ end
61
+
62
+ def col(column)
63
+ @model.human_attribute_name column
64
+ end
65
+
66
+ def val(column)
67
+ @row.send(column)
68
+ end
69
+
70
+ def lval(data, format = :default)
71
+ data ? l(data, :format => format) : raw('&nbsp;')
72
+ end
73
+
74
+ def tab_class(action)
75
+ params[:action] == action ? 'active' : nil
76
+ end
77
+
78
+ # File upload needed resources
79
+ def upload_closure
80
+ raw <<-EOF
81
+ <iframe class="upload-frame" name="upload" src="about:blank"></iframe>
82
+
83
+ <form
84
+ action="#{ url_for :action => :upload_create }"
85
+ class="clearfix"
86
+ enctype="multipart/form-data"
87
+ id="upload-form-0"
88
+ method="post"
89
+ style="display: none"
90
+ target="upload">
91
+ <div id="filedata-0" class="field">
92
+ <input id="filedata-name-0" name="filedata[name]" type="hidden" />
93
+ <input name="filedata[image]" type="file" />
94
+ <input type="submit" value="Enviar" />
95
+ </div>
96
+ </form>
97
+
98
+ <script type="text/javascript">
99
+ $('#upload-field-0').click(function(evt){
100
+ $('#filedata-name-0').val($(this).attr('data-name'));
101
+ $('#upload-form-0').dialog({
102
+ 'modal': true,
103
+ 'title': 'Enviar arquivo',
104
+ 'width': 'auto'
105
+ });
106
+ });
107
+ </script>
108
+ EOF
109
+ end
110
+
111
+ end
metadata ADDED
@@ -0,0 +1,49 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trance
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Giovanni Oliveira
9
+ - Leoncio Caminha
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-04-16 00:00:00.000000000Z
14
+ dependencies: []
15
+ description: Trance is a simple gem
16
+ email: giovanni.oliveira@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/trance.rb
22
+ - lib/trance/base.rb
23
+ - lib/trance/helper.rb
24
+ - lib/trance/form.rb
25
+ homepage: http://www.grupus.net
26
+ licenses: []
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ! '>='
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 1.8.19
46
+ signing_key:
47
+ specification_version: 3
48
+ summary: Trance gem
49
+ test_files: []