maiha-ext 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README +62 -0
- data/Rakefile +22 -0
- data/init.rb +7 -0
- data/install.rb +1 -0
- data/lib/ext.rb +54 -0
- data/lib/ext/assigned.rb +10 -0
- data/lib/ext/base.rb +76 -0
- data/lib/ext/code.rb +9 -0
- data/lib/ext/controllers.rb +8 -0
- data/lib/ext/controllers/paginate.rb +128 -0
- data/lib/ext/data.rb +2 -0
- data/lib/ext/data/active_resource.rb +89 -0
- data/lib/ext/data/active_store.rb +24 -0
- data/lib/ext/data/connection.rb +3 -0
- data/lib/ext/data/debug_connection.rb +14 -0
- data/lib/ext/data/http_proxy.rb +3 -0
- data/lib/ext/data/json_reader.rb +55 -0
- data/lib/ext/data/record.rb +11 -0
- data/lib/ext/data/store.rb +63 -0
- data/lib/ext/form/combo_box.rb +13 -0
- data/lib/ext/form/date_field.rb +3 -0
- data/lib/ext/form/number_field.rb +3 -0
- data/lib/ext/form/text_field.rb +5 -0
- data/lib/ext/function.rb +9 -0
- data/lib/ext/grid/active_record.rb +30 -0
- data/lib/ext/grid/column.rb +80 -0
- data/lib/ext/grid/column_model.rb +46 -0
- data/lib/ext/grid/editable_column_model.rb +2 -0
- data/lib/ext/grid/editor_grid.rb +5 -0
- data/lib/ext/grid/grid.rb +14 -0
- data/lib/ext/grid/grid_editor.rb +6 -0
- data/lib/ext/helper.rb +172 -0
- data/lib/ext/java_script.rb +71 -0
- data/lib/ext/tree/tree_loader.rb +16 -0
- data/lib/ext/tree/tree_panel.rb +31 -0
- data/spec/base.rb +198 -0
- data/spec/data/json_reader.rb +106 -0
- data/spec/data/store.rb +60 -0
- data/spec/form/combo_box.rb +27 -0
- data/spec/grid/column_model.rb +26 -0
- data/spec/grid/grid.rb +32 -0
- data/spec/grid/grid_editor.rb +17 -0
- data/spec/java_script.rb +28 -0
- data/spec/on_ready.rb +16 -0
- data/spec/spec_helper.rb +55 -0
- data/spec/tree/tree_loader.rb +50 -0
- data/spec/tree/tree_panel.rb +72 -0
- data/tasks/ext_tasks.rake +4 -0
- data/test/ext_test.rb +8 -0
- data/uninstall.rb +1 -0
- metadata +103 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 [name of plugin creator]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
Ext
|
2
|
+
===
|
3
|
+
|
4
|
+
Ext on Rails plugin
|
5
|
+
|
6
|
+
|
7
|
+
Install
|
8
|
+
=======
|
9
|
+
|
10
|
+
1) install this plugin
|
11
|
+
|
12
|
+
Use "svn co" or "svn export" for installing method, because "plugin install"
|
13
|
+
has a bug that fails to fetch all files named 'httpXXX'.
|
14
|
+
|
15
|
+
cd vendor/plugins
|
16
|
+
svn co http://wota.jp/svn/rails/plugins/branches/stable/ext/
|
17
|
+
|
18
|
+
|
19
|
+
2) install following plugins too.
|
20
|
+
|
21
|
+
This plugin depends on following plugins.
|
22
|
+
|
23
|
+
ruby script/plugin install http://wota.jp/svn/rails/plugins/branches/stable/dsl_accessor/
|
24
|
+
ruby script/plugin install http://wota.jp/svn/rails/plugins/branches/stable/named_options/
|
25
|
+
|
26
|
+
Here, feel free to run plugin command, because these don't contain
|
27
|
+
any 'httpXXX' files.
|
28
|
+
|
29
|
+
|
30
|
+
3) install Ext js library
|
31
|
+
|
32
|
+
|
33
|
+
Execute following lines in RAILS_ROOT directory.
|
34
|
+
|
35
|
+
wget http://www.yui-ext.com/deploy/ext-1.0-alpha3.zip
|
36
|
+
unzip ext-1.0-alpha3.zip
|
37
|
+
cd public
|
38
|
+
ln -s ../ext-1.0-alpha3 ext
|
39
|
+
|
40
|
+
Now, you can see "public/ext/XXX" files.
|
41
|
+
|
42
|
+
|
43
|
+
Paginate
|
44
|
+
========
|
45
|
+
|
46
|
+
class UserController < ApplicationController
|
47
|
+
ext_paginate User, 10
|
48
|
+
end
|
49
|
+
|
50
|
+
then visit '/user/index'
|
51
|
+
|
52
|
+
|
53
|
+
More
|
54
|
+
====
|
55
|
+
|
56
|
+
see this page
|
57
|
+
|
58
|
+
http://wota.jp/ac/?date=20070406
|
59
|
+
|
60
|
+
|
61
|
+
Copyright (c) 2008 maiha@wota.jp, released under the MIT license
|
62
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the ext plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the ext plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'Ext'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
data/init.rb
ADDED
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
data/lib/ext.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
|
2
|
+
class Ext
|
3
|
+
dsl_accessor :url_prefix, :default=>"/ext/"
|
4
|
+
|
5
|
+
class << self
|
6
|
+
def patches
|
7
|
+
@patches ||= ActiveSupport::OrderedHash.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def patch
|
11
|
+
patches.map{|name, code| code.to_s}.join("\n")
|
12
|
+
end
|
13
|
+
|
14
|
+
def onReady(&block)
|
15
|
+
js = Ext::JavaScript.new(&block)
|
16
|
+
"Ext.onReady(%s);" % js.to_function
|
17
|
+
end
|
18
|
+
|
19
|
+
def function(*args, &block)
|
20
|
+
arg = args.map(&:to_s).join(',')
|
21
|
+
code = "function(%s){%s}" % [arg, block.call]
|
22
|
+
Ext::Code.new(code)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
require 'ext/data' # force to load Ext::Data
|
28
|
+
|
29
|
+
# patches
|
30
|
+
require 'ext/data/record'
|
31
|
+
require 'ext/data/active_resource'
|
32
|
+
require 'ext/data/active_store'
|
33
|
+
|
34
|
+
|
35
|
+
__END__
|
36
|
+
|
37
|
+
Ext.onReady do
|
38
|
+
var :tree do
|
39
|
+
Ext::Tree::TreePanel.new 'items',
|
40
|
+
:url =>'/foo',
|
41
|
+
:animate => true,
|
42
|
+
:enable_dd => true
|
43
|
+
end
|
44
|
+
|
45
|
+
var :root do
|
46
|
+
Ext::Tree::AsyncTreeNode :id => 'source', :text => 'root', :draggable => false
|
47
|
+
end
|
48
|
+
|
49
|
+
# tree.setRootNode(root);
|
50
|
+
tree.set_root_node(root)
|
51
|
+
|
52
|
+
tree.render
|
53
|
+
root.expand
|
54
|
+
end
|
data/lib/ext/assigned.rb
ADDED
data/lib/ext/base.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
class Ext::Base
|
2
|
+
include ActionView::Helpers::JavaScriptHelper
|
3
|
+
|
4
|
+
dsl_accessor :ext_class
|
5
|
+
dsl_accessor :fixed_args, :default=>[], :writer=>proc{|v| [v].flatten}
|
6
|
+
delegate :ext_class, :fixed_args, :to=>"self.class"
|
7
|
+
|
8
|
+
class << self
|
9
|
+
def to_json
|
10
|
+
ext_class.to_s
|
11
|
+
end
|
12
|
+
|
13
|
+
def ext_patch(file_path)
|
14
|
+
Ext.patches[name] = File.read(file_path).gsub(/\A.*?\n__END__\n/m, '')
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(*args)
|
19
|
+
@options = args.optionize(*fixed_args)
|
20
|
+
end
|
21
|
+
|
22
|
+
def fixed_values
|
23
|
+
fixed_args.map{|i| @options[i]}
|
24
|
+
end
|
25
|
+
|
26
|
+
def options
|
27
|
+
returning opts = @options.dup do
|
28
|
+
fixed_args.each{|arg| opts.delete(arg)}
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def option(key)
|
33
|
+
key = key.to_s.intern
|
34
|
+
if @options.has_key?(key)
|
35
|
+
@options[key]
|
36
|
+
else
|
37
|
+
__send__ key
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def camelize_keys(hash)
|
42
|
+
return hash unless hash.is_a?(Hash)
|
43
|
+
|
44
|
+
hash.inject({}) do |options, (key, value)|
|
45
|
+
key = key.to_s.camelize(:lower) if key.to_s.include?('_')
|
46
|
+
options[key.to_s] = value
|
47
|
+
options
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def args
|
52
|
+
array = fixed_values
|
53
|
+
array << camelize_keys(options) unless options.empty?
|
54
|
+
return array
|
55
|
+
end
|
56
|
+
|
57
|
+
def to_json
|
58
|
+
params = args.map(&:to_json).join(', ')
|
59
|
+
"new %s(%s)" % [ext_class, params]
|
60
|
+
end
|
61
|
+
|
62
|
+
def to_s
|
63
|
+
to_json
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
def klass
|
68
|
+
if @options[:class].is_a?(Class)
|
69
|
+
@options[:class]
|
70
|
+
else
|
71
|
+
nil
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
end
|
data/lib/ext/code.rb
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
module Ext::Controllers::Paginate
|
2
|
+
def self.included(base)
|
3
|
+
base.extend ClassMethods
|
4
|
+
end
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def ext_paginate(*args)
|
8
|
+
options = Options.parse(*args)
|
9
|
+
dsl_accessor :ext_paginate_options, :default=>options
|
10
|
+
include InstanceMethods
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
module InstanceMethods
|
16
|
+
|
17
|
+
public
|
18
|
+
def index
|
19
|
+
@opts = index_options
|
20
|
+
render :inline=>"<%= ext_include %><%= ext_paginate @opts %><%= ext_grid %>"
|
21
|
+
end
|
22
|
+
|
23
|
+
def list
|
24
|
+
opts = {
|
25
|
+
:select => options[:select],
|
26
|
+
:offset => [params[:start].to_i-1, 0].max,
|
27
|
+
:limit => [params[:limit].to_i, options[:limit]].max,
|
28
|
+
:order => sorts.blank? ? nil : sorts.map{|i| "%s %s" % [i, params[:dir]]}.join(', '),
|
29
|
+
}
|
30
|
+
json = {
|
31
|
+
"count" => count.to_s,
|
32
|
+
"items" => search(opts).map{|item| data(item)},
|
33
|
+
}.to_json
|
34
|
+
render :text=>json
|
35
|
+
end
|
36
|
+
|
37
|
+
def show
|
38
|
+
record = options[:model].find(params[:id])
|
39
|
+
render :text=>data(record).to_json
|
40
|
+
end
|
41
|
+
|
42
|
+
def update
|
43
|
+
pkey = options[:model].primary_key
|
44
|
+
attributes = CGIMethods.parse_request_parameters(request.cgi.params)
|
45
|
+
options[:model].update(params[pkey], attributes)
|
46
|
+
render :nothing=>true
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
def count
|
51
|
+
options[:model].count
|
52
|
+
end
|
53
|
+
|
54
|
+
def search(opts)
|
55
|
+
options[:model].find(:all, opts)
|
56
|
+
end
|
57
|
+
|
58
|
+
def data(record)
|
59
|
+
options[:model].columns.each do |column|
|
60
|
+
case column.type
|
61
|
+
when :datetime, :time, :date
|
62
|
+
record[column.name] = record[column.name].to_i unless record[column.name].blank?
|
63
|
+
end
|
64
|
+
end
|
65
|
+
record.attributes
|
66
|
+
end
|
67
|
+
|
68
|
+
def options
|
69
|
+
self.class.ext_paginate_options
|
70
|
+
end
|
71
|
+
|
72
|
+
def sorts
|
73
|
+
key = params[:sort].to_s
|
74
|
+
key = options[:model].primary_key if key.blank?
|
75
|
+
[key]
|
76
|
+
end
|
77
|
+
|
78
|
+
def index_options
|
79
|
+
options.merge(:class=>options[:model])
|
80
|
+
end
|
81
|
+
|
82
|
+
def rescue_action(err)
|
83
|
+
return super unless params.has_key?('ext')
|
84
|
+
|
85
|
+
logger.warn err.inspect
|
86
|
+
|
87
|
+
@title = err.class.name
|
88
|
+
@body = "%s<BR>%s" % [err.message, err.application_backtrace.first]
|
89
|
+
|
90
|
+
render :update do |page|
|
91
|
+
title = escape_javascript(@title)
|
92
|
+
body = escape_javascript(@body)
|
93
|
+
page << "Ext.MessageBox.alert('#{ title }', '#{body}')"
|
94
|
+
end
|
95
|
+
response.headers['Status'] = 500
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
module Options
|
102
|
+
module_function
|
103
|
+
def parse(*args)
|
104
|
+
options = args.optionize(:model, :limit)
|
105
|
+
|
106
|
+
options[:ds_class] ||= Ext::Data::ActiveStore
|
107
|
+
options[:cm_class] ||= Ext::Grid::ActiveRecord
|
108
|
+
options[:grid_class] ||= options[:edit] ? Ext::Grid::EditorGrid : Ext::Grid::Grid
|
109
|
+
|
110
|
+
options[:limit] = [options[:limit].to_i, 10].max
|
111
|
+
options[:model] =
|
112
|
+
case (model = options[:model])
|
113
|
+
when Class then model
|
114
|
+
when String, Symbol then model.to_s.classify.constantize
|
115
|
+
else raise ArgumentError, "ext_paginate needs a class but got `#{model.class}'"
|
116
|
+
end
|
117
|
+
options[:select] =
|
118
|
+
case (select = options[:select])
|
119
|
+
when NilClass then "*"
|
120
|
+
when Array then select.join(', ')
|
121
|
+
else select.to_s
|
122
|
+
end
|
123
|
+
|
124
|
+
return options
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
data/lib/ext/data.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
class Ext::Data::ActiveResource < Ext::Data::HttpProxy
|
2
|
+
ext_class "Ext.data.ActiveResource"
|
3
|
+
ext_patch __FILE__
|
4
|
+
end
|
5
|
+
|
6
|
+
__END__
|
7
|
+
// Ext.data.ActiveRequest
|
8
|
+
|
9
|
+
Ext.data.ActiveRequest = function(config){
|
10
|
+
Ext.data.ActiveRequest.superclass.constructor.call(this, config);
|
11
|
+
if (this.actionName) {
|
12
|
+
this.url = config.url.replace(new RegExp('[^/]*$'), this.actionName);
|
13
|
+
}
|
14
|
+
if (this.before) { this.on("requestbefore", this.before) }
|
15
|
+
if (this.complete) { this.on("requestcomplete", this.complete) }
|
16
|
+
if (this.exception) { this.on("requestexception", this.exception) }
|
17
|
+
};
|
18
|
+
Ext.extend(Ext.data.ActiveRequest, Ext.data.Connection, {});
|
19
|
+
|
20
|
+
|
21
|
+
// Ext.data.ActiveRequest.List
|
22
|
+
|
23
|
+
Ext.data.ActiveRequest.List = function(config){
|
24
|
+
Ext.data.ActiveRequest.List.superclass.constructor.call(this, config);
|
25
|
+
};
|
26
|
+
Ext.extend(Ext.data.ActiveRequest.List, Ext.data.ActiveRequest, { actionName : 'list'});
|
27
|
+
|
28
|
+
|
29
|
+
// Ext.data.ActiveRequest.Update
|
30
|
+
|
31
|
+
Ext.data.ActiveRequest.Update = function(config){
|
32
|
+
Ext.data.ActiveRequest.Update.superclass.constructor.call(this, config);
|
33
|
+
};
|
34
|
+
|
35
|
+
Ext.extend(Ext.data.ActiveRequest.Update, Ext.data.ActiveRequest, {
|
36
|
+
actionName : 'update',
|
37
|
+
|
38
|
+
buildParams : function(options) {
|
39
|
+
return options.record.getData();
|
40
|
+
},
|
41
|
+
|
42
|
+
execute : function(options) {
|
43
|
+
options["params"] = this.buildParams(options);
|
44
|
+
this.request(options);
|
45
|
+
},
|
46
|
+
|
47
|
+
complete : function(conn, response, options){
|
48
|
+
var code = response.responseText;
|
49
|
+
try {
|
50
|
+
eval(code);
|
51
|
+
options.record.commit();
|
52
|
+
options.store.reload();
|
53
|
+
}catch(e){
|
54
|
+
options.record.reject();
|
55
|
+
this.handleFailure(response, e);
|
56
|
+
}
|
57
|
+
},
|
58
|
+
|
59
|
+
exception : function(conn, response, options){
|
60
|
+
var content_type = response.getResponseHeader['Content-Type'];
|
61
|
+
if (content_type && content_type.indexOf('text/javascript') != -1) {
|
62
|
+
try {
|
63
|
+
eval(response.responseText);
|
64
|
+
}catch(e){
|
65
|
+
Ext.MessageBox.alert('Update Response Error', response.responseText);
|
66
|
+
}
|
67
|
+
} else {
|
68
|
+
Ext.MessageBox.alert('Update Error', response.responseText);
|
69
|
+
}
|
70
|
+
}
|
71
|
+
});
|
72
|
+
|
73
|
+
|
74
|
+
// Ext.data.ActiveResource
|
75
|
+
|
76
|
+
|
77
|
+
Ext.data.ActiveResource = function(config){
|
78
|
+
Ext.data.ActiveResource.superclass.constructor.call(this, config);
|
79
|
+
this.conn = new Ext.data.ActiveRequest.List({url : this.conn.url});
|
80
|
+
};
|
81
|
+
|
82
|
+
Ext.extend(Ext.data.ActiveResource, Ext.data.HttpProxy, {
|
83
|
+
update: function(store, record) {
|
84
|
+
if (!this.updateConnection) {
|
85
|
+
this.updateConnection = new Ext.data.ActiveRequest.Update({url : this.conn.url});
|
86
|
+
}
|
87
|
+
this.updateConnection.execute({record: record, store: store});
|
88
|
+
}
|
89
|
+
});
|