Implement spring and reset counters functionality

This commit is contained in:
2014-03-07 13:37:01 +01:00
parent 775e7965a2
commit 95e268adea
14 changed files with 104 additions and 84 deletions
+2
View File
@@ -72,6 +72,8 @@ group :development do
gem 'thin' gem 'thin'
gem 'faye' gem 'faye'
gem 'pry-rails' gem 'pry-rails'
gem 'spring'
gem 'spring-commands-rspec'
end end
group :test do group :test do
+5
View File
@@ -336,6 +336,9 @@ GEM
railties (>= 3.0, < 4.1) railties (>= 3.0, < 4.1)
slim (~> 2.0) slim (~> 2.0)
slop (3.4.7) slop (3.4.7)
spring (1.1.2)
spring-commands-rspec (1.0.1)
spring (>= 0.9.1)
sprockets (2.10.1) sprockets (2.10.1)
hike (~> 1.2) hike (~> 1.2)
multi_json (~> 1.0) multi_json (~> 1.0)
@@ -420,6 +423,8 @@ DEPENDENCIES
simplecov simplecov
simply_stored! simply_stored!
slim-rails slim-rails
spring
spring-commands-rspec
thin thin
turnip turnip
uglifier (>= 1.0.3) uglifier (>= 1.0.3)
+2 -1
View File
@@ -26,7 +26,8 @@ class Order
} }
}], reduce_function: '_sum' }], reduce_function: '_sum'
view :by_supplier_id_and_id, key: [:supplier_id, :_id] # Do not comment me out view :by_supplier_id_and_id, key: [:supplier_id, :_id] # Do not comment me out this is for security, TODO: make the security server side for speed and space optimization
view :by_supplier_id_and_state, key: [:supplier_id, :state], reduce_function: '_sum'
def self.for_supplier(supplier, options = {}) def self.for_supplier(supplier, options = {})
total_entries = database.view(by_supplier_id_and_id({startkey: ["#{supplier.id}\u9999"], endkey: [supplier.id], include_docs: false, reduce: true, descending: true})) total_entries = database.view(by_supplier_id_and_id({startkey: ["#{supplier.id}\u9999"], endkey: [supplier.id], include_docs: false, reduce: true, descending: true}))
+29
View File
@@ -115,6 +115,35 @@ class Supplier
confirmable confirmable
end end
def self.reset_counters!
spec = Order.by_supplier_id_and_state(reduce: true, group_level: 2)
reset_order_counters_with_spec spec
end
def reset_counters!
spec = Order.by_supplier_id_and_state(startkey: [id], endkey: [id, {}], reduce: true, group_level: 2)
self.class.reset_order_counters_with_spec spec
end
def self.reset_order_counters_with_spec(spec)
# taken from the couch_potato source since we want a model/custom mix here (hmmmm, something that should be fixed in the couchbase version)
results = CouchPotato::View::ViewQuery.new(
database.couchrest_database,
spec.design_document,
{spec.view_name => { map: spec.map_function, reduce: spec.reduce_function} },
({spec.list_name => spec.list_function} unless spec.list_name.nil?),
spec.language
).query_view!(spec.view_parameters)
Array.wrap(results['rows']).each do |result|
supplier_id, state = result['key']
case state
when 'placed' then Qwaiter::Counter.set "supplier:#{supplier_id}:orders_in_process", result['value']
when 'active' then Qwaiter::Counter.set "supplier:#{supplier_id}:orders_delivered", result['value']
end
end
end
# Send confirmation instructions by email # Send confirmation instructions by email
def send_confirmation_instructions(*args) def send_confirmation_instructions(*args)
self.confirmation_token = nil if reconfirmation_required? self.confirmation_token = nil if reconfirmation_required?
+4
View File
@@ -1,4 +1,8 @@
#!/usr/bin/env ruby #!/usr/bin/env ruby
begin
load File.expand_path("../spring", __FILE__)
rescue LoadError
end
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application. # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
APP_PATH = File.expand_path('../../config/application', __FILE__) APP_PATH = File.expand_path('../../config/application', __FILE__)
Executable
+7
View File
@@ -0,0 +1,7 @@
#!/usr/bin/env ruby
begin
load File.expand_path("../spring", __FILE__)
rescue LoadError
end
require 'bundler/setup'
load Gem.bin_path('rake', 'rake')
Executable
+18
View File
@@ -0,0 +1,18 @@
#!/usr/bin/env ruby
# This file loads spring without using Bundler, in order to be fast
# It gets overwritten when you run the `spring binstub` command
unless defined?(Spring)
require "rubygems"
require "bundler"
if match = Bundler.default_lockfile.read.match(/^GEM$.*?^ spring \((.*?)\)$.*?^$/m)
ENV["GEM_PATH"] = ([Bundler.bundle_path.to_s] + Gem.path).join(File::PATH_SEPARATOR)
ENV["GEM_HOME"] = ""
Gem.paths = ENV
gem "spring", match[1]
require "spring/binstub"
end
end
-8
View File
@@ -1,8 +0,0 @@
<%
rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip"
%>
default: <%= std_opts %> features
wip: --tags @wip:3 --wip features
rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip
+5
View File
@@ -0,0 +1,5 @@
namespace :counters do
task reset: :environment do
Supplier.reset_counters!
end
end
-65
View File
@@ -1,65 +0,0 @@
# IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
# It is recommended to regenerate this file in the future when you upgrade to a
# newer version of cucumber-rails. Consider adding your own code to a new file
# instead of editing this one. Cucumber will automatically load all features/**/*.rb
# files.
unless ARGV.any? {|a| a =~ /^gems/} # Don't load anything when running the gems:* tasks
vendored_cucumber_bin = Dir["#{Rails.root}/vendor/{gems,plugins}/cucumber*/bin/cucumber"].first
$LOAD_PATH.unshift(File.dirname(vendored_cucumber_bin) + '/../lib') unless vendored_cucumber_bin.nil?
begin
require 'cucumber/rake/task'
namespace :cucumber do
Cucumber::Rake::Task.new({:ok => 'db:test:prepare'}, 'Run features that should pass') do |t|
t.binary = vendored_cucumber_bin # If nil, the gem's binary is used.
t.fork = true # You may get faster startup if you set this to false
t.profile = 'default'
end
Cucumber::Rake::Task.new({:wip => 'db:test:prepare'}, 'Run features that are being worked on') do |t|
t.binary = vendored_cucumber_bin
t.fork = true # You may get faster startup if you set this to false
t.profile = 'wip'
end
Cucumber::Rake::Task.new({:rerun => 'db:test:prepare'}, 'Record failing features and run only them if any exist') do |t|
t.binary = vendored_cucumber_bin
t.fork = true # You may get faster startup if you set this to false
t.profile = 'rerun'
end
desc 'Run all features'
task :all => [:ok, :wip]
task :statsetup do
require 'rails/code_statistics'
::STATS_DIRECTORIES << %w(Cucumber\ features features) if File.exist?('features')
::CodeStatistics::TEST_TYPES << "Cucumber features" if File.exist?('features')
end
end
desc 'Alias for cucumber:ok'
task :cucumber => 'cucumber:ok'
task :default => :cucumber
task :features => :cucumber do
STDERR.puts "*** The 'features' task is deprecated. See rake -T cucumber ***"
end
# In case we don't have ActiveRecord, append a no-op task that we can depend upon.
task 'db:test:prepare' do
end
task :stats => 'cucumber:statsetup'
rescue LoadError
desc 'cucumber rake task not available (cucumber not installed)'
task :cucumber do
abort 'Cucumber rake task is not available. Be sure to install cucumber as a gem or plugin'
end
end
end
-10
View File
@@ -1,10 +0,0 @@
#!/usr/bin/env ruby
vendored_cucumber_bin = Dir["#{File.dirname(__FILE__)}/../vendor/{gems,plugins}/cucumber*/bin/cucumber"].first
if vendored_cucumber_bin
load File.expand_path(vendored_cucumber_bin)
else
require 'rubygems' unless ENV['NO_RUBYGEMS']
require 'cucumber'
load Cucumber::BINARY
end
+6
View File
@@ -3,5 +3,11 @@ FactoryGirl.define do
association :list association :list
association :user association :user
association :supplier #TODO warning! this may create a different supplier than the one created by the associated table association :supplier #TODO warning! this may create a different supplier than the one created by the associated table
trait :placed do
state 'placed'
end
trait :active do
state 'active'
end
end end
end end
+26
View File
@@ -2,6 +2,32 @@ require 'spec_helper'
describe Supplier do describe Supplier do
let(:supplier){ build :supplier } let(:supplier){ build :supplier }
describe '.reset_counters!' do
it 'sets the counters to the right values' do
supplier1 = create :supplier
supplier2 = create :supplier
create_list :order, 2, :placed, supplier: supplier1
create_list :order, 7, :active, supplier: supplier1
create_list :order, 3, :placed, supplier: supplier2
Supplier.reset_counters!
supplier1.orders_in_process_count.should == 2
supplier1.orders_delivered_count.should == 7
supplier2.orders_in_process_count.should == 3
end
end
describe '#reset_counters!' do
it 'sets the counters to the right values' do
supplier = create :supplier
create_list :order, 5, :placed, supplier: supplier
create_list :order, 9, :active, supplier: supplier
supplier.reset_counters!
supplier.orders_in_process_count.should == 5
supplier.orders_delivered_count.should == 9
end
end
# property open # property open
describe :open do describe :open do
it 'should be false by default' do it 'should be false by default' do