Permalink
Browse files

initial commit ..

  • Loading branch information...
0 parents commit 785fdf92eed2f386f1b2dc97ed0e5223b4e85670 Galin Yordanov committed Dec 16, 2008
Showing with 271 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +2 −0 CHANGELOG.rdoc
  3. +24 −0 LICENSE
  4. +29 −0 README.rdoc
  5. 0 Rakefile
  6. +50 −0 lib/array_extjs.rb
  7. +137 −0 lib/sequel_extjs.rb
  8. +23 −0 sequel_extjs.gemspec
@@ -0,0 +1,6 @@
+.DS_Store
+doc
+*~
+.#*
+\#*#
+._*
@@ -0,0 +1,2 @@
+== 0.1 - initial release
+ - initial release ;)
24 LICENSE
@@ -0,0 +1,24 @@
+Copyright (c) 2008 Galin Yordanov <gyordanov@gmail.com>, Nabbr Inc
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+3. The name of the author may not be used to endorse or promote products
+ derived from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,29 @@
+== is :ExtJS
+
+Add a to_extjs function to any Sequel Dataset, it will generate json that is consumable by the ExtJS JsonStore.
+
+Take a look at Sequel::Plugins::ExtJS::DatasetMethods.to_extjs function for more information.
+
+== Usage
+ # this will require the Sequel's ExtJS plugin
+ require 'sequel_extjs'
+ # this will add .to_extjs on the Array class, so you can use the same code for datasets and array results
+ # not required, if you don't like additions to the Array class
+ require 'array_extjs'
+
+ class MyModel < Sequel::Model(:mymodel)
+ is :ExtJS
+ end
+
+ # now output all MyModel records in a way the JsonStore expects:
+ MyModel.to_extjs
+ # or use any filters before that
+ MyModel.filter(:status => true).to_exts
+ # or limit it for pagination
+ cnt = MyModel.filter(:status => true).count
+ MyModel.filter(:status => true).limit(10,100).to_extjs(cnt)
+ # also give it a block and it will more or less work like a 'map'
+ MyModel.filter(:status => true).to_exts do |rec|
+ rec[:newprop] = "Status is #{rec.status}"
+ rec
+ end
No changes.
@@ -0,0 +1,50 @@
+# Copyright (c) 2008 Galin Yordanov <gyordanov@gmail.com>, Nabbr Inc
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+# 3. The name of the author may not be used to endorse or promote products
+# derived from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+# Part of the ExtJS sequel plugin, this will allow us to use the .to_extjs after .all in the filter chain
+class Array
+ def to_extjs(id = "id", overwrite_count = nil)
+ data = self
+ return '{}' if data.size == 0
+ unless overwrite_count
+ overwrite_count = data.size
+ end
+
+ if block_given?
+ data.map! {|rec| yield rec}
+ end
+ fields = data.first.keys.map{|a| {:name => a}}
+ {
+ :totalCount => overwrite_count,
+ :metaData => {
+ :totalProperty => 'totalCount',
+ :root => 'result',
+ :id => id,
+ :fields => fields
+ },
+ :result => data
+ }.to_json
+ end
+end
@@ -0,0 +1,137 @@
+# Copyright (c) 2008 Galin Yordanov <gyordanov@gmail.com>, Nabbr Inc
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+# 3. The name of the author may not be used to endorse or promote products
+# derived from this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+module Sequel
+ module Plugins
+
+ # This module will extend the regular Model to add few ExtJS related functions, mainly to_json and to_extjs
+ #
+ # Take a look at the Sequel::Plugins::ExtJS::DatasetMethods.to_extjs function
+ module ExtJS
+
+ def self.apply(model, *a)
+ end
+
+ module InstanceMethods
+
+ # return a json hash from the current Model instance,
+ # this will work even on joins and it will include all selected fields
+ def to_json(*a)
+ values.to_json(*a)
+ end
+
+ # Same as the dataset method, but it only works for single records (including ones that are not saved yet)
+ def to_extjs()
+ data = self
+ if block_given?
+ data = yield self
+ end
+ fields = data.keys.map{|a| {:name => a}}
+
+ {
+ :totalCount => 1,
+ :metaData => {
+ :totalProperty => 'totalCount',
+ :root => 'result',
+ :id => primary_key,
+ :fields => fields
+ },
+ :result => [data]
+ }.to_json
+ end
+
+
+ end
+
+ module ClassMethods
+ # this will return a properly formated ExtJS metaData hash for its JsonReader, keep in mind that it will only return
+ # fields that beling to that model and wont honor joins (its a class method after all).
+ # To include extra fields please pass a array with the name of the fields as a parameter
+ def extjs_metadata(ary = [])
+ (self.columns + ary).inject(""){|res,el| "#{res}{name : '#{el}'}," }
+ end
+ end
+
+ module DatasetMethods
+ # return a array of json hashes for the current dataset (i.e. current query result with multiple records)
+ def to_json
+ map{|b| b.values}.to_json
+ end
+
+ # Generate everything ExtJS needs to display this dataset into a grid
+ # you can overwrite the returned count (in case you are using pagination) by supplying it as a first parameter
+ # this function also accepts a block that you can iterate over and modify the element (adding/removing keys) (more or less replicate map)
+ # this should work just about any dataset (including joins and stuff)
+ # For simple dataset to ExtJS use:
+ # MyModel.to_extjs # all records
+ # MyModel.filter(:type => 'primary').to_extjs # (only the ones with type 'primary')
+ #
+ # Using blocks:
+ # This sample will return JSON structure that is the same as MyModel.to_extjs BUT with the added 'newkey' element for all records
+ # MyModel.to_extjs do |mymodel|
+ # mymodel[:newkey] = 'somethingelse'
+ # mymodel
+ # end
+ #
+ # To remove a column(s) you have 2 options
+ #
+ # 1) use MyModel.select(:id,:name).to_extjs - it will display only the selected fields OR
+ #
+ # 2) use blocks to do it
+ # MyModel.to_extjs do |swf|
+ # res = mymodel.values
+ # res[:newkey] = 'somethingelse'
+ # res.delete(:name)
+ # res
+ # end
+ #
+ def to_extjs(overwrite_count = nil)
+ data = all
+ return '{}' if data.size == 0
+ unless overwrite_count
+ overwrite_count = data.size
+ end
+
+ if block_given?
+ data.map! {|rec| yield rec}
+ end
+ fields = data.first.keys.map{|a| {:name => a}}
+
+ {
+ :totalCount => overwrite_count,
+ :metaData => {
+ :totalProperty => 'totalCount',
+ :root => 'result',
+ :id => first.primary_key,
+ :fields => fields
+ },
+ :result => data
+ }.to_json
+ end
+ end
+
+ end
+ end
+end
@@ -0,0 +1,23 @@
+Gem::Specification.new do |s|
+ s.name = "sequel_extjs"
+ s.version = "0.1"
+ s.date = "2008-12-10"
+ s.summary = "Transform Sequel Datasets to ExtJS JsonStore feed"
+ s.email = "gyordanov@gmail.com"
+ s.homepage = "http://github.com/gyordanov/sequel_extjs"
+ s.description = "Transform Sequel Dataset to ExtJS JsonStore feed"
+ s.has_rdoc = true
+ s.authors = ["Galin Yordanov"]
+ s.files = ["CHANGELOG.rdoc",
+ "README.rdoc",
+ "Rakefile",
+ "sequel_extjs.gemspec",
+ ".gitignore",
+ "LICENSE",
+ "lib/sequel_extjs.rb",
+ "lib/array_extjs.rb"]
+ s.require_paths = ["lib"]
+ s.rdoc_options = ["--main", "README.rdoc"]
+ s.extra_rdoc_files = ["README.rdoc","CHANGELOG.rdoc"]
+ s.add_dependency("sequel", ["> 2.7.0"])
+end

0 comments on commit 785fdf9

Please sign in to comment.