Permalink
Browse files

mocked up example application for tests

  • Loading branch information...
ronalchn committed Feb 19, 2012
1 parent a0d9562 commit f286c1281ef1ffe31186bedf1548f907b6adbc58
View
@@ -1,3 +1,11 @@
+## v0.0.3 TO BE RELEASED
+* Added functional tests for controller and helper additions
+* Mocked up a rails application using ajax_pagination, at test/rails_app/. You can run this example application using:
+```sh
+bundle install
+bundle exec rails server
+```
+
## v0.0.2
* Semi-transparent white rectangle changed to completely transparent rectangle. Instead, the opacity of the content behind it is lowered to achieve the same effect, but works better on backgrounds of other colours as well.
* Added rdoc comments to code
View
@@ -151,6 +151,20 @@ Instead of passing in the Array/Hash Ruby object, a string in json form is accep
```erb
<%= ajax_pagination :reload => '[{"urlregex":"page=([0-9]+)","regexindex":1},{"query":"page"}]' %>
```
+## Example Application
+This gem contains an example application (actually, it is there also for testing purposes), however it is nevertheless a good example.
+
+Clone this repository using:
+
+```sh
+git clone git://github.com/ronalchn/ajax_pagination.git
+cd ajax_pagination
+cd test/rails_app
+bundle install
+bundle exec rails server
+```
+
+Then point your browser to http://localhost:3000/
## AJAX Call
The AJAX Call is triggered by a link wrapped in any container with a certain class. The AJAX Call is to the same address, but with the ?pagination=NAME parameter added. The format requested is javascript. If the controller also returns javascript for other uses, AJAX Pagination does not necessarily prevent such uses. The ajax_pagination(format, :pagination => "page") function in the controller handles the AJAX Call when the format is javascript, and the ?pagination parameter is set to the correct string. It also returns true if the pagination parameter matches. Therefore, you can use use the javascript format when it does not match, as shown below:
View
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
# specify any dependencies here; for example:
s.add_development_dependency "rspec"
+ s.add_development_dependency "sqlite3"
# s.add_runtime_dependency "rest-client"
s.add_runtime_dependency "rails", '>= 3.1'
@@ -1,6 +1,7 @@
.bundle
db/schema.rb
db/*.sqlite3
+!db/development.sqlite3
tmp/
log/*.log
View
@@ -0,0 +1,42 @@
+source 'https://rubygems.org'
+
+gem 'rails', '3.2.1'
+
+# Bundle edge Rails instead:
+# gem 'rails', :git => 'git://github.com/rails/rails.git'
+
+gem 'sqlite3'
+
+gem 'json'
+
+# Gems used only for assets and not required
+# in production environments by default.
+group :assets do
+ gem 'sass-rails', '~> 3.2.3'
+ gem 'coffee-rails', '~> 3.2.1'
+
+ # See https://github.com/sstephenson/execjs#readme for more supported runtimes
+ # gem 'therubyracer'
+
+ gem 'uglifier', '>= 1.0.3'
+end
+
+gem 'jquery-rails'
+gem 'jquery-historyjs'
+gem 'will_paginate'
+gem 'ajax_pagination'
+
+# To use ActiveModel has_secure_password
+# gem 'bcrypt-ruby', '~> 3.0.0'
+
+# To use Jbuilder templates for JSON
+# gem 'jbuilder'
+
+# Use unicorn as the web server
+# gem 'unicorn'
+
+# Deploy with Capistrano
+# gem 'capistrano'
+
+# To use debugger
+# gem 'ruby-debug'
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -12,4 +12,6 @@
//
//= require jquery
//= require jquery_ujs
+//= require history
+//= require ajax_pagination
//= require_tree .
@@ -0,0 +1,2 @@
+// Place all the behaviors and hooks related to the matching controller here.
+// All this logic will automatically be available in application.js.
@@ -0,0 +1,4 @@
+/*
+ Place all the styles related to the matching controller here.
+ They will automatically be included in application.css.
+*/
@@ -0,0 +1,56 @@
+body { background-color: #fff; color: #333; }
+
+body, p, ol, ul, td {
+ font-family: verdana, arial, helvetica, sans-serif;
+ font-size: 13px;
+ line-height: 18px;
+}
+
+pre {
+ background-color: #eee;
+ padding: 10px;
+ font-size: 11px;
+}
+
+a { color: #000; }
+a:visited { color: #666; }
+a:hover { color: #fff; background-color:#000; }
+
+div.field, div.actions {
+ margin-bottom: 10px;
+}
+
+#notice {
+ color: green;
+}
+
+.field_with_errors {
+ padding: 2px;
+ background-color: red;
+ display: table;
+}
+
+#error_explanation {
+ width: 450px;
+ border: 2px solid red;
+ padding: 7px;
+ padding-bottom: 0;
+ margin-bottom: 20px;
+ background-color: #f0f0f0;
+}
+
+#error_explanation h2 {
+ text-align: left;
+ font-weight: bold;
+ padding: 5px 5px 5px 15px;
+ font-size: 12px;
+ margin: -7px;
+ margin-bottom: 0px;
+ background-color: #c00;
+ color: #fff;
+}
+
+#error_explanation ul li {
+ font-size: 12px;
+ list-style: square;
+}
@@ -0,0 +1,84 @@
+class PostsController < ApplicationController
+ # GET /posts
+ # GET /posts.json
+ def index
+ @posts = Post.order('created_at DESC').paginate(:page => params[:page], :per_page => 2)
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.json { render :json => @posts }
+ ajax_pagination(format)
+ end
+ end
+
+ # GET /posts/1
+ # GET /posts/1.json
+ def show
+ @post = Post.find(params[:id])
+
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render :json => @post }
+ end
+ end
+
+ # GET /posts/new
+ # GET /posts/new.json
+ def new
+ @post = Post.new
+
+ respond_to do |format|
+ format.html # new.html.erb
+ format.json { render :json => @post }
+ end
+ end
+
+ # GET /posts/1/edit
+ def edit
+ @post = Post.find(params[:id])
+ end
+
+ # POST /posts
+ # POST /posts.json
+ def create
+ @post = Post.new(params[:post])
+
+ respond_to do |format|
+ if @post.save
+ format.html { redirect_to @post, :notice => 'Post was successfully created.' }
+ format.json { render :json => @post, :status => :created, :location => @post }
+ else
+ format.html { render :action => "new" }
+ format.json { render :json => @post.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # PUT /posts/1
+ # PUT /posts/1.json
+ def update
+ @post = Post.find(params[:id])
+
+ respond_to do |format|
+ if @post.update_attributes(params[:post])
+ format.html { redirect_to @post, :notice => 'Post was successfully updated.' }
+ format.json { head :no_content }
+ else
+ format.html { render :action => "edit" }
+ format.json { render :json => @post.errors, :status => :unprocessable_entity }
+ end
+ end
+ end
+
+ # DELETE /posts/1
+ # DELETE /posts/1.json
+ def destroy
+ @post = Post.find(params[:id])
+ @post.destroy
+
+ respond_to do |format|
+ format.html { redirect_to posts_url }
+ format.json { head :no_content }
+ end
+ end
+end
@@ -0,0 +1,2 @@
+module PostsHelper
+end
@@ -0,0 +1,2 @@
+class Post < ActiveRecord::Base
+end
@@ -0,0 +1,25 @@
+<%= form_for(@post) do |f| %>
+ <% if @post.errors.any? %>
+ <div id="error_explanation">
+ <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
+
+ <ul>
+ <% @post.errors.full_messages.each do |msg| %>
+ <li><%= msg %></li>
+ <% end %>
+ </ul>
+ </div>
+ <% end %>
+
+ <div class="field">
+ <%= f.label :title %><br />
+ <%= f.text_field :title %>
+ </div>
+ <div class="field">
+ <%= f.label :content %><br />
+ <%= f.text_area :content %>
+ </div>
+ <div class="actions">
+ <%= f.submit %>
+ </div>
+<% end %>
@@ -0,0 +1,12 @@
+<%= will_paginate @posts, :params => { :pagination => nil } %>
+<%= ajax_pagination_loadzone do %>
+ <% @posts.each do |post| %>
+ <div style="border-bottom: 1px solid black; padding-bottom: 10px;">
+ <p><h2><%= post.title %></h2><i>Published <%= post.created_at.strftime('%d %B %Y') %></i></p>
+ <%= simple_format(post.content) %>
+ <%= link_to 'Show', post %> | <%= link_to 'Edit', edit_post_path(post) %> |
+ <%= link_to 'Destroy', post, :confirm => 'Are you sure?', :method => :delete %>
+ </div>
+ <% end %>
+<% end %>
+<%= will_paginate @posts, :params => { :pagination => nil } %>
@@ -0,0 +1,6 @@
+<h1>Editing post</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Show', @post %> |
+<%= link_to 'Back', posts_path %>
@@ -0,0 +1,6 @@
+
+<%= ajax_pagination %>
+
+<br />
+
+<%= link_to 'New Post', new_post_path %>
@@ -0,0 +1,5 @@
+<h1>New post</h1>
+
+<%= render 'form' %>
+
+<%= link_to 'Back', posts_path %>
@@ -0,0 +1,13 @@
+<p id="notice"><%= notice %></p>
+
+<h1>
+ <%= @post.title %>
+</h1>
+<i>Published <%= @post.created_at.strftime('%d %B %Y') %></i>
+<p>
+ <%= simple_format(@post.content) %>
+</p>
+
+
+<%= link_to 'Edit', edit_post_path(@post) %> |
+<%= link_to 'Back', posts_path %>
@@ -3,7 +3,7 @@
require 'rails/all'
Bundler.require
-require "ajax_pagination"
+#require "ajax_pagination"
module RailsApp
class Application < Rails::Application
@@ -1,4 +1,6 @@
RailsApp::Application.routes.draw do
+ resources :posts
+
# The priority is based upon order of creation:
# first created -> highest priority.
@@ -48,7 +50,7 @@
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
- # root :to => 'welcome#index'
+ root :to => 'posts#index'
# See how all your routes lay out with "rake routes"
Binary file not shown.
@@ -0,0 +1,10 @@
+class CreatePosts < ActiveRecord::Migration
+ def change
+ create_table :posts do |t|
+ t.string :title
+ t.text :content
+
+ t.timestamps
+ end
+ end
+end
@@ -0,0 +1,9 @@
+# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html
+
+one:
+ title: MyString
+ content: MyText
+
+two:
+ title: MyString
+ content: MyText
Oops, something went wrong.

0 comments on commit f286c12

Please sign in to comment.