Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

some nice updates to make it better to work with for Rails 3.2

  • Loading branch information...
commit 2799ba7ba00d215b1feee27325406b4b95ce63eb 1 parent 8a0c60d
Kristian Mandrup authored
94 lib/apotomo/javascript_generator.rb
View
@@ -47,6 +47,100 @@ def update(id, markup); element(id) + '.html("'+escape(markup)+'");'; end
def replace(id, markup); element(id) + '.replaceWith("'+escape(markup)+'");'; end
def update_id(id, markup); update("##{id}", markup); end
def replace_id(id, markup); replace("##{id}", markup); end
+
+ def update_text(id, selector, markup)
+ element(id) + ".find(#{selector}).text('#{escape(markup)}');"
+ end
+
+ def append(id, selector, markup)
+ element(id) + ".find(#{selector}).append('#{escape(markup)}');"
+ end
+
+ def prepend(id, selector, markup)
+ element(id) + ".find(#{selector}).prepend('#{escape(markup)}');"
+ end
+
+ def append_to(selector, markup)
+ "$(#{escape(markup)}').appendTo('#{selector}');"
+ end
+
+ def prepend_to(selector, markup)
+ "$(#{escape(markup)}').prependTo('#{selector}');"
+ end
+
+ def after selector, markup
+ "$('#{selector}').after('#{escape(markup)}');"
+ end
+
+ def before selector, markup
+ "$('#{selector}').before('#{escape(markup)}');"
+ end
+
+ def replace_all(selector, markup)
+ "$(#{escape(markup)}').replaceAll('#{selector}');"
+ end
+
+ def unwrap (selector)
+ "$('#{selector}').unwrap();"
+ end
+
+ def wrap (selector, markup)
+ "$('#{selector}').wrap('#{escape(markup)}');"
+ end
+
+ def wrap_innet (selector, markup)
Kurakin Alexander
kuraga added a note

Ou!!!!!!!! Name error!!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ "$('#{selector}').wrapInner('#{escape(markup)}');"
+ end
+
+ def wrap_all (selector, markup)
+ "$('#{selector}').wrap_all('#{escape(markup)}');"
+ end
+
+ def remove(selector)
+ "$('#{selector}').remove();"
+ end
+
+ def remove_class(selector, *classes)
+ classes = classes.flatten.join(' ')
+ "$('#{selector}').removeClass('#{classes}');"
+ end
+ alias_method :remove_classes, :remove_class
+
+ def add_class(selector, *classes)
+ classes = classes.flatten.join(' ')
+ "$('#{selector}').addClass('#{classes}');"
+ end
+ alias_method :add_classes, :add_class
+
+ def toggle_class(selector, *classes)
+ classes = classes.flatten.join(' ')
+ "$('#{selector}').toggleClass('#{classes}');"
+ end
+ alias_method :toggle_classes, :toggle_class
+
+ def toggle_class_fun(selector, fun)
Kurakin Alexander
kuraga added a note

Why callback-version method is defined for toggle variant only?
And why doesn't callback receive any arguments?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ "$('#{selector}').toggleClass(function() {#{fun}});"
+ end
+
+ def get_attr(selector, name)
+ "$('#{selector}').attr('#{name}');"
+ end
+
+ def get_prop(selector, name)
+ "$('#{selector}').prop('#{name}');"
+ end
+
+ def get_val selector
+ "$('#{selector}').val();"
+ end
+
+ def get_html(selector)
+ "$('#{selector}').html();"
+ end
+
+ def empty(selector)
+ "$('#{selector}').empty();"
+ end
end
end
end
34 lib/generators/apotomo/widget_generator.rb
View
@@ -3,12 +3,32 @@
module Apotomo
module Generators
module BasePathMethods
- private
+ private
+
def 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
+
+ def css_path
+ File.join('app/assets/stylesheets/widgets', class_path, file_name)
+ end
end
+ module Views
+ 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.
+ template "view.#{handler}", @path
+ end
+ end
+ end
+
class WidgetGenerator < ::Cells::Generators::Base
include BasePathMethods
@@ -18,11 +38,15 @@ class WidgetGenerator < ::Cells::Generators::Base
hook_for(:test_framework) # TODO: implement rspec-apotomo.
check_class_collision :suffix => "Widget"
-
-
+
def create_cell_file
- template 'widget.rb', "#{base_path}_widget.rb"
+ template 'widget.rb', File.join(base_path, "#{file_name}_widget.rb")
end
+
+ def create_assets_files
+ template 'widget.coffee', "#{js_path}_widget.coffee"
+ template 'widget.css', "#{css_path}_widget.css"
+ end
end
end
-end
+end
1  lib/generators/erb/widget_generator.rb
View
@@ -5,6 +5,7 @@ module Erb
module Generators
class WidgetGenerator < CellGenerator
include ::Apotomo::Generators::BasePathMethods
+ include ::Apotomo::Generators::Views
source_root File.expand_path('../../templates', __FILE__)
end
end
1  lib/generators/haml/widget_generator.rb
View
@@ -5,6 +5,7 @@ module Haml
module Generators
class WidgetGenerator < CellGenerator
include ::Apotomo::Generators::BasePathMethods
+ include ::Apotomo::Generators::Views
source_root File.expand_path('../../templates', __FILE__)
end
end
1  lib/generators/slim/widget_generator.rb
View
@@ -5,6 +5,7 @@ module Slim
module Generators
class WidgetGenerator < CellGenerator
include ::Apotomo::Generators::BasePathMethods
+ include ::Apotomo::Generators::Views
source_root File.expand_path('../../templates', __FILE__)
end
end
7 lib/generators/templates/view.slim
View
@@ -1,4 +1,3 @@
-h1
- <%= class_name %>Widget#<%= @state %>
-p
- Find me in <%= @path %>
+= widget_div do
+ h1 <%= class_name %>Widget#<%= @state %>
+ p Find me in <%= @path %>
1  lib/generators/templates/widget.coffee
View
@@ -0,0 +1 @@
+# Define your coffeescript code for the <%= class_name %> widget
1  lib/generators/templates/widget.css
View
@@ -0,0 +1 @@
+/* Define your css code for the <%= class_name %> widget */
8 lib/generators/test_unit/widget_generator.rb
View
@@ -7,7 +7,13 @@ class WidgetGenerator < ::Cells::Generators::Base
def create_test
@states = actions
- template 'widget_test.rb', File.join('test/widgets/', class_path, "#{file_name}_widget_test.rb")
+ template 'widget_test.rb', File.join(test_path, "#{file_name}_widget_test.rb")
+ end
+
+ protected
+
+ def test_path
+ File.join('test/widgets/', class_path, file_name)
end
end
end
69 test/rails/widget_generator_test.rb
View
@@ -5,57 +5,70 @@ class WidgetGeneratorTest < Rails::Generators::TestCase
destination File.join(Rails.root, "tmp")
setup :prepare_destination
tests ::Apotomo::Generators::WidgetGenerator
-
+
context "Running rails g apotomo::widget" do
context "Gerbil squeak snuggle" do
should "create the standard assets" do
-
+
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/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 "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 "test/widgets/gerbil_widget_test.rb", %r(class GerbilWidgetTest < Apotomo::TestCase)
assert_file "test/widgets/gerbil_widget_test.rb", %r(widget\(:gerbil\))
end
-
+
+ should "create javascript 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/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/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_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/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_widget_test.rb"
end
should "create slim assets with -e slim" do
run_generator %w(Gerbil squeak snuggle -e slim -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/views/snuggle.html.slim", %r(app/widgets/gerbil/views/snuggle\.html\.slim)
+ assert_file "app/widgets/gerbil/views/snuggle.html.slim", %r(p)
+ assert_file "app/widgets/gerbil/views/squeak.html.slim", %r(app/widgets/gerbil/views/squeak\.html\.slim)
- 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/snuggle.html.slim", %r(app/widgets/gerbil/snuggle\.html\.slim)
- assert_file "app/widgets/gerbil/snuggle.html.slim", %r(p)
- assert_file "app/widgets/gerbil/squeak.html.slim", %r(app/widgets/gerbil/squeak\.html\.slim)
assert_file "test/widgets/gerbil_widget_test.rb"
end
-
+
should "work with namespaces" do
run_generator %w(Gerbil::Mouse squeak -t test_unit)
- 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/squeak.html.erb", %r(app/widgets/gerbil/mouse/squeak\.html\.erb)
+ 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/views/squeak.html.erb", %r(app/widgets/gerbil/mouse/views/squeak\.html\.erb)
+
assert_file "test/widgets/gerbil/mouse_widget_test.rb"
end
-
+
end
- end
-end
+ end

1 comment on commit 2799ba7

Kurakin Alexander

Small style code fixes are needed here and later... I.e. Convert def meth var to def meth(var). And don't you prefer each to for?

Kurakin Alexander

Ou!!!!!!!! Name error!!!!

Kurakin Alexander

Why callback-version method is defined for toggle variant only?
And why doesn't callback receive any arguments?

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