Skip to content

Commit

Permalink
AJAX delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Anatoliy Chakkaev committed Sep 24, 2010
1 parent d9bbf99 commit f5c6069
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 21 deletions.
12 changes: 8 additions & 4 deletions app/controllers/outlays_controller.rb
Expand Up @@ -8,15 +8,19 @@ def index
end

def create
if @house_book.outlay_records.create_from_string(params[:outlay_record])
if @record = @house_book.outlay_records.create_from_string(params[:outlay_record])
flash[:notice] = 'Record created'
@day = @house_book.outlay_records.by_date(Date.today)
end
redirect_to root_path
redirect_to root_path unless request.xhr?
end

def destroy
@house_book.outlay_records.find(params[:id]).destroy
redirect_to root_path
record = @house_book.outlay_records.find(params[:id])
@time = record.created_at
@day = @house_book.outlay_records.by_date(@time)
record.destroy
redirect_to root_path unless request.xhr?
end

def edit
Expand Down
2 changes: 1 addition & 1 deletion app/models/outlay_record.rb
Expand Up @@ -16,6 +16,6 @@ def self.create_from_string(s)
:note => match[3]
})
record.outlay_category = record.house_book.outlay_categories.find_or_create_by_name(match[2])
record.save
record.save && record
end
end
22 changes: 9 additions & 13 deletions app/views/outlays/_day_block.html.erb
@@ -1,14 +1,8 @@
<% date = records.present? ? records.to_a.first.created_at.to_date : Date.today %>
<div class="block_wrapper">
<div class="day_block upper_rounded">
<div class="day_block upper_rounded" data-date="<%= date %>">
<div class="header upper_rounded">
<%=
if records.present?
date = records.to_a.first.created_at
else
date = Date.today
end
date.strftime '%A, %e %B'
%>
<%= date.strftime '%A, %e %B' %>
<span class="amount">
<%= records.to_a.sum(&:amount) %>
</span>
Expand All @@ -25,8 +19,8 @@
<div class="records">
<% records.last.each do |record| %>
<div class="record" data-id="<%= record.id %>">
<%= record.note %>
<%= link_to "Delete", record, :method => :delete, :confirm => 'Sure?' %>
<%= record.note %>&nbsp;
<%= link_to "Delete", record, :method => :delete, :confirm => 'Sure?', :remote => true %>
<%= link_to "Edit", [:edit, record] %>
<span class="amount">
<%= record.amount %>
Expand All @@ -37,11 +31,13 @@
<% end %>
<% else %>
<p class="no_records">
No outlay records for today
No outlay records for today
</p>
<% end %>
</div>
</div>
</div>

<%= link_to "Earlier", { :action => 'earlier', :date => date.to_date }, :id => 'load_earlier', :remote => true %>
<% unless defined?(only_block) && only_block %>
<%= link_to "Earlier", { :action => 'earlier', :date => date.to_date }, :id => 'load_earlier', :remote => true %>
<% end %>
9 changes: 9 additions & 0 deletions app/views/outlays/create.js.erb
@@ -0,0 +1,9 @@
$('.day_block:first').parent().replaceWith('<%= escape_javascript(render :partial => 'day_block', :locals => { :records => @day, :only_block => true } ) %>')

<% if @record %>
if ($.inArray(cat, categories) === -1) {
var cat = '<%= @record.outlay_category.name %>';
categories.push(cat);
categories.sort();
}
<% end %>
6 changes: 6 additions & 0 deletions app/views/outlays/destroy.js.erb
@@ -0,0 +1,6 @@
var $block = $('.day_block[data-date=<%= @time.to_date %>]');
<% if @day.empty? %>
$block.remove();
<% else %>
$block.parent().replaceWith('<%= escape_javascript(render :partial => 'day_block', :locals => { :records => @day, :only_block => true } ) %>')
<% end %>
6 changes: 3 additions & 3 deletions app/views/outlays/index.html.erb
@@ -1,8 +1,8 @@
<% content_for :head do %>
<script>
var categories = ['<%= @house_book.outlay_categories.map(&:name).join("','") %>'].sort();
var notes = ['<%= @house_book.outlay_records.map(&:note).uniq.join("','") %>'].sort();
jQuery(function () {
var categories = ['<%= @house_book.outlay_categories.map(&:name).join("','") %>'].sort();
var notes = ['<%= @house_book.outlay_records.map(&:note).uniq.join("','") %>'].sort();
var $input = jQuery('.form_container input[type=text]');
$input.autocomplete([
{
Expand All @@ -20,7 +20,7 @@
<% end %>
<div class="block_wrapper">
<div class="form_container upper_rounded">
<%= form_for @house_book.outlay_records.build do |f| %>
<%= form_for @house_book.outlay_records.build, :remote => true do |f| %>
<%= text_field_tag :outlay_record %><br/>
<p class="example">
Example: <strong>
Expand Down

0 comments on commit f5c6069

Please sign in to comment.