Skip to content

Instantly share code, notes, and snippets.

@elrok123
Last active December 20, 2015 21:19
Show Gist options
  • Save elrok123/6197023 to your computer and use it in GitHub Desktop.
Save elrok123/6197023 to your computer and use it in GitHub Desktop.
class ApplicationController < ActionController::Base
#This is the base application controller in which the current user is held and various other methods
protect_from_forgery
helper_method :current_user, :vat
def vat(income)
vat = (income / 100) * 20
return vat
end
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
module ApplicationHelper
def link_to_add_fields(name, f, association)
new_object = f.object.send(association).klass.new
id = new_object.object_id
fields = f.fields_for(association, new_object, child_index: id) do |builder|
render(association.to_s.singularize + "_fields", f: builder)
end
link_to(name, '#', onClick: "return false", class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
end
end
function remove_fields(link) {
$(link).previous("input[type=hidden]").value = "1";
$(link).up(".fields").hide();
}
function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")
$(link).mouseup().insert({
before: content.replace(regexp, new_id)
});
}
<% if current_user %>
<div id="dashboard">
<div id="logo"></div>
<table id="go_back_link_container">
<tr>
<td>
<div class="go_back_link">
<%= link_to "<- Go Back", "/orders/view" %>
</div>
</td>
<td>
<div id="user_display">
Logged in as <%= current_user.email %>.
<%= link_to "Log out", log_out_path %>
</div>
</td>
</tr>
</table>
<%= form_for @order, method: :post do |f| %>
<% if @order.errors.any? %>
<div class="error_messages">
<% for message in @order.errors.full_messages %>
* <%= message %> <br>
<% end %>
</div>
<% end %>
<p>
<%= f.label 'Select The Client' %><br />
<% if current_user.account_type != 2 %>
<%= select :order, :client_id, Client.where("affiliated_by = #{current_user.id}").collect { |c| [ (c.firstname + " " + c.surname), c.id ] } %>
<% else %>
<%= select :order, :client_id, Client.all().collect { |c| [ (c.firstname + " " + c.surname), c.id ] } %>
<% end %>
</p>
<%= f.fields_for :orderedproducts do |pf| %>
<% end %>
<%= link_to_add_fields "Add Product", f, :orderedproducts %>
<p>
<%= f.label 'Date of Delivery' %><br />
<%= f.date_select :delivery_date, :order => [:day, :month, :year]%>
</p>
<%= f.hidden_field :user_id, value: current_user.id %>
<p class="button"><%= f.submit data: { confirm: "Are you sure?" } %></p>
<% end %>
<% flash.each do |name, msg| %>
<%= content_tag :div, "* " + msg, :id => "flash_#{name}" %><br />
<% end %>
<div id="copyright-notice"><div id="copyright_border">Copyright © Conner McCabe, all rights reserved.</div></div>
</div>
<% else %>
<script type="text/javascript">
window.location="<%= root_url %>"
</script>
<% end %>
class Order < ActiveRecord::Base
has_many :orderedproducts
has_many :products, through: :orderedproducts
has_one :client
attr_accessible :client_id, :order_total, :approved, :delivery_date, :orderedproducts, :orderedproducts_attributes, :user_id
accepts_nested_attributes_for :orderedproducts, :allow_destroy => true
validates_presence_of :orderedproducts
before_save :validate_quantity
before_save :generate_total
def generate_total
self.order_total = self.orderedproducts.map(&:total_price).sum
end
def validate_quantity
orderedproducts.each do |orderedproduct|
orderedproduct.quantity_ordered = 1 if orderedproduct.quantity_ordered < 1
end
end
end
<fieldset style="border: none;">
<%= select_tag(:orderedproducts, options_from_collection_for_select(@products, "id", "product_name")) %>
<% text_field_tag(:quantity_ordered, :value => "Qty", :onfocus => "clearValue(this)", :onblur => "restoreValue(this)", :style => "width: 60px;") %>
<%= hidden_field_tag(:_destroy) %>
<%= link_to "remove", '#', class: "remove_fields" %>
</fieldset>
class Orderedproduct < ActiveRecord::Base
attr_accessible :order_id, :product_id, :quantity_ordered, :total_price
belongs_to :order
belongs_to :product
before_save :total_price
def product_price
product.product_price
end
def total_price
product_price * quantity_ordered
end
end
class OrdersController < ApplicationController
def view
unless current_user.account_type == 2
@orders = Order.where("user_id = #{current_user.id}")
end
@orders ||= Order.all
end
def new
@order = Order.new
@orderedproducts = Orderedproduct.new
@suppliers = Supplier.all
@products = Product.all
end
def create
@order = Order.new(params[:order])
if @order.save
@order.orderedproducts.each do |orderedproduct|
@product = Product.find(orderedproduct.product_id)
@product.update_attributes(:product_quantity => (@product.product_quantity - orderedproduct.quantity_ordered)) unless orderedproduct.quantity_ordered > @product.product_quantity
end
redirect_to '/orders/view', :notice => "Order Created!"
else
render "new"
end
end
def show
@order = Order.find(params[:id])
@client = Client.find(@order.client_id)
@orderedproducts = Orderedproduct.where(:order_id => @order.id)
@salesperson = User.find(@order.user_id)
end
def update
@order = Order.find(params[:id])
session[:return_to] ||= request.referer
@order.update_attributes(params[:order])
if @order.save
redirect_to session[:return_to], :notice => "Order Updated!"
else
render "/users/view"
end
end
def destroy
@order = Order.find(params[:id])
session[:return_to] ||= request.referer
if @order.destroy
redirect_to session[:return_to], :notice => "Order Deleted!"
else
redirect_to session[:return_to], :flash => "Order Could Not Be Deleted!"
end
end
end
class Product < ActiveRecord::Base
#This line makes these elements accessible outside of the class.
attr_accessible :product_name, :product_price, :product_quantity, :supplier_id
has_many :orderedproducts
#has_many :orders, through: :orderedproducts
belongs_to :supplier
#These attributes ensure that the data entered for each element is valid and present.
validates_presence_of :product_name
validates_presence_of :product_price
validates_numericality_of :product_price
validates_presence_of :product_quantity
validates_numericality_of :product_quantity
validates_presence_of :supplier_id
end
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
$(document).on 'click', 'form .remove_fields', (event) ->
$(this).prev('input[type=hidden]').val('1')
$(this).closest('fieldset').hide()
event.preventDefault()
$(document).on 'click', 'form .add_fields', (event) ->
time = new Date().getTime()
regexp = new RegExp($(this).data('id'), 'g')
$(this).before($(this).data('fields').replace(regexp, time))
event.preventDefault()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment