padrino-admin 0.6.3 → 0.6.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +16 -0
- data/VERSION +1 -1
- data/lib/padrino-admin.rb +24 -5
- data/lib/padrino-admin/access_control.rb +75 -29
- data/lib/padrino-admin/{ext_js/column_store.rb → column_store.rb} +31 -15
- data/lib/padrino-admin/config.rb +36 -0
- data/lib/padrino-admin/generators/actions.rb +62 -7
- data/lib/padrino-admin/generators/admin_app.rb +27 -26
- data/lib/padrino-admin/generators/admin_page.rb +20 -18
- data/lib/padrino-admin/generators/admin_uploader.rb +83 -0
- data/lib/padrino-admin/generators/app/app.rb.tt +1 -3
- data/lib/padrino-admin/generators/app/controllers/accounts.rb +1 -1
- data/lib/padrino-admin/generators/app/public/flash/swfupload.swf +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/back.gif +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/background.png +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/bg-content.png +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/bg-hd-slate.png +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/bg-hd.png +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/bg-intro.png +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/bg-login.png +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/bg-menu-slate.png +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/bg-menu.png +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/bg.png +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/btn-login.png +0 -0
- data/lib/padrino-admin/generators/app/public/images/admin/cancel.gif +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/categories.gif +0 -0
- data/lib/padrino-admin/generators/app/public/images/admin/close.gif +0 -0
- data/lib/padrino-admin/generators/app/public/images/admin/close.png +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/delete.gif +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/download.gif +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/duplicate.gif +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/edit.gif +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/export.gif +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/hd-bg.gif +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/image.gif +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/loader.gif +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/logo-loader.png +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/logo-small.png +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/new.gif +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/no-image.png +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/preview.gif +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/print.gif +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/save.gif +0 -0
- data/lib/padrino-admin/generators/app/public/images/{backend → admin}/support.gif +0 -0
- data/lib/padrino-admin/generators/app/public/images/admin/up.gif +0 -0
- data/lib/padrino-admin/generators/app/public/javascripts/ext.js +4 -1
- data/lib/padrino-admin/generators/app/public/javascripts/swfupload.js +4 -0
- data/lib/padrino-admin/generators/app/public/stylesheets/admin.css +25 -43
- data/lib/padrino-admin/generators/app/public/stylesheets/login.css +3 -3
- data/lib/padrino-admin/generators/app/public/stylesheets/standard.css +53 -52
- data/lib/padrino-admin/generators/app/views/accounts/_form.haml +1 -0
- data/lib/padrino-admin/generators/app/views/base/index.haml +5 -7
- data/lib/padrino-admin/generators/app/views/javascripts/admin.js.erb +217 -297
- data/lib/padrino-admin/generators/app/views/sessions/new.haml +1 -1
- data/lib/padrino-admin/generators/templates/{controller.rb.tt → page/controller.rb.tt} +1 -1
- data/lib/padrino-admin/generators/templates/{db → page/db}/seeds.rb.tt +0 -0
- data/lib/padrino-admin/generators/templates/{views → page/views}/_form.haml.tt +0 -0
- data/lib/padrino-admin/generators/templates/{views → page/views}/edit.haml.tt +0 -0
- data/lib/padrino-admin/generators/templates/{views → page/views}/grid.js.erb.tt +1 -1
- data/lib/padrino-admin/generators/templates/{views → page/views}/new.haml.tt +0 -0
- data/lib/padrino-admin/generators/templates/{views → page/views}/store.jml.tt +0 -0
- data/lib/padrino-admin/generators/templates/uploader/controller.rb +24 -0
- data/lib/padrino-admin/generators/templates/uploader/lib/uploader.rb +54 -0
- data/lib/padrino-admin/generators/templates/uploader/views/grid.js.erb +56 -0
- data/lib/padrino-admin/generators/templates/uploader/views/store.jml +10 -0
- data/lib/padrino-admin/helpers/authentication.rb +30 -19
- data/lib/padrino-admin/helpers/view.rb +172 -35
- data/lib/padrino-admin/locale/admin/en.yml +1 -0
- data/lib/padrino-admin/middleware/flash_middleware.rb +36 -0
- data/lib/padrino-admin/orm.rb +33 -0
- data/lib/padrino-admin/orm/abstract.rb +94 -0
- data/lib/padrino-admin/{adapters/ar.rb → orm/activerecord.rb} +69 -36
- data/lib/padrino-admin/orm/datamapper.rb +214 -0
- data/lib/padrino-admin/{adapters/mm.rb → orm/mongomapper.rb} +36 -20
- data/lib/padrino-admin/utils/literal.rb +1 -1
- data/padrino-admin.gemspec +62 -51
- data/test/fixtures/active_record.rb +14 -2
- data/test/fixtures/data_mapper.rb +2 -1
- data/test/fixtures/mongo_mapper.rb +1 -1
- data/test/fixtures/test_column_store.jml +1 -0
- data/test/helper.rb +3 -2
- data/test/test_access_control.rb +1 -1
- data/test/test_active_record.rb +56 -1
- data/test/test_column_store.rb +56 -10
- data/test/test_data_mapper.rb +67 -1
- metadata +58 -47
- data/lib/padrino-admin/adapters.rb +0 -108
- data/lib/padrino-admin/adapters/dm.rb +0 -147
- data/lib/padrino-admin/ext_js/config.rb +0 -186
- data/test/fixtures/test_generic.jml +0 -7
- data/test/fixtures/test_javascript.jml +0 -81
data/README.rdoc
CHANGED
@@ -75,6 +75,22 @@ Account with role <tt>editor</tt> can manage <tt>only</tt> post/comments because
|
|
75
75
|
Another good fature of Padrino admin is that when you define a <tt>Project Module</tt> role you also build the Menu Tree of the Admin.
|
76
76
|
Trust us that in future you appreciate so much this feature.
|
77
77
|
|
78
|
+
=== Admin Uploads
|
79
|
+
|
80
|
+
Padrino admin has a builtin upload managment that leave you to be free as possible.
|
81
|
+
|
82
|
+
fun-test$ padrino-gen admin_uploader
|
83
|
+
fun-test$ rake dm:auto:upgrade # or ar:migrate
|
84
|
+
|
85
|
+
Finish! Now you can browse into your admin and you can see a new menu called +upload+ where you can see all your uploads,
|
86
|
+
upload other file, delete ...
|
87
|
+
|
88
|
+
All upload definitions are defined in lib/uploader.rb, here you can preproces your attachments (like resize) or manage versions.
|
89
|
+
|
90
|
+
See http://github.com/jnicklas/carrierwave
|
91
|
+
|
92
|
+
... here some example howto attach uploads to any model ...
|
93
|
+
|
78
94
|
== Copyright
|
79
95
|
|
80
96
|
Copyright (c) 2010 Padrino. See LICENSE for details.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.6.
|
1
|
+
0.6.7
|
data/lib/padrino-admin.rb
CHANGED
@@ -4,12 +4,31 @@ require 'padrino-core'
|
|
4
4
|
require 'padrino-gen'
|
5
5
|
|
6
6
|
Dir[File.dirname(__FILE__) + '/padrino-admin/*.rb'].each {|file| require file }
|
7
|
-
Dir[File.dirname(__FILE__) + '/padrino-admin/{helpers,
|
7
|
+
Dir[File.dirname(__FILE__) + '/padrino-admin/{helpers,orm,generators,middleware,utils}/*.rb'].each {|file| require file }
|
8
8
|
|
9
|
-
|
10
|
-
|
9
|
+
##
|
10
|
+
# We need to apply Padrino::Admin::Utils::Extensions
|
11
|
+
#
|
11
12
|
String.send(:include, Padrino::Admin::Utils::Crypt)
|
12
13
|
String.send(:include, Padrino::Admin::Utils::Literal)
|
13
14
|
|
14
|
-
|
15
|
-
|
15
|
+
##
|
16
|
+
# We need to add to Padrino::Application a +access_control+ class
|
17
|
+
#
|
18
|
+
Padrino::Application.send(:cattr_accessor, :access_control)
|
19
|
+
Padrino::Application.send(:access_control=, Class.new(Padrino::AccessControl::Base))
|
20
|
+
|
21
|
+
##
|
22
|
+
# If CarrierWave is defined we set the root directory
|
23
|
+
#
|
24
|
+
CarrierWave.root = Padrino.root if defined?(CarrierWave)
|
25
|
+
|
26
|
+
##
|
27
|
+
# Load our Padrino::Admin locales
|
28
|
+
#
|
29
|
+
I18n.load_path += Dir["#{File.dirname(__FILE__)}/padrino-admin/locale/**/*.yml"]
|
30
|
+
|
31
|
+
##
|
32
|
+
# Load our databases extensions
|
33
|
+
#
|
34
|
+
Padrino::Admin::Orm.register!
|
@@ -1,14 +1,14 @@
|
|
1
1
|
module Padrino
|
2
|
-
|
2
|
+
|
3
3
|
class AccessControlError < StandardError; end
|
4
|
-
|
4
|
+
|
5
|
+
##
|
5
6
|
# This module give to a padrino application an access control functionality like:
|
6
7
|
#
|
7
8
|
# class EcommerceDemo < Padrino::Application
|
8
9
|
# enable :authentication
|
9
10
|
# set :login_page, "/login" # or your login page
|
10
11
|
# enable :store_location # if you want know what is the page that need authentication
|
11
|
-
# set :use_orm, :active_record # or :data_mapper, :mongo_mapper
|
12
12
|
#
|
13
13
|
# access_control.roles_for :any do
|
14
14
|
# role.require_login "/cart"
|
@@ -17,7 +17,7 @@ module Padrino
|
|
17
17
|
# end
|
18
18
|
# end
|
19
19
|
#
|
20
|
-
# In the EcommerceDemo, we
|
20
|
+
# In the EcommerceDemo, we +only+ require logins for all paths that start with "/cart" like:
|
21
21
|
#
|
22
22
|
# - "/cart/add"
|
23
23
|
# - "/cart/empty"
|
@@ -30,7 +30,7 @@ module Padrino
|
|
30
30
|
# - "/account/update"
|
31
31
|
#
|
32
32
|
# but if we call "/account/create" we don't need to be logged in our site for do that.
|
33
|
-
# In EcommerceDemo example we set
|
33
|
+
# In EcommerceDemo example we set +redirect_back_or_default+ so if a +unlogged+
|
34
34
|
# user try to access "/account/edit" will be redirected to "/login" when login is done will be
|
35
35
|
# redirected to "/account/edit".
|
36
36
|
#
|
@@ -62,9 +62,9 @@ module Padrino
|
|
62
62
|
# If a user logged with role editor can:
|
63
63
|
#
|
64
64
|
# - Access to all paths that start with "/session" like "/sessions/{new,create}"
|
65
|
-
# - Access
|
65
|
+
# - Access +only+ to paths that start with "/posts" like "/post/{new,edit,destroy}"
|
66
66
|
#
|
67
|
-
# Finally we have another good fatures, the possibility in the same time we build role build also
|
67
|
+
# Finally we have another good fatures, the possibility in the same time we build role build also +tree+.
|
68
68
|
# Figure this scenario: in my admin every account need their own menu, so an Account with role editor have
|
69
69
|
# a menu different than an Account with role admin.
|
70
70
|
#
|
@@ -123,14 +123,15 @@ module Padrino
|
|
123
123
|
#
|
124
124
|
module AccessControl
|
125
125
|
|
126
|
+
##
|
127
|
+
# Method used by Padrino::Application when we register the extension
|
128
|
+
#
|
126
129
|
def self.registered(app)
|
130
|
+
app.set :session_id, "_padrino_#{File.basename(Padrino.root)}_#{app.app_name}".to_sym
|
127
131
|
app.helpers Padrino::Admin::Helpers
|
128
132
|
app.before { login_required }
|
129
|
-
|
130
|
-
|
131
|
-
else
|
132
|
-
raise Padrino::ApplicationSetupError, "You must define in your app the setting :use_orm!"
|
133
|
-
end
|
133
|
+
app.use Padrino::Admin::Middleware::FlashMiddleware, app.session_id # make sure that is the same of session_name in helpers
|
134
|
+
Padrino::Admin::Orm.extend_account!
|
134
135
|
end
|
135
136
|
|
136
137
|
class Base
|
@@ -142,8 +143,10 @@ module Padrino
|
|
142
143
|
base.send(:cattr_reader, :cache)
|
143
144
|
super
|
144
145
|
end
|
145
|
-
|
146
|
+
|
147
|
+
##
|
146
148
|
# We map project modules for a given role or roles
|
149
|
+
#
|
147
150
|
def roles_for(*roles, &block)
|
148
151
|
raise Padrino::AccessControlError, "Role #{role} must be present and must be a symbol!" if roles.any? { |r| !r.kind_of?(Symbol) } || roles.empty?
|
149
152
|
raise Padrino::AccessControlError, "You can't merge :any with other roles" if roles.size > 1 && roles.any? { |r| r == :any }
|
@@ -157,8 +160,10 @@ module Padrino
|
|
157
160
|
end
|
158
161
|
end
|
159
162
|
|
163
|
+
##
|
160
164
|
# Returns (allowed && denied paths).
|
161
165
|
# If an account given we also give allowed & denied paths for their role.
|
166
|
+
#
|
162
167
|
def auths(account=nil)
|
163
168
|
if account
|
164
169
|
cache[account.id] ||= Auths.new(@authorizations, @mappers, account)
|
@@ -167,12 +172,12 @@ module Padrino
|
|
167
172
|
end
|
168
173
|
end
|
169
174
|
end
|
170
|
-
end
|
175
|
+
end # Base
|
171
176
|
|
172
|
-
class Auths
|
177
|
+
class Auths #:nodoc:
|
173
178
|
attr_reader :allowed, :denied, :project_modules
|
174
179
|
|
175
|
-
def initialize(authorizations, mappers=nil, account=nil)
|
180
|
+
def initialize(authorizations, mappers=nil, account=nil) #:nodoc:
|
176
181
|
@allowed, @denied = [], []
|
177
182
|
unless authorizations.empty?
|
178
183
|
@allowed = authorizations.collect(&:allowed).flatten
|
@@ -190,38 +195,49 @@ module Padrino
|
|
190
195
|
@denied.uniq!
|
191
196
|
end
|
192
197
|
|
198
|
+
##
|
199
|
+
# Return true if the requested path (like request.path_info) is allowed.
|
200
|
+
#
|
193
201
|
def can?(request_path)
|
194
202
|
return true if @allowed.empty?
|
195
203
|
request_path = "/" if request_path.blank?
|
196
204
|
@allowed.any? { |path| request_path =~ /^#{path}/ } && !cannot?(request_path)
|
197
205
|
end
|
198
206
|
|
207
|
+
##
|
208
|
+
# Return true if the requested path (like request.path_info) is +not+ allowed.
|
209
|
+
#
|
199
210
|
def cannot?(request_path)
|
200
211
|
return false if @denied.empty?
|
201
212
|
request_path = "/" if request_path.blank?
|
202
213
|
@denied.any? { |path| request_path =~ /^#{path}/ }
|
203
214
|
end
|
204
|
-
|
205
|
-
end
|
215
|
+
end # Auths
|
206
216
|
|
207
217
|
class Authorization
|
208
218
|
attr_reader :allowed, :denied
|
209
219
|
|
210
|
-
def initialize(&block)
|
220
|
+
def initialize(&block) #:nodoc:
|
211
221
|
@allowed = []
|
212
222
|
@denied = []
|
213
223
|
yield self
|
214
224
|
end
|
215
225
|
|
226
|
+
##
|
227
|
+
# Allow a specified path
|
228
|
+
#
|
216
229
|
def allow(path)
|
217
230
|
@allowed << path unless @allowed.include?(path)
|
218
231
|
end
|
219
232
|
|
233
|
+
##
|
234
|
+
# Deny a specified path
|
235
|
+
#
|
220
236
|
def require_login(path)
|
221
237
|
@denied << path unless @denied.include?(path)
|
222
238
|
end
|
223
239
|
alias :deny :require_login
|
224
|
-
end
|
240
|
+
end # Authorization
|
225
241
|
|
226
242
|
class Mapper
|
227
243
|
attr_reader :project_modules, :roles, :denied
|
@@ -235,75 +251,95 @@ module Padrino
|
|
235
251
|
yield(self, @account)
|
236
252
|
end
|
237
253
|
|
254
|
+
##
|
238
255
|
# Create a new project module
|
256
|
+
#
|
239
257
|
def project_module(name, path=nil, &block)
|
240
258
|
@project_modules << ProjectModule.new(name, path, &block)
|
241
259
|
end
|
242
260
|
|
261
|
+
##
|
243
262
|
# Globally allow an paths for the current role
|
263
|
+
#
|
244
264
|
def allow(path)
|
245
265
|
@allowed << path unless @allowed.include?(path)
|
246
266
|
end
|
247
267
|
|
268
|
+
##
|
248
269
|
# Globally deny an pathsfor the current role
|
270
|
+
#
|
249
271
|
def deny(path)
|
250
272
|
@denied << path unless @allowed.include?(path)
|
251
273
|
end
|
252
274
|
|
275
|
+
##
|
253
276
|
# Return true if role is included in given roles
|
277
|
+
#
|
254
278
|
def allowed?
|
255
279
|
@roles.any? { |r| r == @account.role.to_s.downcase.to_sym }
|
256
280
|
end
|
257
281
|
|
282
|
+
##
|
258
283
|
# Return allowed paths
|
284
|
+
#
|
259
285
|
def allowed
|
260
286
|
@project_modules.each { |pm| @allowed.concat(pm.allowed) }
|
261
287
|
@allowed.uniq
|
262
288
|
end
|
263
|
-
end
|
289
|
+
end # Mapper
|
264
290
|
|
265
291
|
class ProjectModule
|
266
292
|
attr_reader :name, :menus, :path
|
267
293
|
|
268
|
-
def initialize(name, path=nil, options={}, &block)#:nodoc:
|
294
|
+
def initialize(name, path=nil, options={}, &block) #:nodoc:
|
269
295
|
@name = name
|
270
296
|
@options = options
|
271
297
|
@allowed = []
|
272
298
|
@menus = []
|
273
299
|
@path = path
|
274
300
|
@allowed << path if path
|
275
|
-
yield self
|
301
|
+
yield self if block_given?
|
276
302
|
end
|
277
303
|
|
304
|
+
##
|
278
305
|
# Build a new menu and automaitcally add the action on the allowed actions.
|
306
|
+
#
|
279
307
|
def menu(name, path=nil, options={}, &block)
|
280
308
|
@menus << Menu.new(name, path, options, &block)
|
281
309
|
end
|
282
310
|
|
311
|
+
##
|
283
312
|
# Return allowed controllers
|
313
|
+
#
|
284
314
|
def allowed
|
285
315
|
@menus.each { |m| @allowed.concat(m.allowed) }
|
286
316
|
@allowed.uniq
|
287
317
|
end
|
288
318
|
|
319
|
+
##
|
289
320
|
# Return the original name or try to translate or humanize the symbol
|
321
|
+
#
|
290
322
|
def human_name
|
291
323
|
@name.is_a?(Symbol) ? I18n.t("admin.menus.#{@name}", :default => @name.to_s.humanize) : @name
|
292
324
|
end
|
293
325
|
|
326
|
+
##
|
294
327
|
# Return symbol for the given project module
|
328
|
+
#
|
295
329
|
def uid
|
296
330
|
@name.to_s.downcase.gsub(/[^a-z0-9]+/, '').gsub(/-+$/, '').gsub(/^-+$/, '').to_sym
|
297
331
|
end
|
298
332
|
|
333
|
+
##
|
299
334
|
# Return ExtJs Config for this project module
|
335
|
+
#
|
300
336
|
def config
|
301
337
|
options = @options.merge(:text => human_name)
|
302
338
|
options.merge!(:menu => @menus.collect(&:config)) if @menus.size > 0
|
303
|
-
options.merge!(:handler =>
|
339
|
+
options.merge!(:handler => Padrino::Admin::Config::Variable.new("function(){ Admin.app.load('#{path}') }")) if @path
|
304
340
|
options
|
305
341
|
end
|
306
|
-
end
|
342
|
+
end # ProjectModule
|
307
343
|
|
308
344
|
class Menu
|
309
345
|
attr_reader :name, :options, :items, :path
|
@@ -318,38 +354,48 @@ module Padrino
|
|
318
354
|
yield self if block_given?
|
319
355
|
end
|
320
356
|
|
357
|
+
##
|
321
358
|
# Add a new submenu to the menu
|
359
|
+
#
|
322
360
|
def add(name, path=nil, options={}, &block)
|
323
361
|
@items << Menu.new(name, path, options, &block)
|
324
362
|
end
|
325
363
|
|
364
|
+
##
|
326
365
|
# Return allowed controllers
|
366
|
+
#
|
327
367
|
def allowed
|
328
368
|
@items.each { |i| @allowed.concat(i.allowed) }
|
329
369
|
@allowed.uniq
|
330
370
|
end
|
331
371
|
|
372
|
+
##
|
332
373
|
# Return the original name or try to translate or humanize the symbol
|
374
|
+
#
|
333
375
|
def human_name
|
334
376
|
@name.is_a?(Symbol) ? I18n.t("admin.menus.#{@name}", :default => @name.to_s.humanize) : @name
|
335
377
|
end
|
336
378
|
|
379
|
+
##
|
337
380
|
# Return a unique id for the given project module
|
381
|
+
#
|
338
382
|
def uid
|
339
383
|
@name.to_s.downcase.gsub(/[^a-z0-9]+/, '').gsub(/-+$/, '').gsub(/^-+$/, '').to_sym
|
340
384
|
end
|
341
385
|
|
386
|
+
##
|
342
387
|
# Return ExtJs Config for this menu
|
388
|
+
#
|
343
389
|
def config
|
344
390
|
if @path.blank? && @items.empty?
|
345
391
|
options = human_name
|
346
392
|
else
|
347
393
|
options = @options.merge(:text => human_name)
|
348
394
|
options.merge!(:menu => @items.collect(&:config)) if @items.size > 0
|
349
|
-
options.merge!(:handler =>
|
395
|
+
options.merge!(:handler => "function(){ Admin.app.load('#{path}') }".to_l) if @path
|
350
396
|
end
|
351
397
|
options
|
352
398
|
end
|
353
|
-
end
|
354
|
-
end
|
355
|
-
end
|
399
|
+
end # Menu
|
400
|
+
end # AccessControl
|
401
|
+
end # Padrino
|
@@ -1,6 +1,8 @@
|
|
1
1
|
module Padrino
|
2
|
-
module
|
2
|
+
module Admin
|
3
|
+
##
|
3
4
|
# Return column config, and store config/data for ExtJS ColumnModel and Store
|
5
|
+
#
|
4
6
|
class ColumnStore
|
5
7
|
attr_reader :data
|
6
8
|
|
@@ -18,7 +20,7 @@ module Padrino
|
|
18
20
|
# Try to reformat the dataIndex
|
19
21
|
data_indexes = Array(column["dataIndex"]).collect do |data_index|
|
20
22
|
if data_index =~ /\./ # if we have some like categories.names we use this
|
21
|
-
cols = data_index.split(".")
|
23
|
+
cols = data_index.split(".").collect(&:singularize)
|
22
24
|
column["name"] ||= cols[0] + "[" + cols[1..-1].join("][") + "]" # accounts.name will be => accounts[name]
|
23
25
|
else
|
24
26
|
column["name"] ||= "#{table_name.singularize}[#{data_index}]"
|
@@ -34,6 +36,7 @@ module Padrino
|
|
34
36
|
column["mapping"] ||= column["name"].sub(/\[/,"_").sub(/\]$/, "").sub(/\]\[/,"_")
|
35
37
|
|
36
38
|
# Now is necessary for our columns an ID
|
39
|
+
#
|
37
40
|
# TODO: check duplicates here
|
38
41
|
column["id"] = column["mapping"]
|
39
42
|
|
@@ -42,8 +45,12 @@ module Padrino
|
|
42
45
|
end
|
43
46
|
end
|
44
47
|
|
48
|
+
##
|
45
49
|
# Return an array config for build an Ext.grid.ColumnModel() config
|
46
|
-
|
50
|
+
#
|
51
|
+
# If +json+ false we return the Hash object, else a JSON pretty printed string
|
52
|
+
#
|
53
|
+
def column_fields(json=true)
|
47
54
|
data = @data.map do |data|
|
48
55
|
data = data.dup
|
49
56
|
editor = parse_column_editor(data.delete("editor"))
|
@@ -54,21 +61,27 @@ module Padrino
|
|
54
61
|
data.delete("mapping")
|
55
62
|
data
|
56
63
|
end
|
57
|
-
JSON.pretty_generate(data)
|
64
|
+
json ? JSON.pretty_generate(data) : data
|
58
65
|
end
|
59
66
|
|
67
|
+
##
|
60
68
|
# Return an array config for build an Ext.data.GroupingStore()
|
61
|
-
|
69
|
+
#
|
70
|
+
# If +json+ false we return the Hash object, else a JSON pretty printed string
|
71
|
+
#
|
72
|
+
def store_fields(json=true)
|
62
73
|
data = @data.map do |data|
|
63
74
|
type = parse_store_renderer(data["renderer"])
|
64
75
|
hash = { :name => data["dataIndex"] , :mapping => data["mapping"] }
|
65
76
|
hash.merge!(type) if type
|
66
77
|
hash
|
67
78
|
end
|
68
|
-
JSON.pretty_generate(data)
|
79
|
+
json ? JSON.pretty_generate(data) : data
|
69
80
|
end
|
70
81
|
|
82
|
+
##
|
71
83
|
# Return data for a custom collection for the ExtJS Ext.data.GroupingStore() json
|
84
|
+
#
|
72
85
|
def store_data_from(collection)
|
73
86
|
collection.map do |c|
|
74
87
|
@data.dup.inject({ "id" => c.id }) do |options, data|
|
@@ -78,20 +91,24 @@ module Padrino
|
|
78
91
|
end
|
79
92
|
end
|
80
93
|
|
94
|
+
##
|
81
95
|
# Return a searched and paginated data collection for the ExtJS Ext.data.GroupingStore() json
|
82
96
|
# You can pass options like:
|
83
97
|
#
|
84
|
-
#
|
98
|
+
# If options has key +json+ false we return the Hash object, else a JSON string
|
99
|
+
#
|
100
|
+
# Examples:
|
85
101
|
#
|
86
|
-
# store_data(params, :conditions => "found = 1")
|
87
102
|
# store_data(params, :include => :posts)
|
88
103
|
#
|
89
104
|
def store_data(params={}, options={})
|
90
105
|
# Some can tell me that this method made two identical queries one for count one for paginate.
|
91
106
|
# We don't use the select count because in some circumstances require much time than select *.
|
92
|
-
params[:limit]
|
93
|
-
|
94
|
-
|
107
|
+
params[:limit] ||= 50
|
108
|
+
json = params.has_key?(:json) ? params.delete(:json) : true
|
109
|
+
collection = @model.ext_search(params, options)
|
110
|
+
result = { :results => store_data_from(collection.records), :count => collection.count }
|
111
|
+
json ? result.to_json : result
|
95
112
|
end
|
96
113
|
|
97
114
|
private
|
@@ -134,7 +151,6 @@ module Padrino
|
|
134
151
|
when "upcase" then { :renderer => "Ext.util.Format.uppercase".to_l }
|
135
152
|
end
|
136
153
|
end
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
end
|
154
|
+
end # ColumnStore
|
155
|
+
end # Admin
|
156
|
+
end # Padrino
|