README.md in cachai-0.1.4 vs README.md in cachai-0.2.0

- old
+ new

@@ -1,26 +1,111 @@ -# Commentary +# Cachai -Add comments to your blog. +A clean way to add support for comments and pingbacks in your Rack app. Si pos weon. -## Setup -* Edit `config/production.yml` to add your site. Specify a name and the domain where the site will be hosted. Example configuration will be like this: +## Setting up -``` -name: Blog -domain: blog.sdqali.in -``` +First you need to create a config/database.yml file, as you'd have on a regular Rails app, or if using Sinatra::ActiveRecord. -* Run `RACK_ENV=production setup.rb` -* Start the server with `RACK_ENV=production app.rb` -* Add the following to your HTML pages or templates. `selector` is the CSS selector for the DOM element where comments will be rendered. + # database.yml + development: + adapter: sqlite3 + database: db/development.sqlite3 + pool: 16 -```html -<script type="text/javascript" src="<server>/jquery-1.10.2.min.js"></script> -<script type="text/javascript" src="<server>//commentary.js"></script> -<script type="text/javascript"> - $(document).ready(function() { - Commentary.initialize("<server>", "<selector>"); - }); -</script> +Now insert the middleware before the app, either in your rackup file or in the app itself. We'll go with method one. -``` + # rackup.ru + require 'sinatra' + require 'cachai' + + map '/' do + use Cachai::Middleware, { domain: 'yourdomain.com' } + run Sinatra::Application + end + +Now, the next time the app is loaded, Cachai will set up the necessary tables in the database provided. Basically a `posts` table and a `responses` one. + +## Posting new comments + +Now, to send our first comment to Cachai, let's insert a form within your blog post view. + + # views/blog/post.erb (or something like it) + + ... + + <form action="/comments" method="post" id="comment-form"> + <input type="hidden" name="url" value="http://yourdomain.com/path/to/your/blog/post" /> + <input type="text" name="author_name" /> + <input type="email" name="author_email" /> + <input type="text" name="author_url" /> + <textarea name="content"></textarea> + </form> + +Now load your app and try submitting a comment. It should work. + +## Posting comment using AJAX + +Using Javascript, you'd do something like this: + + // app.js + + $(function() { + + $('#comment-form').on('submit', function(e) { + e.preventDefault(); + + var data = { + author_name : this.author_name.value, + author_email : this.author_email.value, + author_url : this.author_url.value, + content : this.content.value, + protocol : window.location.protocol, + domain : 'yourdomain.com', + path : encodeURI(window.location.pathname) + } + + $.ajax({ + type : 'post', + data : JSON.stringify(data), + url : '/comments.json', + success : function() { alert('Thanks!') }, + error : function() { alert('Oh rats. Try again please.') }, + }) + }) + + }) + +## Reading comments via AJAX + +You get them using GET /comments.json. + + // app.js + + var domain = 'yourdomain.com', + path = encodeURI(window.location.pathname), + url = '/comments.json?callback=?&domain=' + domain + '&path=' + path; + + $.ajax({ + dataType : 'json', + url : url, + success : function(list) { /* render comments */ }), + error : function(err) { alert('Damn!') } + }); + } + +Or if you wish to insert them in the view itself. + + # views/blog/post.erb + + <ul id="comments"> + <% Cachai.get_comments_for(@post.url).each do |comment| %> + <li> + <strong><%= comment.author_name %></strong> + <strong><%= simple_format(comment.content) %></strong> + </li> + <% end %> + </ul> + +# Small print. + +(c) 2016 Tomas Pollak. MIT Licensed. \ No newline at end of file