Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

much better generator structure and flexibility - all tests pass

  • Loading branch information...
commit 9a9b9cd2a59f77eadea8386667339cc59fd16371 1 parent 706a353
@kristianmandrup authored
View
15 lib/apotomo/cell/rendering.rb
@@ -1,8 +1,6 @@
module Cell
module Rendering
def render(*args)
- puts "render: #{args}"
-
if args.first.kind_of?(Hash)
if args.first[:view]
hash = args.first
@@ -14,9 +12,16 @@ def render(*args)
end
end
- view_name ||= File.join('views', self.action_name || '')
-
- puts "view_name: #{view_name}, #{args}"
+ view_name ||= File.join('views', self.action_name || '')
+ render_view_for(view_name, *args)
+ rescue
+ # try without view
+ view_name = view_name.gsub(/views\//, '')
+ if args.first.kind_of?(Hash) && args.first[:view]
+ hash = args.first
+ hash[:view] = File.join(hash[:view].to_s)
+ args = [hash, args[1..-1]]
+ end
render_view_for(view_name, *args)
end
end
View
1  lib/apotomo/widget.rb
@@ -122,7 +122,6 @@ def invoke(state, *args)
#
# issues a squeaking alert dialog on the page.
def render(*args, &block)
- puts "apotomo render: #{args}"
super
end
View
42 lib/generators/apotomo/widget_generator.rb
@@ -9,6 +9,10 @@ def base_path
File.join('app/widgets', class_path)
end
+ def view_base_path
+ File.join('app/widgets', class_path, file_name)
+ end
+
def js_path
File.join('app/assets/javascripts/widgets', class_path, file_name)
end
@@ -19,13 +23,33 @@ def css_path
end
module Views
+ extend ActiveSupport::Concern
+
+ included do
+ class_option :parent, :type => :boolean, :default => false, :desc => 'Parent widget'
+
+ source_root File.expand_path('../../templates', __FILE__)
+ end
+
def create_views
for state in actions do
@state = state
- @path = File.join(base_path, 'views', "#{state}.html.#{handler}") #base_path defined in Cells::Generators::Base.
+ # only make views container for top level widgets, since they are most likely to have
+ # child widgets
+ @path = if parent_widget?
+ File.join(view_base_path, 'views', "#{state}.html.#{handler}") #base_path defined in Cells::Generators::Base.
+ else
+ File.join(view_base_path, "#{state}.html.#{handler}") #base_path defined in Cells::Generators::Base.
+ end
template "view.#{handler}", @path
end
end
+
+ protected
+
+ def parent_widget?
+ options[:parent]
+ end
end
class WidgetGenerator < ::Cells::Generators::Base
@@ -38,7 +62,7 @@ class WidgetGenerator < ::Cells::Generators::Base
check_class_collision :suffix => "Widget"
- class_option :js, :type => :boolean, :default => false, :desc => 'Generate javascript asset file'
+ class_option :js, :type => :boolean, :default => false, :desc => 'Generate javascript asset file'
def create_cell_file
template 'widget.rb', File.join(base_path, "#{file_name}_widget.rb")
@@ -48,9 +72,15 @@ def create_stylesheet_file
template 'widget.css', "#{css_path}_widget.css"
end
- def creates_script_file
- return template 'widget.js.coffee', "#{js_path}_widget.js.coffee" if !javascript?
- template 'widget.js', "#{js_path}_widget.js"
+ def create_script_file
+ puts "create_script_file"
+ if !javascript?
+ # raise "coffee: #{js_path}_widget.js.coffee"
+ template 'widget.js.coffee', "#{js_path}_widget.js.coffee"
+ else
+ puts "js: #{js_path}_widget.js"
+ template 'widget.js', "#{js_path}_widget.js"
+ end
end
protected
@@ -68,7 +98,7 @@ def js_camelize str
end
def javascript?
- options[:js]
+ options[:js] == true
end
end
end
View
1  lib/generators/erb/widget_generator.rb
@@ -6,7 +6,6 @@ module Generators
class WidgetGenerator < CellGenerator
include ::Apotomo::Generators::BasePathMethods
include ::Apotomo::Generators::Views
- source_root File.expand_path('../../templates', __FILE__)
end
end
end
View
3  lib/generators/haml/widget_generator.rb
@@ -5,8 +5,7 @@ module Haml
module Generators
class WidgetGenerator < CellGenerator
include ::Apotomo::Generators::BasePathMethods
- include ::Apotomo::Generators::Views
- source_root File.expand_path('../../templates', __FILE__)
+ include ::Apotomo::Generators::Views
end
end
end
View
1  lib/generators/slim/widget_generator.rb
@@ -6,7 +6,6 @@ module Generators
class WidgetGenerator < CellGenerator
include ::Apotomo::Generators::BasePathMethods
include ::Apotomo::Generators::Views
- source_root File.expand_path('../../templates', __FILE__)
end
end
end
View
1  lib/generators/templates/widget.js
@@ -1,3 +1,4 @@
+// Define your javascript code for the <%= class_name %> widget
var Widget.<%= ns_name %> = namespace('Widget.<%= ns_name %>');
Widget.<%= ns_name %> = {
View
49 test/rails/widget_generator_test.rb
@@ -12,45 +12,52 @@ class WidgetGeneratorTest < Rails::Generators::TestCase
run_generator %w(Gerbil squeak snuggle -t test_unit)
- assert_file "app/widgets/gerbil/gerbil_widget.rb", /class GerbilWidget < Apotomo::Widget/
- assert_file "app/widgets/gerbil/gerbil_widget.rb", /def snuggle/
- assert_file "app/widgets/gerbil/gerbil_widget.rb", /def squeak/
+ assert_file "app/widgets/gerbil_widget.rb", /class GerbilWidget < Apotomo::Widget/
+ assert_file "app/widgets/gerbil_widget.rb", /def snuggle/
+ assert_file "app/widgets/gerbil_widget.rb", /def squeak/
- assert_file "app/widgets/gerbil/views/snuggle.html.erb", %r(app/widgets/gerbil/views/snuggle\.html\.erb)
- assert_file "app/widgets/gerbil/views/snuggle.html.erb", %r(<p>)
- assert_file "app/widgets/gerbil/views/squeak.html.erb", %r(app/widgets/gerbil/views/squeak\.html\.erb)
+ assert_file "app/widgets/gerbil/snuggle.html.erb", %r(app/widgets/gerbil/snuggle\.html\.erb)
+ assert_file "app/widgets/gerbil/snuggle.html.erb", %r(<p>)
+ assert_file "app/widgets/gerbil/squeak.html.erb", %r(app/widgets/gerbil/squeak\.html\.erb)
assert_file "test/widgets/gerbil/gerbil_widget_test.rb", %r(class GerbilWidgetTest < Apotomo::TestCase)
assert_file "test/widgets/gerbil/gerbil_widget_test.rb", %r(widget\(:gerbil\))
end
- should "create javascript and css assets" do
+ should "create coffescript and css assets" do
run_generator %w(Gerbil squeak snuggle -t test_unit)
- assert_file "app/assets/javascripts/widgets/gerbil_widget.coffee", /Define your coffeescript code for the Gerbil widget*/
+ assert_file "app/assets/javascripts/widgets/gerbil_widget.js.coffee", /Define your coffeescript code for the Gerbil widget*/
+ assert_file "app/assets/stylesheets/widgets/gerbil_widget.css", /Define your css code for the Gerbil widget*/
+ end
+
+ should "create javascript and css assets" do
+ run_generator %w(Gerbil squeak snuggle -t test_unit --js)
+
+ assert_file "app/assets/javascripts/widgets/gerbil_widget.js", /Define your javascript code for the Gerbil widget*/
assert_file "app/assets/stylesheets/widgets/gerbil_widget.css", /Define your css code for the Gerbil widget*/
end
should "create haml assets with -e haml" do
run_generator %w(Gerbil squeak snuggle -e haml -t test_unit)
- assert_file "app/widgets/gerbil/gerbil_widget.rb", /class GerbilWidget < Apotomo::Widget/
- assert_file "app/widgets/gerbil/gerbil_widget.rb", /def snuggle/
- assert_file "app/widgets/gerbil/gerbil_widget.rb", /def squeak/
+ assert_file "app/widgets/gerbil_widget.rb", /class GerbilWidget < Apotomo::Widget/
+ assert_file "app/widgets/gerbil_widget.rb", /def snuggle/
+ assert_file "app/widgets/gerbil_widget.rb", /def squeak/
- assert_file "app/widgets/gerbil/views/snuggle.html.haml", %r(app/widgets/gerbil/views/snuggle\.html\.haml)
- assert_file "app/widgets/gerbil/views/snuggle.html.haml", %r(%p)
- assert_file "app/widgets/gerbil/views/squeak.html.haml", %r(app/widgets/gerbil/views/squeak\.html\.haml)
+ assert_file "app/widgets/gerbil/snuggle.html.haml", %r(app/widgets/gerbil/snuggle\.html\.haml)
+ assert_file "app/widgets/gerbil/snuggle.html.haml", %r(%p)
+ assert_file "app/widgets/gerbil/squeak.html.haml", %r(app/widgets/gerbil/squeak\.html\.haml)
assert_file "test/widgets/gerbil/gerbil_widget_test.rb"
end
should "create slim assets with -e slim" do
- run_generator %w(Gerbil squeak snuggle -e slim -t test_unit)
+ run_generator %w(Gerbil squeak snuggle -e slim -t test_unit --parent)
- assert_file "app/widgets/gerbil/gerbil_widget.rb", /class GerbilWidget < Apotomo::Widget/
- assert_file "app/widgets/gerbil/gerbil_widget.rb", /def snuggle/
- assert_file "app/widgets/gerbil/gerbil_widget.rb", /def squeak/
+ assert_file "app/widgets/gerbil_widget.rb", /class GerbilWidget < Apotomo::Widget/
+ assert_file "app/widgets/gerbil_widget.rb", /def snuggle/
+ assert_file "app/widgets/gerbil_widget.rb", /def squeak/
assert_file "app/widgets/gerbil/views/snuggle.html.slim", %r(app/widgets/gerbil/views/snuggle\.html\.slim)
assert_file "app/widgets/gerbil/views/snuggle.html.slim", %r(p)
@@ -62,10 +69,10 @@ class WidgetGeneratorTest < Rails::Generators::TestCase
should "work with namespaces" do
run_generator %w(Gerbil::Mouse squeak -t test_unit)
- assert_file "app/widgets/gerbil/mouse/mouse_widget.rb", /class Gerbil::MouseWidget < Apotomo::Widget/
- assert_file "app/widgets/gerbil/mouse/mouse_widget.rb", /def squeak/
+ assert_file "app/widgets/gerbil/mouse_widget.rb", /class Gerbil::MouseWidget < Apotomo::Widget/
+ assert_file "app/widgets/gerbil/mouse_widget.rb", /def squeak/
- assert_file "app/widgets/gerbil/mouse/views/squeak.html.erb", %r(app/widgets/gerbil/mouse/views/squeak\.html\.erb)
+ assert_file "app/widgets/gerbil/mouse/squeak.html.erb", %r(app/widgets/gerbil/mouse/squeak\.html\.erb)
assert_file "test/widgets/gerbil/mouse/mouse_widget_test.rb"
end
View
0  test/widgets/mouse/views/educate.html.erb → test/widgets/mouse/educate.html.erb
File renamed without changes
Please sign in to comment.
Something went wrong with that request. Please try again.