Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

jquery helper tests wip

  • Loading branch information...
commit cb80a00e647ef55a4d456418d65ad8110dc4522a 1 parent 492dc03
Kristian Mandrup authored
1  README.rdoc
View
@@ -170,6 +170,7 @@ Apotomo doesn't depend on _any_ JS framework - you choose!
Extras (jQuery only):
+* find_element(id, selector)
* selector_for(var, id, selector)
Example usage:
2  Rakefile
View
@@ -8,6 +8,6 @@ task :default => :test
Rake::TestTask.new(:test) do |test|
test.libs << 'test'
- test.test_files = FileList['test/*_test.rb', 'test/rails/*_test.rb']
+ test.test_files = FileList['test/javascript_generator/*_test.rb'] # FileList['test/*_test.rb', 'test/rails/*_test.rb']
test.verbose = true
end
125 lib/apotomo/javascript_generator.rb
View
@@ -1,7 +1,10 @@
require 'action_view/helpers/javascript_helper'
+require 'apotomo/javascript_generator/jquery_helper'
module Apotomo
class JavascriptGenerator
+ autoload :JqueryHelper, 'apotomo/javascript_generator/jquery_helper'
+
def initialize(framework)
raise "No JS framework specified" if framework.blank?
extend "apotomo/javascript_generator/#{framework}".camelize.constantize
@@ -45,7 +48,7 @@ def replace(selector, markup); element(selector) + '.replace("'+escape(markup
module Jquery
def jquery; end
def element(selector)
- selector = selector =~ /^_apo_/ ? selector : "'#{selector}'"
+ selector = jq_helper.calc_selector selector
"$(#{selector})"
end
@@ -60,154 +63,76 @@ def update_text(id, selector, markup)
end
def selector_for var, id, selector
- raise ArgumentError, "Must not be an _apo_ selector here: #{selector}" if apo_match?(selector)
+ raise ArgumentError, "Must not be an _apo_ selector here: #{selector}" if jq_helper.apo_match?(selector)
"var _apo_#{var} = " + element("##{id}") + ".find('#{selector}');"
end
- def find_element id, selector
- if id == nil || apo_selector?(selector)
- return element(calc_selector selector)
- end
- element("##{id}") + ".find('#{selector}')"
- end
-
[:replace_all, :prepend_to, :append_to].each do |name|
define_method name do |selector, markup|
- _jq_inverse_action(selector, markup, name.to_sym)
+ jq_helper.inv_markup_action selector, markup, name.to_sym
end
end
[:append, :prepend, :after, :before, :wrap, :wrap_inner, :wrap_all].each do |name|
define_method name do |args|
- _jquery_action *args, name
+ jq_helper.markup_action *args, name
end
end
def unwrap *args
- jquery_action *args, 'unwrap()'
+ jq_helper.markup_action *args, 'unwrap()'
end
def remove *args
- _jquery_action *args, 'remove()'
+ jq_helper.markup_action *args, 'remove()'
end
def remove_class(id, selector, *classes)
classes = classes.flatten.join(' ')
- find_element(id, selector) + _js_action("removeClass('#{classes}')")
+ jq_helper.jq_action id, selector, "removeClass('#{classes}')"
end
alias_method :remove_classes, :remove_class
def add_class(id, selector, *classes)
classes = classes.flatten.join(' ')
- find_element(id, selector) + _js_action("addClass('#{classes}')"
+ jq_helper.jq_action id, selector, "addClass('#{classes}')"
end
alias_method :add_classes, :add_class
def toggle_class(id, selector, *classes)
classes = classes.flatten.join(' ')
- find_element(id, selector) + _js_action("toggleClass('#{classes}')")
+ jq_helper.jq_action id, selector, "toggleClass('#{classes}')"
end
alias_method :toggle_classes, :toggle_class
def toggle_class_fun(id, selector, fun)
- find_element(id, selector) + _js_action("toggleClass(function() {#{fun}})")
+ jq_helper.jq_action id, selector, "toggleClass(function() {#{fun}})"
end
- def get_attr(id. selector, name)
- find_element(id, selector) + _js_action("attr('#{name}')")
+ def get_attr(id, selector, name)
+ jq_helper.jq_action id, selector, "attr('#{name}')"
end
- def get_prop(selector, name)
- find_element(id, selector) + _js_action("prop('#{name}')")
+ def get_prop(id, selector, name)
+ jq_helper.jq_action id, selector, "prop('#{name}')"
end
- def get_val selector
- find_element(id, selector) + _js_action("val()")
+ def get_val id, selector
+ jq_helper.jq_action id, selector, "val()"
end
- def get_html(selector)
- find_element(id, selector) + _js_action("html()")
+ def get_html(id, selector)
+ jq_helper.jq_action id, selector, "html()"
end
- def empty(selector)
- find_element(id, selector) + _js_action("empty()")
+ def empty(id, selector)
+ jq_helper.jq_action id, selector, "empty()"
end
private
- def _jq_inverse_action(selector, markup, action)
- selector = calc_selector selector
- action = _js_camelize(action)
- "$(#{escaped(markup)}).#{action}(#{selector});"
- end
-
- def _jquery_action *args, &block
- args = args.flatten
- [id, selector, markup, action] = _extract_args(*args)
- action ||= yield if block_given?
- raise ArgumentError, "Must take action block or Symbol as last argument" unless action
- elem_action = case action
- when String
- action = _js_camelize(action)
- ".#{action};"
- else
- action = _js_camelize(action)
- _action(action, markup)
- end
- find_element(id, selector) + elem_action
- end
-
- def escaped markup
- "'#{escape(markup)}'"
- end
-
- def _action name, markup
- _js_action "#{name}(#{_escaped(markup)})"
- end
-
- def _js_action action
- ".#{action};"
- end
-
- def _js_camelize str
- str.to_s.camelize.sub(/^\w/, s[0].downcase)
- end
-
- # id, selector, markup, action
- def _extract_args *args
- [_extract_id(*args), _extract_selector(*args), _extract_markup(*args), _extract_action(*args)]
- end
-
- def _extract_id *args
- args = args.flatten
- args.size == 4 ? args.first : nil
- end
-
- def _extract_selector *args
- args = args.flatten
- args.size == 4 ? args[1] : args[0]
- end
-
- def _extract_markup(*args)
- args = args.flatten
- args.size == 4 ? args[2] : args.last
- end
-
- def _extract_action(*args)
- args = args.flatten
- args.size == 4 ? args.last : nil
- end
-
- def _apo_match? selector
- selector.to_s =~ /^_apo_/
- end
-
- def _calc_selector selector
- selector = _apo_selector?(selector) ? "_apo_#{selector}" : "'#{selector}'"
- end
-
- def _apo_selector? selector
- selector.kind_of?(Symbol) && selector.to_s[0..1] == '_'
+ def jq_helper
+ JqueryHelper
end
end
end
104 lib/apotomo/javascript_generator/jquery_helper.rb
View
@@ -0,0 +1,104 @@
+module Apotomo
+ class JavascriptGenerator
+ module JqueryHelper
+ def find_element id, selector
+ if id == nil || apo_selector?(selector)
+ return element(selector)
+ end
+ element("##{id}") + ".find('#{selector}')"
+ end
+
+ # - id, selector action
+ # - selector action
+ def jq_action *args, action
+ args = *args, nil, action
+ id, selector, action = [extract_id(*args), extract_selector(*args), args.last]
+ find_element(id, selector) + js_action(action)
+ end
+
+ def inv_markup_action(selector, markup, action)
+ selector = calc_selector selector
+ action = js_camelize(action)
+ "$(#{escaped(markup)}).#{action}(#{selector});"
+ end
+
+ def markup_action *args, &block
+ args = args.flatten
+ id, selector, markup, action = extract_args(*args)
+ action ||= yield if block_given?
+ raise ArgumentError, "Must take action block or Symbol as last argument" unless action
+ elem_action = case action
+ when String
+ action = js_camelize(action)
+ ".#{action};"
+ else
+ action = js_camelize(action)
+ mk_action(action, markup)
+ end
+ find_element(id, selector) + elem_action
+ end
+
+ def escaped markup
+ "'#{escape(markup)}'"
+ end
+
+ def mk_action name, markup
+ js_action "#{name}(#{escaped(markup)})"
+ end
+
+ def js_action action
+ ".#{action};"
+ end
+
+ def js_camelize str
+ str.to_s.camelize.sub(/^\w/, s[0].downcase)
+ end
+
+ def calc_selector selector
+ selector = apo_selector?(selector) ? "_apo_#{selector}" : "'#{selector}'"
+ end
+
+ def apo_selector? selector
+ selector.kind_of?(Symbol) && selector.to_s[0..1] == '_'
+ end
+
+ def apo_match? selector
+ selector.to_s =~ /^_apo_/
+ end
+
+ protected
+
+ def element(selector)
+ selector = calc_selector selector
+ "$(#{selector})"
+ end
+
+ # id, selector, markup, action
+ def extract_args *args
+ [extract_id(*args), extract_selector(*args), extract_markup(*args), extract_action(*args)]
+ end
+
+ def extract_id *args
+ args = args.flatten
+ args.size == 4 ? args.first : nil
+ end
+
+ def extract_selector *args
+ args = args.flatten
+ args.size == 4 ? args[1] : args[0]
+ end
+
+ def extract_markup(*args)
+ args = args.flatten
+ args.size == 4 ? args[2] : args.last
+ end
+
+ def extract_action(*args)
+ args = args.flatten
+ args.size == 4 ? args.last : nil
+ end
+
+ extend self
+ end
+ end
+end
37 test/javascript_generator/jquery_helper_test.rb
View
@@ -0,0 +1,37 @@
+require 'test_helper'
+
+class JQueryHelperTest < Test::Unit::TestCase
+ context "The JQueryHelper" do
+ setup do
+ @helper = Apotomo::JavascriptGenerator::JqueryHelper
+ end
+
+ context '#find_element' do
+ context 'args: id, selector' do
+ should "generate full action" do
+ assert_equal "$('#my_widget').find('.item')", @helper.find_element('my_widget', '.item')
+ end
+ end
+
+ context 'args: selector' do
+ should "generate selector only" do
+ assert_equal "$('.item')", @helper.find_element(nil, '.item')
+ end
+ end
+ end
+
+ context '#jq_action' do
+ context 'args: id, selector, action' do
+ should "generate full action" do
+ assert_equal "$('#my_widget').find('.item').empty();", @helper.jq_action('my_widget', '.item', 'empty()')
+ end
+ end
+
+ context 'args: selector, action' do
+ should "generate selector only" do
+ assert_equal "$('.item').empty();", @helper.jq_action('.item', 'empty()')
+ end
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.