gyordanov-sequel_extjs 0.1
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/.gitignore +6 -0
- data/CHANGELOG.rdoc +2 -0
- data/LICENSE +24 -0
- data/README.rdoc +29 -0
- data/Rakefile +0 -0
- data/lib/array_extjs.rb +50 -0
- data/lib/sequel_extjs.rb +137 -0
- data/sequel_extjs.gemspec +23 -0
- metadata +71 -0
data/CHANGELOG.rdoc
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
Copyright (c) 2008 Galin Yordanov <gyordanov@gmail.com>, Nabbr Inc
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions
|
6
|
+
are met:
|
7
|
+
1. Redistributions of source code must retain the above copyright
|
8
|
+
notice, this list of conditions and the following disclaimer.
|
9
|
+
2. Redistributions in binary form must reproduce the above copyright
|
10
|
+
notice, this list of conditions and the following disclaimer in the
|
11
|
+
documentation and/or other materials provided with the distribution.
|
12
|
+
3. The name of the author may not be used to endorse or promote products
|
13
|
+
derived from this software without specific prior written permission.
|
14
|
+
|
15
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
16
|
+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
17
|
+
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
18
|
+
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
19
|
+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
20
|
+
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
21
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
22
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
23
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
24
|
+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
== is :ExtJS
|
2
|
+
|
3
|
+
Add a to_extjs function to any Sequel Dataset, it will generate json that is consumable by the ExtJS JsonStore.
|
4
|
+
|
5
|
+
Take a look at Sequel::Plugins::ExtJS::DatasetMethods.to_extjs function for more information.
|
6
|
+
|
7
|
+
== Usage
|
8
|
+
# this will require the Sequel's ExtJS plugin
|
9
|
+
require 'sequel_extjs'
|
10
|
+
# this will add .to_extjs on the Array class, so you can use the same code for datasets and array results
|
11
|
+
# not required, if you don't like additions to the Array class
|
12
|
+
require 'array_extjs'
|
13
|
+
|
14
|
+
class MyModel < Sequel::Model(:mymodel)
|
15
|
+
is :ExtJS
|
16
|
+
end
|
17
|
+
|
18
|
+
# now output all MyModel records in a way the JsonStore expects:
|
19
|
+
MyModel.to_extjs
|
20
|
+
# or use any filters before that
|
21
|
+
MyModel.filter(:status => true).to_exts
|
22
|
+
# or limit it for pagination
|
23
|
+
cnt = MyModel.filter(:status => true).count
|
24
|
+
MyModel.filter(:status => true).limit(10,100).to_extjs(cnt)
|
25
|
+
# also give it a block and it will more or less work like a 'map'
|
26
|
+
MyModel.filter(:status => true).to_exts do |rec|
|
27
|
+
rec[:newprop] = "Status is #{rec.status}"
|
28
|
+
rec
|
29
|
+
end
|
data/Rakefile
ADDED
File without changes
|
data/lib/array_extjs.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# Copyright (c) 2008 Galin Yordanov <gyordanov@gmail.com>, Nabbr Inc
|
2
|
+
# All rights reserved.
|
3
|
+
#
|
4
|
+
# Redistribution and use in source and binary forms, with or without
|
5
|
+
# modification, are permitted provided that the following conditions
|
6
|
+
# are met:
|
7
|
+
# 1. Redistributions of source code must retain the above copyright
|
8
|
+
# notice, this list of conditions and the following disclaimer.
|
9
|
+
# 2. Redistributions in binary form must reproduce the above copyright
|
10
|
+
# notice, this list of conditions and the following disclaimer in the
|
11
|
+
# documentation and/or other materials provided with the distribution.
|
12
|
+
# 3. The name of the author may not be used to endorse or promote products
|
13
|
+
# derived from this software without specific prior written permission.
|
14
|
+
#
|
15
|
+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
16
|
+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
17
|
+
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
18
|
+
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
19
|
+
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
20
|
+
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
21
|
+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
22
|
+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
23
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
24
|
+
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
25
|
+
|
26
|
+
# Part of the ExtJS sequel plugin, this will allow us to use the .to_extjs after .all in the filter chain
|
27
|
+
class Array
|
28
|
+
def to_extjs(id = "id", overwrite_count = nil)
|
29
|
+
data = self
|
30
|
+
return '{}' if data.size == 0
|
31
|
+
unless overwrite_count
|
32
|
+
overwrite_count = data.size
|
33
|
+
end
|
34
|
+
|
35
|
+
if block_given?
|
36
|
+
data.map! {|rec| yield rec}
|
37
|
+
end
|
38
|
+
fields = data.first.keys.map{|a| {:name => a}}
|
39
|
+
{
|
40
|
+
:totalCount => overwrite_count,
|
41
|
+
:metaData => {
|
42
|
+
:totalProperty => 'totalCount',
|
43
|
+
:root => 'result',
|
44
|
+
:id => id,
|
45
|
+
:fields => fields
|
46
|
+
},
|
47
|
+
:result => data
|
48
|
+
}.to_json
|
49
|
+
end
|
50
|
+
end
|
data/lib/sequel_extjs.rb
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
# Copyright (c) 2008 Galin Yordanov <gyordanov@gmail.com>, Nabbr Inc
|
2
|
+
# All rights reserved.
|
3
|
+
#
|
4
|
+
# Redistribution and use in source and binary forms, with or without
|
5
|
+
# modification, are permitted provided that the following conditions
|
6
|
+
# are met:
|
7
|
+
# 1. Redistributions of source code must retain the above copyright
|
8
|
+
# notice, this list of conditions and the following disclaimer.
|
9
|
+
# 2. Redistributions in binary form must reproduce the above copyright
|
10
|
+
# notice, this list of conditions and the following disclaimer in the
|
11
|
+
# documentation and/or other materials provided with the distribution.
|
12
|
+
# 3. The name of the author may not be used to endorse or promote products
|
13
|
+
# derived from this software without specific prior written permission.
|
14
|
+
#
|
15
|
+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
16
|
+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
17
|
+
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
18
|
+
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
19
|
+
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
20
|
+
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
21
|
+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
22
|
+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
23
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
24
|
+
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
25
|
+
|
26
|
+
module Sequel
|
27
|
+
module Plugins
|
28
|
+
|
29
|
+
# This module will extend the regular Model to add few ExtJS related functions, mainly to_json and to_extjs
|
30
|
+
#
|
31
|
+
# Take a look at the Sequel::Plugins::ExtJS::DatasetMethods.to_extjs function
|
32
|
+
module ExtJS
|
33
|
+
|
34
|
+
def self.apply(model, *a)
|
35
|
+
end
|
36
|
+
|
37
|
+
module InstanceMethods
|
38
|
+
|
39
|
+
# return a json hash from the current Model instance,
|
40
|
+
# this will work even on joins and it will include all selected fields
|
41
|
+
def to_json(*a)
|
42
|
+
values.to_json(*a)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Same as the dataset method, but it only works for single records (including ones that are not saved yet)
|
46
|
+
def to_extjs()
|
47
|
+
data = self
|
48
|
+
if block_given?
|
49
|
+
data = yield self
|
50
|
+
end
|
51
|
+
fields = data.keys.map{|a| {:name => a}}
|
52
|
+
|
53
|
+
{
|
54
|
+
:totalCount => 1,
|
55
|
+
:metaData => {
|
56
|
+
:totalProperty => 'totalCount',
|
57
|
+
:root => 'result',
|
58
|
+
:id => primary_key,
|
59
|
+
:fields => fields
|
60
|
+
},
|
61
|
+
:result => [data]
|
62
|
+
}.to_json
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
module ClassMethods
|
69
|
+
# this will return a properly formated ExtJS metaData hash for its JsonReader, keep in mind that it will only return
|
70
|
+
# fields that beling to that model and wont honor joins (its a class method after all).
|
71
|
+
# To include extra fields please pass a array with the name of the fields as a parameter
|
72
|
+
def extjs_metadata(ary = [])
|
73
|
+
(self.columns + ary).inject(""){|res,el| "#{res}{name : '#{el}'}," }
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
module DatasetMethods
|
78
|
+
# return a array of json hashes for the current dataset (i.e. current query result with multiple records)
|
79
|
+
def to_json
|
80
|
+
map{|b| b.values}.to_json
|
81
|
+
end
|
82
|
+
|
83
|
+
# Generate everything ExtJS needs to display this dataset into a grid
|
84
|
+
# you can overwrite the returned count (in case you are using pagination) by supplying it as a first parameter
|
85
|
+
# this function also accepts a block that you can iterate over and modify the element (adding/removing keys) (more or less replicate map)
|
86
|
+
# this should work just about any dataset (including joins and stuff)
|
87
|
+
# For simple dataset to ExtJS use:
|
88
|
+
# MyModel.to_extjs # all records
|
89
|
+
# MyModel.filter(:type => 'primary').to_extjs # (only the ones with type 'primary')
|
90
|
+
#
|
91
|
+
# Using blocks:
|
92
|
+
# This sample will return JSON structure that is the same as MyModel.to_extjs BUT with the added 'newkey' element for all records
|
93
|
+
# MyModel.to_extjs do |mymodel|
|
94
|
+
# mymodel[:newkey] = 'somethingelse'
|
95
|
+
# mymodel
|
96
|
+
# end
|
97
|
+
#
|
98
|
+
# To remove a column(s) you have 2 options
|
99
|
+
#
|
100
|
+
# 1) use MyModel.select(:id,:name).to_extjs - it will display only the selected fields OR
|
101
|
+
#
|
102
|
+
# 2) use blocks to do it
|
103
|
+
# MyModel.to_extjs do |swf|
|
104
|
+
# res = mymodel.values
|
105
|
+
# res[:newkey] = 'somethingelse'
|
106
|
+
# res.delete(:name)
|
107
|
+
# res
|
108
|
+
# end
|
109
|
+
#
|
110
|
+
def to_extjs(overwrite_count = nil)
|
111
|
+
data = all
|
112
|
+
return '{}' if data.size == 0
|
113
|
+
unless overwrite_count
|
114
|
+
overwrite_count = data.size
|
115
|
+
end
|
116
|
+
|
117
|
+
if block_given?
|
118
|
+
data.map! {|rec| yield rec}
|
119
|
+
end
|
120
|
+
fields = data.first.keys.map{|a| {:name => a}}
|
121
|
+
|
122
|
+
{
|
123
|
+
:totalCount => overwrite_count,
|
124
|
+
:metaData => {
|
125
|
+
:totalProperty => 'totalCount',
|
126
|
+
:root => 'result',
|
127
|
+
:id => first.primary_key,
|
128
|
+
:fields => fields
|
129
|
+
},
|
130
|
+
:result => data
|
131
|
+
}.to_json
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "sequel_extjs"
|
3
|
+
s.version = "0.1"
|
4
|
+
s.date = "2008-12-16"
|
5
|
+
s.summary = "Transform Sequel Datasets to ExtJS JsonStore feed"
|
6
|
+
s.email = "gyordanov@gmail.com"
|
7
|
+
s.homepage = "http://github.com/gyordanov/sequel_extjs"
|
8
|
+
s.description = "Transform Sequel Dataset to ExtJS JsonStore feed"
|
9
|
+
s.has_rdoc = true
|
10
|
+
s.authors = ["Galin Yordanov"]
|
11
|
+
s.files = ["CHANGELOG.rdoc",
|
12
|
+
"README.rdoc",
|
13
|
+
"Rakefile",
|
14
|
+
"sequel_extjs.gemspec",
|
15
|
+
".gitignore",
|
16
|
+
"LICENSE",
|
17
|
+
"lib/sequel_extjs.rb",
|
18
|
+
"lib/array_extjs.rb"]
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
s.rdoc_options = ["--main", "README.rdoc"]
|
21
|
+
s.extra_rdoc_files = ["README.rdoc","CHANGELOG.rdoc"]
|
22
|
+
s.add_dependency("sequel", ["> 2.7.0"])
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gyordanov-sequel_extjs
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Galin Yordanov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-12-16 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: sequel
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.7.0
|
24
|
+
version:
|
25
|
+
description: Transform Sequel Dataset to ExtJS JsonStore feed
|
26
|
+
email: gyordanov@gmail.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- README.rdoc
|
33
|
+
- CHANGELOG.rdoc
|
34
|
+
files:
|
35
|
+
- CHANGELOG.rdoc
|
36
|
+
- README.rdoc
|
37
|
+
- Rakefile
|
38
|
+
- sequel_extjs.gemspec
|
39
|
+
- .gitignore
|
40
|
+
- LICENSE
|
41
|
+
- lib/sequel_extjs.rb
|
42
|
+
- lib/array_extjs.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://github.com/gyordanov/sequel_extjs
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options:
|
47
|
+
- --main
|
48
|
+
- README.rdoc
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.2.0
|
67
|
+
signing_key:
|
68
|
+
specification_version: 2
|
69
|
+
summary: Transform Sequel Datasets to ExtJS JsonStore feed
|
70
|
+
test_files: []
|
71
|
+
|