padrino-admin 0.2.2 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.2.5
data/lib/padrino-admin.rb CHANGED
@@ -0,0 +1 @@
1
+ Dir[File.dirname(__FILE__) + '/padrino-admin/**/*.rb'].each {|file| require file }
@@ -0,0 +1,170 @@
1
+ require 'rubygems'
2
+ require 'yaml'
3
+ require 'erb'
4
+ require 'json/pure'
5
+
6
+ module ExtJs
7
+
8
+ class ConfigError < RuntimeError; end
9
+
10
+ # This class it's used for JSON variables.
11
+ # Normally if we convert this { :function => "alert('Test')" } will be:
12
+ #
13
+ # { "function": "alert('Test')" }
14
+ #
15
+ # But if in our javascript need to "eval" this function is not possible because
16
+ # it's a string.
17
+ #
18
+ # Using ExtJs::Variable the result will be:
19
+ #
20
+ # { "function" : alert('Test') }
21
+ #
22
+ # Normally an ExtJs Variable can be handled with ExtJs Config like:
23
+ #
24
+ # function: !js alert('Test')
25
+ #
26
+ class Variable < String
27
+ yaml_as "tag:yaml.org,2002:js"
28
+
29
+ def to_json(*a) #:nodoc:
30
+ self
31
+ end
32
+ end
33
+
34
+ # This class it's used for write in a new and simple way json.
35
+ #
36
+ # In ExtJs framework generally each component have a configuration written in json.
37
+ #
38
+ # Write this config in ruby it's not the best choice think this example:
39
+ #
40
+ # # A Generic grid config in JavaScript:
41
+ # var gridPanel = new Ext.grid.GridPanel({
42
+ # bbar: gridPanelPagingToolbar,
43
+ # clicksToEdit: 1,
44
+ # cm: gridPanelColumnModel,
45
+ # region: "center",
46
+ # sm: gridPanelCheckboxSelectionModel,
47
+ # viewConfig: {"forceFit":true},
48
+ # plugins: [new Ext.grid.Search()],
49
+ # border: false,
50
+ # tbar: gridPanelToolbar,
51
+ # id: "grid-accounts",
52
+ # bodyBorder: false,
53
+ # store: gridPanelGroupingStore,
54
+ # view: gridPanelGroupingView
55
+ # });
56
+ #
57
+ # # A Gneric grid config in Ruby:
58
+ # { :bbar => ExtJs::Variable.new('gridPanelPagingToolbar'), :clicksToEdit => 1,
59
+ # :cm => ExtJs::Variable.new('gridPanelColumnModel'), :region => "center",
60
+ # :sm => ExtJs::Variable.new('gridPanelCheckboxSelectionModel'),
61
+ # :viewConfig => { :forceFit => true }, plugins => [ExtJs::Variable.new('new Ext.grid.Search()'].
62
+ # :border => false ... more more code...
63
+ #
64
+ # As you can see writing json in pure ruby (in this case with hash) require much time and is
65
+ # <tt>less</tt> readable.
66
+ #
67
+ # For this reason we build an ExtJs Config, that basically it's an yaml file with
68
+ # some new great functions so the example above will be:
69
+ #
70
+ # # A Generic grid config in ExtJs Config:
71
+ # gridPanel:
72
+ # bbar: !js gridPanelPagingToolbar
73
+ # clicksToEdit: 1,
74
+ # cm: !js gridPanelColumnModel
75
+ # region: center
76
+ # sm: !js gridPanelCheckboxSelectionModel
77
+ # viewConfig:
78
+ # forceFit: true
79
+ # plugins: [!js new Ext.grid.Search()]
80
+ # border: false
81
+ # tbar: !js gridPanelToolbar
82
+ # id: grid-accounts
83
+ # bodyBorder: false
84
+ # store: !js gridPanelGroupingStore
85
+ # view: !js gridPanelGroupingView
86
+ #
87
+ # Now you see that it's more readable, simple and coincise!
88
+ #
89
+ # But our ExtJs config can act also as an xml or an erb partial. See this code:
90
+ #
91
+ # # A template
92
+ # tempate:
93
+ # tbar:
94
+ # here: a custom config
95
+ # grid:
96
+ # viewConfig:
97
+ # forceFit: true
98
+ # plugins: [!js new Ext.grid.Search()]
99
+ # border: false
100
+ #
101
+ # We can "grep" this config in our template with:
102
+ #
103
+ # # A generic grid
104
+ # gridPanel:
105
+ # <<: %template/grid
106
+ # border: true
107
+ #
108
+ # The result will be:
109
+ #
110
+ # gridPanel:
111
+ # viewConfig:
112
+ # forceFit: true
113
+ # plugins: [!js new Ext.grid.Search()]
114
+ # border: true # overidden
115
+ #
116
+ # See our test for more complex examples.
117
+ #
118
+ class Config < Hash
119
+
120
+ # Initialize a new config parsing an Hash
121
+ def initialize(data)
122
+ @data = data
123
+ parsed = parse(@data)
124
+ super
125
+ replace parsed
126
+ end
127
+
128
+ # Load a new config from an yml file and return a parsed hash.
129
+ def self.load_file(path, binding=nil)
130
+ self.load(File.read(path), binding)
131
+ end
132
+
133
+ # Load a new config from a yaml "string" and return a parsed hash.
134
+ def self.load(string, binding=nil)
135
+ self.new YAML.parse(ERB.new(string).result(binding))
136
+ end
137
+
138
+ private
139
+ def parse(node=nil, key=nil)
140
+ case node.value
141
+ when String
142
+ if node.value =~ /^%{1}(.*)/
143
+ node = parse(@data.select($1).first)
144
+ end
145
+ node.respond_to?(:transform) ? node.transform : node
146
+ when Hash
147
+ parsed = {}
148
+ node.value.each do |k,v|
149
+ if k.value == "<<"
150
+ node = parse(v)
151
+ if node.is_a?(Hash)
152
+ node.merge!(parsed)
153
+ end
154
+ parsed = node
155
+ else
156
+ parsed[k.value] = parse(v)
157
+ end
158
+ end
159
+ parsed
160
+ when Array
161
+ parsed = []
162
+ node.value.each do |v|
163
+ node = parse(v)
164
+ node.is_a?(Array) ? parsed.concat(node) : parsed.push(node)
165
+ end
166
+ parsed
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,25 @@
1
+ require 'thor'
2
+
3
+ module Padrino
4
+ module Generators
5
+
6
+ class Backend < Thor::Group
7
+
8
+ # Add this generator to our padrino-gen
9
+ Padrino::Generators.add_generator(:backend, self)
10
+
11
+ # Define the source template root
12
+ def self.source_root; File.expand_path(File.dirname(__FILE__)); end
13
+ def self.banner; "padrino-gen controller [name]"; end
14
+
15
+ # Include related modules
16
+ include Thor::Actions
17
+ include Padrino::Generators::Actions
18
+
19
+ desc "Description:\n\n\tpadrino-gen controller generates a new Padrino Admin"
20
+
21
+ class_option :root, :aliases => '-r', :default => nil, :type => :string
22
+ end
23
+
24
+ end
25
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{padrino-admin}
8
- s.version = "0.2.2"
8
+ s.version = "0.2.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Padrino Team", "Nathan Esquenazi", "Davide D'Agostino", "Arthur Chiu"]
12
- s.date = %q{2009-12-01}
12
+ s.date = %q{2009-12-21}
13
13
  s.description = %q{Admin View for Padrino applications}
14
14
  s.email = %q{nesquena@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -23,9 +23,12 @@ Gem::Specification.new do |s|
23
23
  "Rakefile",
24
24
  "VERSION",
25
25
  "lib/padrino-admin.rb",
26
+ "lib/padrino-admin/ext_js/config.rb",
27
+ "lib/padrino-admin/generators/backend.rb",
26
28
  "padrino-admin.gemspec",
27
29
  "test/helper.rb",
28
- "test/test_padrino_admin.rb"
30
+ "test/test_padrino_admin.rb",
31
+ "test/test_parsing.rb"
29
32
  ]
30
33
  s.homepage = %q{http://github.com/padrino/padrino-framework/tree/master/padrino-admin}
31
34
  s.rdoc_options = ["--charset=UTF-8"]
data/test/helper.rb CHANGED
@@ -7,6 +7,8 @@ require 'webrat'
7
7
 
8
8
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
9
  $LOAD_PATH.unshift(File.dirname(__FILE__))
10
+
11
+ require 'padrino-gen'
10
12
  require 'padrino-admin'
11
13
 
12
14
  class Test::Unit::TestCase
@@ -0,0 +1,136 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ class ParsingTest < Test::Unit::TestCase
4
+
5
+ should "Parse Nested Childs" do
6
+ config = ExtJs::Config.load <<-YAML
7
+ foo:
8
+ bar:
9
+ name: Fred
10
+ bar: %foo/bar
11
+ YAML
12
+ assert_equal config["foo"]["bar"], config["bar"]
13
+ end
14
+
15
+ should "Parse JS and Nested JS" do
16
+ config = ExtJs::Config.load <<-YAML
17
+ nested:
18
+ fn: !js function(){ alert('nested fn') }
19
+ fn: !js function(){ alert('fn') }
20
+ array: [!js function(){ alert('array') }]
21
+ test_one: %fn
22
+ test_two: %nested/fn
23
+ test_three:
24
+ no_nested: %fn
25
+ nested: %nested/fn
26
+ YAML
27
+
28
+ assert_kind_of ExtJs::Variable, config["test_one"]
29
+ assert_kind_of ExtJs::Variable, config["test_one"]
30
+ assert_kind_of ExtJs::Variable, config["test_three"]["no_nested"]
31
+ assert_kind_of ExtJs::Variable, config["test_three"]["nested"]
32
+ assert_kind_of ExtJs::Variable, config["array"].first
33
+
34
+ assert_equal "function(){ alert('fn') }", config["test_one"]
35
+ assert_equal "function(){ alert('nested fn') }", config["test_two"]
36
+ assert_equal "function(){ alert('fn') }", config["test_three"]["no_nested"]
37
+ assert_equal "function(){ alert('nested fn') }", config["test_three"]["nested"]
38
+ assert_equal "function(){ alert('array') }", config["array"].first
39
+ end
40
+
41
+ should "Parse a multinested YAML" do
42
+ config = ExtJs::Config.load <<-YAML
43
+ buttons:
44
+ - id: add
45
+ text: Add Product
46
+ - id: delete
47
+ text: Delete Product
48
+ default:
49
+ tbar:
50
+ buttons: %buttons
51
+ grid:
52
+ tbar: %default/tbar
53
+ YAML
54
+ assert_equal config["default"]["tbar"], config["grid"]["tbar"]
55
+ assert_equal config["buttons"], config["default"]["tbar"]["buttons"]
56
+ assert_equal config["buttons"], config["grid"]["tbar"]["buttons"]
57
+ assert_equal ["add", "delete"], config["grid"]["tbar"]["buttons"].collect { |b| b["id"] }
58
+
59
+ end
60
+
61
+ should "Parse array and hashes" do
62
+ config = ExtJs::Config.load <<-YAML
63
+ a: a
64
+ b: b
65
+ c: c
66
+ array: [%a, %b, %c]
67
+ hash: { a: %a, b: %b, c: %c }
68
+ YAML
69
+ assert_equal ["a", "b", "c"], config["array"]
70
+ assert_equal({"a" => "a", "b" => "b", "c" => "c"}, config["hash"])
71
+ end
72
+
73
+ should "Merge config" do
74
+ config = ExtJs::Config.load <<-YAML
75
+ default:
76
+ grid:
77
+ editable: false
78
+ template: standard
79
+ cls: default
80
+ tbar:
81
+ buttons:
82
+ - text: Add
83
+ cls: x-btn-text-icon add
84
+ - text: Delete
85
+ disabled: true
86
+ cls: x-btn-text-icon print
87
+ handler: !js delete
88
+
89
+ grid:
90
+ <<: %default/grid
91
+ editable: true
92
+ title: Elenco <%= @title %>
93
+ basepath: /backend/orders
94
+ sm: checkbox
95
+ template: custom
96
+ tbar:
97
+ buttons:
98
+ - <<: %default/tbar/buttons
99
+ - text: Test
100
+ YAML
101
+ assert_equal true, config["grid"]["editable"]
102
+ assert_equal "default", config["grid"]["cls"]
103
+ assert_equal "custom", config["grid"]["template"]
104
+ assert_equal ["Add", "Delete", "Test"], config["grid"]["tbar"]["buttons"].collect { |b| b["text"] }
105
+ end
106
+
107
+ should "Merge a complex config" do
108
+ config = ExtJs::Config.load <<-YAML
109
+ default:
110
+ grid:
111
+ editable: false
112
+ template: standard
113
+ cls: default
114
+ tbar:
115
+ buttons:
116
+ - text: Add
117
+ cls: x-btn-text-icon add
118
+ - text: Delete
119
+ disabled: true
120
+ cls: x-btn-text-icon print
121
+ handler: !js delete
122
+
123
+ grid:
124
+ <<: %default/grid
125
+ editable: true
126
+ title: Elenco <%= @title %>
127
+ basepath: /backend/orders
128
+ sm: checkbox
129
+ template: custom
130
+ YAML
131
+ assert_equal true, config["grid"]["editable"]
132
+ assert_equal "default", config["grid"]["cls"]
133
+ assert_equal "custom", config["grid"]["template"]
134
+ assert_equal ["Add", "Delete"], config["grid"]["tbar"]["buttons"].collect { |b| b["text"] }
135
+ end
136
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: padrino-admin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Padrino Team
@@ -12,7 +12,7 @@ autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
14
 
15
- date: 2009-12-01 00:00:00 -08:00
15
+ date: 2009-12-21 00:00:00 -08:00
16
16
  default_executable:
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
@@ -101,9 +101,12 @@ files:
101
101
  - Rakefile
102
102
  - VERSION
103
103
  - lib/padrino-admin.rb
104
+ - lib/padrino-admin/ext_js/config.rb
105
+ - lib/padrino-admin/generators/backend.rb
104
106
  - padrino-admin.gemspec
105
107
  - test/helper.rb
106
108
  - test/test_padrino_admin.rb
109
+ - test/test_parsing.rb
107
110
  has_rdoc: true
108
111
  homepage: http://github.com/padrino/padrino-framework/tree/master/padrino-admin
109
112
  licenses: []