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

Order search #80

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ GEM
erubi (1.7.0)
execjs (2.7.0)
ffi (1.9.18)
ffi (1.9.18-x86-mingw32)
globalid (0.4.1)
activesupport (>= 4.2.0)
i18n (0.9.1)
Expand All @@ -89,6 +90,8 @@ GEM
nio4r (2.1.0)
nokogiri (1.8.1)
mini_portile2 (~> 2.3.0)
nokogiri (1.8.1-x86-mingw32)
mini_portile2 (~> 2.3.0)
public_suffix (3.0.1)
puma (3.11.0)
rack (2.0.3)
Expand Down Expand Up @@ -150,6 +153,7 @@ GEM
activesupport (>= 4.0)
sprockets (>= 3.0.0)
sqlite3 (1.3.13)
sqlite3 (1.3.13-x86-mingw32)
thor (0.20.0)
thread_safe (0.3.6)
tilt (2.0.8)
Expand All @@ -158,8 +162,11 @@ GEM
turbolinks-source (5.0.3)
tzinfo (1.2.4)
thread_safe (~> 0.1)
tzinfo-data (1.2017.3)
tzinfo (>= 1.0.0)
uglifier (3.2.0)
execjs (>= 0.3.0, < 3)
wdm (0.1.1)
web-console (3.5.1)
actionview (>= 5.0)
activemodel (>= 5.0)
Expand All @@ -173,6 +180,7 @@ GEM

PLATFORMS
ruby
x86-mingw32

DEPENDENCIES
byebug
Expand All @@ -190,6 +198,7 @@ DEPENDENCIES
turbolinks (~> 5)
tzinfo-data
uglifier (>= 1.3.0)
wdm (>= 0.1.0)
web-console (>= 3.3.0)

BUNDLED WITH
Expand Down
21 changes: 16 additions & 5 deletions app/controllers/orders_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
class OrdersController < ApplicationController
before_action :set_order, only: [:show, :edit, :update, :destroy]
before_action :set_locale, :set_order, only: [:show, :edit, :update, :destroy]

# GET /orders
# GET /orders.json
def index
@orders = Order.all
@order_customer_pairs = @orders.map { |o| [o, o.customer] }
end

@orders = Order.search(params[:term])
# @order_customer_pairs = @orders.map { |o| [o, o.customer] }

end
# GET /orders/1
# GET /orders/1.json
def show
Expand All @@ -20,8 +21,18 @@ def show
def new
@order = Order.new
end
#sets locale
def set_locale
I18n.locale = extract_locale_from_subdomain || I18n.locale = params[:locale] || I18n.default_locale
end

#extracts locale from possible subdomains
def extract_locale_from_subdomain
parsed_locale = request.subdomains.first
I18n.available_locales.map(&:to_s).include?(parsed_locale) ? parsed_locale : nil
end

# GET /orders/1/edit
# GET /orders/1/edits
def edit
end

Expand Down
9 changes: 9 additions & 0 deletions app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ class Order < ApplicationRecord
has_many :pumpkins, through: :line_items
# has_many(:pumpkins, {through: :line_items})

def self.search(term)
if term
where('customer_id LIKE ?', term)
else
all
end
end


def total
line_items.collect(&:price).inject(&:+).to_f
end
Expand Down
3 changes: 3 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

<%= link_to '[EN]', locale: 'en' %>
<%= link_to '[DE]', locale: 'de' %>

<p> &#127968;<%= link_to 'Home ', root_path %></p>

<%= yield %>
</body>
</html>
28 changes: 19 additions & 9 deletions app/views/orders/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

<h1>Orders</h1>

<%= form_tag orders_path, :method => 'get' do %>
<%= text_field_tag :term, params[:term] %>
<%= submit_tag 'Search' %>
<% end %>

<table>
<thead>
<tr>
Expand All @@ -11,17 +16,22 @@
</thead>

<tbody>
<% @order_customer_pairs.each do | order, customer | %>
<tr>
<td><%= customer.name %></td>
<td><%= link_to 'Show', order %></td>
<td><%= link_to 'Edit', edit_order_path(order) %></td>
<td><%= link_to 'Destroy', order, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
<% @orders.each do | order | %>
<tr>
<td><%= order %></td>

<td><%= link_to 'Show', order %></td>
<td><%= link_to 'Edit', edit_order_path(order) %></td>
<td><%= link_to 'Destroy', order, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>

</tbody>


</table>

<br>

<br>
<%= link_to 'New Order', new_order_path %>