Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

much better generator structure and flexibility - all tests pass

  • Loading branch information...
commit 9a9b9cd2a59f77eadea8386667339cc59fd16371 1 parent 706a353
Kristian Mandrup authored September 10, 2012
15  lib/apotomo/cell/rendering.rb
... ...
@@ -1,8 +1,6 @@
1 1
 module Cell
2 2
   module Rendering
3 3
     def render(*args)
4  
-      puts "render: #{args}"
5  
-
6 4
       if args.first.kind_of?(Hash) 
7 5
         if args.first[:view]
8 6
           hash = args.first
@@ -14,9 +12,16 @@ def render(*args)
14 12
         end
15 13
       end
16 14
       
17  
-      view_name ||= File.join('views', self.action_name || '')  
18  
-
19  
-      puts "view_name: #{view_name}, #{args}"      
  15
+      view_name ||= File.join('views', self.action_name || '')
  16
+      render_view_for(view_name, *args)
  17
+    rescue
  18
+      # try without view
  19
+      view_name = view_name.gsub(/views\//, '')
  20
+      if args.first.kind_of?(Hash) && args.first[:view]
  21
+        hash = args.first
  22
+        hash[:view] = File.join(hash[:view].to_s)
  23
+        args = [hash, args[1..-1]]
  24
+      end
20 25
       render_view_for(view_name, *args)
21 26
     end
22 27
   end
1  lib/apotomo/widget.rb
@@ -122,7 +122,6 @@ def invoke(state, *args)
122 122
     #
123 123
     # issues a squeaking alert dialog on the page.
124 124
     def render(*args, &block)
125  
-      puts "apotomo render: #{args}"
126 125
       super
127 126
     end
128 127
     
42  lib/generators/apotomo/widget_generator.rb
@@ -9,6 +9,10 @@ def base_path
9 9
         File.join('app/widgets', class_path)
10 10
       end
11 11
 
  12
+      def view_base_path
  13
+        File.join('app/widgets', class_path, file_name)
  14
+      end
  15
+
12 16
       def js_path
13 17
         File.join('app/assets/javascripts/widgets', class_path, file_name)
14 18
       end
@@ -19,13 +23,33 @@ def css_path
19 23
     end
20 24
     
21 25
     module Views
  26
+      extend ActiveSupport::Concern
  27
+
  28
+      included do
  29
+        class_option :parent, :type => :boolean, :default => false, :desc => 'Parent widget'
  30
+
  31
+        source_root File.expand_path('../../templates', __FILE__)
  32
+      end
  33
+
22 34
       def create_views
23 35
         for state in actions do
24 36
           @state  = state
25  
-          @path   = File.join(base_path, 'views', "#{state}.html.#{handler}")  #base_path defined in Cells::Generators::Base.
  37
+          # only make views container for top level widgets, since they are most likely to have
  38
+          # child widgets
  39
+          @path = if parent_widget?
  40
+            File.join(view_base_path, 'views', "#{state}.html.#{handler}")  #base_path defined in Cells::Generators::Base.
  41
+          else
  42
+            File.join(view_base_path, "#{state}.html.#{handler}")  #base_path defined in Cells::Generators::Base.
  43
+          end
26 44
           template "view.#{handler}", @path
27 45
         end
28 46
       end
  47
+
  48
+      protected
  49
+
  50
+      def parent_widget?
  51
+        options[:parent]
  52
+      end      
29 53
     end        
30 54
     
31 55
     class WidgetGenerator < ::Cells::Generators::Base
@@ -38,7 +62,7 @@ class WidgetGenerator < ::Cells::Generators::Base
38 62
       
39 63
       check_class_collision :suffix => "Widget"
40 64
 
41  
-      class_option :js, :type => :boolean, :default => false, :desc => 'Generate javascript asset file'
  65
+      class_option :js,     :type => :boolean, :default => false, :desc => 'Generate javascript asset file'
42 66
 
43 67
       def create_cell_file
44 68
         template 'widget.rb', File.join(base_path, "#{file_name}_widget.rb")
@@ -48,9 +72,15 @@ def create_stylesheet_file
48 72
         template 'widget.css', "#{css_path}_widget.css"
49 73
       end            
50 74
 
51  
-      def creates_script_file
52  
-        return template 'widget.js.coffee', "#{js_path}_widget.js.coffee" if !javascript?
53  
-        template 'widget.js', "#{js_path}_widget.js"
  75
+      def create_script_file
  76
+        puts "create_script_file"
  77
+        if !javascript?
  78
+          # raise "coffee: #{js_path}_widget.js.coffee" 
  79
+          template 'widget.js.coffee', "#{js_path}_widget.js.coffee" 
  80
+        else
  81
+          puts "js: #{js_path}_widget.js" 
  82
+          template 'widget.js', "#{js_path}_widget.js"
  83
+        end
54 84
       end
55 85
 
56 86
       protected
@@ -68,7 +98,7 @@ def js_camelize str
68 98
       end
69 99
 
70 100
       def javascript?
71  
-        options[:js]
  101
+        options[:js] == true
72 102
       end
73 103
     end
74 104
   end
1  lib/generators/erb/widget_generator.rb
@@ -6,7 +6,6 @@ module Generators
6 6
     class WidgetGenerator < CellGenerator
7 7
       include ::Apotomo::Generators::BasePathMethods
8 8
       include ::Apotomo::Generators::Views
9  
-      source_root File.expand_path('../../templates', __FILE__)
10 9
     end
11 10
   end
12 11
 end
3  lib/generators/haml/widget_generator.rb
@@ -5,8 +5,7 @@ module Haml
5 5
   module Generators
6 6
     class WidgetGenerator < CellGenerator
7 7
       include ::Apotomo::Generators::BasePathMethods
8  
-      include ::Apotomo::Generators::Views
9  
-      source_root File.expand_path('../../templates', __FILE__)
  8
+      include ::Apotomo::Generators::Views      
10 9
     end
11 10
   end
12 11
 end
1  lib/generators/slim/widget_generator.rb
@@ -6,7 +6,6 @@ module Generators
6 6
     class WidgetGenerator < CellGenerator
7 7
       include ::Apotomo::Generators::BasePathMethods
8 8
       include ::Apotomo::Generators::Views
9  
-      source_root File.expand_path('../../templates', __FILE__)
10 9
     end
11 10
   end
12 11
 end
1  lib/generators/templates/widget.js
... ...
@@ -1,3 +1,4 @@
  1
+// Define your javascript code for the <%= class_name %> widget
1 2
 var Widget.<%= ns_name %> = namespace('Widget.<%= ns_name %>');
2 3
 
3 4
 Widget.<%= ns_name %> = {
49  test/rails/widget_generator_test.rb
@@ -12,45 +12,52 @@ class WidgetGeneratorTest < Rails::Generators::TestCase
12 12
 
13 13
         run_generator %w(Gerbil squeak snuggle -t test_unit)
14 14
 
15  
-        assert_file "app/widgets/gerbil/gerbil_widget.rb", /class GerbilWidget < Apotomo::Widget/
16  
-        assert_file "app/widgets/gerbil/gerbil_widget.rb", /def snuggle/
17  
-        assert_file "app/widgets/gerbil/gerbil_widget.rb", /def squeak/
  15
+        assert_file "app/widgets/gerbil_widget.rb", /class GerbilWidget < Apotomo::Widget/
  16
+        assert_file "app/widgets/gerbil_widget.rb", /def snuggle/
  17
+        assert_file "app/widgets/gerbil_widget.rb", /def squeak/
18 18
         
19  
-        assert_file "app/widgets/gerbil/views/snuggle.html.erb", %r(app/widgets/gerbil/views/snuggle\.html\.erb)
20  
-        assert_file "app/widgets/gerbil/views/snuggle.html.erb", %r(<p>)
21  
-        assert_file "app/widgets/gerbil/views/squeak.html.erb", %r(app/widgets/gerbil/views/squeak\.html\.erb)
  19
+        assert_file "app/widgets/gerbil/snuggle.html.erb", %r(app/widgets/gerbil/snuggle\.html\.erb)
  20
+        assert_file "app/widgets/gerbil/snuggle.html.erb", %r(<p>)
  21
+        assert_file "app/widgets/gerbil/squeak.html.erb", %r(app/widgets/gerbil/squeak\.html\.erb)
22 22
 
23 23
         assert_file "test/widgets/gerbil/gerbil_widget_test.rb", %r(class GerbilWidgetTest < Apotomo::TestCase)
24 24
         assert_file "test/widgets/gerbil/gerbil_widget_test.rb", %r(widget\(:gerbil\))
25 25
       end
26 26
 
27  
-      should "create javascript and css assets" do
  27
+      should "create coffescript and css assets" do
28 28
         run_generator %w(Gerbil squeak snuggle -t test_unit)
29 29
 
30  
-        assert_file "app/assets/javascripts/widgets/gerbil_widget.coffee", /Define your coffeescript code for the Gerbil widget*/
  30
+        assert_file "app/assets/javascripts/widgets/gerbil_widget.js.coffee", /Define your coffeescript code for the Gerbil widget*/
  31
+        assert_file "app/assets/stylesheets/widgets/gerbil_widget.css", /Define your css code for the Gerbil widget*/
  32
+      end
  33
+
  34
+      should "create javascript and css assets" do
  35
+        run_generator %w(Gerbil squeak snuggle -t test_unit --js)
  36
+
  37
+        assert_file "app/assets/javascripts/widgets/gerbil_widget.js", /Define your javascript code for the Gerbil widget*/
31 38
         assert_file "app/assets/stylesheets/widgets/gerbil_widget.css", /Define your css code for the Gerbil widget*/
32 39
       end
33 40
 
34 41
       should "create haml assets with -e haml" do
35 42
         run_generator %w(Gerbil squeak snuggle -e haml -t test_unit)
36 43
 
37  
-        assert_file "app/widgets/gerbil/gerbil_widget.rb", /class GerbilWidget < Apotomo::Widget/
38  
-        assert_file "app/widgets/gerbil/gerbil_widget.rb", /def snuggle/
39  
-        assert_file "app/widgets/gerbil/gerbil_widget.rb", /def squeak/
  44
+        assert_file "app/widgets/gerbil_widget.rb", /class GerbilWidget < Apotomo::Widget/
  45
+        assert_file "app/widgets/gerbil_widget.rb", /def snuggle/
  46
+        assert_file "app/widgets/gerbil_widget.rb", /def squeak/
40 47
         
41  
-        assert_file "app/widgets/gerbil/views/snuggle.html.haml", %r(app/widgets/gerbil/views/snuggle\.html\.haml)
42  
-        assert_file "app/widgets/gerbil/views/snuggle.html.haml", %r(%p)
43  
-        assert_file "app/widgets/gerbil/views/squeak.html.haml", %r(app/widgets/gerbil/views/squeak\.html\.haml)
  48
+        assert_file "app/widgets/gerbil/snuggle.html.haml", %r(app/widgets/gerbil/snuggle\.html\.haml)
  49
+        assert_file "app/widgets/gerbil/snuggle.html.haml", %r(%p)
  50
+        assert_file "app/widgets/gerbil/squeak.html.haml", %r(app/widgets/gerbil/squeak\.html\.haml)
44 51
         
45 52
         assert_file "test/widgets/gerbil/gerbil_widget_test.rb"
46 53
       end
47 54
 
48 55
       should "create slim assets with -e slim" do
49  
-        run_generator %w(Gerbil squeak snuggle -e slim -t test_unit)
  56
+        run_generator %w(Gerbil squeak snuggle -e slim -t test_unit --parent)
50 57
 
51  
-        assert_file "app/widgets/gerbil/gerbil_widget.rb", /class GerbilWidget < Apotomo::Widget/
52  
-        assert_file "app/widgets/gerbil/gerbil_widget.rb", /def snuggle/
53  
-        assert_file "app/widgets/gerbil/gerbil_widget.rb", /def squeak/
  58
+        assert_file "app/widgets/gerbil_widget.rb", /class GerbilWidget < Apotomo::Widget/
  59
+        assert_file "app/widgets/gerbil_widget.rb", /def snuggle/
  60
+        assert_file "app/widgets/gerbil_widget.rb", /def squeak/
54 61
         
55 62
         assert_file "app/widgets/gerbil/views/snuggle.html.slim", %r(app/widgets/gerbil/views/snuggle\.html\.slim)
56 63
         assert_file "app/widgets/gerbil/views/snuggle.html.slim", %r(p)
@@ -62,10 +69,10 @@ class WidgetGeneratorTest < Rails::Generators::TestCase
62 69
       should "work with namespaces" do
63 70
         run_generator %w(Gerbil::Mouse squeak -t test_unit)
64 71
 
65  
-        assert_file "app/widgets/gerbil/mouse/mouse_widget.rb", /class Gerbil::MouseWidget < Apotomo::Widget/
66  
-        assert_file "app/widgets/gerbil/mouse/mouse_widget.rb", /def squeak/
  72
+        assert_file "app/widgets/gerbil/mouse_widget.rb", /class Gerbil::MouseWidget < Apotomo::Widget/
  73
+        assert_file "app/widgets/gerbil/mouse_widget.rb", /def squeak/
67 74
         
68  
-        assert_file "app/widgets/gerbil/mouse/views/squeak.html.erb", %r(app/widgets/gerbil/mouse/views/squeak\.html\.erb)
  75
+        assert_file "app/widgets/gerbil/mouse/squeak.html.erb", %r(app/widgets/gerbil/mouse/squeak\.html\.erb)
69 76
         
70 77
         assert_file "test/widgets/gerbil/mouse/mouse_widget_test.rb"
71 78
       end
0  test/widgets/mouse/views/educate.html.erb → test/widgets/mouse/educate.html.erb
File renamed without changes

0 notes on commit 9a9b9cd

Please sign in to comment.
Something went wrong with that request. Please try again.