da-js 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +4 -0
- data/README.md +33 -0
- data/Rakefile +8 -0
- data/da-js.gemspec +31 -0
- data/features/change_tracker.feature +119 -0
- data/features/step_definitions/change_tracker_steps.rb +128 -0
- data/features/support/env.rb +7 -0
- data/features/support/testapp/config.ru +12 -0
- data/features/support/testapp/testapp.rb +21 -0
- data/features/support/testapp/vendor/jquery-1.7.1.js +9266 -0
- data/features/support/testapp/views/form_change_tracker.erb +152 -0
- data/lib/assets/javascripts/da-js/form_change_tracker.js +64 -0
- data/lib/assets/javascripts/da-js.js +1 -0
- data/lib/da-js/version.rb +5 -0
- data/lib/da-js.rb +9 -0
- metadata +224 -0
data/.gitignore
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# da-js – Collection of small, useful JavaScripts
|
2
|
+
This are some JavaScripts we use in most of our projects. So packaged them into a gem.
|
3
|
+
|
4
|
+
Okay, right now it’s only one, small JavaScript. But just be patient …
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
da-js is meant to be used with Ruby on Rails 3.1 or newer. It's based on [jQuery](http://jquery.com), so be sure you are using [jquery-rails](https://github.com/rails/jquery-rails), too (__jQuery version 1.7.1 or newer is required__).
|
8
|
+
|
9
|
+
To install it:
|
10
|
+
|
11
|
+
* Add the gem to your `Gemfile`:
|
12
|
+
|
13
|
+
gem "da-js"
|
14
|
+
|
15
|
+
* Require the library in your `application.js` (after requiring jQuery):
|
16
|
+
|
17
|
+
require "da-js"
|
18
|
+
|
19
|
+
## Documentation
|
20
|
+
For documentation please refer to the individual files in [`lib/assets/javascripts/da-js`](https://github.com/die-antwort/da-js/tree/master/lib/da-js).
|
21
|
+
|
22
|
+
## Development
|
23
|
+
Use the Rack application in `features/support/testapp`:
|
24
|
+
|
25
|
+
rackup features/support/testapp/config.ru
|
26
|
+
|
27
|
+
Any template file in `features/support/testapp/views` can be accessed by its basename:
|
28
|
+
|
29
|
+
open http:://localhost:9292/form_change_tracker
|
30
|
+
|
31
|
+
Run the tests with
|
32
|
+
|
33
|
+
rake test
|
data/Rakefile
ADDED
data/da-js.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "da-js/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "da-js"
|
7
|
+
s.version = Da::Js::VERSION
|
8
|
+
s.authors = ["Stefan Daschek"]
|
9
|
+
s.email = ["stefan@die-antwort.eu"]
|
10
|
+
s.homepage = "https://github.com/die-antwort/da-js"
|
11
|
+
s.summary = "Mixed jQuery extensions"
|
12
|
+
s.description = "Some jQuery extensions we tend to use in almost all our projects."
|
13
|
+
|
14
|
+
s.rubyforge_project = "da-js"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency "railties", ">= 3.1.0"
|
22
|
+
s.add_dependency "sprockets", "~> 2.0"
|
23
|
+
|
24
|
+
s.add_development_dependency "cucumber", ">= 1.1.4"
|
25
|
+
s.add_development_dependency "rspec-expectations", ">= 2.7.0"
|
26
|
+
s.add_development_dependency "sinatra"
|
27
|
+
s.add_development_dependency "sinatra-contrib"
|
28
|
+
s.add_development_dependency "capybara", ">= 1.1.2"
|
29
|
+
s.add_development_dependency "capybara-webkit", ">= 0.8.0"
|
30
|
+
s.add_development_dependency "launchy"
|
31
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
@javascript
|
2
|
+
Feature: Change tracker for forms
|
3
|
+
There are two special cases:
|
4
|
+
- A select has always a selected option. We use the first option (with empty value) as "empty".
|
5
|
+
- All radiobuttons can initially be unselected, but going back to this state is impossible. So, in the "empty form" scenario, we skip the radiobutton in second part (when we test "resetting" the form elements)
|
6
|
+
|
7
|
+
Scenario Outline: An empty form
|
8
|
+
Given an empty <Form>
|
9
|
+
Then no <Element> should be marked as changed
|
10
|
+
|
11
|
+
When I enter some text in the textfield
|
12
|
+
Then the textfield's <Element> should be marked as changed
|
13
|
+
|
14
|
+
When I enter some text in the textarea
|
15
|
+
Then the textarea's <Element> should be marked as changed
|
16
|
+
|
17
|
+
When I check the checkbox
|
18
|
+
Then the checkbox's <Element> should be marked as changed
|
19
|
+
|
20
|
+
When I select the first option
|
21
|
+
Then the select's <Element> should be marked as changed
|
22
|
+
|
23
|
+
# Now for resetting everything (except radiobuttons, see feature description)
|
24
|
+
When I clear the textfield
|
25
|
+
Then the textfield's <Element> should no longer be marked as changed
|
26
|
+
|
27
|
+
When I clear the textarea
|
28
|
+
Then the textarea's <Element> should no longer be marked as changed
|
29
|
+
|
30
|
+
When I uncheck the checkbox
|
31
|
+
Then the checkbox's <Element> should no longer be marked as changed
|
32
|
+
|
33
|
+
When I select the empty option
|
34
|
+
Then the select's <Element> should no longer be marked as changed
|
35
|
+
|
36
|
+
Examples:
|
37
|
+
| Form | Element |
|
38
|
+
| simple_form | form element and label |
|
39
|
+
| scaffold_form | field container |
|
40
|
+
|
41
|
+
|
42
|
+
Scenario: Radiobuttons in an empty simple form
|
43
|
+
Given an empty simple_form
|
44
|
+
When I choose the first radiobutton
|
45
|
+
Then the first radiobutton's form element and label should be marked as changed
|
46
|
+
But the second radiobutton's form element and label should not be marked as changed
|
47
|
+
And the third radiobutton's form element and label should not be marked as changed
|
48
|
+
|
49
|
+
|
50
|
+
Scenario: Radiobuttons in an empty scaffold form
|
51
|
+
Given an empty scaffold_form
|
52
|
+
When I choose the first radiobutton
|
53
|
+
Then the first radiobutton's field container should be marked as changed
|
54
|
+
|
55
|
+
|
56
|
+
Scenario Outline: A prefilled form
|
57
|
+
Given a prefilled <Form>
|
58
|
+
Then no <Element> should be marked as changed
|
59
|
+
|
60
|
+
When I change the textfield's content
|
61
|
+
Then the textfield's <Element> should be marked as changed
|
62
|
+
When I clear the textfield
|
63
|
+
Then the textfield's <Element> should be marked as changed
|
64
|
+
|
65
|
+
When I change the textarea's content
|
66
|
+
Then the textarea's <Element> should be marked as changed
|
67
|
+
When I clear the textarea
|
68
|
+
Then the textarea's <Element> should be marked as changed
|
69
|
+
|
70
|
+
When I uncheck the checkbox
|
71
|
+
Then the checkbox's <Element> should be marked as changed
|
72
|
+
|
73
|
+
When I select the second option
|
74
|
+
Then the select's <Element> should be marked as changed
|
75
|
+
|
76
|
+
# Now reset everything again
|
77
|
+
When I reset the textfield to its initial value
|
78
|
+
Then the textfield's <Element> should no longer be marked as changed
|
79
|
+
|
80
|
+
When I reset the textarea to its initial value
|
81
|
+
Then the textarea's <Element> should no longer be marked as changed
|
82
|
+
|
83
|
+
When I check the checkbox
|
84
|
+
Then the checkbox's <Element> should no longer be marked as changed
|
85
|
+
|
86
|
+
When I select the first option
|
87
|
+
Then the select's <Element> should no longer be marked as changed
|
88
|
+
|
89
|
+
And no <Element> should be marked as changed
|
90
|
+
|
91
|
+
Examples:
|
92
|
+
| Form | Element |
|
93
|
+
| simple_form | form element and label |
|
94
|
+
| scaffold_form | field container |
|
95
|
+
|
96
|
+
|
97
|
+
Scenario: Radiobuttons in an prefilled simple form
|
98
|
+
Given an prefilled simple_form
|
99
|
+
Then no form element and label should be marked as changed
|
100
|
+
|
101
|
+
When I choose the second radiobutton
|
102
|
+
Then the second radiobutton's form element and label should be marked as changed
|
103
|
+
And the first radiobutton's form element and label should be marked as changed
|
104
|
+
But the third radiobutton's form element and label should not be marked as changed
|
105
|
+
|
106
|
+
When I choose the first radiobutton
|
107
|
+
Then no form element and label should be marked as changed
|
108
|
+
|
109
|
+
|
110
|
+
Scenario: Radiobuttons in an prefilled scaffold form
|
111
|
+
Given an prefilled scaffold_form
|
112
|
+
Then no form element and label should be marked as changed
|
113
|
+
|
114
|
+
When I choose the second radiobutton
|
115
|
+
Then the second radiobutton's field container should be marked as changed
|
116
|
+
|
117
|
+
When I choose the first radiobutton
|
118
|
+
Then no form element and label should be marked as changed
|
119
|
+
|
@@ -0,0 +1,128 @@
|
|
1
|
+
def find_element_and_label(element)
|
2
|
+
id = "#{@form}_#{element}"
|
3
|
+
[find("##{@form} ##{id}"), find("##{@form} label[for='#{id}']")]
|
4
|
+
end
|
5
|
+
|
6
|
+
def find_container(element)
|
7
|
+
find(:xpath, "//form[@id='#{@form}']//div[contains(@class, 'field')][*[@id='#{@form}_#{element}']]")
|
8
|
+
end
|
9
|
+
|
10
|
+
Given /^an? (empty|prefilled) (\w+)$/ do |type, form|
|
11
|
+
visit "/form_change_tracker"
|
12
|
+
@form = form.dup # Without #dup Cucumber prints the step incorrectly
|
13
|
+
@form << "_prefilled" if type == "prefilled"
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
When /^I enter some text in the (\w+)$/ do |field|
|
18
|
+
within "##{@form}" do
|
19
|
+
fill_in field, :with => "foobar"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
When /^I change the (\w+)'s content$/ do |field|
|
24
|
+
within "##{@form}" do
|
25
|
+
fill_in field, :with => find_field(field).value + " plus some new text"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
When /^I clear the (\w+)$/ do |field|
|
30
|
+
within "##{@form}" do
|
31
|
+
fill_in field, :with => ""
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
When /^I reset the (\w+) to its initial value$/ do |field|
|
36
|
+
within "##{@form}" do
|
37
|
+
fill_in field, :with => "text"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
When /^I (check|uncheck) the checkbox$/ do |action|
|
42
|
+
within "##{@form}" do
|
43
|
+
if action == "check"
|
44
|
+
check "checkbox"
|
45
|
+
else
|
46
|
+
uncheck "checkbox"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
When /^I choose the (first|second) radiobutton$/ do |which|
|
52
|
+
button = (which == "first" ? "1" : (which == "second" ? "2" : "3"))
|
53
|
+
within "##{@form}" do
|
54
|
+
choose button
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
When /^I select the (blank|first|second) option$/ do |which|
|
59
|
+
option = (which == "blank" ? "" : (which == "first" ? "option 1" : "option 2"))
|
60
|
+
within "##{@form}" do
|
61
|
+
select option
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
When /^I select the empty option$/ do
|
66
|
+
within "##{@form}" do
|
67
|
+
select ""
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
Then /^no form element and label should be marked as changed$/ do
|
73
|
+
within "##{@form}" do
|
74
|
+
all("input,textarea,select,label").each do |element|
|
75
|
+
element["class"].should_not include("changed")
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
Then /^no field container should be marked as changed$/ do
|
81
|
+
within "##{@form}" do
|
82
|
+
all(".field").each do |element|
|
83
|
+
element["class"].should_not include("changed")
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
Then /^the (first|second|third) radiobutton's form element and label should( not| no longer)? be marked as changed$/ do |which, no_longer|
|
89
|
+
button = (which == "first" ? "1" : (which == "second" ? "2" : "3"))
|
90
|
+
element, label = find_element_and_label("radiobutton_#{button}")
|
91
|
+
if no_longer
|
92
|
+
element["class"].should_not include("changed")
|
93
|
+
label["class"].should_not include("changed")
|
94
|
+
else
|
95
|
+
element["class"].should include("changed")
|
96
|
+
label["class"].should include("changed")
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
Then /^the (first|second|third) radiobutton's field container should( not| no longer)? be marked as changed$/ do |which, no_longer|
|
101
|
+
button = (which == "first" ? "1" : (which == "second" ? "2" : "3"))
|
102
|
+
container = find_container("radiobutton_#{button}")
|
103
|
+
if no_longer
|
104
|
+
container["class"].should_not include("changed")
|
105
|
+
else
|
106
|
+
container["class"].should include("changed")
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
Then /^the (\w+)'s form element and label should( no longer)? be marked as changed$/ do |element, no_longer|
|
111
|
+
element, label = find_element_and_label(element)
|
112
|
+
if no_longer
|
113
|
+
element["class"].should_not include("changed")
|
114
|
+
label["class"].should_not include("changed")
|
115
|
+
else
|
116
|
+
element["class"].should include("changed")
|
117
|
+
label["class"].should include("changed")
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
Then /^the (\w+)'s field container should( no longer)? be marked as changed$/ do |element, no_longer|
|
122
|
+
container = find_container(element)
|
123
|
+
if no_longer
|
124
|
+
container["class"].should_not include("changed")
|
125
|
+
else
|
126
|
+
container["class"].should include("changed")
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'sprockets'
|
2
|
+
require File.dirname(__FILE__) + "/testapp"
|
3
|
+
|
4
|
+
map '/assets' do
|
5
|
+
environment = Sprockets::Environment.new
|
6
|
+
environment.append_path File.dirname(__FILE__) + '/../../../lib/assets'
|
7
|
+
run environment
|
8
|
+
end
|
9
|
+
|
10
|
+
map '/' do
|
11
|
+
run Sinatra::Application
|
12
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "sinatra"
|
2
|
+
require "sinatra/reloader" if development?
|
3
|
+
|
4
|
+
set :app_file, __FILE__
|
5
|
+
|
6
|
+
before do
|
7
|
+
@jquery_version = params[:jquery] || "1.7.1"
|
8
|
+
end
|
9
|
+
|
10
|
+
get "/vendor/:file" do |file|
|
11
|
+
send_file settings.root + "/vendor/#{file}"
|
12
|
+
end
|
13
|
+
|
14
|
+
# Don't spam console/log with errors caused by a missing favicon
|
15
|
+
get "/favicon.ico" do
|
16
|
+
404
|
17
|
+
end
|
18
|
+
|
19
|
+
get "/:view" do |view|
|
20
|
+
erb view.to_sym
|
21
|
+
end
|