Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish subscribe ajax #183

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 8 additions & 43 deletions app/controllers/deliveries_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# encoding: utf-8
class DeliveriesController < ApplicationController

before_filter :find_supplier, :exclude => :fill_new_stock_article_form
before_filter :find_supplier

def index
@deliveries = @supplier.deliveries.all :order => 'delivered_on DESC'
Expand Down Expand Up @@ -49,57 +49,22 @@ def destroy
flash[:notice] = I18n.t('deliveries.destroy.notice')
redirect_to supplier_deliveries_url(@supplier)
end

# three possibilites to fill a new_stock_article form
# (1) start from blank or use params
def new_stock_article
@stock_article = @supplier.stock_articles.build(params[:stock_article])

render :layout => false
end

# (2) StockArticle as template
def copy_stock_article
@stock_article = StockArticle.find(params[:old_stock_article_id]).dup

render :layout => false
end

# (3) non-stock Article as template
def derive_stock_article
@stock_article = Article.find(params[:old_article_id]).becomes(StockArticle).dup

def add_stock_change
@stock_change = StockChange.new
@stock_change.stock_article = StockArticle.find(params[:stock_article_id])
render :layout => false
end

def create_stock_article
@stock_article = StockArticle.new(params[:stock_article])
def form_on_stock_article_create
@stock_article = StockArticle.find(params[:id])

if @stock_article.valid? and @stock_article.save
render :layout => false
else
render :action => 'new_stock_article', :layout => false
end
end

def edit_stock_article
@stock_article = StockArticle.find(params[:stock_article_id])
render :layout => false
end

def update_stock_article
@stock_article = StockArticle.find(params[:stock_article][:id])
def form_on_stock_article_update
@stock_article = StockArticle.find(params[:id])

if @stock_article.update_attributes(params[:stock_article])
render :layout => false
else
render :action => 'edit_stock_article', :layout => false
end
end

def add_stock_change
@stock_change = StockChange.new
@stock_change.stock_article = StockArticle.find(params[:stock_article_id])
render :layout => false
end

Expand Down
54 changes: 46 additions & 8 deletions app/controllers/stockit_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,61 @@ def index
order('suppliers.name, article_categories.name, articles.name')
end

def index_on_stock_article_create
@stock_article = StockArticle.find(params[:id])

render :layout => false
end

def index_on_stock_article_update
@stock_article = StockArticle.find(params[:id])

render :layout => false
end

# three possibilites to fill a new_stock_article form
# (1) start from blank or use params
def new
@stock_article = StockArticle.new
@stock_article = StockArticle.new(params[:stock_article])

render :layout => false
end

# (2) StockArticle as template
def copy
@stock_article = StockArticle.find(params[:stock_article_id]).dup

render :layout => false
end

# (3) non-stock Article as template
def derive
@stock_article = Article.find(params[:old_article_id]).becomes(StockArticle).dup

render :layout => false
end

def create
@stock_article = StockArticle.new(params[:stock_article])
if @stock_article.save
redirect_to stock_articles_path, :notice => I18n.t('stockit.stock_create.notice')
if @stock_article.valid? and @stock_article.save
render :layout => false
else
render :action => 'new'
render :action => 'new', :layout => false
end
end

def edit
@stock_article = StockArticle.find(params[:id])

render :layout => false
end

def update
@stock_article = StockArticle.find(params[:id])
if @stock_article.update_attributes(params[:stock_article])
redirect_to stock_articles_path, :notice => I18n.t('stockit.stock_update.notice')
render :layout => false
else
render :action => 'edit'
render :action => 'edit', :layout => false
end
end

Expand All @@ -36,9 +68,15 @@ def show
@stock_changes = @stock_article.stock_changes.order('stock_changes.created_at DESC')
end

def show_on_stock_article_update
@stock_article = StockArticle.find(params[:id])

render :layout => false
end

def destroy
@article = StockArticle.find(params[:id])
@article.mark_as_deleted
@stock_article = StockArticle.find(params[:id])
@stock_article.mark_as_deleted
render :layout => false
rescue => error
render :partial => "destroy_fail", :layout => false,
Expand Down
9 changes: 0 additions & 9 deletions app/helpers/deliveries_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,4 @@ def stock_change_remove_link(stock_change_form)
return output.html_safe
end

def stock_article_price_hint(stock_article)
t('simple_form.hints.stock_article.edit_stock_article.price',
:stock_article_copy_link => link_to(t('.copy_stock_article'),
copy_stock_article_supplier_deliveries_path(@supplier, :old_stock_article_id => stock_article.id),
:remote => true
)
)
end

end
9 changes: 9 additions & 0 deletions app/helpers/stockit_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@ def link_to_stock_change_reason(stock_change)
link_to t('.stock_taking'), stock_taking_path(stock_change.stock_taking)
end
end

def stock_article_price_hint(stock_article)
t('simple_form.hints.stock_article.edit.price',
:stock_article_copy_link => link_to(t('.copy_stock_article'),
stock_article_copy_path(stock_article),
:remote => true
)
)
end
end
34 changes: 31 additions & 3 deletions app/views/deliveries/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
}
if('new' == selectedArticle.id) {
$.ajax({
url: '#{new_stock_article_supplier_deliveries_path(@supplier)}',
url: '#{new_stock_article_path}',
type: 'get',
data: {stock_article: {name: selectedArticle.text}},
contentType: 'application/json; charset=UTF-8'
Expand All @@ -52,7 +52,7 @@
}
if('' != selectedArticle.id) {
$.ajax({
url: '#{derive_stock_article_supplier_deliveries_path(@supplier)}',
url: '#{derive_stock_articles_path}',
type: 'get',
data: {old_article_id: selectedArticle.id},
contentType: 'application/json; charset=UTF-8'
Expand All @@ -61,6 +61,34 @@
return true;
}
});

#{##########################################################################
# Publish/subscribe pattern for loose coupling of
# (1) AJAX database manipulation and
# (2) DOM updates for the particular view
############################################################################
# * THIS STARTS STEP (2)
# * subscribe to document's events which are published/triggered by step (1)
# * possibly launch a second AJAX request to retreive the source code of the
# new DOM elements
}
$(document).on('StockArticle#create', function(e) {
$.ajax({
url: '#{form_on_stock_article_create_supplier_deliveries_path(@supplier)}',
type: 'get',
data: {id: e.stock_article_id},
contentType: 'application/json; charset=UTF-8'
});
});

$(document).on('StockArticle#update', function(e) {
$.ajax({
url: '#{form_on_stock_article_update_supplier_deliveries_path(@supplier)}',
type: 'get',
data: {id: e.stock_article_id},
contentType: 'application/json; charset=UTF-8'
});
});
});

function mark_article_for_delivery(stock_article_id) {
Expand Down Expand Up @@ -96,7 +124,7 @@
%tr
%th{:colspan => 5}
- if articles_for_select2(@supplier).empty?
= link_to t('.create_stock_article'), new_stock_article_supplier_deliveries_path(@supplier), :remote => true, :class => 'btn'
= link_to t('.create_stock_article'), new_stock_article_path(:params => {:stock_article => {:supplier_id => @supplier.id}}), :remote => true, :class => 'btn'
- else
%input#new_stock_article{:style => 'width: 500px;'}
%tbody
Expand Down
4 changes: 2 additions & 2 deletions app/views/deliveries/_stock_article_for_adding.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
%td= article.unit
%td= article.article_category.name
%td
= link_to t('.action_edit'), edit_stock_article_supplier_deliveries_path(@supplier, :stock_article_id => article.id), remote: true, class: 'btn btn-mini'
= link_to t('.action_other_price'), copy_stock_article_supplier_deliveries_path(@supplier, :old_stock_article_id => article.id), remote: true, class: 'btn btn-mini'
= link_to t('.action_edit'), edit_stock_article_path(article), remote: true, class: 'btn btn-mini'
= link_to t('.action_other_price'), stock_article_copy_path(article), remote: true, class: 'btn btn-mini'
- deliver_button_disabled = ( @delivery and @delivery.includes_article? article ) ? ( 'disabled' ) : ( false )
= link_to t('.action_add_to_delivery'), add_stock_change_supplier_deliveries_path(@supplier, :stock_article_id => article.id), :method => :post, remote: true, class: 'button-add-stock-change btn btn-mini btn-primary', disabled: deliver_button_disabled
23 changes: 0 additions & 23 deletions app/views/deliveries/_stock_article_form.html.haml

This file was deleted.

5 changes: 0 additions & 5 deletions app/views/deliveries/copy_stock_article.js.erb

This file was deleted.

16 changes: 0 additions & 16 deletions app/views/deliveries/create_stock_article.js.erb

This file was deleted.

5 changes: 0 additions & 5 deletions app/views/deliveries/derive_stock_article.js.erb

This file was deleted.

5 changes: 0 additions & 5 deletions app/views/deliveries/edit_stock_article.js.erb

This file was deleted.

13 changes: 13 additions & 0 deletions app/views/deliveries/form_on_stock_article_create.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(function() {
$('#stock_articles_for_adding tr').removeClass('success');

var stock_article_for_adding = $('<%= j(render(
:partial => 'stock_article_for_adding',
:locals => {
:article => @stock_article
}
)) %>').addClass('success');

$('#stock_articles_for_adding tbody').append(stock_article_for_adding);
updateSort('#stock_articles_for_adding');
})();
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
$('div.container-fluid').prepend(
'<%= j(render(:partial => 'shared/alert_success', :locals => {:alert_message => t('.notice', :name => @stock_article.name)})) %>'
);

(function() {
// update entry in stock_article table

$('#stock_articles_for_adding tr').removeClass('success');

var stock_article_for_adding = $(
'<%= j(render(:partial => 'stock_article_for_adding', :locals => {:article => @stock_article, :delivery => @delivery})) %>'
).addClass('success');
var stock_article_for_adding = $('<%= j(render(
:partial => 'stock_article_for_adding',
:locals => {
:article => @stock_article,
:delivery => @delivery
}
)) %>').addClass('success');

$('#stock_article_<%= @stock_article.id %>').replaceWith(stock_article_for_adding);
updateSort('#stock_articles_for_adding');
Expand All @@ -28,5 +28,3 @@ $('div.container-fluid').prepend(

updateSort('#stock_changes');
})();

$('#modalContainer').modal('hide');
5 changes: 0 additions & 5 deletions app/views/deliveries/new_stock_article.js.erb

This file was deleted.

5 changes: 5 additions & 0 deletions app/views/shared/_alert_error.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.alert.fade.in.alert-error
%a.close{:href => '#', :data => {:dismiss => 'alert'}}
= t('ui.marks.close').html_safe
= t('ui.marks.error').html_safe
= alert_message
10 changes: 5 additions & 5 deletions app/views/shared/_article_price_info.html.haml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
%table.table.table-condensed
%tr
%th= t 'activerecord.attributes.article.price'
%th= heading_helper(Article, :price)
%td.numeric= number_to_currency article.price
%tr
%th= t 'activerecord.attributes.article.deposit'
%th= heading_helper(Article, :deposit)
%td.numeric= number_to_currency article.deposit
%tr
%th= t 'activerecord.attributes.article.tax'
%th= heading_helper(Article, :tax)
%td.numeric= number_to_percentage article.tax
- unless article.fc_price == article.gross_price
%tr
%th= t 'activerecord.attributes.article.fc_share'
%th= heading_helper(Article, :fc_share, {:short => true})
%td.numeric= number_to_currency(article.fc_price-article.gross_price)
%tr
%th= t 'activerecord.attributes.article.fc_price'
%th= heading_helper(Article, :fc_price)
%td.numeric= number_to_currency article.fc_price
Loading