Skip to content

Commit

Permalink
Article editing and saving now works w00t!
Browse files Browse the repository at this point in the history
  • Loading branch information
wayneeseguin committed Oct 12, 2008
1 parent d0e3b0c commit 1e64387
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 56 deletions.
26 changes: 17 additions & 9 deletions app/controllers/articles.rb
@@ -1,5 +1,6 @@
class Articles < Application
provides :json
does_not_provide :html

def index
@articles = Article.all
Expand All @@ -13,43 +14,50 @@ def show
end

def new
only_provides :html
@article = Article.new
render
display @article
end

def edit
only_provides :html
@article = Article.get(params[:id])
raise NotFound unless @article
render
display @article
end

def create
raise BadRequest, "No params passed to create a new object, check your new action view!" if params[:article].nil?
@article = Article.new(params[:article])
if @article.save
redirect url(:article, @article)
display @article
else
render :new
display Hash.new(:status => "error", :errors => @article.errors.to_a)
end
end

def update
@article = Article.get(params[:id])
authors = params[:authors]
tags = params[:tags]

published_at = params[:article].delete(:published_at)
unless params[:published_at].blank?
params[:article][:published_at] = DateTime.parse(published_at)
end

raise NotFound unless @article

if @article.update_attributes(params[:article]) || !@article.dirty?
redirect url(:article, @article)
display @article
else
raise BadRequest
end
end

def destroy
@article = Article.get(params[:id])
raise NotFound unless @article
if @article.destroy
redirect url(:articles)
display Hash.new(:status => "ok", :id => params[:id])
else
raise BadRequest
end
Expand Down
8 changes: 4 additions & 4 deletions app/models/article.rb
Expand Up @@ -12,7 +12,7 @@ class Article
property :state, String, :length => 16 # we'll use state_machine on this
property :parent_id, Integer # eg. 4 part series of related articles.
property :rating, Integer # how good of a Article the user thinks it is 1..10
property :publised_at, DateTime # Allow them to set publish date
property :published_at, DateTime # Allow them to set publish date
property :markup, String, :length => 32
property :raw, Text
property :html, Text
Expand Down Expand Up @@ -58,12 +58,12 @@ class Article
elsif markup == "plain"
self.html = "<pre>#{@raw}</pre>"
else
return [false, "No markup was specified."]
[false, "No markup was specified."]
end
true
rescue Exception => e
return [false, e.message]
[false, e.message]
end
true
end

#############################################################################
Expand Down
10 changes: 5 additions & 5 deletions app/views/articles/articles.jqt
Expand Up @@ -2,8 +2,8 @@
<script type="text/x-jquery-template" title="articles">
<div class="articles" blog_id="<%= self.blog_id %>">
<%= partial("articles_menu", {location: "top"}) %>
<% self.articles.push({id: 0}); %>
<% $.each(self.articles || [], function(index, article) { %>

<% $.each((self.articles || []), function(index, article) { %>
<%= partial("article", article) %>
<% }); %>

Expand Down Expand Up @@ -64,7 +64,7 @@
</script>

<script type="text/x-jquery-template" title="article_form">
<form action="/articles" method="article" class="article">
<form article_id="<%= self.id || 0 %>" class="article" action="/articles<%= (self.id > 0) ? '/'+ self.id : '' %>" method="<%= (self.id == '0') ? "POST" : "PUT" %>">
<div class="article" article_id="<%= self.id %>">
<div class="header">
<div class="menu"> </div>
Expand All @@ -76,12 +76,12 @@
</div>
</div>
<div class="content">
<textarea name="article[html]"><%= self.html || "" %></textarea>
<textarea name="article[raw]"><%= self.raw || "" %></textarea>
</div>
<div class="footer">
<span class="authors">
By:
<select name="author" multiple="multiple" name="article[authors][]">
<select multiple="multiple" name="authors[]">
<% $.each($.authors || [], function (index, author) { %>
<option value="<%= author.id %>"> <%= author.name %> </option>
<% }); %>
Expand Down
16 changes: 8 additions & 8 deletions app/views/blogs/blogs.jqt
@@ -1,14 +1,14 @@

<script type="text/x-jquery-template" title="blogs">
<div id="blogs">
<% $.each(self, function(index, blog) { %>
<span class="blog" blog_id="<%= blog.id %>"><%= partial("blog", blog) %></span>
<% }); %>
</div>
<div id="blogs">
<% $.each(self, function(index, blog) { %>
<span class="blog" blog_id="<%= blog.id %>"><%= partial("blog", blog) %></span>
<% }); %>
</div>
</script>

<script type="text/x-jquery-template" title="blog">
<div class="blog">
<%= self.name %>
</div>
<div class="blog">
<%= self.name %>
</div>
</script>
4 changes: 3 additions & 1 deletion config/router.rb
Expand Up @@ -6,7 +6,9 @@
article.resources :comments
end
end

resources :articles
resources :comments

all_slices

match('/').
Expand Down
5 changes: 5 additions & 0 deletions public/javascripts/app/admin.js
@@ -1,3 +1,8 @@
function context () {
user();
admin();
}

function admin () {
// Admin specific overrides
}
32 changes: 21 additions & 11 deletions public/javascripts/app/master.js
Expand Up @@ -8,24 +8,33 @@ $(document).ready(function() {
});

function get_entities_for (owner_type, owner_id, entity_type, entity_id, callback) {
$.app[owner_type] = $.app[owner_type] || {};
$.app[owner_type][owner_id] = $.app[owner_type][owner_id] || {};
$.app[entity_type] = $.app[entity_type] || {};

url = "/"+owner_type+"/"+owner_id+"/"+entity_type+"/"
if(entity_id) { url += entity_id; }
$.getJSON(url, null, function(payload) {
$.app[owner_type] = $.app[owner_type] || {};
$.app[owner_type][owner_id] = $.app[owner_type][owner_id] || {};
if(entity_id) {
// Buffer explicitely
$.app[entity_type][entity_id] = payload;
} else {
$.app[owner_type][owner_id][entity_type] = payload;
$.each(payload, function(index,entity) {
$.app[entity_type][entity.id] = entity;
});
$.app[owner_type][owner_id][entity_type] = payload
}

if(callback) { callback(owner_type,owner_id,entity_type,entity_id,payload); }
});
}

function get_entities (entity_type, callback) {
$.app[entity_type] = $.app[entity_type] || {};
$.getJSON("/" + entity_type, null, function(payload) {
$.app[entity_type] = payload;
$.each(payload, function(index,entity) {
$.app[entity_type][entity.id] = entity;
});
if(callback) { callback(payload); }
});
}
Expand Down Expand Up @@ -67,12 +76,13 @@ function sort_by(list, field) {
return sorted_list;
}

function find_by_id(list,field,value) {
result = {};
for(index in list) {
if(list[index][field] == value) {
result = list[index];
function find(list,field,value) {
list = list || [];
item = null; // So that it's from the outer scope.
$.each(list, function(index,entity) {
if(entity && (entity[field] == value)) {
item = entity;
}
}
return result;
});
return item;
}
52 changes: 35 additions & 17 deletions public/javascripts/app/public.js
@@ -1,11 +1,11 @@
function load_ui () {
display();
special();
public();
context();
}

function special () { }
function context () { }

function display () {
function public () {
$("body").
append($.templates.default_layout());

Expand Down Expand Up @@ -35,15 +35,15 @@ function blogs() {
}

function articles(owner_type,owner_id,entity_type,entity_id) {
owner = find_by_id($.app[owner_type],"id",owner_id);

owner = $.app[owner_type][owner_id];
header("Articles in "+owner.name);

$("div#blogs").hide();

$("div#content").
append($.templates.articles({
"blog_id": owner_id, "articles": $.app[owner_type][owner_id].articles
"blog_id": owner_id, "articles": owner.articles
})
);

Expand All @@ -62,23 +62,41 @@ function articles(owner_type,owner_id,entity_type,entity_id) {
});
}

function article_edit(owner_type,owner_id,entity_type,article_id) {
if(article_id > 0) {
owner = find_by_id($.app[owner_type],owner_id);
article = find_by_id(owner.articles,article_id);
function article_edit(owner_type,owner_id,entity_type,entity_id) {
if(entity_id != 0) {
entity = find($.app[entity_type]||[],"id",entity_id);
} else {
article = {id: 0}
entity = {id: 0}
}

$("div[article_id="+(article_id||0)+"]").empty().append(
$.templates.article_form(article)
$("div[article_id="+(entity_id||0)+"]").empty().append(
$.templates.article_form(entity)
).show();
$("form[]").ajaxForm();

form = $("form[article_id="+(entity_id||0)+"]");
form.ajaxForm({
url: form.attr("action"), type: (entity.id == "0") ? "POST" : "PUT", dataType: "json",
success: article_response, beforeSubmit: validate_article
}
);
}

function validate_article () {
}

function article_response(response) {
$.app.articles[response.id] = response;
$("div[article_id="+(response.id||0)+"]").empty().append(
$.templates.article(response)
);
$("span.edit[article_id="+(response.id)+"]").click(function() {
article_edit(owner_type,owner_id,entity_type,$(this).attr("article_id"));
});
}

function comments(owner_type,owner_id,entity_type,entity_id) {
$("div#articles").
append($.templates.articles({
append($.templates.comments({
"blog_id": owner_id, "comments": $.app[owner_type][owner_id].comments
})
);
Expand Down
5 changes: 5 additions & 0 deletions public/javascripts/app/user.js
@@ -1,3 +1,8 @@
function context () {
user();
}

function user () {
// User specific overrides, edit
}

2 changes: 1 addition & 1 deletion schema/migrations/001_initial.rb
Expand Up @@ -18,7 +18,7 @@
column :state, String, :size => 16, :default => "draft" # we'll use state_machine on this
column :parent_id, Integer # eg. 4 part series of related articles.
column :rating, Integer # how good of a Article the user thinks it is 1..10
column :publised_at, DateTime # Allow them to set publish date
column :published_at, DateTime # Allow them to set publish date
column :markup, String, :size => 32

column :raw, Text
Expand Down

0 comments on commit 1e64387

Please sign in to comment.