Skip to content

Commit

Permalink
Merge pull request #552 from tanmay3011/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
jhawthorn committed Dec 1, 2015
2 parents ec065a3 + d9cee59 commit 3fff3ab
Show file tree
Hide file tree
Showing 70 changed files with 161 additions and 158 deletions.
8 changes: 4 additions & 4 deletions api/app/controllers/spree/api/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ProductsController < Spree::Api::BaseController
def index
if params[:ids]
ids = params[:ids].split(",").flatten
@products = product_scope.where(:id => ids)
@products = product_scope.where(id: ids)
else
@products = product_scope.ransack(params[:q]).result
end
Expand Down Expand Up @@ -59,14 +59,14 @@ def show
#
def create
authorize! :create, Product
params[:product][:available_on] ||= Time.now
params[:product][:available_on] ||= Time.current
set_up_shipping_category

options = { variants_attrs: variants_params, options_attrs: option_types_params }
@product = Core::Importer::Product.new(nil, product_params, options).create

if @product.persisted?
respond_with(@product, :status => 201, :default_template => :show)
respond_with(@product, status: 201, default_template: :show)
else
invalid_resource!(@product)
end
Expand All @@ -80,7 +80,7 @@ def update
@product = Core::Importer::Product.new(@product, product_params, options).update

if @product.errors.empty?
respond_with(@product.reload, :status => 200, :default_template => :show)
respond_with(@product.reload, status: 200, default_template: :show)
else
invalid_resource!(@product)
end
Expand Down
4 changes: 2 additions & 2 deletions api/spec/controllers/spree/api/checkouts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -380,15 +380,15 @@ module Spree
end

it "can transition from confirm to complete" do
order.update_columns(completed_at: Time.now, state: 'complete')
order.update_columns(completed_at: Time.current, state: 'complete')
allow_any_instance_of(Spree::Order).to receive_messages(payment_required?: false)
api_put :update, id: order.to_param, order_token: order.guest_token
expect(json_response['state']).to eq('complete')
expect(response.status).to eq(200)
end

it "returns the order if the order is already complete" do
order.update_columns(completed_at: Time.now, state: 'complete')
order.update_columns(completed_at: Time.current, state: 'complete')
api_put :update, id: order.to_param, order_token: order.guest_token
expect(json_response['number']).to eq(order.number)
expect(response.status).to eq(200)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ module Spree
end

it "should touch the taxon" do
taxon.update_attributes(updated_at: Time.now - 10.seconds)
taxon.update_attributes(updated_at: Time.current - 10.seconds)
taxon_last_updated_at = taxon.updated_at
api_put :update, taxon_id: taxon, product_id: last_product, position: 0
taxon.reload
Expand Down
8 changes: 4 additions & 4 deletions api/spec/controllers/spree/api/orders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ module Spree
end

it "returns orders in reverse chronological order by completed_at" do
order.update_columns completed_at: Time.now
order.update_columns completed_at: Time.current

order2 = Order.create user: order.user, completed_at: Time.now - 1.day, store: store
order2 = Order.create user: order.user, completed_at: Time.current - 1.day, store: store
expect(order2.created_at).to be > order.created_at
order3 = Order.create user: order.user, completed_at: nil, store: store
expect(order3.created_at).to be > order2.created_at
Expand Down Expand Up @@ -321,7 +321,7 @@ module Spree
end

it "cannot cancel an order that doesn't belong to them" do
order.update_attribute(:completed_at, Time.now)
order.update_attribute(:completed_at, Time.current)
order.update_attribute(:shipment_state, "ready")
api_put :cancel, :id => order.to_param
assert_unauthorized!
Expand Down Expand Up @@ -754,7 +754,7 @@ module Spree
before do
Spree::Config[:mails_from] = "[email protected]"

order.completed_at = Time.now
order.completed_at = Time.current
order.state = 'complete'
order.shipment_state = 'ready'
order.save!
Expand Down
2 changes: 1 addition & 1 deletion api/spec/controllers/spree/api/products_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Spree
render_views

let!(:product) { create(:product) }
let!(:inactive_product) { create(:product, available_on: Time.now.tomorrow, name: "inactive") }
let!(:inactive_product) { create(:product, available_on: Time.current.tomorrow, name: "inactive") }
let(:base_attributes) { Api::ApiHelpers.product_attributes }
let(:show_attributes) { base_attributes.dup.push(:has_variants) }
let(:new_attributes) { base_attributes }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
subject
shipment.reload
expect(shipment.state).to eq 'shipped'
expect(shipment.shipped_at.to_i).to eq Time.now.to_i
expect(shipment.shipped_at.to_i).to eq Time.current.to_i
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module Spree

before do
stock_transfer.finalize(user)
stock_transfer.ship(shipped_at: Time.now)
stock_transfer.ship(shipped_at: Time.current)
stock_transfer.source_location.stock_item(transfer_item.variant_id).set_count_on_hand(0)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ module Spree

context "has been finalized" do
before do
stock_transfer.update_attributes(finalized_at: Time.now)
stock_transfer.update_attributes(finalized_at: Time.current)
end

it "returns an error status code" do
Expand Down
4 changes: 2 additions & 2 deletions api/spec/controllers/spree/api/variants_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module Spree
# Regression test for #2141
context "a deleted variant" do
before do
variant.update_column(:deleted_at, Time.now)
variant.update_column(:deleted_at, Time.current)
end

it "is not returned in the results" do
Expand Down Expand Up @@ -226,7 +226,7 @@ module Spree
# Test for #2141
context "deleted variants" do
before do
variant.update_column(:deleted_at, Time.now)
variant.update_column(:deleted_at, Time.current)
end

it "are visible by admin" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def update
if @payment_method['type'].to_s != payment_method_type
@payment_method.update_columns(
type: payment_method_type,
updated_at: Time.now,
updated_at: Time.current,
)
@payment_method = PaymentMethod.find(params[:id])
end
Expand Down
4 changes: 2 additions & 2 deletions backend/app/controllers/spree/admin/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ def sales_total
params[:q] = {} unless params[:q]

if params[:q][:completed_at_gt].blank?
params[:q][:completed_at_gt] = Time.zone.now.beginning_of_month
params[:q][:completed_at_gt] = Time.current.beginning_of_month
else
params[:q][:completed_at_gt] = Time.zone.parse(params[:q][:completed_at_gt]).beginning_of_day rescue Time.zone.now.beginning_of_month
params[:q][:completed_at_gt] = Time.zone.parse(params[:q][:completed_at_gt]).beginning_of_day rescue Time.current.beginning_of_month
end

if params[:q] && !params[:q][:completed_at_lt].blank?
Expand Down
16 changes: 9 additions & 7 deletions backend/app/controllers/spree/admin/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,28 @@ class SearchController < Spree::Admin::BaseController
# And then JSON building with something like Active Model Serializers
def users
if params[:ids]
@users = Spree.user_class.where(:id => params[:ids].split(','))
# split here may be String#split or Array#split, so we must flatten the results
@users = Spree.user_class.where(id: params[:ids].split(',').flatten)
else
@users = Spree.user_class.ransack({
:m => 'or',
:email_start => params[:q],
:addresses_firstname_start => params[:q],
:addresses_lastname_start => params[:q]
m: 'or',
email_start: params[:q],
addresses_firstname_start: params[:q],
addresses_lastname_start: params[:q]
}).result.limit(10)
end
end

def products
if params[:ids]
@products = Product.where(:id => params[:ids].split(","))
# split here may be String#split or Array#split, so we must flatten the results
@products = Product.where(id: params[:ids].split(",").flatten)
else
@products = Product.ransack(params[:q]).result
end

@products = @products.distinct.page(params[:page]).per(params[:per_page])
expires_in 15.minutes, :public => true
expires_in 15.minutes, public: true
headers['Surrogate-Control'] = "max-age=#{15.minutes}"
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def close

def ship
if @stock_transfer.transfer
@stock_transfer.ship(shipped_at: DateTime.now)
@stock_transfer.ship(shipped_at: DateTime.current)
flash[:success] = Spree.t(:stock_transfer_complete)
redirect_to admin_stock_transfers_path
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module Admin
:source_attributes => {
:name => "Test User",
:number => "4111 1111 1111 1111",
:expiry => "09 / #{Time.now.year + 1}",
:expiry => "09 / #{Time.current.year + 1}",
:verification_value => "123"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@

before do
# can't set completed_at during factory creation
order_complete_start_of_month.completed_at = Time.zone.now.beginning_of_month + 1.minute
order_complete_start_of_month.completed_at = Time.current.beginning_of_month + 1.minute
order_complete_start_of_month.save!

order_complete_mid_month.completed_at = Time.zone.now.beginning_of_month + 15.days
order_complete_mid_month.completed_at = Time.current.beginning_of_month + 15.days
order_complete_mid_month.save!
end

Expand Down Expand Up @@ -84,7 +84,7 @@
end

context 'when params has a completed_at_gt' do
let(:params) { { q: { completed_at_gt: Time.zone.now.beginning_of_month + 1.day } } }
let(:params) { { q: { completed_at_gt: Time.current.beginning_of_month + 1.day } } }

it_behaves_like 'sales total report' do
let(:expected_returned_orders) { [order_complete_mid_month] }
Expand All @@ -101,7 +101,7 @@
end

context 'when params has a compeleted_at_lt' do
let(:params) { { q: { completed_at_lt: Time.zone.now.beginning_of_month } } }
let(:params) { { q: { completed_at_lt: Time.current.beginning_of_month } } }

it_behaves_like 'sales total report' do
let(:expected_returned_orders) { [order_complete_start_of_month] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ module Spree
StockTransfer.create do |transfer|
transfer.source_location_id = warehouse.id
transfer.destination_location_id = la_store.id
transfer.finalized_at = DateTime.now
transfer.closed_at = DateTime.now
transfer.finalized_at = DateTime.current
transfer.closed_at = DateTime.current
end }

describe "stock location filtering" do
Expand Down Expand Up @@ -163,7 +163,7 @@ module Spree

context 'stock transfer is not finalizable' do
before do
transfer_with_items.update_attributes(finalized_at: Time.now)
transfer_with_items.update_attributes(finalized_at: Time.current)
end

it 'redirects back to edit' do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

# Regression test for #535
it "can see a tax rate in the list if the tax category has been deleted" do
tax_rate.tax_category.update_column(:deleted_at, Time.now)
tax_rate.tax_category.update_column(:deleted_at, Time.current)
click_link "Tax Rates"

expect(find("table tbody td:nth-child(3)")).to have_content('N/A')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
let(:order) do
order = create(:order)
order.update_columns({
:state => 'complete',
:completed_at => Time.now
state: 'complete',
completed_at: Time.current
})
order
end
Expand Down
2 changes: 1 addition & 1 deletion backend/spec/features/admin/orders/payments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def refresh_page
it "is able to create a new credit card payment with valid information", :js => true do
fill_in "Card Number", :with => "4111 1111 1111 1111"
fill_in "Name *", :with => "Test User"
fill_in "Expiration", :with => "09 / #{Time.now.year + 1}"
fill_in "Expiration", :with => "09 / #{Time.current.year + 1}"
fill_in "Card Code", :with => "007"
# Regression test for #4277
expect(page).to have_css('.ccType[value="visa"]', visible: false)
Expand Down
4 changes: 2 additions & 2 deletions backend/spec/features/admin/reports_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
before do
order = create(:order)
order.update_columns({:adjustment_total => 100})
order.completed_at = Time.now
order.completed_at = Time.current
order.save!

order = create(:order)
order.update_columns({:adjustment_total => 200})
order.completed_at = Time.now
order.completed_at = Time.current
order.save!

#incomplete order
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/concerns/spree/adjustment_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def deals_with_adjustments_for_deleted_source
# This would mean that the order's total is not altered at all.
attrs = {
source_id: nil,
updated_at: Time.now
updated_at: Time.current
}
adjustment_scope.where.not(spree_orders: { completed_at: nil }).update_all(attrs)
end
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/adjustment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def update!(target = nil)
# Persist only if changed
# This is only not a save! to avoid the extra queries to load the order
# (for validations) and to touch the adjustment.
update_columns(eligible: eligible, amount: amount, updated_at: Time.now) if changed?
update_columns(eligible: eligible, amount: amount, updated_at: Time.current) if changed?
end
amount
end
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/credit_card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class CreditCard < Spree::Base

accepts_nested_attributes_for :address

attr_reader :number
attr_accessor :encrypted_data,
:number,
:imported,
:verification_value

Expand Down Expand Up @@ -181,7 +181,7 @@ def require_card_numbers?
def ensure_one_default
if self.user_id && self.default
CreditCard.where(default: true).where.not(id: self.id).where(user_id: self.user_id).each do |ucc|
ucc.update_columns(default: false, updated_at: Time.now)
ucc.update_columns(default: false, updated_at: Time.current)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/inventory_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def self.finalize_units!(inventory_units)
inventory_units.map do |iu|
iu.update_columns(
pending: false,
updated_at: Time.now,
updated_at: Time.current,
)
end
end
Expand Down
10 changes: 5 additions & 5 deletions core/app/models/spree/item_adjustments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ def update
@item.adjustment_total = @item.promo_total + @item.additional_tax_total + item_cancellation_total

@item.update_columns(
:promo_total => @item.promo_total,
:included_tax_total => @item.included_tax_total,
:additional_tax_total => @item.additional_tax_total,
:adjustment_total => @item.adjustment_total,
:updated_at => Time.now,
promo_total: @item.promo_total,
included_tax_total: @item.included_tax_total,
additional_tax_total: @item.additional_tax_total,
adjustment_total: @item.adjustment_total,
updated_at: Time.current,
) if @item.changed?

@item
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ def restart_checkout_flow

self.update_columns(
state: 'cart',
updated_at: Time.now,
updated_at: Time.current,
)
self.next! if self.line_items.size > 0
end
Expand Down Expand Up @@ -556,7 +556,7 @@ def canceled_by(user)
cancel!
self.update_columns(
canceler_id: user.id,
canceled_at: Time.now,
canceled_at: Time.current,
)
end
end
Expand Down
Loading

0 comments on commit 3fff3ab

Please sign in to comment.