Skip to content

Commit

Permalink
rubocop style guides
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo Maia committed Oct 3, 2015
1 parent f7bb87f commit 4ef4ced
Show file tree
Hide file tree
Showing 19 changed files with 105 additions and 88 deletions.
2 changes: 2 additions & 0 deletions .rspec
@@ -0,0 +1,2 @@
--color
--require spec_helper
11 changes: 11 additions & 0 deletions .rubocop.yml
@@ -0,0 +1,11 @@
AllCops:
RunRailsCops: true
DisplayCopNames: true
DisplayStyleGuide: true
StyleGuideCopsOnly: true

Style/StringLiterals:
EnforcedStyle: double_quotes

Metrics/LineLength:
Max: 110
8 changes: 4 additions & 4 deletions Rakefile
@@ -1,13 +1,13 @@
require "bundler/gem_tasks"

require 'cucumber/rake/task'
require "cucumber/rake/task"
Cucumber::Rake::Task.new do |t|
t.cucumber_opts = %w{--format progress}
t.cucumber_opts = "--format progress"
end

require 'rspec/core/rake_task'
require "rspec/core/rake_task"
RSpec::Core::RakeTask.new do |t|
t.fail_on_error = true
end

task :default => [:spec, :cucumber]
task default: [:spec, :cucumber]
14 changes: 8 additions & 6 deletions acts_as_shopping_cart.gemspec
@@ -1,5 +1,6 @@
# -*- encoding: utf-8 -*-
$:.push File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)

require "acts_as_shopping_cart/version"

Gem::Specification.new do |s|
Expand All @@ -8,8 +9,8 @@ Gem::Specification.new do |s|
s.authors = ["David Padilla"]
s.email = ["david@crowdint.com"]
s.homepage = ""
s.summary = %q{Simple Shopping Cart implementation}
s.description = %q{Simple Shopping Cart implementation}
s.summary = "Simple Shopping Cart implementation"
s.description = "Simple Shopping Cart implementation"

s.rubyforge_project = "acts_as_shopping_cart"

Expand All @@ -18,15 +19,16 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_dependency 'rails', '~> 4'
s.add_dependency 'money-rails', '~> 1.3'
s.add_dependency "rails", "~> 4"
s.add_dependency "money-rails", "~> 1.3"

s.add_development_dependency "cucumber", '~> 1.3.14'
s.add_development_dependency "cucumber", "~> 1.3.14"
s.add_development_dependency "database_cleaner"
s.add_development_dependency "rake", "~> 10.0.0"
s.add_development_dependency "rspec", "~> 2.12.0"
s.add_development_dependency "sqlite3", "~> 1.3.0"
s.add_development_dependency "simplecov"
s.add_development_dependency "rubocop"
s.add_development_dependency "codeclimate-test-reporter"

end
2 changes: 1 addition & 1 deletion features/step_definitions/product_steps.rb
@@ -1,3 +1,3 @@
Given /^a product "([^"]*)" exists$/ do |name|
Product.create(:name => name)
Product.create(name: name)
end
5 changes: 2 additions & 3 deletions features/step_definitions/shopping_cart_steps.rb
Expand Up @@ -27,7 +27,7 @@
@cart.total_unique_items.should eq(total.to_i)
end

When /^I remove (\d+) "([^"]*)" unit(s?) from cart$/ do |quantity, product_name, plural|
When /^I remove (\d+) "([^"]*)" unit(s?) from cart$/ do |quantity, product_name, _|
@cart.reload
product = Product.find_by_name(product_name)
@cart.remove(product, quantity.to_i)
Expand Down Expand Up @@ -77,9 +77,8 @@
@cart.update_price_for(product, price.to_f)
end

Then /^shopping cart item "([^"]*)" should belong to cart$/ do |arg1|
Then /^shopping cart item "([^"]*)" should belong to cart$/ do |_|
@cart.reload
shopping_cart_item = ShoppingCartItem.last
shopping_cart_item.owner.should == @cart
end

16 changes: 8 additions & 8 deletions features/support/env.rb
@@ -1,21 +1,21 @@
require 'bundler/setup'
require "bundler/setup"

require "codeclimate-test-reporter"
CodeClimate::TestReporter.start

require 'active_record'
require 'database_cleaner'
require 'money-rails'
require "active_record"
require "database_cleaner"
require "money-rails"

$: << './lib'
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)

require 'acts_as_shopping_cart'
require "acts_as_shopping_cart"

ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"

MoneyRails::Hooks.init

require 'simplecov'
require "simplecov"

SimpleCov.coverage_dir 'coverage.features'
SimpleCov.coverage_dir "coverage.features"
SimpleCov.start
6 changes: 3 additions & 3 deletions features/support/rails_env.rb
@@ -1,7 +1,7 @@
ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
#ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
# ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define(:version => 1) do
ActiveRecord::Schema.define(version: 1) do
create_table :shopping_carts
create_table :shopping_cart_items do |t|
t.shopping_cart_item_fields
Expand Down
7 changes: 3 additions & 4 deletions lib/active_record/acts/shopping_cart.rb
Expand Up @@ -17,10 +17,9 @@ module ClassMethods
#
#
def acts_as_shopping_cart_using(item_class)
self.send :include, ActiveRecord::Acts::ShoppingCart::Collection
self.send :include, ActiveRecord::Acts::ShoppingCart::Item
has_many :shopping_cart_items, :class_name => item_class.to_s.classify,
:as => :owner, :dependent => :destroy
send :include, ActiveRecord::Acts::ShoppingCart::Collection
send :include, ActiveRecord::Acts::ShoppingCart::Item
has_many :shopping_cart_items, class_name: item_class.to_s.classify, as: :owner, dependent: :destroy
end

#
Expand Down
30 changes: 17 additions & 13 deletions lib/active_record/acts/shopping_cart/collection.rb
Expand Up @@ -36,13 +36,15 @@ def empty?
# Remove an item from the cart
#
def remove(object, quantity = 1)
if cart_item = item_for(object)
if cart_item.quantity <= quantity
cart_item.delete
else
cart_item.quantity = (cart_item.quantity - quantity)
cart_item.save
end
cart_item = item_for(object)

return unless cart_item

if cart_item.quantity <= quantity
cart_item.delete
else
cart_item.quantity = (cart_item.quantity - quantity)
cart_item.save
end
end

Expand All @@ -51,15 +53,15 @@ def remove(object, quantity = 1)
# items in the cart
#
def subtotal
shopping_cart_items.inject(Money.new(0)) { |sum, item| sum += (item.price * item.quantity) }
shopping_cart_items.inject(Money.new(0)) { |a, e| a + (e.price * e.quantity) }
end

def shipping_cost
Money.new(0)
end

def taxes
subtotal * self.tax_pct * 0.01
subtotal * tax_pct * 0.01
end

def tax_pct
Expand All @@ -70,19 +72,21 @@ def tax_pct
# Returns the total by summing the subtotal, taxes and shipping_cost
#
def total
self.subtotal + self.taxes + self.shipping_cost
subtotal + taxes + shipping_cost
end

#
# Return the number of unique items in the cart
#
def total_unique_items
shopping_cart_items.inject(0) { |sum, item| sum += item.quantity }
shopping_cart_items.map(&:quantity).sum
end

def cart_items
warn "ShoppingCart#cart_items WILL BE DEPRECATED IN LATER VERSIONS OF acts_as_shopping_cart, please use ShoppingCart#shopping_cart_items instead"
self.shopping_cart_items
warn "ShoppingCart#cart_items WILL BE DEPRECATED IN LATER VERSIONS OF acts_as_shopping_cart," \
" please use ShoppingCart#shopping_cart_items instead"

shopping_cart_items
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_record/acts/shopping_cart/item.rb
Expand Up @@ -7,7 +7,7 @@ module Item
# Returns the cart item for the specified object
#
def item_for(object)
shopping_cart_items.where(:item => object).first
shopping_cart_items.where(item: object).first
end

#
Expand Down
8 changes: 4 additions & 4 deletions lib/active_record/acts/shopping_cart_item.rb
Expand Up @@ -16,10 +16,10 @@ module ClassMethods
# acts_as_shopping_cart_item :cart
#
#
def acts_as_shopping_cart_item_for(cart_class)
self.send :include, ActiveRecord::Acts::ShoppingCartItem::InstanceMethods
belongs_to :owner, :polymorphic => true
belongs_to :item, :polymorphic => true
def acts_as_shopping_cart_item_for(*)
send :include, ActiveRecord::Acts::ShoppingCartItem::InstanceMethods
belongs_to :owner, polymorphic: true
belongs_to :item, polymorphic: true
monetize :price_cents
end

Expand Down
10 changes: 5 additions & 5 deletions lib/active_record/acts/shopping_cart_item/instance_methods.rb
Expand Up @@ -6,23 +6,23 @@ module InstanceMethods
# Returns the subtotal, multiplying the quantity times the price of the item.
#
def subtotal
("%.2f" % (self.quantity * self.price)).to_f
format("%.2f", quantity * price).to_f
end

#
# Updates the quantity of the item
#
def update_quantity(new_quantity)
self.quantity = new_quantity
self.save
self.quantity = new_quantity
save
end

#
# Updates the price of the item
#
def update_price(new_price)
self.price = new_price
self.save
self.price = new_price
save
end
end
end
Expand Down
18 changes: 9 additions & 9 deletions lib/acts_as_shopping_cart.rb
@@ -1,19 +1,19 @@
require 'acts_as_shopping_cart/version'
require 'rails'
require 'money-rails'
require 'active_record/acts/shopping_cart'
require 'active_record/acts/shopping_cart_item'
require 'acts_as_shopping_cart/schema'
require "acts_as_shopping_cart/version"
require "rails"
require "money-rails"
require "active_record/acts/shopping_cart"
require "active_record/acts/shopping_cart_item"
require "acts_as_shopping_cart/schema"

module ActiveRecord
module Acts
module ShoppingCart
autoload :Collection , 'active_record/acts/shopping_cart/collection'
autoload :Item , 'active_record/acts/shopping_cart/item'
autoload :Collection , "active_record/acts/shopping_cart/collection"
autoload :Item , "active_record/acts/shopping_cart/item"
end

module ShoppingCartItem
autoload :InstanceMethods, 'active_record/acts/shopping_cart_item/instance_methods'
autoload :InstanceMethods, "active_record/acts/shopping_cart_item/instance_methods"
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/acts_as_shopping_cart/schema.rb
@@ -1,4 +1,4 @@
require 'active_record/connection_adapters/abstract/schema_definitions'
require "active_record/connection_adapters/abstract/schema_definitions"

module ActsAsShoppingCart
module Schema
Expand Down
20 changes: 10 additions & 10 deletions spec/active_record/acts/shopping_cart/collection_spec.rb
@@ -1,5 +1,5 @@
require File.expand_path(File.dirname(__FILE__) + '../../../../spec_helper')
# require 'spec_helper'
require File.expand_path(File.dirname(__FILE__) + "../../../../spec_helper")
# require "spec_helper"

describe ActiveRecord::Acts::ShoppingCart::Collection do
let(:klass) do
Expand All @@ -16,7 +16,7 @@
let(:object) { stub }

let(:shopping_cart_item) do
stub(:quantity => 2, :save => true)
stub(quantity: 2, save: true)
end

describe :add do
Expand All @@ -27,9 +27,9 @@

it "creates a new shopping cart item" do
created_object = mock
subject.shopping_cart_items.should_receive(:create).
with(:item => object, :price => 19.99, :quantity => 3).
and_return(created_object)
subject.shopping_cart_items.should_receive(:create)
.with(item: object, price: 19.99, quantity: 3)
.and_return(created_object)
item = subject.add(object, 19.99, 3)
item.should be created_object
end
Expand All @@ -41,7 +41,7 @@
end

it "creates a new shopping cart item non-cumulatively" do
subject.shopping_cart_items.should_receive(:create).with(:item => object, :price => 19.99, :quantity => 3)
subject.shopping_cart_items.should_receive(:create).with(item: object, price: 19.99, quantity: 3)
subject.add(object, 19.99, 3, false)
end
end
Expand Down Expand Up @@ -145,7 +145,7 @@

context "cart has items" do
before do
items = [stub(:quantity => 2, :price => Money.new(3399)), stub(:quantity => 1, :price => Money.new(4599))]
items = [stub(quantity: 2, price: Money.new(3399)), stub(quantity: 1, price: Money.new(4599))]
subject.stub(:shopping_cart_items).and_return(items)
end

Expand All @@ -166,7 +166,7 @@
describe :taxes do
context "subtotal is 100" do
before do
subject.stub(:subtotal).and_return(Money.new(10000))
subject.stub(:subtotal).and_return(Money.new(10_000))
end

it "returns 8.25" do
Expand Down Expand Up @@ -204,7 +204,7 @@

context "cart has some items" do
before do
items = [stub(:quantity => 2, :price => 33.99), stub(:quantity => 1, :price => 45.99)]
items = [stub(quantity: 2, price: 33.99), stub(quantity: 1, price: 45.99)]
subject.stub(:shopping_cart_items).and_return(items)
end

Expand Down

0 comments on commit 4ef4ced

Please sign in to comment.