ROR - Point of Sale (POS)A Library Class : PointShared access pointLibrary Class: Point - Follow...
What does routing an IP address mean?
Pre-modern battle - command it, or fight in it?
copy and scale one figure (wheel)
Added a new user on Ubuntu, set password not working?
What will be next at the bottom row and why?
Biological Blimps: Propulsion
Why does the Sun have different day lengths, but not the gas giants?
What should you do if you miss a job interview (deliberately)?
Why a symmetric relation is defined: ∀x∀y( xRy⟹yRx) and not ∀x∀y (xRy⟺yRx)?
Electoral considerations aside, what are potential benefits, for the US, of policy changes proposed by the tweet recognizing Golan annexation?
Loading commands from file
How could a planet have erratic days?
Did arcade monitors have same pixel aspect ratio as TV sets?
New brakes for 90s road bike
If a character has darkvision, can they see through an area of nonmagical darkness filled with lightly obscuring gas?
What if a revenant (monster) gains fire resistance?
Is aluminum electrical wire used on aircraft?
Pre-mixing cryogenic fuels and using only one fuel tank
Rising and falling intonation
Freedom of speech and where it applies
How do you make your own symbol when Detexify fails?
How does a computer interpret real numbers?
Creepy dinosaur pc game identification
Fantasy book from my childhood: female protagonist, Blood Ore or Blood Metal for taking attributes
ROR - Point of Sale (POS)
A Library Class : PointShared access pointLibrary Class: Point - Follow upIoC.Resolve deep in the project or Constructor Injection on entry point of appClass to create dynamic php + mysql queries (safely)Skipping Google search results that point to certain sitesQuiz MVC applicationC++ code to find distance between line and pointFormatting floating point number as mixed fractional in RubyPopup classes hierarchy design
$begingroup$
I've the following scenario: the user clicks the "Sales" button which redirects to the creation view of the Shopping Cart. Then, the user enters the EAN code and the Quantity, presses "Add" and the view sends an AJAX request to the add_item
method of the SalesController
. Then, the controller searches the data for the EAN given and a row is added to the table via JS. Also, an Item object is added to an Items array in the Session object. I decided to persist this data in the Session because I want to save all the objects (Sale
and SaleItems
) at the same time when the user presses "Save".
SalesController
class SalesController < ApplicationController
def new
@sale = Sale.new
@sale.saleItems.build
cart_item
end
def create
@sale = Sale.new()
if(@sale.grabar(session[:items]))
session[:items] = []
redirect_to new_sale_url
end
end
def add_item
@item = SaleItem.new
@item_atributos = Material.price(params[:item][:gtin]).first
@item.quantity = params[:item][:cantidad]
@item.material_id = @item_atributos.id
@item.unit_price = @item_atributos.unit_price
@item.total = @item.quantity*@item_atributos.unit_price
cart_item
session[:items] << @item
respond_to do |format|
format.js #{render :json => @precio}
end
end
private
def sales_params
params.require(:sale).permit(:total,saleItems_attributes:[:sale_id,:material_id,:unit_price,
:quantity,:total])
end
end
Sale model
class Sale < ApplicationRecord
has_many :saleItems
accepts_nested_attributes_for :saleItems
def total(array)
@total = 0
array.map { |item| item["total"]}.sum
end
def grabar (array)
ActiveRecord::Base.transaction do
@sale = Sale.create!(total: total(array))
array.each do |item|
item[:sale_id] = @sale.id
SaleItem.create!(item)
end
end
end
end
new.html.erb
<h3><%= yield(:title_text) %></h3>
<table id="tabla" class="table table-bordered">
<thead>
<tr>
<th scope="col">Producto</th>
<th scope="col">Cantidad</th>
<th scope="col">Precio unitario</th>
<th scope="col">Subtotal</th>
<th scope="col">Acciones</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<div style="position: fixed;left: 0;bottom: 0;width: 100%;text-align:center">
<%= form_tag(add_item_path(:gtin,:cantidad), method: 'get', remote: true) do %>
<div style="display-block:inline">
<%= label_tag(:gtin, 'Código') %>
<%= text_field_tag "item[gtin]" %>
<%= text_field_tag "item[cantidad]" %>
<%= submit_tag("Agregar", :class => "btn btn-primary") %>
<%= link_to "Vender", sales_path, class:"btn btn-success", :method => :post %>
<% end %>
</div>
</div>
add_item.js.erb
$("#tabla tbody").append("<%= j render partial: 'saleItem', locals: {item: @item, atributos: @item_atributos} %>");
$("#tabla").on('click', '#borrar', function () {
$(this).closest('tr').remove();
});
_saleItem.html.erb
<tr>
<th scope="row">
<%= @item_atributos.description %>
</th>
<td><%= @item.quantity %></td>
<td><%= @item_atributos.unit_price %></td>
<td><%= @item.quantity * @item_atributos.unit_price%></td>
<td>
<button type="button" id="borrar" class="btn btn-danger">Borrar</button>
</td>
</tr>
The code works well but my questions aims at code quality and design. I want a solution based on the OOP. Have I used the ROR tools well? Where should I improve my design? For example, I'm not using the strong parameters. The DB transaction was developed manually looping through Item objects. I feel that this solution doesn't use all the ROR features that makes coding easier!
object-oriented ruby design-patterns ruby-on-rails mvc
$endgroup$
add a comment |
$begingroup$
I've the following scenario: the user clicks the "Sales" button which redirects to the creation view of the Shopping Cart. Then, the user enters the EAN code and the Quantity, presses "Add" and the view sends an AJAX request to the add_item
method of the SalesController
. Then, the controller searches the data for the EAN given and a row is added to the table via JS. Also, an Item object is added to an Items array in the Session object. I decided to persist this data in the Session because I want to save all the objects (Sale
and SaleItems
) at the same time when the user presses "Save".
SalesController
class SalesController < ApplicationController
def new
@sale = Sale.new
@sale.saleItems.build
cart_item
end
def create
@sale = Sale.new()
if(@sale.grabar(session[:items]))
session[:items] = []
redirect_to new_sale_url
end
end
def add_item
@item = SaleItem.new
@item_atributos = Material.price(params[:item][:gtin]).first
@item.quantity = params[:item][:cantidad]
@item.material_id = @item_atributos.id
@item.unit_price = @item_atributos.unit_price
@item.total = @item.quantity*@item_atributos.unit_price
cart_item
session[:items] << @item
respond_to do |format|
format.js #{render :json => @precio}
end
end
private
def sales_params
params.require(:sale).permit(:total,saleItems_attributes:[:sale_id,:material_id,:unit_price,
:quantity,:total])
end
end
Sale model
class Sale < ApplicationRecord
has_many :saleItems
accepts_nested_attributes_for :saleItems
def total(array)
@total = 0
array.map { |item| item["total"]}.sum
end
def grabar (array)
ActiveRecord::Base.transaction do
@sale = Sale.create!(total: total(array))
array.each do |item|
item[:sale_id] = @sale.id
SaleItem.create!(item)
end
end
end
end
new.html.erb
<h3><%= yield(:title_text) %></h3>
<table id="tabla" class="table table-bordered">
<thead>
<tr>
<th scope="col">Producto</th>
<th scope="col">Cantidad</th>
<th scope="col">Precio unitario</th>
<th scope="col">Subtotal</th>
<th scope="col">Acciones</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<div style="position: fixed;left: 0;bottom: 0;width: 100%;text-align:center">
<%= form_tag(add_item_path(:gtin,:cantidad), method: 'get', remote: true) do %>
<div style="display-block:inline">
<%= label_tag(:gtin, 'Código') %>
<%= text_field_tag "item[gtin]" %>
<%= text_field_tag "item[cantidad]" %>
<%= submit_tag("Agregar", :class => "btn btn-primary") %>
<%= link_to "Vender", sales_path, class:"btn btn-success", :method => :post %>
<% end %>
</div>
</div>
add_item.js.erb
$("#tabla tbody").append("<%= j render partial: 'saleItem', locals: {item: @item, atributos: @item_atributos} %>");
$("#tabla").on('click', '#borrar', function () {
$(this).closest('tr').remove();
});
_saleItem.html.erb
<tr>
<th scope="row">
<%= @item_atributos.description %>
</th>
<td><%= @item.quantity %></td>
<td><%= @item_atributos.unit_price %></td>
<td><%= @item.quantity * @item_atributos.unit_price%></td>
<td>
<button type="button" id="borrar" class="btn btn-danger">Borrar</button>
</td>
</tr>
The code works well but my questions aims at code quality and design. I want a solution based on the OOP. Have I used the ROR tools well? Where should I improve my design? For example, I'm not using the strong parameters. The DB transaction was developed manually looping through Item objects. I feel that this solution doesn't use all the ROR features that makes coding easier!
object-oriented ruby design-patterns ruby-on-rails mvc
$endgroup$
add a comment |
$begingroup$
I've the following scenario: the user clicks the "Sales" button which redirects to the creation view of the Shopping Cart. Then, the user enters the EAN code and the Quantity, presses "Add" and the view sends an AJAX request to the add_item
method of the SalesController
. Then, the controller searches the data for the EAN given and a row is added to the table via JS. Also, an Item object is added to an Items array in the Session object. I decided to persist this data in the Session because I want to save all the objects (Sale
and SaleItems
) at the same time when the user presses "Save".
SalesController
class SalesController < ApplicationController
def new
@sale = Sale.new
@sale.saleItems.build
cart_item
end
def create
@sale = Sale.new()
if(@sale.grabar(session[:items]))
session[:items] = []
redirect_to new_sale_url
end
end
def add_item
@item = SaleItem.new
@item_atributos = Material.price(params[:item][:gtin]).first
@item.quantity = params[:item][:cantidad]
@item.material_id = @item_atributos.id
@item.unit_price = @item_atributos.unit_price
@item.total = @item.quantity*@item_atributos.unit_price
cart_item
session[:items] << @item
respond_to do |format|
format.js #{render :json => @precio}
end
end
private
def sales_params
params.require(:sale).permit(:total,saleItems_attributes:[:sale_id,:material_id,:unit_price,
:quantity,:total])
end
end
Sale model
class Sale < ApplicationRecord
has_many :saleItems
accepts_nested_attributes_for :saleItems
def total(array)
@total = 0
array.map { |item| item["total"]}.sum
end
def grabar (array)
ActiveRecord::Base.transaction do
@sale = Sale.create!(total: total(array))
array.each do |item|
item[:sale_id] = @sale.id
SaleItem.create!(item)
end
end
end
end
new.html.erb
<h3><%= yield(:title_text) %></h3>
<table id="tabla" class="table table-bordered">
<thead>
<tr>
<th scope="col">Producto</th>
<th scope="col">Cantidad</th>
<th scope="col">Precio unitario</th>
<th scope="col">Subtotal</th>
<th scope="col">Acciones</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<div style="position: fixed;left: 0;bottom: 0;width: 100%;text-align:center">
<%= form_tag(add_item_path(:gtin,:cantidad), method: 'get', remote: true) do %>
<div style="display-block:inline">
<%= label_tag(:gtin, 'Código') %>
<%= text_field_tag "item[gtin]" %>
<%= text_field_tag "item[cantidad]" %>
<%= submit_tag("Agregar", :class => "btn btn-primary") %>
<%= link_to "Vender", sales_path, class:"btn btn-success", :method => :post %>
<% end %>
</div>
</div>
add_item.js.erb
$("#tabla tbody").append("<%= j render partial: 'saleItem', locals: {item: @item, atributos: @item_atributos} %>");
$("#tabla").on('click', '#borrar', function () {
$(this).closest('tr').remove();
});
_saleItem.html.erb
<tr>
<th scope="row">
<%= @item_atributos.description %>
</th>
<td><%= @item.quantity %></td>
<td><%= @item_atributos.unit_price %></td>
<td><%= @item.quantity * @item_atributos.unit_price%></td>
<td>
<button type="button" id="borrar" class="btn btn-danger">Borrar</button>
</td>
</tr>
The code works well but my questions aims at code quality and design. I want a solution based on the OOP. Have I used the ROR tools well? Where should I improve my design? For example, I'm not using the strong parameters. The DB transaction was developed manually looping through Item objects. I feel that this solution doesn't use all the ROR features that makes coding easier!
object-oriented ruby design-patterns ruby-on-rails mvc
$endgroup$
I've the following scenario: the user clicks the "Sales" button which redirects to the creation view of the Shopping Cart. Then, the user enters the EAN code and the Quantity, presses "Add" and the view sends an AJAX request to the add_item
method of the SalesController
. Then, the controller searches the data for the EAN given and a row is added to the table via JS. Also, an Item object is added to an Items array in the Session object. I decided to persist this data in the Session because I want to save all the objects (Sale
and SaleItems
) at the same time when the user presses "Save".
SalesController
class SalesController < ApplicationController
def new
@sale = Sale.new
@sale.saleItems.build
cart_item
end
def create
@sale = Sale.new()
if(@sale.grabar(session[:items]))
session[:items] = []
redirect_to new_sale_url
end
end
def add_item
@item = SaleItem.new
@item_atributos = Material.price(params[:item][:gtin]).first
@item.quantity = params[:item][:cantidad]
@item.material_id = @item_atributos.id
@item.unit_price = @item_atributos.unit_price
@item.total = @item.quantity*@item_atributos.unit_price
cart_item
session[:items] << @item
respond_to do |format|
format.js #{render :json => @precio}
end
end
private
def sales_params
params.require(:sale).permit(:total,saleItems_attributes:[:sale_id,:material_id,:unit_price,
:quantity,:total])
end
end
Sale model
class Sale < ApplicationRecord
has_many :saleItems
accepts_nested_attributes_for :saleItems
def total(array)
@total = 0
array.map { |item| item["total"]}.sum
end
def grabar (array)
ActiveRecord::Base.transaction do
@sale = Sale.create!(total: total(array))
array.each do |item|
item[:sale_id] = @sale.id
SaleItem.create!(item)
end
end
end
end
new.html.erb
<h3><%= yield(:title_text) %></h3>
<table id="tabla" class="table table-bordered">
<thead>
<tr>
<th scope="col">Producto</th>
<th scope="col">Cantidad</th>
<th scope="col">Precio unitario</th>
<th scope="col">Subtotal</th>
<th scope="col">Acciones</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<div style="position: fixed;left: 0;bottom: 0;width: 100%;text-align:center">
<%= form_tag(add_item_path(:gtin,:cantidad), method: 'get', remote: true) do %>
<div style="display-block:inline">
<%= label_tag(:gtin, 'Código') %>
<%= text_field_tag "item[gtin]" %>
<%= text_field_tag "item[cantidad]" %>
<%= submit_tag("Agregar", :class => "btn btn-primary") %>
<%= link_to "Vender", sales_path, class:"btn btn-success", :method => :post %>
<% end %>
</div>
</div>
add_item.js.erb
$("#tabla tbody").append("<%= j render partial: 'saleItem', locals: {item: @item, atributos: @item_atributos} %>");
$("#tabla").on('click', '#borrar', function () {
$(this).closest('tr').remove();
});
_saleItem.html.erb
<tr>
<th scope="row">
<%= @item_atributos.description %>
</th>
<td><%= @item.quantity %></td>
<td><%= @item_atributos.unit_price %></td>
<td><%= @item.quantity * @item_atributos.unit_price%></td>
<td>
<button type="button" id="borrar" class="btn btn-danger">Borrar</button>
</td>
</tr>
The code works well but my questions aims at code quality and design. I want a solution based on the OOP. Have I used the ROR tools well? Where should I improve my design? For example, I'm not using the strong parameters. The DB transaction was developed manually looping through Item objects. I feel that this solution doesn't use all the ROR features that makes coding easier!
object-oriented ruby design-patterns ruby-on-rails mvc
object-oriented ruby design-patterns ruby-on-rails mvc
edited Mar 16 at 2:25
Jamal♦
30.4k11121227
30.4k11121227
asked Mar 15 at 20:00
evarevar
12
12
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215527%2fror-point-of-sale-pos%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215527%2fror-point-of-sale-pos%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown