callback tests
This commit is contained in:
parent
e46411ba79
commit
cd8e2e8bb6
|
@ -2,19 +2,32 @@
|
|||
|
||||
<div class="callback-details" id="callback-details-<%= callback_details_id %>">
|
||||
<script type="text/javascript">
|
||||
var id = "callback-details-<%= callback_details_id %>";
|
||||
$(".callback-details[id!=" + id + "]").remove(); // Remove any callback details if any
|
||||
var id = "callback-details-<%= callback_details_id %>",
|
||||
callbacks = [];
|
||||
|
||||
// Remove any callback details if any
|
||||
$('.callback-details[id!=" + id + "]').remove();
|
||||
|
||||
|
||||
// Convert callbacks to javascript variable
|
||||
<% callbacks.each do |callback| %>
|
||||
var params = eval($('<div/>').html("(<%= callback[:params].to_json %>)").text()),
|
||||
callbackName = "<%= callback[:name] %>",
|
||||
callback = Paloma.callbacks[callbackName];
|
||||
|
||||
console.log("Paloma: Fetching [" + callbackName + "]");
|
||||
callbacks.push({
|
||||
name: '<%= callback[:name] %>',
|
||||
params: eval($('<div/>').html("(<%= callback[:params].to_json %>)").text())
|
||||
});
|
||||
<% end %>
|
||||
|
||||
|
||||
// Run Callbacks
|
||||
for (var i = 0, len = callbacks.length; i < len; i++){
|
||||
var callback = callbacks[i],
|
||||
callbackFunction = Paloma.callbacks[callback.name];
|
||||
|
||||
console.log("Paloma: Fetching [" + callback.name + "]");
|
||||
if (callback != undefined){
|
||||
console.log("Paloma: Calling [" + callbackName + "]");
|
||||
$(document).ready(function(e){ callback(params); });
|
||||
console.log("Paloma: Calling [" + callback.name + "]");
|
||||
callbackFunction(callback.params);
|
||||
}
|
||||
<% end %>
|
||||
}
|
||||
</script>
|
||||
</div>
|
||||
|
|
|
@ -19,7 +19,7 @@ module Paloma
|
|||
|
||||
def update_callback
|
||||
add_to_callbacks @__callback__, @__js_params__
|
||||
|
||||
|
||||
response_body[0] += view_context.render(
|
||||
:partial => "paloma/callback_hook",
|
||||
:locals => {:callbacks => session[:callbacks]})
|
||||
|
@ -30,6 +30,7 @@ module Paloma
|
|||
|
||||
|
||||
def add_to_callbacks name, params
|
||||
return true if name.nil?
|
||||
session[:callbacks] ||= []
|
||||
session[:callbacks].push({:name => name, :params => params})
|
||||
end
|
||||
|
|
|
@ -1,11 +1,65 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe "Rendering HTML response of controller's action ", :type => :feature do
|
||||
feature 'Callbacks' do
|
||||
|
||||
it "executes the correct javascript callback" do
|
||||
visit new_article_path
|
||||
it 'should execute articles/new callback', :js => true do
|
||||
visit new_article_path
|
||||
page.has_selector?('#from-articles-new-callback').should == true
|
||||
end
|
||||
|
||||
|
||||
it 'should execute callbacks for articles/create and articles/show', :js => true do
|
||||
visit new_article_path
|
||||
|
||||
page.has_css?('.callback-details').should == true
|
||||
fill_in 'article[title]', :with => 'sexy paloma'
|
||||
fill_in 'article[body]', :with => 'sexy paloma body'
|
||||
click_button 'Save'
|
||||
|
||||
page.has_selector?('#from-articles-create-callback').should == true
|
||||
page.has_selector?('#from-articles-show-callback').should == true
|
||||
end
|
||||
|
||||
|
||||
it 'should execute "new" callback instead of "create" after failed save', :js => true do
|
||||
visit new_article_path
|
||||
|
||||
fill_in 'article[body]', :with => 'sexy paloma body'
|
||||
click_button 'Save'
|
||||
|
||||
page.has_selector?('#from-articles-create-callback').should == false
|
||||
page.has_selector?('#from-articles-new-callback').should == true
|
||||
end
|
||||
|
||||
|
||||
it 'should have an access on the passed parameters on js_callback', :js => true do
|
||||
1.upto(30) do |i|
|
||||
Article.create :title => "Sexy Paloma #{i}", :body => "Sexy Body"
|
||||
end
|
||||
|
||||
visit articles_path
|
||||
page.has_selector?('#article-count-30').should == true
|
||||
end
|
||||
|
||||
|
||||
it 'should not execute articles/update callback', :js => true do
|
||||
article = Article.create :title => "Sexy Paloma Baby!", :body => "OMG"
|
||||
|
||||
visit edit_article_path(article)
|
||||
fill_in 'article[body]', :with => 'Updated Body'
|
||||
click_button 'Save'
|
||||
|
||||
page.has_selector?('#from-articles-update-callback').should == false
|
||||
page.has_selector?('#from-articles-show-callback').should == true
|
||||
end
|
||||
|
||||
|
||||
it 'should execute articles/edit callback after failed update', :js => true do
|
||||
article = Article.create :title => 'Sexy Paloma Baby!', :body => 'Yeah'
|
||||
|
||||
visit edit_article_path(article)
|
||||
fill_in 'article[title]', :with => ''
|
||||
click_button 'Save'
|
||||
|
||||
page.has_selector?('#from-articles-edit-callback').should == true
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
//
|
||||
//= require jquery.js
|
||||
//= require jquery_ujs.js
|
||||
//= require_tree .
|
||||
//= require paloma
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
Paloma.callbacks['articles/create'] = function(params){
|
||||
$('body').append($("<div id='from-articles-create-callback'></div>"));
|
||||
};
|
|
@ -0,0 +1,3 @@
|
|||
Paloma.callbacks['articles/edit'] = function(params){
|
||||
$('body').append($("<div id='from-articles-edit-callback'></div>"));
|
||||
};
|
|
@ -0,0 +1,3 @@
|
|||
Paloma.callbacks['articles/index'] = function(params){
|
||||
$('body').append($("<div id='article-count-" + params['article_count'] + "'></div>"));
|
||||
};
|
|
@ -0,0 +1,3 @@
|
|||
Paloma.callbacks['articles/show'] = function(params){
|
||||
$('body').append($("<div id='from-articles-show-callback'></div>"));
|
||||
};
|
|
@ -0,0 +1,3 @@
|
|||
Paloma.callbacks['articles/update'] = function(params){
|
||||
$('body').append($("<div id='from-articles-update-callback'></div>"));
|
||||
};
|
|
@ -1,3 +1,2 @@
|
|||
window.Paloma = {callbacks:{}};
|
||||
|
||||
//= require ./paloma
|
||||
//= require ./articles/callbacks
|
|
@ -0,0 +1 @@
|
|||
window.Paloma = {callbacks:{}};
|
|
@ -0,0 +1,5 @@
|
|||
<% @articles.each do |article| %>
|
||||
<div>
|
||||
<%= article.title %>: <%= article.body %>
|
||||
</div>
|
||||
<% end %>
|
|
@ -5,5 +5,5 @@
|
|||
Body:
|
||||
<%= f.text_area :body %>
|
||||
|
||||
<%= f.submit %>
|
||||
<%= f.submit :Save %>
|
||||
<% end %>
|
||||
|
|
|
@ -5,6 +5,12 @@ end
|
|||
|
||||
class ArticlesController < ApplicationController
|
||||
|
||||
def index
|
||||
@articles = Article.all
|
||||
js_callback :params => {:article_count => @articles.size}
|
||||
end
|
||||
|
||||
|
||||
def show
|
||||
@article = Article.find params[:id]
|
||||
end
|
||||
|
@ -17,11 +23,30 @@ class ArticlesController < ApplicationController
|
|||
|
||||
def create
|
||||
@article = Article.new params[:article]
|
||||
|
||||
|
||||
if @article.save
|
||||
redirect_to @article
|
||||
else
|
||||
js_callback :new
|
||||
render :new
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def edit
|
||||
@article = Article.find params[:id]
|
||||
render :new
|
||||
end
|
||||
|
||||
|
||||
def update
|
||||
@article = Article.find params[:id]
|
||||
|
||||
if @article.update_attributes params[:article]
|
||||
js_callback false
|
||||
redirect_to @article
|
||||
else
|
||||
js_callback :controller => :articles, :action => :edit
|
||||
render :new
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,19 +1,22 @@
|
|||
# ActiveRecord Configuration
|
||||
# We are not using :memory: database to handle javascript requests on controller
|
||||
require 'active_record/railtie'
|
||||
ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => ':memory:'}}
|
||||
ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => 'paloma_test'}}
|
||||
ActiveRecord::Base.establish_connection('test')
|
||||
|
||||
|
||||
# Model
|
||||
class Article < ActiveRecord::Base
|
||||
attr_accessible :title, :body
|
||||
|
||||
validates_presence_of :title
|
||||
end
|
||||
|
||||
|
||||
# Migration
|
||||
class CreateArticles < ActiveRecord::Migration
|
||||
def self.up
|
||||
create_table :articles do |t|
|
||||
create_table :articles, :force => true do |t|
|
||||
t.string :title
|
||||
t.string :body
|
||||
end
|
||||
|
|
Binary file not shown.
|
@ -8,3 +8,14 @@ require 'rspec/rails'
|
|||
|
||||
require 'capybara/rspec'
|
||||
require 'database_cleaner'
|
||||
|
||||
RSpec.configure do |config|
|
||||
config.before :each do
|
||||
DatabaseCleaner.strategy = :truncation
|
||||
DatabaseCleaner.start
|
||||
end
|
||||
|
||||
config.after do
|
||||
DatabaseCleaner.clean
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue