best_in_place 0.1.9 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +9 -3
- data/.rspec +1 -0
- data/.travis.yml +11 -0
- data/Gemfile +2 -0
- data/README.md +3 -1
- data/Rakefile +6 -0
- data/best_in_place.gemspec +5 -0
- data/lib/best_in_place.rb +3 -40
- data/lib/best_in_place/helper.rb +52 -0
- data/lib/best_in_place/test_helpers.rb +26 -0
- data/lib/best_in_place/version.rb +2 -2
- data/public/javascripts/best_in_place.js +148 -125
- data/spec/helpers/best_in_place_spec.rb +229 -0
- data/spec/integration/double_init_spec.rb +32 -0
- data/spec/integration/js_spec.rb +165 -0
- data/spec/spec_helper.rb +23 -0
- data/test_app/Gemfile +1 -25
- data/test_app/Gemfile.lock +57 -50
- data/test_app/app/controllers/users_controller.rb +15 -1
- data/test_app/app/views/users/double_init.html.erb +65 -0
- data/test_app/app/views/users/show.html.erb +15 -15
- data/test_app/config/environments/development.rb +0 -1
- data/test_app/config/initializers/countries.rb +1 -1
- data/test_app/config/routes.rb +6 -56
- data/test_app/db/schema.rb +1 -0
- data/test_app/db/seeds.rb +1 -0
- data/test_app/public/javascripts/application.js +2 -2
- data/test_app/public/javascripts/best_in_place.js +146 -125
- data/test_app/public/stylesheets/style.css +17 -17
- metadata +79 -56
- data/Gemfile.lock +0 -78
- data/test_app/.gitignore +0 -4
- data/test_app/public/javascripts/jquery.rest_in_place.js +0 -254
@@ -0,0 +1,229 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "spec_helper"
|
3
|
+
|
4
|
+
describe BestInPlace::BestInPlaceHelpers do
|
5
|
+
describe "#best_in_place" do
|
6
|
+
before do
|
7
|
+
@user = User.new :name => "Lucia",
|
8
|
+
:last_name => "Napoli",
|
9
|
+
:email => "lucianapoli@gmail.com",
|
10
|
+
:address => "Via Roma 99",
|
11
|
+
:zip => "25123",
|
12
|
+
:country => "2",
|
13
|
+
:receive_email => false,
|
14
|
+
:description => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus a lectus et lacus ultrices auctor. Morbi aliquet convallis tincidunt. Praesent enim libero, iaculis at commodo nec, fermentum a dolor. Quisque eget eros id felis lacinia faucibus feugiat et ante. Aenean justo nisi, aliquam vel egestas vel, porta in ligula. Etiam molestie, lacus eget tincidunt accumsan, elit justo rhoncus urna, nec pretium neque mi et lorem. Aliquam posuere, dolor quis pulvinar luctus, felis dolor tincidunt leo, eget pretium orci purus ac nibh. Ut enim sem, suscipit ac elementum vitae, sodales vel sem."
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should generate a proper span" do
|
18
|
+
nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
|
19
|
+
span = nk.css("span")
|
20
|
+
span.should_not be_empty
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "general properties" do
|
24
|
+
before do
|
25
|
+
nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
|
26
|
+
@span = nk.css("span")
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should have a proper id" do
|
30
|
+
@span.attribute("id").value.should == "best_in_place_user_name"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should have the best_in_place class" do
|
34
|
+
@span.attribute("class").value.should == "best_in_place"
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should have the correct data-attribute" do
|
38
|
+
@span.attribute("data-attribute").value.should == "name"
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should have the correct data-object" do
|
42
|
+
@span.attribute("data-object").value.should == "user"
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should have no activator by default" do
|
46
|
+
@span.attribute("data-activator").should be_nil
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should have no inner_class by default" do
|
50
|
+
@span.attribute("data-inner-class").should be_nil
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "url generation" do
|
54
|
+
it "should have the correct default url" do
|
55
|
+
@user.save!
|
56
|
+
nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
|
57
|
+
span = nk.css("span")
|
58
|
+
span.attribute("data-url").value.should == "/users/#{@user.id}"
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should use the custom url specified in string format" do
|
62
|
+
out = helper.best_in_place @user, :name, :path => "/custom/path"
|
63
|
+
nk = Nokogiri::HTML.parse(out)
|
64
|
+
span = nk.css("span")
|
65
|
+
span.attribute("data-url").value.should == "/custom/path"
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should use the path given in a named_path format" do
|
69
|
+
out = helper.best_in_place @user, :name, :path => helper.users_path
|
70
|
+
nk = Nokogiri::HTML.parse(out)
|
71
|
+
span = nk.css("span")
|
72
|
+
span.attribute("data-url").value.should == "/users"
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should use the given path in a hash format" do
|
76
|
+
out = helper.best_in_place @user, :name, :path => {:controller => :users, :action => :edit, :id => 23}
|
77
|
+
nk = Nokogiri::HTML.parse(out)
|
78
|
+
span = nk.css("span")
|
79
|
+
span.attribute("data-url").value.should == "/users/23/edit"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "nil option" do
|
84
|
+
it "should have no nil data by default" do
|
85
|
+
@span.attribute("data-nil").should be_nil
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should show '' if the object responds with nil for the passed attribute" do
|
89
|
+
@user.stub!(:name).and_return(nil)
|
90
|
+
nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
|
91
|
+
span = nk.css("span")
|
92
|
+
span.text.should == ""
|
93
|
+
end
|
94
|
+
|
95
|
+
it "should show '' if the object responds with an empty string for the passed attribute" do
|
96
|
+
@user.stub!(:name).and_return("")
|
97
|
+
nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
|
98
|
+
span = nk.css("span")
|
99
|
+
span.text.should == ""
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should have the given inner_class" do
|
104
|
+
out = helper.best_in_place @user, :name, :inner_class => "awesome"
|
105
|
+
nk = Nokogiri::HTML.parse(out)
|
106
|
+
span = nk.css("span")
|
107
|
+
span.attribute("data-inner-class").value.should == "awesome"
|
108
|
+
end
|
109
|
+
|
110
|
+
it "should have the given activator" do
|
111
|
+
out = helper.best_in_place @user, :name, :activator => "awesome"
|
112
|
+
nk = Nokogiri::HTML.parse(out)
|
113
|
+
span = nk.css("span")
|
114
|
+
span.attribute("data-activator").value.should == "awesome"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
|
119
|
+
context "with a text field attribute" do
|
120
|
+
before do
|
121
|
+
nk = Nokogiri::HTML.parse(helper.best_in_place @user, :name)
|
122
|
+
@span = nk.css("span")
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should render the name as text" do
|
126
|
+
@span.text.should == "Lucia"
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should have an input data-type" do
|
130
|
+
@span.attribute("data-type").value.should == "input"
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should have no data-collection" do
|
134
|
+
@span.attribute("data-collection").should be_nil
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
context "with a boolean attribute" do
|
139
|
+
before do
|
140
|
+
nk = Nokogiri::HTML.parse(helper.best_in_place @user, :receive_email, :type => :checkbox)
|
141
|
+
@span = nk.css("span")
|
142
|
+
end
|
143
|
+
|
144
|
+
it "should have a checkbox data-type" do
|
145
|
+
@span.attribute("data-type").value.should == "checkbox"
|
146
|
+
end
|
147
|
+
|
148
|
+
it "should have the default data-collection" do
|
149
|
+
data = ["No", "Yes"]
|
150
|
+
@span.attribute("data-collection").value.should == data.to_json
|
151
|
+
end
|
152
|
+
|
153
|
+
it "should render the current option as No" do
|
154
|
+
@span.text.should == "No"
|
155
|
+
end
|
156
|
+
|
157
|
+
describe "custom collection" do
|
158
|
+
before do
|
159
|
+
nk = Nokogiri::HTML.parse(helper.best_in_place @user, :receive_email, :type => :checkbox, :collection => ["Nain", "Da"])
|
160
|
+
@span = nk.css("span")
|
161
|
+
end
|
162
|
+
|
163
|
+
it "should show the message with the custom values" do
|
164
|
+
@span.text.should == "Nain"
|
165
|
+
end
|
166
|
+
|
167
|
+
it "should render the proper data-collection" do
|
168
|
+
@span.attribute("data-collection").value.should == ["Nain", "Da"].to_json
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
end
|
173
|
+
|
174
|
+
context "with a select attribute" do
|
175
|
+
before do
|
176
|
+
@countries = COUNTRIES.to_a
|
177
|
+
nk = Nokogiri::HTML.parse(helper.best_in_place @user, :country, :type => :select, :collection => @countries)
|
178
|
+
@span = nk.css("span")
|
179
|
+
end
|
180
|
+
|
181
|
+
it "should have a select data-type" do
|
182
|
+
@span.attribute("data-type").value.should == "select"
|
183
|
+
end
|
184
|
+
|
185
|
+
it "should have a proper data collection" do
|
186
|
+
@span.attribute("data-collection").value.should == @countries.to_json
|
187
|
+
end
|
188
|
+
|
189
|
+
it "should show the current country" do
|
190
|
+
@span.text.should == "Italy"
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
describe "#best_in_place_if" do
|
196
|
+
context "when the parameters are valid" do
|
197
|
+
before(:each) do
|
198
|
+
@output = "Some Value"
|
199
|
+
@field = :somefield
|
200
|
+
@object = mock("object", @field => @output)
|
201
|
+
@options = mock("options")
|
202
|
+
end
|
203
|
+
context "when the condition is true" do
|
204
|
+
before {@condition = true}
|
205
|
+
context "when the options parameter is left off" do
|
206
|
+
it "should call best_in_place with the rest of the parameters and empty options" do
|
207
|
+
helper.should_receive(:best_in_place).with(@object, @field, {})
|
208
|
+
helper.best_in_place_if @condition, @object, @field
|
209
|
+
end
|
210
|
+
end
|
211
|
+
context "when the options parameter is included" do
|
212
|
+
it "should call best_in_place with the rest of the parameters" do
|
213
|
+
helper.should_receive(:best_in_place).with(@object, @field, @options)
|
214
|
+
helper.best_in_place_if @condition, @object, @field, @options
|
215
|
+
end
|
216
|
+
end
|
217
|
+
end
|
218
|
+
context "when the condition is false" do
|
219
|
+
before {@condition = false}
|
220
|
+
it "should return the value of the field when the options value is left off" do
|
221
|
+
helper.best_in_place_if(@condition, @object, @field).should eq @output
|
222
|
+
end
|
223
|
+
it "should return the value of the field when the options value is included" do
|
224
|
+
helper.best_in_place_if(@condition, @object, @field, @options).should eq @output
|
225
|
+
end
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "spec_helper"
|
3
|
+
|
4
|
+
describe "Double initialization bug", :js => true, :pending => true do
|
5
|
+
before do
|
6
|
+
@user = User.new :name => "Lucia",
|
7
|
+
:last_name => "Napoli",
|
8
|
+
:email => "lucianapoli@gmail.com",
|
9
|
+
:address => "Via Roma 99",
|
10
|
+
:zip => "25123",
|
11
|
+
:country => "2",
|
12
|
+
:receive_email => false,
|
13
|
+
:description => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus a lectus et lacus ultrices auctor. Morbi aliquet convallis tincidunt. Praesent enim libero, iaculis at commodo nec, fermentum a dolor. Quisque eget eros id felis lacinia faucibus feugiat et ante. Aenean justo nisi, aliquam vel egestas vel, porta in ligula. Etiam molestie, lacus eget tincidunt accumsan, elit justo rhoncus urna, nec pretium neque mi et lorem. Aliquam posuere, dolor quis pulvinar luctus, felis dolor tincidunt leo, eget pretium orci purus ac nibh. Ut enim sem, suscipit ac elementum vitae, sodales vel sem."
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should be able to change a boolean value" do
|
17
|
+
@user.save!
|
18
|
+
visit double_init_user_path(@user)
|
19
|
+
|
20
|
+
within("#receive_email") do
|
21
|
+
page.should have_content("No thanks")
|
22
|
+
end
|
23
|
+
|
24
|
+
bip_bool :user, :receive_email
|
25
|
+
|
26
|
+
visit double_init_user_path(@user)
|
27
|
+
within("#receive_email") do
|
28
|
+
page.should have_content("Yes of course")
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,165 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "spec_helper"
|
3
|
+
|
4
|
+
describe "JS behaviour", :js => true do
|
5
|
+
before do
|
6
|
+
@user = User.new :name => "Lucia",
|
7
|
+
:last_name => "Napoli",
|
8
|
+
:email => "lucianapoli@gmail.com",
|
9
|
+
:address => "Via Roma 99",
|
10
|
+
:zip => "25123",
|
11
|
+
:country => "2",
|
12
|
+
:receive_email => false,
|
13
|
+
:description => "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus a lectus et lacus ultrices auctor. Morbi aliquet convallis tincidunt. Praesent enim libero, iaculis at commodo nec, fermentum a dolor. Quisque eget eros id felis lacinia faucibus feugiat et ante. Aenean justo nisi, aliquam vel egestas vel, porta in ligula. Etiam molestie, lacus eget tincidunt accumsan, elit justo rhoncus urna, nec pretium neque mi et lorem. Aliquam posuere, dolor quis pulvinar luctus, felis dolor tincidunt leo, eget pretium orci purus ac nibh. Ut enim sem, suscipit ac elementum vitae, sodales vel sem."
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "nil option" do
|
17
|
+
it "should render the default '-' string when the field is empty" do
|
18
|
+
@user.name = ""
|
19
|
+
@user.save :validate => false
|
20
|
+
visit user_path(@user)
|
21
|
+
|
22
|
+
within("#name") do
|
23
|
+
page.should have_content("-")
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should render the passed nil value if the field is empty" do
|
28
|
+
@user.last_name = ""
|
29
|
+
@user.save :validate => false
|
30
|
+
visit user_path(@user)
|
31
|
+
|
32
|
+
within("#last_name") do
|
33
|
+
page.should have_content("Nothing to show")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should be able to use the bip_text to update a text field" do
|
39
|
+
@user.save!
|
40
|
+
visit user_path(@user)
|
41
|
+
within("#email") do
|
42
|
+
page.should have_content("lucianapoli@gmail.com")
|
43
|
+
end
|
44
|
+
|
45
|
+
bip_text :user, :email, "new@email.com"
|
46
|
+
|
47
|
+
visit user_path(@user)
|
48
|
+
within("#email") do
|
49
|
+
page.should have_content("new@email.com")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should be able to update a field two consecutive times" do
|
54
|
+
@user.save!
|
55
|
+
visit user_path(@user)
|
56
|
+
|
57
|
+
bip_text :user, :email, "new@email.com"
|
58
|
+
|
59
|
+
within("#email") do
|
60
|
+
page.should have_content("new@email.com")
|
61
|
+
end
|
62
|
+
|
63
|
+
bip_text :user, :email, "new_two@email.com"
|
64
|
+
|
65
|
+
within("#email") do
|
66
|
+
page.should have_content("new_two@email.com")
|
67
|
+
end
|
68
|
+
|
69
|
+
visit user_path(@user)
|
70
|
+
within("#email") do
|
71
|
+
page.should have_content("new_two@email.com")
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should be able to update a field after an error" do
|
76
|
+
@user.save!
|
77
|
+
visit user_path(@user)
|
78
|
+
|
79
|
+
bip_text :user, :email, "wrong format"
|
80
|
+
page.should have_content("Email has wrong email format")
|
81
|
+
|
82
|
+
bip_text :user, :email, "another@email.com"
|
83
|
+
within("#email") do
|
84
|
+
page.should have_content("another@email.com")
|
85
|
+
end
|
86
|
+
|
87
|
+
visit user_path(@user)
|
88
|
+
within("#email") do
|
89
|
+
page.should have_content("another@email.com")
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should be able to use bil_select to change a select field" do
|
94
|
+
@user.save!
|
95
|
+
visit user_path(@user)
|
96
|
+
within("#country") do
|
97
|
+
page.should have_content("Italy")
|
98
|
+
end
|
99
|
+
|
100
|
+
bip_select :user, :country, "France"
|
101
|
+
|
102
|
+
visit user_path(@user)
|
103
|
+
within("#country") do
|
104
|
+
page.should have_content("France")
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should be able to use bip_bool to change a boolean value" do
|
109
|
+
@user.save!
|
110
|
+
visit user_path(@user)
|
111
|
+
|
112
|
+
within("#receive_email") do
|
113
|
+
page.should have_content("No thanks")
|
114
|
+
end
|
115
|
+
|
116
|
+
bip_bool :user, :receive_email
|
117
|
+
|
118
|
+
visit user_path(@user)
|
119
|
+
within("#receive_email") do
|
120
|
+
page.should have_content("Yes of course")
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
it "should show validation errors" do
|
125
|
+
@user.save!
|
126
|
+
visit user_path(@user)
|
127
|
+
|
128
|
+
bip_text :user, :address, ""
|
129
|
+
page.should have_content("Address can't be blank")
|
130
|
+
within("#address") do
|
131
|
+
page.should have_content("Via Roma 99")
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
it "should show validation errors using respond_with in the controller" do
|
136
|
+
@user.save!
|
137
|
+
visit user_path(@user)
|
138
|
+
|
139
|
+
bip_text :user, :last_name, "a"
|
140
|
+
page.should have_content("last_name has invalid length")
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should be able to update a field after an error using respond_with in the controller" do
|
144
|
+
@user.save!
|
145
|
+
visit user_path(@user)
|
146
|
+
|
147
|
+
bip_text :user, :last_name, "a"
|
148
|
+
|
149
|
+
within("#last_name") do
|
150
|
+
page.should have_content("Napoli")
|
151
|
+
end
|
152
|
+
|
153
|
+
bip_text :user, :last_name, "Another"
|
154
|
+
|
155
|
+
within("#last_name") do
|
156
|
+
page.should have_content("Another")
|
157
|
+
end
|
158
|
+
|
159
|
+
visit user_path(@user)
|
160
|
+
within("#last_name") do
|
161
|
+
page.should have_content("Another")
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# Configure Rails Envinronment
|
2
|
+
ENV["RAILS_ENV"] = "test"
|
3
|
+
|
4
|
+
require File.expand_path('../../test_app/config/environment', __FILE__)
|
5
|
+
require "rspec/rails"
|
6
|
+
require "nokogiri"
|
7
|
+
|
8
|
+
# Load support files
|
9
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each{|f| require f}
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
# Remove this line if you don't want RSpec's should and should_not
|
13
|
+
# methods or matchers
|
14
|
+
require 'rspec/expectations'
|
15
|
+
|
16
|
+
config.include RSpec::Matchers
|
17
|
+
config.include BestInPlace::TestHelpers
|
18
|
+
|
19
|
+
# == Mock Framework
|
20
|
+
config.mock_with :rspec
|
21
|
+
end
|
22
|
+
|
23
|
+
Capybara.default_wait_time = 5
|