Skip to content

Commit

Permalink
add product for cart via ajax
Browse files Browse the repository at this point in the history
  • Loading branch information
airled committed Apr 17, 2016
1 parent f417b73 commit fb675e5
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 60 deletions.
7 changes: 5 additions & 2 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,22 @@ ul {
border-bottom: 0px;
}
.add-favorite {
display: none;
display: table-cell;
background-color: #428bca;
border-color: #428bca;
width: 40px;
height: 40px;
text-align: center;
vertical-align: middle;
color: white;
border-radius: 3px;
font-size: 25px;
cursor: default;
opacity: 0;
transition: opacity 0.2s
}
.product-row:hover .add-favorite {
display: inline-block;
opacity: 1;
}
.alert {
position: absolute;
Expand Down
7 changes: 5 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ def profile
end

def add_product
current_user.cart.products << Product.find(params[:product_id])
redirect_to :back
product_id = params[:product_id].to_i
ids = current_user.cart.products.pluck(:id)
current_user.cart.products << Product.find(product_id) unless ids.include?(product_id)
render json: current_user.cart.products.count
end

end
7 changes: 7 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@ class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :cart

after_create :add_cart_for_user

def add_cart_for_user
self.create_cart
end

end
13 changes: 0 additions & 13 deletions app/views/categories/index.html.erb

This file was deleted.

9 changes: 9 additions & 0 deletions app/views/categories/index.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
h1 Категории продуктов
hr

- @categories.each_slice(4) do |slice|
.row
- slice.each do |category|
.col-lg-3
= link_to category.name_ru, category_path(category.name)
br
10 changes: 4 additions & 6 deletions app/views/devise/sessions/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@
</div>
</div>

<%= f.submit "Войти", class: "btn btn-primary" %>
<% if devise_mapping.rememberable? -%>
<div class="form-group">
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
</div>
<%= f.check_box :remember_me %>
<%= f.label :remember_me %>
<% end %>
<%= f.submit "Войти", class: "btn btn-primary" %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<li><%= link_to "Информация", "/info" if user_signed_in? && current_user.admin? %></li>

<% if user_signed_in? %>
<li><%= link_to current_user.email, users_profile_path %></li>
<li><%= link_to "#{current_user.email} (#{current_user.cart.products.count})", users_profile_path, id: 'account-link' %></li>
<li><%= link_to 'Выйти', destroy_user_session_path, :method => :delete %></li>
<% else %>
<li><%= link_to 'Войти', new_user_session_path %></li>
Expand Down
47 changes: 33 additions & 14 deletions app/views/shared/_products.html.slim
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
= paginate @products, theme: 'twitter-bootstrap-3', pagination_class: "pagination-sm"

- @products.each do |product|
div class="row product-row"

div class="col-lg-3"
.row.product-row
.col-lg-3
= image_tag product.small_image_url
.col-lg-8

div = link_to product.name, product_path(url_name: product.url_name)

- if product.description?
div = product.description

div class="col-lg-8"
= link_to product.name, product_path(url_name: product.url_name)
br
= product.description
br
- if product.min_price
| цена:
= product.prices
- if user_signed_in?
div class="col-lg-1"
div class="add-favorite" title="Добавить в интересуемое"
= link_to '+', users_add_product_path(product_id: product.id), method: :post
div
| цена:
= product.prices

- if user_signed_in?
.col-lg-1
.add-favorite data-id=product.id title="Добавить в интересуемое" +
br

= paginate @products, theme: 'twitter-bootstrap-3', pagination_class: "pagination-sm"

javascript:
$(document).ready(function() {
$('.add-favorite').click(function() {
var productId = this.dataset.id;
$.ajax({
url: "#{users_add_product_path}",
type: "POST",
data: {
product_id: productId
},
success: function(jsonResponse) {
var newUserProductsQuantity = JSON.parse(jsonResponse),
accountEmail = $('#account-link').html().split(' ')[0];
$('#account-link').html(accountEmail + ' (' + newUserProductsQuantity + ')');
}
});
});
});
19 changes: 10 additions & 9 deletions app/views/users/profile.html.slim
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
h1 Профиль
hr

- @products.each do |product|
div class="row product-row"
.row.product-row

div class="col-lg-3"
.col-lg-3
= image_tag product.small_image_url

div class="col-lg-9"
= link_to product.name, product_path(url_name: product.url_name)
br
= product.description
br
.col-lg-9
= link_to product.name, product.url, target: "_blank"
- if product.description?
div = product.description
- if product.min_price
| цена:
= product.prices
div
| цена:
= product.prices
7 changes: 0 additions & 7 deletions app/views/welcome/index.html.erb

This file was deleted.

12 changes: 12 additions & 0 deletions app/views/welcome/index.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
h1 Railiner
hr

div
' Продукты
= link_to 'catalog.onliner.by', 'https://catalog.onliner.by', target: '_blank'
| . Добро пожаловать!

- if @date
div
| База данных от
= @date
4 changes: 2 additions & 2 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class Application < Rails::Application
# config.time_zone = 'Central Time (US & Canada)'

# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
# config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
# config.i18n.default_locale = :de
config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
config.i18n.default_locale = :ru

# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true
Expand Down
8 changes: 4 additions & 4 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ en:
hello: "Hello world"
views:
pagination:
first: "&laquo; Первая"
last: "Последняя &raquo;"
previous: "&lsaquo; Предыдущая"
next: "Следующая &rsaquo;"
first: "&laquo; First"
last: "Last &raquo;"
previous: "&lsaquo; Previous"
next: "Next &rsaquo;"
67 changes: 67 additions & 0 deletions config/locales/ru.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
ru:
hello: "Привет!"
views:
pagination:
first: "&laquo; Первая"
last: "Последняя &raquo;"
previous: "&lsaquo; Предыдущая"
next: "Следующая &rsaquo;"
truncate: "&hellip;"
devise:
confirmations:
confirmed: "Ваш email успешно подтвержден."
send_instructions: "You will receive an email with instructions for how to confirm your email address in a few minutes."
send_paranoid_instructions: "If your email address exists in our database, you will receive an email with instructions for how to confirm your email address in a few minutes."
failure:
already_authenticated: "You are already signed in."
inactive: "Your account is not activated yet."
invalid: "Invalid %{authentication_keys} or password."
locked: "Your account is locked."
last_attempt: "You have one more attempt before your account is locked."
not_found_in_database: "Invalid %{authentication_keys} or password."
timeout: "Your session expired. Please sign in again to continue."
unauthenticated: "You need to sign in or sign up before continuing."
unconfirmed: "You have to confirm your email address before continuing."
mailer:
confirmation_instructions:
subject: "Confirmation instructions"
reset_password_instructions:
subject: "Reset password instructions"
unlock_instructions:
subject: "Unlock instructions"
omniauth_callbacks:
failure: "Could not authenticate you from %{kind} because \"%{reason}\"."
success: "Successfully authenticated from %{kind} account."
passwords:
no_token: "You can't access this page without coming from a password reset email. If you do come from a password reset email, please make sure you used the full URL provided."
send_instructions: "You will receive an email with instructions on how to reset your password in a few minutes."
send_paranoid_instructions: "If your email address exists in our database, you will receive a password recovery link at your email address in a few minutes."
updated: "Your password has been changed successfully. You are now signed in."
updated_not_active: "Your password has been changed successfully."
registrations:
destroyed: "Bye! Your account has been successfully cancelled. We hope to see you again soon."
signed_up: "Welcome! You have signed up successfully."
signed_up_but_inactive: "You have signed up successfully. However, we could not sign you in because your account is not yet activated."
signed_up_but_locked: "You have signed up successfully. However, we could not sign you in because your account is locked."
signed_up_but_unconfirmed: "A message with a confirmation link has been sent to your email address. Please follow the link to activate your account."
update_needs_confirmation: "You updated your account successfully, but we need to verify your new email address. Please check your email and follow the confirm link to confirm your new email address."
updated: "Your account has been updated successfully."
sessions:
signed_in: "Вы успешно зашли."
signed_out: "Вы успешно вышли."
already_signed_out: "Вы успешно вышли."
unlocks:
send_instructions: "You will receive an email with instructions for how to unlock your account in a few minutes."
send_paranoid_instructions: "If your account exists, you will receive an email with instructions for how to unlock it in a few minutes."
unlocked: "Your account has been unlocked successfully. Please sign in to continue."
errors:
messages:
already_confirmed: "was already confirmed, please try signing in"
confirmation_period_expired: "needs to be confirmed within %{period}, please request a new one"
expired: "has expired, please request a new one"
not_found: "not found"
not_locked: "was not locked"
not_saved:
one: "1 error prohibited this %{resource} from being saved:"
other: "%{count} errors prohibited this %{resource} from being saved:"

0 comments on commit fb675e5

Please sign in to comment.